Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

ComputeBuffer.count

Discussion in 'General Graphics' started by kelloh, Feb 28, 2019.

  1. kelloh

    kelloh

    Joined:
    Mar 2, 2015
    Posts:
    29
    If I create a ComputeBuffer with ComputeBufferType.Append and some capacity, is the buffer's .count property the capacity I created it with, or the count of the number of items actually .Appended to it inside the compute shader?

    I feel like the docs are confusing here.
     
    gattusoarthur likes this.
  2. GamesEngineer

    GamesEngineer

    Joined:
    Aug 22, 2014
    Posts:
    9
    The .count property is the total capacity of the buffer. In order to get the buffer's current counter value in C# script, you'll need to do something like this:

    Code (CSharp):
    1. private ComputeBuffer AppendBuffer;
    2. private ComputeBuffer CounterValueBuffer; // holds one uint value
    3. private AsyncGPUReadbackRequest asyncRequest;
    4.  
    5. void Update()
    6. {
    7.     // Process previous frame's data
    8.     asyncRequest.WaitForCompletion(); // HACK - avoid this blocking wait if possible
    9.     if (!asyncRequest.hasError)
    10.     {
    11.         int numItemsInAppendBuffer = (int)(asyncRequest.GetData<uint>()[0]);
    12.         DoSomething(numItemsInAppendBuffer);
    13.     }
    14.  
    15.     DispatchYourComputeShaderKernel();
    16.  
    17.     // Asynchronously get the buffer's counter value
    18.     int byteOffset = 0;
    19.     ComputeBuffer.CopyCount(AppendBuffer, CounterValueBuffer, byteOffset);
    20.     asyncRequest = AsyncGPUReadback.Request(CounterValueBuffer, CounterValueBuffer.stride, byteOffset);
    21. }
     
    gattusoarthur likes this.