2

In “3D Game programming with DirectX 12”(the dragon book) chapter 13 Geometry Shader.It describes an example about using AppendStructuredBuffer and ConsumeStructuredBuffer, but there's no detailed code about this.I tried to use these kind of buffers but failed.So I wonder if it's allowed to use AppendStructuredBuffer or ConsumeStructuredBuffer in DirectX12?How to use it, how should I set rootSignature?

ConsumeStructuredBuffer<int3> gInput : register(u0);
AppendStructuredBuffer<float> gOutput : register(u1);

[numthreads(64, 1, 1)]
void CS(int3 dispatchThreadID : SV_DispatchThreadID)
{
    // gOutput[dispatchThreadID.x] = length(gInput[dispatchThreadID.x]);
    int3 p = gInput.Consume();
    gOutput.Append(length(float3(p)));
}

I find some tips about using AppendStructuredBuffer in the official document of directx12.

It says:

The UAV format bound to this resource needs to be created with the DXGI_FORMAT_UNKNOWN format.
The UAV bound to this resource must have been created with D3D11_BUFFER_UAV_FLAG_APPEND.

So I tried to build UAV,its heap and use descriptor table.When I filling in the members of D3D12_UNORDERED_ACCESS_VIEW_DESC, I got stuck.

D3D12_UNORDERED_ACCESS_VIEW_DESC uavDesc;
uavDesc.Format = DXGI_FORMAT_UNKNOWN; 
uavDesc.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
uavDesc.Buffer.FirstElement = 0;
uavDesc.Buffer.NumElements = 64;
uavDesc.Buffer.Flags = D3D11_BUFFER_UAV_FLAG_APPEND; // ????

There's no such member of enumeration in DirectX 12.

1 Answers1

2

In d3d11, append buffer used to be stored in an opaque manner, so you had to specify the flag when creating the UAV.

In d3d12, this counter is stored in a gpu buffer that you provide.

If we look at ID3D12Device::CreateUnorderedAccessView,

void CreateUnorderedAccessView(
       ID3D12Resource                         *pResource,
       ID3D12Resource                         *pCounterResource,
       const D3D12_UNORDERED_ACCESS_VIEW_DESC *pDesc,
       [in] D3D12_CPU_DESCRIPTOR_HANDLE            DestDescriptor
);

The pCounterResource specifies the buffer that contains the counter, so if you set it to nullptr, you can use this UAV only with RW type, if you specify a buffer you are allowed to use it in a shader as either Append/Consume or use Increment/Decrement counter.

Please note that resetting the counter is now done by yourself.

This is done using : ID3D12GraphicsCommandList::ClearUnorderedAccessViewUint

You are also allowed to use compute shader to change that buffer value, so you are not limited to CPU only reset, which is nice in some use cases.

mateeeeeee
  • 885
  • 1
  • 6
  • 15
mrvux
  • 8,523
  • 1
  • 27
  • 61