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

Question Preprocessing Compute Buffer Data

Discussion in 'Shaders' started by magebuzzcutt, Jul 15, 2023.

  1. magebuzzcutt

    magebuzzcutt

    Joined:
    Jun 20, 2021
    Posts:
    16
    Hi,

    I am new to compute shaders and currently use a compute shader to read a normal buffer and albedo buffer and based on thresholds, append pixel information to an append compute buffer. The compute buffer persists through frames, and I want to remove elements based on the current time and the time at which the element was appended to the compute buffer. So to do this I want to preprocess the compute buffer data. So I've got the code that goes something like this:

    Code (CSharp):
    1.  
    2. //COPY BUFFER
    3.             buffer.SetComputeBufferParam(CopyBufferCompute, 0, thresholdPointsId, thresholdPoints);
    4.             buffer.SetComputeBufferParam(CopyBufferCompute, 0, copyPointsId, copyPoints);
    5.             int dispatchCount = Mathf.CeilToInt(bufferSize/ 64.0f);
    6.             buffer.DispatchCompute(CopyBufferCompute, 0, dispatchCount, 1, 1);
    7.  
    8.             // PREPROCESS BUFFER
    9.             if (thresholdPoints != null)
    10.                 thresholdPoints.Release();
    11.             thresholdPoints = new ComputeBuffer(bufferSize, bufferStride, ComputeBufferType.Append);  
    12.             thresholdPoints.SetCounterValue(0);
    13.  
    14.             buffer.SetComputeFloatParam(preprocessBufferCompute, "_Time", Time.time);
    15.             buffer.SetComputeBufferParam(preprocessBufferCompute, 0, thresholdPointsId, thresholdPoints);
    16.             buffer.SetComputeBufferParam(preprocessBufferCompute, 0, copyPointsId, copyPoints);
    17.             dispatchCount = Mathf.CeilToInt(bufferSize/ 64.0f);
    18.             buffer.DispatchCompute(preprocessBufferCompute, 0, dispatchCount, 1, 1);
    19.  
    20.            // Append to thresholdBuffer based on albedo and normal buffers
    21.            /*
    22.            ..........................
    23.             buffer.DispatchCompute(thresholdBufferCompute, 0,
    24.                                     Mathf.CeilToInt(bufferSize.x / groupSizeX),
    25.                                     Mathf.CeilToInt(bufferSize.y / groupSizeY),
    26.                                     1
    27.             );
    28.             */
    29.  
    So first, I copy the buffer with the pixel information (thresholdPoints) to a temporary copy points buffer. Then I clear the thresholdPoints buffer and use the copied buffer to check the time and append it if the change in time is less than x seconds. However, trying to implement this does not work. I think the issue I am at right now is that the thresholdPoints buffer is cleared while the dispatch for CopyBufferCompute is running. But I'm not sure how to get around it. Does anyone that knows about compute shaders able to give me pointers about how to use them or any good tutorials?

    TIA
     
  2. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    177
    You have one obvious mistake: you are destroying "thresholdPoints" buffer (line 10) while it is used in your CommandBuffer. Command buffers built around delayed execution principle, so at a time of execution of line 3 "thresholdPoints" cannot be set because it is alreay destroyed. And clearing compute buffer by recreating is a very bad practice. In your case you should just reset counter of existing buffer, but you should do this as command buffer command invocation. So, instead of lines 9-12 in your code snippet, put the following:
    Code (CSharp):
    1.  
    2. buffer.SetBufferCounterValue(thresholdPoints, 0);
    3.  
     
    magebuzzcutt likes this.
  3. magebuzzcutt

    magebuzzcutt

    Joined:
    Jun 20, 2021
    Posts:
    16
    do you know if the compute shaders run in parallel or are sequential? I want these compute shaders to complete before the next compute shaders run.
     
  4. Rukhanka

    Rukhanka

    Joined:
    Dec 14, 2022
    Posts:
    177
    Unless you are explicitly using async compute queue, all GPU commands are executed sequentially. So, answer to your question is: they will run sequentially.
     
    magebuzzcutt likes this.