// // GPU STUFF // private static void SetShaderVars(UnityEngine.ComputeShader shader, UnityEngine.Vector2 noiseOffset, bool normalize, float noiseScale) { shader.SetInt("octaves", octaves); shader.SetFloat("falloff", falloff); shader.SetInt("normalize", System.Convert.ToInt32(normalize)); shader.SetFloat("noiseScale", noiseScale); shader.SetVector("offset", noiseOffset); }
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)); }
static public int SetInt(IntPtr l) { try { int argc = LuaDLL.lua_gettop(l); if (matchType(l, argc, 2, typeof(int), typeof(int))) { UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetInt(a1, a2); pushValue(l, true); return(1); } else if (matchType(l, argc, 2, typeof(string), typeof(int))) { UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l); System.String a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetInt(a1, a2); pushValue(l, true); return(1); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function SetInt to call"); return(2); } catch (Exception e) { return(error(l, e)); } }
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 SetInt(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 int argc = LuaDLL.lua_gettop(l); if (matchType(l, argc, 2, typeof(int), typeof(int))) { UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l); System.Int32 a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetInt(a1, a2); pushValue(l, true); return(1); } else if (matchType(l, argc, 2, typeof(string), typeof(int))) { UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l); System.String a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetInt(a1, a2); pushValue(l, true); return(1); } pushValue(l, false); LuaDLL.lua_pushstring(l, "No matched override function SetInt to call"); return(2); } 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 }
static public int SetInt(IntPtr l) { try { UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l); System.String a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetInt(a1, a2); return(0); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
static public int SetInt(IntPtr l) { try { UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l); System.String a1; checkType(l, 2, out a1); System.Int32 a2; checkType(l, 3, out a2); self.SetInt(a1, a2); pushValue(l, true); return(1); } catch (Exception e) { return(error(l, e)); } }
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; }
/// <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 void SetParams(ComputeShader c) { c.SetInt(ShaderConst.PROP_HASH_GRID_NX, _grid.nx); c.SetInt(ShaderConst.PROP_HASH_GRID_NY, _grid.ny); c.SetVector(ShaderConst.PROP_HASH_GRID_PARAMS, new Vector4(_grid.w, _grid.h, _grid.nx / _grid.w, _grid.ny / _grid.h)); }
void SetParameters(ComputeShader mat) { mat.SetFloat("Rg", Rg); mat.SetFloat("Rt", Rt); mat.SetFloat("RL", RL); mat.SetInt("TRANSMITTANCE_W", TRANSMITTANCE_W); mat.SetInt("TRANSMITTANCE_H", TRANSMITTANCE_H); mat.SetInt("SKY_W", SKY_W); mat.SetInt("SKY_H", SKY_H); mat.SetInt("RES_R", RES_R); mat.SetInt("RES_MU", RES_MU); mat.SetInt("RES_MU_S", RES_MU_S); mat.SetInt("RES_NU", RES_NU); mat.SetFloat("AVERAGE_GROUND_REFLECTANCE", AVERAGE_GROUND_REFLECTANCE); mat.SetFloat("HR", HR); mat.SetFloat("HM", HM); mat.SetVector("betaR", BETA_R); mat.SetVector("betaMSca", BETA_MSca); mat.SetVector("betaMEx", BETA_MSca / 0.9f); mat.SetFloat("mieG", Mathf.Clamp(MIE_G, 0.0f, 0.99f)); }
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); }
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)); }
public void SetBuffer(ComputeShader compute, int kernel) { compute.SetInt(ShaderConst.PROP_WALL_COUNT, _colliders.Count); compute.SetBuffer(kernel, ShaderConst.BUF_WALL, Walls); }
/* 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 void SetData(ComputeShader c, int k) { c.SetInt(ShaderConsts.PROP_POINT_COUNT, _count); c.SetBuffer(k, ShaderConsts.BUF_POINT, _points); }