public BitonicMergeSort(ComputeShader compute) { _compute = compute; _kernelInit = compute.FindKernel(KERNEL_INIT); _kernelSort = compute.FindKernel(KERNEL_SORT); _kernelSortInt = compute.FindKernel(KERNEL_SORT_INT); }
public LifeService(ComputeShader compute, int capacity) { _compute = compute; _kernelSimulate = compute.FindKernel(ShaderConst.KERNEL_SIMULATE_LIFE); _kernelUpload = compute.FindKernel(ShaderConst.KERNEL_UPLOAD_LIFE); _lifes = new float[capacity]; Lifes = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(float))); Lifes.SetData(_lifes); _uploader = new ComputeBuffer(INIT_CAPACITY, Marshal.SizeOf(typeof(float))); ShaderUtil.CalcWorkSize(capacity, out SimSizeX, out SimSizeY, out SimSizeZ); }
public BroadPhase(ComputeShader compute, ComputeShader computeSort, LifeService l, PositionService p) { var capacity = l.Lifes.count; _lifes = l; _positions = p; _kernelInitYs = compute.FindKernel(ShaderConst.KERNEL_INIT_BROADPHASE); _kernelSolve = compute.FindKernel (ShaderConst.KERNEL_SOVLE_BROADPHASE); _compute = compute; _sort = new BitonicMergeSort(computeSort); Keys = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(uint))); _ys = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(float))); Bands = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(Band))); }
void CreateAssets() { _material = Resources.Load<Material>("ParticleInstancingMat"); _shaderCompute = Resources.Load<ComputeShader>("ParticleMeshCompute"); _initKernelId = _shaderCompute.FindKernel(kInitKernel); _updateKernelId = _shaderCompute.FindKernel(kUpdateKernel); _instancesCount = _mesh.vertices.Length/3; _numThreadGroupsX = Mathf.CeilToInt(((float)_instancesCount) / ((float)kNumThreadsX)); Debug.Log("_numThreadGroupsX " + _numThreadGroupsX); }
public VelocityService(ComputeShader compute, int capacity) { _kernelUpload = compute.FindKernel(ShaderConst.KERNEL_UPLOAD_VELOCITY); _kernelClampVelocity = compute.FindKernel(ShaderConst.KERNEL_CLAMP_VELOCITY); _compute = compute; _velocities = new Vector2[capacity]; V0 = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(Vector2))); V1 = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(Vector2))); V0.SetData(_velocities); V1.SetData(_velocities); _uploader = new ComputeBuffer(INITIAL_CAP, Marshal.SizeOf(_velocities[0])); ShaderUtil.CalcWorkSize(capacity, out SimSizeX, out SimSizeY, out SimSizeZ); }
void CreateAssets() { _material = Resources.Load<Material>("ParticleImageMat"); _shaderCompute = Resources.Load<ComputeShader>("ParticleImageCompute"); _initImageKernelId = _shaderCompute.FindKernel(kInitParticlesKernel); _updateParticlesKernel = _shaderCompute.FindKernel(kUpdateParticlesKernel); _numThreadGroupsX = Mathf.CeilToInt(_image.width / kNumThreadsX); _numThreadGroupsY = Mathf.CeilToInt(_image.height / kNumThreadsY); _numThreadGroupsZ = Mathf.Max(Mathf.CeilToInt(_imageLayers / kNumThreadsX), 1); _totalNumParticles = _image.width * _image.height * _imageLayers; }
/// <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 BoundsChecker(ComputeShader compute, LifeService l, PositionService p) { _kernel = compute.FindKernel(ShaderConst.KERNEL_CHECK_BOUNDS); _compute = compute; _lifes = l; _positions = p; }
public GridService(ComputeShader compute, Grid g, HashService h) { _compute = compute; _grid = g; _hashes = h; _kernelInit = compute.FindKernel(ShaderConst.KERNEL_INIT_GRID); _kernelConstruct = compute.FindKernel(ShaderConst.KERNEL_CONSTRUCT_GRID); var gridCellCount = g.nx * g.ny; StartData = new uint[gridCellCount]; EndData = new uint[gridCellCount]; _hashGridStart = new ComputeBuffer(gridCellCount, Marshal.SizeOf(typeof(uint))); _hashGridEnd = new ComputeBuffer(gridCellCount, Marshal.SizeOf(typeof(uint))); _hashGridStart.SetData (StartData); _hashGridEnd.SetData (EndData); }
static public int FindKernel(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.String a1; checkType(l, 2, out a1); var ret = self.FindKernel(a1); pushValue(l, true); pushValue(l, ret); 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 }
public VelocitySimulation(ComputeShader compute, VelocityService v, ConstantService c) { _kernelSimulate = compute.FindKernel(ShaderConst.KERNEL_SIMULATE_VELOCITY); _compute = compute; _velocities = v; _constants = c; }
public PositionSimulation(ComputeShader compute, VelocityService v, PositionService p) { _kernelSimulate = compute.FindKernel(ShaderConst.KERNEL_SIMULATE_POSITION); _compute = compute; _velocities = v; _positions = p; }
public void Init() { _numParticlesX = _particleGroupsX * kNumThreadsX; _numParticlesY = _particleGroupsY * kNumThreadsY; _numParticles = _numParticlesX * _numParticlesY; _currentCopiedVertices = 0; _particleMaterial = Resources.Load<Material>("GPUParticleMat"); _computeShader = Resources.Load<ComputeShader>("ComputeShaders/GPUParticleUpdater"); _kernelMoveParticles = _computeShader.FindKernel(kKernelMoveParticles); _particlesData = new Particle[_numParticles]; InitBuffer(_particlesData); for (int i = 0; i < _particlesData.Length; ++i) { float id = ((float)i) / 10000.1f; CreateParticle(id); } // Set ComputeShader Parameters _particlesBuffer = new ComputeBuffer(_particlesData.Length, System.Runtime.InteropServices.Marshal.SizeOf(typeof(GPUParticleSystem.Particle))); _particlesBuffer.SetData(_particlesData); _computeShader.SetBuffer(_kernelMoveParticles, "_ParticlesBuffer", _particlesBuffer); _computeShader.SetFloat("_Width", _numParticlesX); _computeShader.SetFloat("_Height", _numParticlesY); // Set Shader Parameters _particleMaterial.SetBuffer("_ParticlesBuffer", _particlesBuffer); }
public WallCollisionSolver(ComputeShader compute, VelocityService v, PositionService p, WallService w) { _kernel = compute.FindKernel(ShaderConst.KERNEL_SOLVE_WALL_COLLISION); _compute = compute; _velocities = v; _positions = p; _walls = w; }
public PolygonCollisionSolver(ComputeShader c, VelocityService v, PositionService p, LifeService l, PolygonColliderService poly) { _compute = c; _kernel = c.FindKernel(ShaderConst.KERNEL_SOLVE_POLYGON_COLLISION); _velocities = v; _positions = p; _lifes = l; _polygons = poly; }
public ParticleCollisionSolver(ComputeShader compute, VelocityService v, PositionService p, LifeService l, BroadPhase b) { _kernel = compute.FindKernel(ShaderConst.KERNEL_SOLVE_PARTICLE_COLLISION); _compute = compute; _velocities = v; _positions = p; _lifes = l; _broadphase = b; }
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 PositionService(ComputeShader compute, int capacity) { _kernelUpload = compute.FindKernel(ShaderConst.KERNEL_UPLOAD_POSITION); _compute = compute; _positions = new Vector2[capacity]; P0 = new ComputeBuffer(capacity, Marshal.SizeOf(_positions[0])); P0.SetData(_positions); _uploader = new ComputeBuffer(INITIAL_CAP, Marshal.SizeOf(_positions[0])); ShaderUtil.CalcWorkSize(capacity, out SimSizeX, out SimSizeY, out SimSizeZ); }
public HashService(ComputeShader compute, LifeService l, PositionService p) { _compute = compute; _lifes = l; _positions = p; _kernelInitHashes = compute.FindKernel(ShaderConst.KERNEL_INIT_HASHES); HashData = new int[l.Lifes.count]; Hashes = new ComputeBuffer(l.Lifes.count, Marshal.SizeOf(typeof(int))); Hashes.SetData (HashData); }
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 FindKernel(IntPtr l) { try { UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l); System.String a1; checkType(l, 2, out a1); var ret = self.FindKernel(a1); pushValue(l, true); pushValue(l, ret); return(2); } catch (Exception e) { return(error(l, e)); } }
static public int FindKernel(IntPtr l) { try{ UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l); System.String a1; checkType(l, 2, out a1); System.Int32 ret = self.FindKernel(a1); pushValue(l, ret); return(1); } catch (Exception e) { LuaDLL.luaL_error(l, e.ToString()); return(0); } }
public HashGrid(ComputeShader compute, ComputeShader computeSort, LifeService l, PositionService p, GridService.Grid g) { var capacity = l.Lifes.count; _compute = compute; _lifes = l; _positions = p; _kernelSolve = compute.FindKernel (ShaderConst.KERNEL_SOVLE_COLLISION_DETECTION); _sort = new BitonicMergeSort(computeSort); _keys = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(uint))); CollisionData = new Collision[capacity]; Collisions = new ComputeBuffer(capacity, Marshal.SizeOf(typeof(Collision))); _hashes = new HashService(compute, l, p); _grid = new GridService(compute, g, _hashes); }
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)); }
public RotationService(ComputeShader c, VelocityService v, LifeService l, float radius, Vector2 angularSpeedRange) { Count = v.V0.count; _compute = c; _velocities = v; _lifes = l; _kernelVelocityBasedRotate = c.FindKernel(KERNEL_VELOCITY_BASED_ROTATE); _data = new Vector4[Count]; _rotations = new ComputeBuffer(Count, Marshal.SizeOf(typeof(Vector4))); for (var i = 0; i < Count; i++) _data[i] = QuaternionExtension.IDENTITY; Upload(); ShaderUtil.CalcWorkSize(Count, out SimSizeX, out SimSizeY, out SimSizeZ); _angularSpeedData = new float[ShaderConst.WARP_SIZE]; _angularSpeed = new ComputeBuffer(_angularSpeedData.Length, Marshal.SizeOf(typeof(float))); UpdateAngularSpeed(angularSpeedRange); }
/// <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 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 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); }
/* 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 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)); }