Search Unity

Question Consume buffer doesnt consume anything

Discussion in 'Shaders' started by BotsOP, May 15, 2023.

  1. BotsOP

    BotsOP

    Joined:
    Jun 9, 2020
    Posts:
    9
    Im trying to use a consume buffer in a compute shader but it doesnt matter what I do the vaule consumed is always 0. What am I missing? The docs https://docs.unity3d.com/ScriptReference/ComputeBufferType.Append.html also say that I should use the append type

    hlsl code:
    Code (CSharp):
    1. ConsumeStructuredBuffer<int> consumeValue;
    2. RWStructuredBuffer<int> appendValue;
    3.  
    4. [numthreads(1,1,1)]
    5. void test (uint3 id : SV_DispatchThreadID)
    6. {
    7.     appendValue[id.x] = (consumeValue.Consume());
    8. }
    C# code:
    Code (CSharp):
    1.  
    2.         ComputeBuffer append = new ComputeBuffer(10, sizeof(int), ComputeBufferType.Structured);
    3.         ComputeBuffer consume = new ComputeBuffer(10, sizeof(int), ComputeBufferType.Append);
    4.         append.SetCounterValue(0);
    5.         consume.SetCounterValue(0);
    6.      
    7.         consume.SetData(new int[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
    8.      
    9.         vectorFieldShader.SetBuffer(0, "appendValue", append);
    10.         vectorFieldShader.SetBuffer(0, "consumeValue", consume);
    11.      
    12.         vectorFieldShader.Dispatch(0, 10, 1, 1);
    13.      
    14.         int[] intArray = new int[10];
    15.         append.GetData(intArray);
    16.         int[] intArray2 = new int[10];
    17.         consume.GetData(intArray2);
    18.  
     
  2. BotsOP

    BotsOP

    Joined:
    Jun 9, 2020
    Posts:
    9
    the consume buffer returns 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 and the structured buffer returns 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
     
  3. BotsOP

    BotsOP

    Joined:
    Jun 9, 2020
    Posts:
    9
    I found the problem you have to set the counter value of the consume buffer to 10 for this code to work