All ComputeShader GPU

DirectCompute tutorial for Unity 7: Counter Buffers

So to continue this tutorial is about counter buffers. All the compute buffer types in Unity have a count property. For structured buffers this is a fixed value as they act like an array. For append buffers the count changes as the buffer is appended/consumed from as they act like a dynamic array.

(所有类型buffer都有count property)


Direct Compute also provides a way to manually increment and decrement the count value. This gives you greater freedom over how the buffer stores its elements and should allow for custom data containers to be implemented. I have seen this used to create an array of linked list all completely done on the GPU.

(count property 支持append/consume buffer的实时大小衡量)


First start by creating a new script and paste in the following code. The code does not do anything interesting. It just creates the buffer, runs the shader and then prints out the buffer contents.

(代码:打印buffer contents)


public class CounterBufferExample : MonoBehaviour


    public ComputeShader incrementCount;

    void Start ()


        int size = 4 * 4 * 1 * 1;

        ComputeBuffer counterBuffer = new ComputeBuffer(size, sizeof(int), ComputeBufferType.Counter);


        incrementCount.SetBuffer(0, "counterBuffer", counterBuffer);

        incrementCount.Dispatch(0, 1, 1, 1);

        int[] data = new int[size];


        for(int i = 0; i < data.Length; i++)






On this line you will see the creation of the buffer.



ComputeBuffer counterBuffer = new ComputeBuffer(size, sizeof(int), ComputeBufferType.Counter);



Note the buffers type is counter. The buffer count value is also set to zero. I recommend doing this when the buffer is created as Unity will not always create the buffer with its count set to zero. I am not sure why. I think it maybe a bug.



Next create a new compute shader and paste in the following code.



#pragma kernel CSMain

#define THREADS 4

#define GROUPS 1

RWStructuredBuffer<int> counterBuffer;


void CSMain (uint3 dispatchID : SV_DispatchThreadID)


    uint id = dispatchID.x + dispatchID.y * THREADS * GROUPS;

    int count = counterBuffer.IncrementCounter();

    counterBuffer[id] = count;



First notice the way the buffer is declared.



RWStructuredBuffer<int> counterBuffer;

It’s just declared as a structured buffer. There is no counter type buffer.


The buffers count value is then incremented here. The increment function will also return what the count value was before it gets incremented.

(函数中则是增加counter,返回buffer size的做法:)


int count = counterBuffer.IncrementCounter();

Then the count is stored in the buffer so we can print out the contents to check it worked.

(这样就可以打印出buffer size)


If you run the scene you should see the numbers 0 – 15 printed out.



So how do you decrement the counter? You guessed it. With the decrement function.



int count = counterBuffer.DecrementCounter();

The decrement function will also return what the count value was after it gets decremented.


Now let’s say you have run a shader that increments and adds elements to the buffer but you don’t know how many were added. How do you find out the count of the buffer? Well you may recall from the append buffer tutorial that you can use Unity’s CopyCount function to retrieve the count value. You can do the same with the counter buffer. Add this code to the end of the start function. It should print out that the buffers count is 16.

(append buffer结合使用counter buffer就可以做到你增加buffer size的时候实时得到其大小。)


ComputeBuffer argBuffer = new ComputeBuffer(1, sizeof(int), ComputeBufferType.IndirectArguments);

int[] args = new int[] { 0 };

ComputeBuffer.CopyCount(counterBuffer, argBuffer, 0);


Debug.Log("Count " + args[0]);




4 thoughts on “DirectCompute tutorial for Unity 7: Counter Buffers”

  1. Unquestionably consider that that you said. Your favorite justification appeared to be on the web the simplest factor to take into accout of. I say to you, I certainly get irked at the same time as people consider worries that they plainly do not know about. You managed to hit the nail upon the highest as well as outlined out the whole thing with no need side effect , folks could take a signal. Will probably be back to get more. Thanks

  2. This tutorial series was great and I’ve learnt a lot from them.
    Thanks for beeing awesome and sharing your knowledge! =)

Comments are closed.