[D3D12] ResourceBarrier State Mismatch Error in DispatchRays (Lumen + RDG)

I was hitting this too in 5.7.4 while trying to use -d3ddebug, and after debugging, the issue stems from this seemingly innocuous line in GetD3D12ResourceState in D3D12LegacyBarriers.cpp:

// Translate the requested after state to a D3D state
if (EnumHasAnyFlags(D3D12AccessWithoutDiscard, ED3D12Access::SRVGraphics) && InQueueType == ED3D12QueueType::Direct)
{
	State |= D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | ExtraReadState;
}

SRVGraphics is 2 flags OR’d together (SRVGraphicsPixel | SRVGraphicsNonPixel), which means if either flag is true, EnumHasAnyFlags returns true. In this Lumen case, because SRVGraphicsNonPixel is set, the if is evaluated to true and it OR’s in the wrong flags into State. Splitting the if up into 2 if’s that check SRVGraphicsPixel and SRVGraphicsNonPixel separately and then ORing in the appropriate D3D12 flag fixes the issue.

// Translate the requested after state to a D3D state
if (EnumHasAnyFlags(D3D12AccessWithoutDiscard, ED3D12Access::SRVGraphicsPixel) && InQueueType == ED3D12QueueType::Direct)
{
	State |= D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | ExtraReadState;
}
if (EnumHasAnyFlags(D3D12AccessWithoutDiscard, ED3D12Access::SRVGraphicsNonPixel))
{
	State |= D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE | ExtraReadState;
}

NVidia’s Streamline also seems to have a similar bug.

Now I’m hitting a lot of

Error: [D3DDebug] ID3D12CommandQueue1::ExecuteCommandLists: Placed resources, reserved resources, or committed resources with D3D12_HEAP_FLAG_CREATE_NOT_ZEROED flag with either render target or depth stencil flags must be initialized with a Discard/Clear/Copy operations before other operations are supported

though :slight_smile: , so I’ll have to dig into this.