'InterlockedAdd on R32_Sint with negative number
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 ?
Solution 1:[1]
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).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mrvux |
