From 969e7ed88fdc1bb0326c0727ea1b7ece8b6acf95 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 29 Dec 2024 20:29:18 -0800 Subject: [PATCH] D3D11: fixed validation error when a UAV is used in PS after being used in CS --- .../src/DeviceContextD3D11Impl.cpp | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index 732bdbd8a..c94fea2df 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -314,13 +314,41 @@ void DeviceContextD3D11Impl::BindCacheResources(const ShaderResourceCacheD3D11& if (ShaderInd == PSInd && PsUavBindMode != PixelShaderUAVBindMode::Bind) PsUavBindMode = PixelShaderUAVBindMode::Keep; - auto* d3d11UAVs = m_CommittedRes.d3d11UAVs[ShaderInd]; - auto* d3d11UAVRes = m_CommittedRes.d3d11UAVResources[ShaderInd]; + ID3D11UnorderedAccessView** d3d11UAVs = m_CommittedRes.d3d11UAVs[ShaderInd]; + ID3D11Resource** d3d11UAVRes = m_CommittedRes.d3d11UAVResources[ShaderInd]; if (auto Slots = ResourceCache.BindResourceViews(ShaderInd, d3d11UAVs, d3d11UAVRes, BaseBindings)) { if (ShaderInd == PSInd) { PsUavBindMode = PixelShaderUAVBindMode::Bind; + + // In Direct3D11, a resource can only be bound as UAV once. + // Check if any resource in the range is currently bound as compute shader UAV and unbind it. + if (Uint8& NumCsUavSlots = m_CommittedRes.NumUAVs[CSInd]) + { + ID3D11UnorderedAccessView** d3d11CsUAVs = m_CommittedRes.d3d11UAVs[CSInd]; + ID3D11Resource** d3d11CsUAVRes = m_CommittedRes.d3d11UAVResources[CSInd]; + for (Uint32 ps_slot = Slots.MinSlot; ps_slot <= Slots.MaxSlot; ++ps_slot) + { + if (ID3D11Resource* d3d11Res = d3d11UAVRes[ps_slot]) + { + for (Uint32 cs_slot = 0; cs_slot < NumCsUavSlots; ++cs_slot) + { + if (d3d11CsUAVRes[cs_slot] == d3d11Res) + { + d3d11CsUAVs[cs_slot] = nullptr; + d3d11CsUAVRes[cs_slot] = nullptr; + m_pd3d11DeviceContext->CSSetUnorderedAccessViews(cs_slot, 1, &d3d11CsUAVs[cs_slot], nullptr); + } + } + } + } + while (NumCsUavSlots > 0 && d3d11CsUAVRes[NumCsUavSlots - 1] == nullptr) + { + VERIFY_EXPR(d3d11CsUAVs[NumCsUavSlots - 1] == nullptr); + --NumCsUavSlots; + } + } } else if (ShaderInd == CSInd) {