# 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）

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

（创建buffer的代码）

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.

（注意buffer类型是counter）

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

First notice the way the buffer is declared.

（除以buffer声明，这里不是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的做法：）

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.

（结果是打印0-15的值）

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

（然后考虑怎么减少counter，如下）

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的时候实时得到其大小。）

#### Bysc1991327

##### 4 Comments for “DirectCompute tutorial for Unity 7: Counter Buffers”
1. God help me, I put aside a whole afeoonrtn to figure this out.

2. 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

3. Thomas Nilsen says:

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

4. B says:

Thank you so much for these. These tutorials have helped me no end in university.