private Dispatch ( int kernelIndex, int threadGroupsX, int threadGroupsY, int threadGroupsZ ) : void | ||
kernelIndex | int | |
threadGroupsX | int | |
threadGroupsY | int | |
threadGroupsZ | int | |
return | void |
/// <summary> /// Uses the GPU to generate a RenderTexture where the pixels in the texture represent noise. /// Set the octaves variable before calling this to a desired value. /// </summary> /// <returns>RenderTexture</returns> /// <param name="width"> Width of the texture to generate. </param> /// <param name="height"> Height of the texture to generate. </param> /// <param name="noiseOffsetX"> X Coordinate of the offset for the noise on the texture. </param> /// <param name="noiseOffsetY"> Y Coordinate of the offset for the noise on the texture. </param> /// <param name="noiseScale"> Value to scale the noise coordinates by. </param> /// <param name="normalize"> Whether or not to remap the noise from (-1, 1) to (0, 1). </param> public static UnityEngine.RenderTexture GetNoiseRenderTexture(int width, int height, float noiseOffsetX = 0, float noiseOffsetY = 0, float noiseScale = 0.01f, bool normalize = true) { UnityEngine.RenderTexture retTex = new UnityEngine.RenderTexture(width, height, 0); retTex.enableRandomWrite = true; retTex.Create(); UnityEngine.ComputeShader shader = UnityEngine.Resources.Load(shaderPath) as UnityEngine.ComputeShader; if (shader == null) { UnityEngine.Debug.LogError(noShaderMsg); return(null); } int[] resInts = { width, height }; int kernel = shader.FindKernel("ComputeNoise"); shader.SetTexture(kernel, "Result", retTex); SetShaderVars(shader, new UnityEngine.Vector2(noiseOffsetX, noiseOffsetY), normalize, noiseScale); shader.SetInts("reses", resInts); UnityEngine.ComputeBuffer permBuffer = new UnityEngine.ComputeBuffer(perm.Length, 4); permBuffer.SetData(perm); shader.SetBuffer(kernel, "perm", permBuffer); shader.Dispatch(kernel, width / 14, height / 15, 1); permBuffer.Release(); return(retTex); }
public static void ReadFromRenderTexture(RenderTexture tex, int channels, ComputeBuffer buffer, ComputeShader readData) { if(tex == null) { Debug.Log("CBUtility::ReadFromRenderTexture - RenderTexture is null"); return; } if(buffer == null) { Debug.Log("CBUtility::ReadFromRenderTexture - buffer is null"); return; } if(readData == null) { Debug.Log("CBUtility::ReadFromRenderTexture - Computer shader is null"); return; } if(channels < 1 || channels > 4) { Debug.Log("CBUtility::ReadFromRenderTexture - Channels must be 1, 2, 3, or 4"); return; } int kernel = -1; int depth = 1; string D = "2D"; string C = "C" + channels.ToString(); if(tex.isVolume) { depth = tex.volumeDepth; D = "3D"; } kernel = readData.FindKernel("read"+D+C); if(kernel == -1) { Debug.Log("CBUtility::ReadFromRenderTexture - could not find kernel " + "read"+D+C); return; } int width = tex.width; int height = tex.height; //set the compute shader uniforms readData.SetTexture(kernel, "_Tex"+D, tex); readData.SetInt("_Width", width); readData.SetInt("_Height", height); readData.SetBuffer(kernel, "_Buffer"+D+C, buffer); //tex will be write into this //run the compute shader readData.Dispatch(kernel, Mathf.Max(1, width/8), Mathf.Max(1, height/8), Mathf.Max(1, depth/8)); }
public static void ReadFromRenderTexture(RenderTexture tex, int channels, ComputeBuffer buffer, ComputeShader readData) { if (tex == null) { Debug.Log("CBUtility::ReadFromRenderTexture - RenderTexture is null"); return; } if (buffer == null) { Debug.Log("CBUtility::ReadFromRenderTexture - buffer is null"); return; } if (readData == null) { Debug.Log("CBUtility::ReadFromRenderTexture - Computer shader is null"); return; } if (channels < 1 || channels > 4) { Debug.Log("CBUtility::ReadFromRenderTexture - Channels must be 1, 2, 3, or 4"); return; } if (!tex.IsCreated()) { Debug.Log("CBUtility::ReadFromRenderTexture - tex has not been created (Call Create() on tex)"); return; } int num = 1; int num2 = readData.FindKernel(CBUtility.readNames2D[channels - 1, 0]); readData.SetTexture(num2, CBUtility.readNames2D[channels - 1, 1], tex); readData.SetBuffer(num2, CBUtility.readNames2D[channels - 1, 2], buffer); if (num2 == -1) { Debug.Log("CBUtility::ReadFromRenderTexture - could not find kernels"); return; } int width = tex.width; int height = tex.height; readData.SetInt("_Width", width); readData.SetInt("_Height", height); readData.SetInt("_Depth", num); int num3 = (width % 8 != 0) ? 1 : 0; int num4 = (height % 8 != 0) ? 1 : 0; int num5 = (num % 8 != 0) ? 1 : 0; readData.Dispatch(num2, Mathf.Max(1, width / 8 + num3), Mathf.Max(1, height / 8 + num4), Mathf.Max(1, num / 8 + num5)); }
static public int Dispatch(IntPtr l) { try { UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); System.Int32 a3; checkType(l, 4, out a3); System.Int32 a4; checkType(l, 5, out a4); self.Dispatch(a1, a2, a3, a4); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
static public int Dispatch(IntPtr l) { try { UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); System.Int32 a3; checkType(l, 4, out a3); System.Int32 a4; checkType(l, 5, out a4); self.Dispatch(a1, a2, a3, a4); return(0); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
public void Init() { _grassShader = Resources.Load<Shader>("Shaders/GrassGeneratorShader"); _grassMaterial = Resources.Load<Material>("GrassGeneratorMat"); _noiseTex = Resources.Load<Texture>("Noise"); if(_noiseTex == null) { Debug.LogError("Not found noise"); } _grassComputeShader = Resources.Load<ComputeShader>("ComputeShaders/GrassComputeShader"); _initGrassKernelId = _grassComputeShader.FindKernel(kInitGrassKernel); _updateGrassKernelId = _grassComputeShader.FindKernel(kUpdateGrassKernel); _numGrassItems = _numGroupGrassX*_numGroupGrassY*kThreadsX*kThreadsY; _grassBuffer = new ComputeBuffer(_numGrassItems, System.Runtime.InteropServices.Marshal.SizeOf(typeof(GrassData))); _obstaclesBuffer = new ComputeBuffer(kMaxObstacles, System.Runtime.InteropServices.Marshal.SizeOf(typeof(ObstacleData))); _grassComputeShader.SetFloat("_Width", _numGroupGrassX*kThreadsX); _grassComputeShader.SetFloat("_Height", _numGroupGrassY*kThreadsY); _grassComputeShader.SetTexture(_initGrassKernelId, "_NoiseTex", _noiseTex); _grassMaterial.SetTexture("_NoiseTex", _noiseTex); _grassMaterial.SetFloat("_Width", _numGroupGrassX*kThreadsX); _grassMaterial.SetFloat("_Height", _numGroupGrassY*kThreadsY); _grassComputeShader.SetBuffer(_initGrassKernelId, "_GrassBuffer", _grassBuffer); _grassComputeShader.SetBuffer(_updateGrassKernelId, "_GrassBuffer", _grassBuffer); _grassComputeShader.SetBuffer(_updateGrassKernelId, "_ObstaclesBuffer", _obstaclesBuffer); _grassComputeShader.SetInt("_NumObstacles", 0); _grassMaterial.SetBuffer("_GrassBuffer", _grassBuffer); _grassComputeShader.Dispatch(_initGrassKernelId, _numGroupGrassX, _numGroupGrassY, 1); #if GRASS_CPU _grassDataTestCPU = new GrassData[_numGrassItems]; _grassBuffer.GetData(_grassDataTestCPU); #endif _isInit = true; }
static public int Dispatch(IntPtr l) { try { #if DEBUG var method = System.Reflection.MethodBase.GetCurrentMethod(); string methodName = GetMethodName(method); #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.BeginSample(methodName); #else Profiler.BeginSample(methodName); #endif #endif UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); System.Int32 a3; checkType(l, 4, out a3); System.Int32 a4; checkType(l, 5, out a4); self.Dispatch(a1, a2, a3, a4); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } #if DEBUG finally { #if UNITY_5_5_OR_NEWER UnityEngine.Profiling.Profiler.EndSample(); #else Profiler.EndSample(); #endif } #endif }
/// <summary> /// Uses the GPU to process an array of 4D coordinates for noise and return an array with the noise at the specified coordinates. /// </summary> /// <returns>Float array</returns> /// <param name="positions"> Array of coordinates to process. </param> /// <param name="noiseScale"> Value to scale the noise coordinates by. </param> /// <param name="normalize"> Whether or not to remap the noise from (-1, 1) to (0, 1). </param> public static float[] NoiseArrayGPU(UnityEngine.Vector4[] positions, float noiseScale = 0.01f, bool normalize = true) { UnityEngine.ComputeShader shader = UnityEngine.Resources.Load(shaderPath) as UnityEngine.ComputeShader; if (shader == null) { UnityEngine.Debug.LogError(noShaderMsg); return(null); } int kernel = shader.FindKernel("ComputeNoiseArray"); SetShaderVars(shader, UnityEngine.Vector2.zero, normalize, noiseScale); shader.SetInt("dimension", 4); UnityEngine.ComputeBuffer permBuffer = new UnityEngine.ComputeBuffer(perm.Length, 4); permBuffer.SetData(perm); shader.SetBuffer(kernel, "perm", permBuffer); UnityEngine.ComputeBuffer posBuffer = new UnityEngine.ComputeBuffer(positions.Length, 16); posBuffer.SetData(positions); shader.SetBuffer(kernel, "float4Array", posBuffer); UnityEngine.ComputeBuffer outputBuffer = new UnityEngine.ComputeBuffer(positions.Length, 4); shader.SetBuffer(kernel, "outputArray", outputBuffer); shader.Dispatch(kernel, positions.Length / 14, 1, 1); float[] outputData = new float[positions.Length]; outputBuffer.GetData(outputData); permBuffer.Release(); posBuffer.Release(); outputBuffer.Release(); return(outputData); }
public static void WriteIntoRenderTexture(RenderTexture tex, int channels, ComputeBuffer buffer, ComputeShader writeData) { if(tex == null) { Debug.Log("CBUtility::WriteIntoRenderTexture - RenderTexture is null"); return; } if(buffer == null) { Debug.Log("CBUtility::WriteIntoRenderTexture - buffer is null"); return; } if(writeData == null) { Debug.Log("CBUtility::WriteIntoRenderTexture - Computer shader is null"); return; } if(channels < 1 || channels > 4) { Debug.Log("CBUtility::WriteIntoRenderTexture - Channels must be 1, 2, 3, or 4"); return; } if(!tex.enableRandomWrite) { Debug.Log("CBUtility::WriteIntoRenderTexture - you must enable random write on render texture"); return; } int kernel = -1; int depth = 1; string D = "2D"; string C = "C" + channels.ToString(); if(tex.isVolume) { depth = tex.volumeDepth; D = "3D"; } kernel = writeData.FindKernel("write"+D+C); if(kernel == -1) { Debug.Log("CBUtility::WriteIntoRenderTexture - could not find kernel " + "write"+D+C); return; } int width = tex.width; int height = tex.height; //set the compute shader uniforms writeData.SetTexture(kernel, "_Des"+D+C, tex); writeData.SetInt("_Width", width); writeData.SetInt("_Height", height); writeData.SetBuffer(kernel, "_Buffer"+D+C, buffer); //run the compute shader writeData.Dispatch(kernel, Mathf.Max(1, width/8), Mathf.Max(1, height/8), Mathf.Max(1, depth/8)); }
private static void DecodeFloat3D(int w, int h, int d, int c, float min, float max, RenderTexture tex, Color[] map, ComputeShader shader) { Color[] array = new Color[w * h * d]; Color[] array2 = new Color[w * h * d]; Color[] array3 = new Color[w * h * d]; Color[] array4 = new Color[w * h * d]; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { for (int k = 0; k < d; k++) { array[i + j * w + k * w * h] = new Color(0f, 0f, 0f, 0f); array2[i + j * w + k * w * h] = new Color(0f, 0f, 0f, 0f); array3[i + j * w + k * w * h] = new Color(0f, 0f, 0f, 0f); array4[i + j * w + k * w * h] = new Color(0f, 0f, 0f, 0f); if (c > 0) { array[i + j * w + k * w * h] = map[(i + j * w + k * w * h) * c]; } if (c > 1) { array2[i + j * w + k * w * h] = map[(i + j * w + k * w * h) * c + 1]; } if (c > 2) { array3[i + j * w + k * w * h] = map[(i + j * w + k * w * h) * c + 2]; } if (c > 3) { array4[i + j * w + k * w * h] = map[(i + j * w + k * w * h) * c + 3]; } } } } Texture3D texture3D = new Texture3D(w, h, d, TextureFormat.ARGB32, false); texture3D.hideFlags = HideFlags.HideAndDontSave; texture3D.filterMode = FilterMode.Point; texture3D.wrapMode = TextureWrapMode.Clamp; texture3D.SetPixels(array); texture3D.Apply(); Texture3D texture3D2 = new Texture3D(w, h, d, TextureFormat.ARGB32, false); texture3D2.hideFlags = HideFlags.HideAndDontSave; texture3D2.filterMode = FilterMode.Point; texture3D2.wrapMode = TextureWrapMode.Clamp; texture3D2.SetPixels(array2); texture3D2.Apply(); Texture3D texture3D3 = new Texture3D(w, h, d, TextureFormat.ARGB32, false); texture3D3.hideFlags = HideFlags.HideAndDontSave; texture3D3.filterMode = FilterMode.Point; texture3D3.wrapMode = TextureWrapMode.Clamp; texture3D3.SetPixels(array3); texture3D3.Apply(); Texture3D texture3D4 = new Texture3D(w, h, d, TextureFormat.ARGB32, false); texture3D4.hideFlags = HideFlags.HideAndDontSave; texture3D4.filterMode = FilterMode.Point; texture3D4.wrapMode = TextureWrapMode.Clamp; texture3D4.SetPixels(array4); texture3D4.Apply(); shader.SetFloat("_Min", min); shader.SetFloat("_Max", max); shader.SetTexture(0, "_TexR", texture3D); shader.SetTexture(0, "_TexG", texture3D2); shader.SetTexture(0, "_TexB", texture3D3); shader.SetTexture(0, "_TexA", texture3D4); shader.SetTexture(0, "des", tex); shader.Dispatch(0, w, h, d); UnityEngine.Object.Destroy(texture3D); UnityEngine.Object.Destroy(texture3D2); UnityEngine.Object.Destroy(texture3D3); UnityEngine.Object.Destroy(texture3D4); }
/* static string[,] readNames3D = new string[,] { {"read3DC1", "_Tex3D", "_Buffer3DC1"}, {"read3DC2", "_Tex3D", "_Buffer3DC2"}, {"read3DC3", "_Tex3D", "_Buffer3DC3"}, {"read3DC4", "_Tex3D", "_Buffer3DC4"} }; */ public static void ReadFromRenderTexture(RenderTexture tex, int channels, ComputeBuffer buffer, ComputeShader readData) { if(tex == null) { Debug.Log("CBUtility::ReadFromRenderTexture - RenderTexture is null"); return; } if(buffer == null) { Debug.Log("CBUtility::ReadFromRenderTexture - buffer is null"); return; } if(readData == null) { Debug.Log("CBUtility::ReadFromRenderTexture - Computer shader is null"); return; } if(channels < 1 || channels > 4) { Debug.Log("CBUtility::ReadFromRenderTexture - Channels must be 1, 2, 3, or 4"); return; } if(!tex.IsCreated()) { Debug.Log("CBUtility::ReadFromRenderTexture - tex has not been created (Call Create() on tex)"); return; } int kernel = -1; int depth = 1; //if(tex.isVolume) //{ // depth = tex.volumeDepth; // kernel = readData.FindKernel(readNames3D[channels - 1, 0]); // readData.SetTexture(kernel, readNames3D[channels - 1, 1], tex); // readData.SetBuffer(kernel, readNames3D[channels - 1, 2], buffer); //} //else //{ kernel = readData.FindKernel(readNames2D[channels - 1, 0]); readData.SetTexture(kernel, readNames2D[channels - 1, 1], tex); readData.SetBuffer(kernel, readNames2D[channels - 1, 2], buffer); //} if(kernel == -1) { Debug.Log("CBUtility::ReadFromRenderTexture - could not find kernels"); return; } int width = tex.width; int height = tex.height; //set the compute shader uniforms readData.SetInt("_Width", width); readData.SetInt("_Height", height); readData.SetInt("_Depth", depth); //run the compute shader. Runs in threads of 8 so non divisible by 8 numbers will need //some extra threadBlocks. This will result in some unneeded threads running int padX = (width%8 == 0) ? 0 : 1; int padY = (height%8 == 0) ? 0 : 1; int padZ = (depth%8 == 0) ? 0 : 1; readData.Dispatch(kernel, Mathf.Max(1,width/8 + padX), Mathf.Max(1, height/8 + padY), Mathf.Max(1, depth/8 + padZ)); }
public static void ReadSingleFromRenderTexture(RenderTexture tex, float x, float y, float z, ComputeBuffer buffer, ComputeShader readData, bool useBilinear) { if(tex == null) { Debug.Log("CBUtility::ReadSingleFromRenderTexture - RenderTexture is null"); return; } if(buffer == null) { Debug.Log("CBUtility::ReadSingleFromRenderTexture - buffer is null"); return; } if(readData == null) { Debug.Log("CBUtility::ReadSingleFromRenderTexture - Computer shader is null"); return; } if(!tex.IsCreated()) { Debug.Log("CBUtility::ReadSingleFromRenderTexture - tex has not been created (Call Create() on tex)"); return; } int kernel = -1; int depth = 1; //if(tex.isVolume) //{ // depth = tex.volumeDepth; // // if(useBilinear) // kernel = readData.FindKernel("readSingle3D"); // else // kernel = readData.FindKernel("readSingleBilinear3D"); // // readData.SetTexture(kernel, "_Tex3D", tex); // readData.SetBuffer(kernel, "BufferSingle3D", buffer); //} //else //{ if(useBilinear) kernel = readData.FindKernel("readSingle2D"); else kernel = readData.FindKernel("readSingleBilinear2D"); readData.SetTexture(kernel, "_Tex2D", tex); readData.SetBuffer(kernel, "BufferSingle2D", buffer); //} if(kernel == -1) { Debug.Log("CBUtility::ReadSingleFromRenderTexture - could not find kernels"); return; } int width = tex.width; int height = tex.height; //used for point sampling readData.SetInt("_IdxX", (int)x); readData.SetInt("_IdxY", (int)y); readData.SetInt("_IdxZ", (int)z); //used for bilinear sampling readData.SetVector("_UV", new Vector4( x / (float)(width-1), y / (float)(height-1), z / (float)(depth-1), 0.0f)); readData.Dispatch(kernel, 1, 1, 1); }
public static void ReadSingleFromRenderTexture(RenderTexture tex, float x, float y, float z, ComputeBuffer buffer, ComputeShader readData, bool useBilinear) { if (tex == null) { Debug.Log("CBUtility::ReadSingleFromRenderTexture - RenderTexture is null"); return; } if (buffer == null) { Debug.Log("CBUtility::ReadSingleFromRenderTexture - buffer is null"); return; } if (readData == null) { Debug.Log("CBUtility::ReadSingleFromRenderTexture - Computer shader is null"); return; } if (!tex.IsCreated()) { Debug.Log("CBUtility::ReadSingleFromRenderTexture - tex has not been created (Call Create() on tex)"); return; } int num = 1; int num2; if (useBilinear) { num2 = readData.FindKernel("readSingle2D"); } else { num2 = readData.FindKernel("readSingleBilinear2D"); } readData.SetTexture(num2, "_Tex2D", tex); readData.SetBuffer(num2, "BufferSingle2D", buffer); if (num2 == -1) { Debug.Log("CBUtility::ReadSingleFromRenderTexture - could not find kernels"); return; } int width = tex.width; int height = tex.height; readData.SetInt("_IdxX", (int)x); readData.SetInt("_IdxY", (int)y); readData.SetInt("_IdxZ", (int)z); readData.SetVector("_UV", new Vector4(x / (float)(width - 1), y / (float)(height - 1), z / (float)(num - 1), 0f)); readData.Dispatch(num2, 1, 1, 1); }