Search code examples
hlsl

What happens when you call RWByteAddressBuffer.InterlockedAdd?


I usually program in OpenGL but I need to translate some code from HLSL.

I have seen the documentation of RWByteAddressBuffer, but I'm not sure how it works with InterlockedAdd.

Suposedly, RWByteAddressBuffer is addressed by bytes. However, I think InterlockedAdd works on uints, which are 32-bit unsigned integers.

My doubt is, what happens when I do this?

uint c;
buffer.InterlockedAdd(0, 1, c);

Am I incrementing the first byte of the buffer, or the first 32-bit word of the buffer? What if I use offsets that are not multiples of 4?


Solution

  • When you call :

    uint c;
    uint location = 0;
    buffer.InterlockedAdd(location, 1, c);
    

    Location is specified in bytes, but you are indeed increasing 1 on the first 4 bytes (int size).

    As per documentation, ByteAddressBuffer/RWByteAddressBuffer memory offsets need to be multiple of 4, anything else is undefined.

    See (Byte Address Buffer section): https://learn.microsoft.com/en-us/windows/win32/direct3d11/direct3d-11-advanced-stages-cs-resources