Let’s say I have result data that kernel “compute” evaluates from buffer data, at the end of this evaluate I want all this result data to be copied over to buffer so the next execution. How do I do that?
I currently have two kernels, “compute” and “swap” and after I dispatch compute I dispatch swap. Not working, maybe because compute buffers are local to one kernel.
I read somewhere than you do this swap thing by setting up registry, then my brain melted.
Is there a way to have the compute shader somehow run that buffer swap at the end of all the threads, somehow? Or do I need to dispatch sequentially from the c# code?
Note: the end result will be copied over to an array in c# land.
I would do the swap this way: on C# side there would be a variable that contains buffer index. At start of each Update() it swaps to the next index, then buffer.SetInt() gets called to put this buffer index to shader side.
And then you run your “compute” shader, that writes its results to one of the buffers, depending on the buffer index.
And then you read one of the buffers depending on buffer’s index variable. So it would be something like this:
though, I heard those conditions don’t do well, so you might consider having two versions of compute kernel, each will write to its own buffer, and you will call one of them depending on bufferIndex.
No it’s because I want to keep blurring this field of data as time passes, that’s how I do diffusion.
And it’s not a swap, it’s a copy to buffer.
I copy to buffer to avoid race condition, but maybe gpu don’t have that, I’ll try result[index] = (emitter[index] + result[index+1] + result[index-1]) / 3 and see if it causes artifacts.
so I verified with this simple shader and I don’t see any artifact so I’ll just do that, it seems that gpu store outputs in a buffer, or maybe the blur hides the artifact good enough for my purposes