Search Unity

Question ComputeShader's dispatcher schedule the dispatches as a queue?

Discussion in 'Shaders' started by randomdragon, Aug 30, 2021.

  1. randomdragon

    randomdragon

    Joined:
    Feb 11, 2020
    Posts:
    31
    For example, I have several different compute shaders which each has several kernels.
    Now I dispatch them one by one adjacently:
    Code (CSharp):
    1.  
    2. cs1.Dispatch(cs1.FindKernel("Pass1"),100,1,1);
    3. cs1.Dispatch(cs1.FindKernel("Pass2"),100,1,1);
    4.  
    5. cs2.Dispatch(cs2.FindKernel("Pass3"),100,1,1);
    6. cs2.Dispatch(cs2.FindKernel("Pass4"),100,1,1);
    7.  
    8. cs3.Dispatch(cs3.FindKernel("Pass5"),100,1,1);
    9. cs3.Dispatch(cs3.FindKernel("Pass6"),100,1,1);
    10.  
    These compute shaders and kernels of them share the same compute buffer called buffer1.
    Pass1 will generate data into buffer1, Pass2 will read input from buffer1 and generate data into buffer1, Pass3 ........., Pass6 will read input from buffer1 and generate data into buffer1.
    Will the code above work fine which means the dispatches will be executed one after another and in the order of the dispatch code order?

    And if so, what will the result of AsyncGPUReadbackRequest be after multiple dispatched?
    Like when I call a request after the code above, will the request.GetData finally get the data of the output of Pass6?
    And when I call a request after the Pass3 dispatch, will the request.GetData finally get the data of the output of Pass3?
     
  2. sky247

    sky247

    Joined:
    Apr 20, 2023
    Posts:
    2
    The code you provided will dispatch the compute shaders one after the other in the specified order. Since all of the compute shaders share the same compute buffer, the output of each pass will be written to buffer1 and will be used as the input for the next pass.

    As for the AsyncGPUReadbackRequest, it will retrieve the data from the buffer at the time the request is made. So if you call a request after the Pass6 dispatch, the request.GetData will retrieve the data of the output of Pass6. If you call a request after the dispatch, the request.GetData will retrieve the data of the output of Pass3.

    Note that if you need to synchronize the execution of the dispatches to ensure that each pass has completed before the next one begins, you may need to use synchronization techniques such as barriers or events.