- Home /
Process multiple buffers with compute shader
I need to process a dynamic number of buffers using a compute shader. Problem, I don't know how to do this and there's sparse information on them online.
As the number is dynamic I can't just do
RWStructuredBuffer<uint> _Buffer1
RWStructuredBuffer<uint> _Buffer2
RWStructuredBuffer<uint> _Buffer3
At first I tried to
for(int i = 0; i < buffers.length; i++)
{
compute.SetBuffer(0, "_BufferToProcess", buffers[i]);
compute.Dispatch(0, 64, 1, 1);
}
But quickly realized that because I'm setting buffers in a loop and Dispatch schedules, the shader will only process the last buffer. Then I tried to Instantiate() multiple copies of the shader and set their buffer accordingly but that didn't work either.
I really don't want to convert multiple buffers into a single one as the data is dynamic, which means I'll need to resize it on every change. Would it be possible for a compute shader to process multiple buffers in one frame? Or is it not a good design to begin with?
Answer by esgnn · Jan 21 at 06:33 PM
It can process multiple buffers but, as far as I know, but compute shader has to know how many buffers in advance. One thing that comes to my mind is to directly edit compute shader source code through a c# script and add variables to the code in the runtime, but I am not sure it is a good idea.
If you can share what you are trying to achieve it will be easier to discuss an easier and robust solution. By the way, when you set buffer and dispatch in a loop like you did, if i am not mistaken, you overwrite previous request.