SetBuffer() 개인적인 메소드

private SetBuffer ( int kernelIndex, int nameID, ComputeBuffer buffer ) : void
kernelIndex int
nameID int
buffer ComputeBuffer
리턴 void
예제 #1
0
    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);
    }
예제 #2
0
    /// <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);
    }
 static public int SetBuffer(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), typeof(UnityEngine.ComputeBuffer)))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 2, out a1);
             System.Int32 a2;
             checkType(l, 3, out a2);
             UnityEngine.ComputeBuffer a3;
             checkType(l, 4, out a3);
             self.SetBuffer(a1, a2, a3);
             pushValue(l, true);
             return(1);
         }
         else if (matchType(l, argc, 2, typeof(int), typeof(string), typeof(UnityEngine.ComputeBuffer)))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 2, out a1);
             System.String a2;
             checkType(l, 3, out a2);
             UnityEngine.ComputeBuffer a3;
             checkType(l, 4, out a3);
             self.SetBuffer(a1, a2, a3);
             pushValue(l, true);
             return(1);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function SetBuffer 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
 }
예제 #4
0
    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));
    }
예제 #5
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;
    }
예제 #6
0
    /// <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);
    }
예제 #7
0
 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));
 }
예제 #8
0
 static public int SetBuffer(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
         System.Int32 a1;
         checkType(l, 2, out a1);
         System.String a2;
         checkType(l, 3, out a2);
         UnityEngine.ComputeBuffer a3;
         checkType(l, 4, out a3);
         self.SetBuffer(a1, a2, a3);
         pushValue(l, true);
         return(1);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
 static public int SetBuffer(IntPtr l)
 {
     try {
         UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
         System.Int32 a1;
         checkType(l, 2, out a1);
         System.String a2;
         checkType(l, 3, out a2);
         UnityEngine.ComputeBuffer a3;
         checkType(l, 4, out a3);
         self.SetBuffer(a1, a2, a3);
         return(0);
     }
     catch (Exception e) {
         LuaDLL.luaL_error(l, e.ToString());
         return(0);
     }
 }
예제 #10
0
 static public int SetBuffer(IntPtr l)
 {
     try {
         int argc = LuaDLL.lua_gettop(l);
         if (matchType(l, argc, 2, typeof(int), typeof(int), typeof(UnityEngine.ComputeBuffer)))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 2, out a1);
             System.Int32 a2;
             checkType(l, 3, out a2);
             UnityEngine.ComputeBuffer a3;
             checkType(l, 4, out a3);
             self.SetBuffer(a1, a2, a3);
             pushValue(l, true);
             return(1);
         }
         else if (matchType(l, argc, 2, typeof(int), typeof(string), typeof(UnityEngine.ComputeBuffer)))
         {
             UnityEngine.ComputeShader self = (UnityEngine.ComputeShader)checkSelf(l);
             System.Int32 a1;
             checkType(l, 2, out a1);
             System.String a2;
             checkType(l, 3, out a2);
             UnityEngine.ComputeBuffer a3;
             checkType(l, 4, out a3);
             self.SetBuffer(a1, a2, a3);
             pushValue(l, true);
             return(1);
         }
         pushValue(l, false);
         LuaDLL.lua_pushstring(l, "No matched override function SetBuffer to call");
         return(2);
     }
     catch (Exception e) {
         return(error(l, e));
     }
 }
예제 #11
0
 public void SetBuffer(ComputeShader compute, int kernel)
 {
     compute.SetBuffer(kernel, ShaderConst.BUF_POSITION, P0);
 }
예제 #12
0
 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);
 }
예제 #13
0
 public void SetBuffer(ComputeShader c, int kernel)
 {
     c.SetBuffer(kernel, ShaderConst.BUF_COLLISIONS, Collisions);
 }
예제 #14
0
 public void SetBuffer(ComputeShader c, int kernel)
 {
     c.SetBuffer(kernel, ShaderConst.BUF_BROADPHASE_KEYS, Keys);
     c.SetBuffer(kernel, ShaderConst.BUF_BROADPHASE_BAND, Bands);
 }
예제 #15
0
 public void SetBuffer(ComputeShader c, int kernel)
 {
     c.SetBuffer(kernel, ShaderConst.BUF_GRID_START, _hashGridStart);
     c.SetBuffer(kernel, ShaderConst.BUF_GRID_END, _hashGridEnd);
 }
예제 #16
0
 public void SetBuffer(ComputeShader c, int kernel)
 {
     c.SetBuffer(kernel, ShaderConst.BUF_HASHES, Hashes);
 }
예제 #17
0
 public void SetData(ComputeShader c, int k)
 {
     c.SetInt(ShaderConsts.PROP_POINT_COUNT, _count);
     c.SetBuffer(k, ShaderConsts.BUF_POINT, _points);
 }
예제 #18
0
        /*
        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));
        }
예제 #19
0
        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);
        }
예제 #20
0
 public void SetBuffer(ComputeShader compute, int kernel)
 {
     compute.SetBuffer(kernel, ShaderConst.BUF_LIFE, Lifes);
 }
예제 #21
0
 public void SetBuffer(ComputeShader compute, int kernel)
 {
     compute.SetBuffer (kernel, ShaderConst.BUF_DRAG_COEFFS, _dragCoeffs);
 }
예제 #22
0
 public void SetBuffer(ComputeShader compute, int kernel)
 {
     compute.SetInt(ShaderConst.PROP_WALL_COUNT, _colliders.Count);
     compute.SetBuffer(kernel, ShaderConst.BUF_WALL, Walls);
 }
예제 #23
0
 public void SetBuffer(ComputeShader compute, int kernel)
 {
     compute.SetBuffer(kernel, ShaderConst.BUF_VELOCITY_CURR, V0);
     compute.SetBuffer(kernel, ShaderConst.BUF_VELOCITY_NEXT, V1);
 }
예제 #24
0
    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));
    }