Search code examples
directx

ClearUnorderedAccessViewFloat on a Buffer


On a RGBA Texture this works as expected. Each component of the FLOAT[4] argument gets casted to the corresponding component of the DXGI_FORMAT of the texture.

However, with a Buffer this doesn't work and some rubbish is assigned the buffer based on the first component of the FLOAT[4] argument.

Although, this makes sense since a buffer UAV has no DXGI_FORMAT to specify what cast happens.


Solution

  • The docs said

    If you want to clear the UAV to a specific bit pattern, consider using ID3D12GraphicsCommandList::ClearUnorderedAccessViewUint.

    so you can just use as follows:

    float fill_value = ...;
    auto Values = std::array<UINT, 4>{ *((UINT*)&fill_value),0,0,0};
    commandList->ClearUnorderedAccessViewUint(
      ViewGPUHandleInCurrentHeap,
      ViewCPUHandle,
      pResource,
      Values.data(),
      0,
      nullptr
    );
    

    I believe the debug layer should raise a error when using ClearUnorderedAccessViewFloat on a Buffer.

    Edit: it actually does I just missed it.

    D3D12 ERROR: ID3D12CommandList::ClearUnorderedAccessViewUint: ClearUnorderedAccessView* methods are not compatible with Structured Buffers. StructuredByteStride is set to 4 for resource 0x0000023899A09A40'. [ RESOURCE_MANIPULATION ERROR #1156: CLEARUNORDEREDACCESSVIEW_INCOMPATIBLE_WITH_STRUCTURED_BUFFERS]