Understanding UAV counters

Started by
2 comments, last by fido9dido 2 years, 4 months ago
typedef struct D3D12_BUFFER_UAV {
UINT64 FirstElement;
UINT NumElements;
UINT StructureByteStride;
UINT64 CounterOffsetInBytes; // The counter offset, in bytes.
D3D12_BUFFER_UAV_FLAGS Flags;
} D3D12_BUFFER_UAV;

say I have a buffer of 2 UAV for simplicity with a UAV counter

the first is 10 objects lets name them struct input

the second is 20 objects let's call it struct output

and a buffer of size 10*sizeof(input)+ 20*sizeof(output)+sizeof(UINT)

so I will have to make two D3D12_UNORDERED_ACCESS_VIEW_DESC

for the first description

FirstElement=0

NumElements =10

StructureByteStride= sizeof(Input)

CounterOffsetInBytes=?? // The counter offset, in bytes.

for the second description

FirstElement=10

NumElements =20

StructureByteStride =sizeof(output)

CounterOffsetInBytes=?? // The counter offset, in bytes.

1- What is CounterOffsetInBytes and how to set it, for instance where the counter is located at the beginning or at the end the doc doesn't mention.

2- am I suppose to make a counter for each buffer or one will buffer will be enough, if i need to make two counters then how does it work

Advertisement

It's for the “hidden” counter that can be used with a structured buffer. In your shaders you access it through the IncrementCounter() and DecrementCounter() methods on RWStructuredBuffer, or by using the AppendStructuredBuffer/ConsumeStructuredBuffer types. To use it you need to have 4 bytes somewhere in a resource (which you pass in as the pCounterResource of CreateUnorderedAccessView), and you need to specify where those 4 bytes are located relative to the start of the resource using CounterOffsetInBytes. Since it sounds like you have just one resource, your counter would be located at an offset of 10*sizeof(input)+ 20*sizeof(output). Be aware though that the offset of your counter needs to be a multiple of D3D12_UAV_COUNTER_PLACEMENT_ALIGNMENT which is 4096, so you would likely need to add a lot of padding to satisfy that requirement.

The hidden counter is mostly a relic of D3D11 where it could use some fast paths in certain hardware. If you just want a generic atomic counter that you can increment and decrement, you can just create a UAV for a RWByteAddressBuffer and call InterlockedAdd on it. “Raw” buffers only need 16-byte alignment when you create the UAV or SRV.

ah thank you

This topic is closed to new replies.

Advertisement