I’m working on a Compute Shader particle system and I’m trying to use InterlockedAdd to accumulate some spatial information to read back to the CPU. Each particle increments an index of two RWStructuredBuffer buffers inside its update:
InterlockedAdd(countBuffer[index], 1);
InterlockedAdd(speedBuffer[index], (int)(currentSpeed * 1000.0));
What I have found is that when I run both of these lines, it breaks and my particles don’t render anymore. If I comment out one line or the other, it works fine. It doesn’t matter which line I comment out, so I’m pretty sure there is nothing wrong with how any of it is set up.
I also discovered that if I do two calls using InterlockedAdd to the same buffer, different indexes and different values, it also works. Something like this:
InterlockedAdd(speedBuffer[index+1], 1);
InterlockedAdd(speedBuffer[index], (int)(currentSpeed * 1000.0));
So if all else fails, I could just use one big buffer and offset the speedBuffer part of it by the size of the countBuffer, but I’d really rather not if it can be avoided.
Am I missing something about how InterlockedAdd is supposed to be used?
-A