calling InterlockedAdd twice in a Compute Shader breaks it

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

Mmh hard to say! Never seen problems like that. The only thing that comes to mind - how many other RW buffers do you have? I think unity has some limit that’s a little lower than the DX11 limit. Could explain why usage makes it break. Maybe also try explicitly binding the RW buffers to some unordered access view register (RWStructureBuffer myBuf: register(u1)); etc. I think u0 is (during rendering) reserved for some unity stuff.

Hope any of that works!