示例#1
0
    private void RunShader()
    {
        // 要传入的数据
        VecMatPair[] data = new VecMatPair[128];
        // 创建ComputeBuffer(数据个数,单个数据大小)
        ComputeBuffer buffer = new ComputeBuffer(data.Length, 24);

        // 设置数据
        for (uint i = 0; i < data.Length; ++i)
        {
            data[i].a = Vector4.zero;
            data[i].b = Vector2.one;
        }
        // 数据塞入ComputeBuffer
        buffer.SetData(data);
        // 设置到相应的buffer(数字0与shader.FindKernel("CSMain")同等效果  与CS中的#pra gma kernel CSMain排列顺序有关,
        // CS中StructuredBuffer名字符串,
        // 要传入的buffer)
        shader.SetBuffer(0, "_TestStruct", buffer);
        // 设置发送数组长度 吧~
        shader.Dispatch(0, data.Length, 1, 1);
        // 数据回读
        buffer.GetData(data);
        for (uint i = 0; i < data.Length; ++i)
        {
            Debug.Log(data[i].a + "  " + data[i].b);
        }
    }
    void Output()
    {
        VecMatPair[] output = new VecMatPair[5];
        buffer.GetData(output);

        for (int i = 0; i < output.Length; i++)
        {
            Debug.Log(i + ": " + output[i].point);
        }
    }
示例#3
0
    // Start is called before the first frame update
    void Start()
    {
        int kernel = cs.FindKernel("CSMain");

        // Render texture test
        //RenderTexture rt = new RenderTexture(256, 256, 24);
        //rt.enableRandomWrite = true;
        //rt.Create();

        // move data to gpu
        //cs.SetTexture(kernel, "Result", rt);
        // x*y is how many thread groups to spawn
        // each group is 64 threads, want one thread per pixel
        //cs.Dispatch(kernel, 256 / 8, 256 / 8, 1);

        //RTMaterial.material.SetTexture("_BaseMap", rt);

        // Buffer test
        VecMatPair[] data =
        {
            new VecMatPair(new Vector3(1, 1, 1), new Vector3(1, 1, 1)),
            new VecMatPair(new Vector3(1, 1, 1), new Vector3(1, 1, 1)),
            new VecMatPair(new Vector3(1, 1, 1), new Vector3(1, 1, 1)),
            new VecMatPair(new Vector3(1, 1, 1), new Vector3(1, 1, 1)),
            new VecMatPair(new Vector3(1, 1, 1), new Vector3(1, 1, 1)),
        };
        string log = "";

        foreach (var item in data)
        {
            log += item.point.ToString() + " " + item.otherPoint.ToString();
        }
        Debug.Log(log);

        ComputeBuffer buffer = new ComputeBuffer(data.Length, (4 * 3) + (4 * 3));

        buffer.SetData(data);
        kernel = cs.FindKernel("Multiply");
        cs.SetBuffer(kernel, "dataBuffer", buffer);
        cs.Dispatch(kernel, data.Length, 1, 1);

        // get data back from shader - can be expensive
        VecMatPair[] multipliedData = new VecMatPair[5];
        buffer.GetData(multipliedData); // does this stall until calculations are done?
        buffer.Dispose();

        log = "";
        foreach (var item in multipliedData)
        {
            log += item.point.ToString() + " " + item.otherPoint.ToString();
        }
        Debug.Log(log);
    }
示例#4
0
    public void RunMultiplyShader()
    {
        VecMatPair[] data   = new VecMatPair[5];
        VecMatPair[] output = new VecMatPair[5];
        // Init Data here!!!
        for (int i = 0; i < data.Length; i++)
        {
            data[i].point  = UnityEngine.Random.onUnitSphere;
            data[i].matrix = Matrix4x4.TRS(UnityEngine.Random.onUnitSphere, UnityEngine.Random.rotation, UnityEngine.Random.onUnitSphere);
            Debug.Log("PreShader! Pos: " + data[i].point.ToString() + ", Matrix: " + data[i].matrix.ToString());
        }

        ComputeBuffer buffer       = new ComputeBuffer(data.Length, 76);
        int           kernelHandle = shader.FindKernel("Multiply");

        buffer.SetData(data);
        shader.SetBuffer(kernelHandle, "dataBuffer", buffer);
        shader.Dispatch(kernelHandle, data.Length, 1, 1);
        buffer.GetData(output);

        for (int i = 0; i < output.Length; i++)
        {
            Debug.Log("PostShader! Pos: " + output[i].point.ToString() + ", Matrix: " + output[i].matrix.ToString());
        }

        buffer.Dispose();

        /*public ComputeShader compute;
         * public ComputeBuffer buffer;
         * public int[] cols;
         *
         * void Start () {
         * var mesh = GetComponent<MeshFilter>().mesh;
         * int n = mesh.vertexCount;
         *  ///
         * buffer = new ComputeBuffer (n, 16);
         * ///
         * cols = new int[n];
         *  ///
         * for (int i = 0; i < n; ++i)
         *   cols[i] = 0;
         * buffer.SetData (cols);
         *  ///
         * compute.SetBuffer(compute.FindKernel ("CSMain"),"bufColors", buffer);
         *  ///
         *  compute.Dispatch(0,4,4,1);
         * ///
         * buffer.GetData(cols);
         * Debug.Log (cols[0]);
         */
    }
示例#5
0
    /// <summary>
    /// There are a few things to note here. First is setting the enableRandomWrite flag of your render texture BEFORE you create it.
    /// This gives your compute shaders access to write to the texture.
    /// If you don’t set this flag you won’t be able to use the texture as a write target for the shader.

    //Next we need a way to identify what function we want to call in our compute shader.
    //The FindKernel function takes a string name, which corresponds to one of the kernel names we set up at the beginning of our compute shader.
    //Remember, a Compute Shader can have multiple kernels (functions) in a single file.

    //The ComputeShader.SetTexture call lets us move the data we want to work with from CPU memory to GPU memory.
    //Moving data between memory spaces is what will introduce latency to your program,
    //and the amount of slowdown you see is proportional to the amount of data that you are transferring.
    //For this reason, if you plan on running a compute shader every frame you’ll need to aggressively optimize how much data is actually get operated on.

    //The three integers passed to the Dispatch call specify the number of thread groups we want to spawn.
    //Recall that each thread group’s size is specified in the numthreads block of the compute shader, so in the above example,
    //the number of total threads we’re spawning is as follows:

    //32*32 thread groups * 64 threads per group = 65536 threads total.

    //This ends up equating to 1 thread per pixel in the render texture,
    //which makes sense given that the kernel function can only operate on 1 pixel per call.

    /// </summary>


    void RunShader()
    {
        VecMatPair[] data   = new VecMatPair[5];
        VecMatPair[] output = new VecMatPair[5];

        //INITIALIZE DATA HERE

        ComputeBuffer buffer = new ComputeBuffer(data.Length, 76);
        int           kernel = shader.FindKernel("Multiply");

        shader.SetBuffer(kernel, "dataBuffer", buffer);
        shader.Dispatch(kernel, data.Length, 1, 1);
        buffer.GetData(output);
    }
    public void RunMultiplyShader() {
        VecMatPair[] data = new VecMatPair[5];
        VecMatPair[] output = new VecMatPair[5];
        // Init Data here!!!
        for (int i = 0; i < data.Length; i++) {
            data[i].point = UnityEngine.Random.onUnitSphere;
            data[i].matrix = Matrix4x4.TRS(UnityEngine.Random.onUnitSphere, UnityEngine.Random.rotation, UnityEngine.Random.onUnitSphere);
            Debug.Log("PreShader! Pos: " + data[i].point.ToString() + ", Matrix: " + data[i].matrix.ToString());
        }

        ComputeBuffer buffer = new ComputeBuffer(data.Length, 76);
        int kernelHandle = shader.FindKernel("Multiply");
        buffer.SetData(data);
        shader.SetBuffer(kernelHandle, "dataBuffer", buffer);
        shader.Dispatch(kernelHandle, data.Length, 1, 1);
        buffer.GetData(output);

        for (int i = 0; i < output.Length; i++) {            
            Debug.Log("PostShader! Pos: " + output[i].point.ToString() + ", Matrix: " + output[i].matrix.ToString());
        }

        buffer.Dispose();

        /*public ComputeShader compute;
        public ComputeBuffer buffer;
        public int[] cols;
   
        void Start () {
        var mesh = GetComponent<MeshFilter>().mesh;
        int n = mesh.vertexCount;
            ///
        buffer = new ComputeBuffer (n, 16);
        ///
        cols = new int[n];
            ///
        for (int i = 0; i < n; ++i)
             cols[i] = 0;      
        buffer.SetData (cols); 
            ///
        compute.SetBuffer(compute.FindKernel ("CSMain"),"bufColors", buffer);
            ///
            compute.Dispatch(0,4,4,1);
        ///
        buffer.GetData(cols);
        Debug.Log (cols[0]); 
        */
    }
    void RunShader()
    {
        data = new VecMatPair[5];
        for (int i = 0; i < data.Length; i++)
        {
            Matrix4x4  mat   = Matrix4x4.identity;
            Vector3    point = Random.insideUnitSphere;
            VecMatPair vmp   = new VecMatPair();
            vmp.point  = point;
            vmp.matrix = mat;
            data[i]    = vmp;
        }

        buffer = new ComputeBuffer(data.Length, (3 + 4 * 4) * sizeof(float));
        buffer.SetData(data);

        kernel = shader.FindKernel("Multiply");
        shader.SetBuffer(kernel, "dataBuffer", buffer);
    }
示例#8
0
    void RunShader()
    {
        VecMatPair[] data   = new VecMatPair[5];
        VecMatPair[] output = new VecMatPair[5];

        for (int i = 0; i < 5; i++)
        {
            data[i].point  = new Vector3();
            data[i].matrix = new Matrix4x4();
        }

        ComputeBuffer buffer = new ComputeBuffer(data.Length, 76);

        buffer.SetData(data);
        int kernel = shader.FindKernel("Multiply");

        shader.SetBuffer(kernel, "dataBuffer", buffer);
        shader.Dispatch(kernel, data.Length, 1, 1);

        //
        buffer.GetData(output);
    }