I am writing to an RWBuffer<int>
using InterlockedAdd
- originally I had an RWBuffer<uint>
but I needed my values to go negative sometimes.
I find that using InterlockedAdd
passing a negative number doesn't update the underlying int buffer - I tested this by using abs() on the value being passed in, and it worked.
I realize using an Add method to add a negative number might seem like "doh ! what did you expect" but there isnt an InterlockedSubtract()
so ...
Is this a known issue that I just haven't managed to find the docs for, or would you normally expect InterlockedAdd(-1)
to subtract 1 from an RWBuffer<int>
like I did ?
I'm not sure how atomics are handled with typed buffers, but they definitely work with structured buffers.
In your case since typed buffer is R32 it would perfectly map to a int structured buffer.
Syntax would be :
RWStructuredBuffer<int> OutputBuffer : register(u0);
Then interlocked operation would be like (if you want to apply it on the 2nd element for example):
uint idx = 1;
uint current_value;
InterlockedAdd(OutputBuffer[idx],-1,current_value);
Buffer creation is slightly different, but nothing too complicated as a change (need to set the structured flag and also set element stride, which is 4 in that case).