Hello! I’m trying to get some data from my compute shader in custom pass with compute buffers.
My C# code in custom pass looks like this (this part in Execute method):
I always get the value = 1.
I see that the compute shader works in Frame Debugger, but why it doesn’t rewrite data in a buffer?
Maybe in the case of a custom pass, we need to use something else?
Thanks in advance!
When you write code that uses graphics command buffers, you have to keep in mind that the execution is deferred. In the case of custom passes, the execution of the command buffer is controlled by the HDRP render loop, so in your code, the compute shader will actually be executed after your GetData() which explains the result.
For reading back data from the GPU, it’s recommended to use the async readback API: Unity - Scripting API: AsyncGPUReadbackRequest This API doesn’t stall the CPU while waiting for the memory transfer so it’s more efficient, though it’s possible that in some cases you end up with a frame of delay between your compute shader execution and getting back your data.
If you want to execute the compute shader immediately, then you could either use a local command buffer or the Dispatch function on the compute. Though keep in mind that with this solution you won’t be able to access HDRP rendering information as your code might be executed outside of the render loop.
Thanks for the answer!
Can I clarify, I understand correctly, that I cannot get information instantly in a custom pass (inside Execute() method) if I make a call DispatchCompute(), as above in 11 line? Or all the same, if it can be done, point out an error in the code, or please give a small example with a local command buffer.
In general, this is strange, the textures can be overwritten in the same way. I thought that you might mean the Debug.Log function works, but I checked the output of the value to the texture channel (without call .GetData(), sended it like this:
ctx.propertyBlock.SetConstantBuffer
) and this does not work either.