From 1b4cb66dc360d1a798163cfe0e65cfea87777ecc Mon Sep 17 00:00:00 2001 From: Philip Rebohle Date: Thu, 4 Aug 2022 15:56:49 +0200 Subject: [PATCH] [d3d11] Refactor shader resource state --- src/d3d11/d3d11_context.cpp | 119 +++++++++++++------------------- src/d3d11/d3d11_context.h | 9 +-- src/d3d11/d3d11_context_state.h | 33 +++++---- 3 files changed, 71 insertions(+), 90 deletions(-) diff --git a/src/d3d11/d3d11_context.cpp b/src/d3d11/d3d11_context.cpp index a6c60d24..4688f147 100644 --- a/src/d3d11/d3d11_context.cpp +++ b/src/d3d11/d3d11_context.cpp @@ -1361,9 +1361,7 @@ namespace dxvk { D3D10DeviceLock lock = LockContext(); SetShaderResources( - m_state.vs.shaderResources, - StartSlot, NumViews, - ppShaderResourceViews); + StartSlot, NumViews, ppShaderResourceViews); } @@ -1431,7 +1429,7 @@ namespace dxvk { ID3D11ShaderResourceView** ppShaderResourceViews) { D3D10DeviceLock lock = LockContext(); - GetShaderResources(m_state.vs.shaderResources, + GetShaderResources( StartSlot, NumViews, ppShaderResourceViews); } @@ -1503,9 +1501,7 @@ namespace dxvk { D3D10DeviceLock lock = LockContext(); SetShaderResources( - m_state.hs.shaderResources, - StartSlot, NumViews, - ppShaderResourceViews); + StartSlot, NumViews, ppShaderResourceViews); } @@ -1573,7 +1569,7 @@ namespace dxvk { ID3D11ShaderResourceView** ppShaderResourceViews) { D3D10DeviceLock lock = LockContext(); - GetShaderResources(m_state.hs.shaderResources, + GetShaderResources( StartSlot, NumViews, ppShaderResourceViews); } @@ -1645,9 +1641,7 @@ namespace dxvk { D3D10DeviceLock lock = LockContext(); SetShaderResources( - m_state.ds.shaderResources, - StartSlot, NumViews, - ppShaderResourceViews); + StartSlot, NumViews, ppShaderResourceViews); } @@ -1715,7 +1709,7 @@ namespace dxvk { ID3D11ShaderResourceView** ppShaderResourceViews) { D3D10DeviceLock lock = LockContext(); - GetShaderResources(m_state.ds.shaderResources, + GetShaderResources( StartSlot, NumViews, ppShaderResourceViews); } @@ -1787,9 +1781,7 @@ namespace dxvk { D3D10DeviceLock lock = LockContext(); SetShaderResources( - m_state.gs.shaderResources, - StartSlot, NumViews, - ppShaderResourceViews); + StartSlot, NumViews, ppShaderResourceViews); } @@ -1857,7 +1849,7 @@ namespace dxvk { ID3D11ShaderResourceView** ppShaderResourceViews) { D3D10DeviceLock lock = LockContext(); - GetShaderResources(m_state.gs.shaderResources, + GetShaderResources( StartSlot, NumViews, ppShaderResourceViews); } @@ -1929,9 +1921,7 @@ namespace dxvk { D3D10DeviceLock lock = LockContext(); SetShaderResources( - m_state.ps.shaderResources, - StartSlot, NumViews, - ppShaderResourceViews); + StartSlot, NumViews, ppShaderResourceViews); } @@ -1999,7 +1989,7 @@ namespace dxvk { ID3D11ShaderResourceView** ppShaderResourceViews) { D3D10DeviceLock lock = LockContext(); - GetShaderResources(m_state.ps.shaderResources, + GetShaderResources( StartSlot, NumViews, ppShaderResourceViews); } @@ -2071,9 +2061,7 @@ namespace dxvk { D3D10DeviceLock lock = LockContext(); SetShaderResources( - m_state.cs.shaderResources, - StartSlot, NumViews, - ppShaderResourceViews); + StartSlot, NumViews, ppShaderResourceViews); } @@ -2198,7 +2186,7 @@ namespace dxvk { ID3D11ShaderResourceView** ppShaderResourceViews) { D3D10DeviceLock lock = LockContext(); - GetShaderResources(m_state.cs.shaderResources, + GetShaderResources( StartSlot, NumViews, ppShaderResourceViews); } @@ -3769,14 +3757,16 @@ namespace dxvk { template + template void D3D11CommonContext::GetShaderResources( - const D3D11ShaderResourceBindings& Bindings, UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView** ppShaderResourceViews) { + const auto& bindings = m_state.srv[ShaderStage]; + for (uint32_t i = 0; i < NumViews; i++) { - ppShaderResourceViews[i] = StartSlot + i < Bindings.views.size() - ? Bindings.views[StartSlot + i].ref() + ppShaderResourceViews[i] = StartSlot + i < bindings.views.size() + ? bindings.views[StartSlot + i].ref() : nullptr; } } @@ -3914,6 +3904,7 @@ namespace dxvk { // Reset resource bindings m_state.cbv.reset(); + m_state.srv.reset(); // Default samplers for (uint32_t i = 0; i < D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT; i++) { @@ -3925,23 +3916,6 @@ namespace dxvk { m_state.cs.samplers[i] = nullptr; } - // Default shader resources - for (uint32_t i = 0; i < D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT; i++) { - m_state.vs.shaderResources.views[i] = nullptr; - m_state.hs.shaderResources.views[i] = nullptr; - m_state.ds.shaderResources.views[i] = nullptr; - m_state.gs.shaderResources.views[i] = nullptr; - m_state.ps.shaderResources.views[i] = nullptr; - m_state.cs.shaderResources.views[i] = nullptr; - } - - m_state.vs.shaderResources.hazardous.clear(); - m_state.hs.shaderResources.hazardous.clear(); - m_state.ds.shaderResources.hazardous.clear(); - m_state.gs.shaderResources.hazardous.clear(); - m_state.ps.shaderResources.hazardous.clear(); - m_state.cs.shaderResources.hazardous.clear(); - // Default UAVs for (uint32_t i = 0; i < D3D11_1_UAV_SLOT_COUNT; i++) { m_state.ps.unorderedAccessViews[i] = nullptr; @@ -4016,29 +3990,30 @@ namespace dxvk { template template void D3D11CommonContext::ResolveSrvHazards( - T* pView, - D3D11ShaderResourceBindings& Bindings) { + T* pView) { + auto& bindings = m_state.srv[ShaderStage]; + uint32_t slotId = computeSrvBinding(ShaderStage, 0); - int32_t srvId = Bindings.hazardous.findNext(0); + int32_t srvId = bindings.hazardous.findNext(0); while (srvId >= 0) { - auto srv = Bindings.views[srvId].ptr(); + auto srv = bindings.views[srvId].ptr(); if (likely(srv && srv->TestHazards())) { bool hazard = CheckViewOverlap(pView, srv); if (unlikely(hazard)) { - Bindings.views[srvId] = nullptr; - Bindings.hazardous.clr(srvId); + bindings.views[srvId] = nullptr; + bindings.hazardous.clr(srvId); BindShaderResource(slotId + srvId, nullptr); } } else { // Avoid further redundant iterations - Bindings.hazardous.clr(srvId); + bindings.hazardous.clr(srvId); } - srvId = Bindings.hazardous.findNext(srvId + 1); + srvId = bindings.hazardous.findNext(srvId + 1); } } @@ -4048,7 +4023,7 @@ namespace dxvk { void D3D11CommonContext::ResolveCsSrvHazards( T* pView) { if (!pView) return; - ResolveSrvHazards (pView, m_state.cs.shaderResources); + ResolveSrvHazards(pView); } @@ -4057,11 +4032,11 @@ namespace dxvk { void D3D11CommonContext::ResolveOmSrvHazards( T* pView) { if (!pView) return; - ResolveSrvHazards (pView, m_state.vs.shaderResources); - ResolveSrvHazards (pView, m_state.hs.shaderResources); - ResolveSrvHazards (pView, m_state.ds.shaderResources); - ResolveSrvHazards (pView, m_state.gs.shaderResources); - ResolveSrvHazards (pView, m_state.ps.shaderResources); + ResolveSrvHazards(pView); + ResolveSrvHazards(pView); + ResolveSrvHazards(pView); + ResolveSrvHazards(pView); + ResolveSrvHazards(pView); } @@ -4164,12 +4139,12 @@ namespace dxvk { RestoreSamplers (m_state.ps.samplers); RestoreSamplers (m_state.cs.samplers); - RestoreShaderResources (m_state.vs.shaderResources); - RestoreShaderResources (m_state.hs.shaderResources); - RestoreShaderResources (m_state.ds.shaderResources); - RestoreShaderResources (m_state.gs.shaderResources); - RestoreShaderResources (m_state.ps.shaderResources); - RestoreShaderResources (m_state.cs.shaderResources); + RestoreShaderResources(); + RestoreShaderResources(); + RestoreShaderResources(); + RestoreShaderResources(); + RestoreShaderResources(); + RestoreShaderResources(); RestoreUnorderedAccessViews (m_state.ps.unorderedAccessViews); RestoreUnorderedAccessViews (m_state.cs.unorderedAccessViews); @@ -4202,12 +4177,12 @@ namespace dxvk { template template - void D3D11CommonContext::RestoreShaderResources( - D3D11ShaderResourceBindings& Bindings) { + void D3D11CommonContext::RestoreShaderResources() { + const auto& bindings = m_state.srv[Stage]; uint32_t slotId = computeSrvBinding(Stage, 0); - for (uint32_t i = 0; i < Bindings.views.size(); i++) - BindShaderResource(slotId + i, Bindings.views[i].ptr()); + for (uint32_t i = 0; i < bindings.views.size(); i++) + BindShaderResource(slotId + i, bindings.views[i].ptr()); } @@ -4332,16 +4307,16 @@ namespace dxvk { template template void D3D11CommonContext::SetShaderResources( - D3D11ShaderResourceBindings& Bindings, UINT StartSlot, UINT NumResources, ID3D11ShaderResourceView* const* ppResources) { + auto& bindings = m_state.srv[ShaderStage]; uint32_t slotId = computeSrvBinding(ShaderStage, StartSlot); for (uint32_t i = 0; i < NumResources; i++) { auto resView = static_cast(ppResources[i]); - if (Bindings.views[StartSlot + i] != resView) { + if (bindings.views[StartSlot + i] != resView) { if (unlikely(resView && resView->TestHazards())) { if (TestSrvHazards(resView)) resView = nullptr; @@ -4349,10 +4324,10 @@ namespace dxvk { // Only set if necessary, but don't reset it on every // bind as this would be more expensive than a few // redundant checks in OMSetRenderTargets and friends. - Bindings.hazardous.set(StartSlot + i, resView); + bindings.hazardous.set(StartSlot + i, resView); } - Bindings.views[StartSlot + i] = resView; + bindings.views[StartSlot + i] = resView; BindShaderResource(slotId + i, resView); } } diff --git a/src/d3d11/d3d11_context.h b/src/d3d11/d3d11_context.h index 89e22a15..5c47ab55 100644 --- a/src/d3d11/d3d11_context.h +++ b/src/d3d11/d3d11_context.h @@ -894,8 +894,8 @@ namespace dxvk { UINT* pFirstConstant, UINT* pNumConstants); + template void GetShaderResources( - const D3D11ShaderResourceBindings& Bindings, UINT StartSlot, UINT NumViews, ID3D11ShaderResourceView** ppShaderResourceViews); @@ -914,8 +914,7 @@ namespace dxvk { template void ResolveSrvHazards( - T* pView, - D3D11ShaderResourceBindings& Bindings); + T* pView); template void ResolveCsSrvHazards( @@ -941,8 +940,7 @@ namespace dxvk { D3D11SamplerBindings& Bindings); template - void RestoreShaderResources( - D3D11ShaderResourceBindings& Bindings); + void RestoreShaderResources(); template void RestoreUnorderedAccessViews( @@ -964,7 +962,6 @@ namespace dxvk { template void SetShaderResources( - D3D11ShaderResourceBindings& Bindings, UINT StartSlot, UINT NumResources, ID3D11ShaderResourceView* const* ppResources); diff --git a/src/d3d11/d3d11_context_state.h b/src/d3d11/d3d11_context_state.h index 9e1388de..260e8c00 100644 --- a/src/d3d11/d3d11_context_state.h +++ b/src/d3d11/d3d11_context_state.h @@ -68,16 +68,30 @@ namespace dxvk { using D3D11CbvBindings = D3D11ShaderStageState; + /** + * \brief Shader resource bindings + * + * Stores bound shader resource views, as well as a bit + * set of views that are potentially hazardous. + */ + struct D3D11ShaderStageSrvBinding { + std::array, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT> views = { }; + DxvkBindingSet hazardous = { }; + + void reset() { + for (uint32_t i = 0; i < views.size(); i++) + views[i] = nullptr; + + hazardous.clear(); + } + }; + + using D3D11SrvBindings = D3D11ShaderStageState; + using D3D11SamplerBindings = std::array< D3D11SamplerState*, D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT>; - struct D3D11ShaderResourceBindings { - std::array, D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT> views = { }; - DxvkBindingSet hazardous = { }; - }; - - using D3D11UnorderedAccessBindings = std::array< Com, D3D11_1_UAV_SLOT_COUNT>; @@ -85,35 +99,30 @@ namespace dxvk { struct D3D11ContextStateVS { Com shader = nullptr; D3D11SamplerBindings samplers = { }; - D3D11ShaderResourceBindings shaderResources = { }; }; struct D3D11ContextStateHS { Com shader = nullptr; D3D11SamplerBindings samplers = { }; - D3D11ShaderResourceBindings shaderResources = { }; }; struct D3D11ContextStateDS { Com shader = nullptr; D3D11SamplerBindings samplers = { }; - D3D11ShaderResourceBindings shaderResources = { }; }; struct D3D11ContextStateGS { Com shader = nullptr; D3D11SamplerBindings samplers = { }; - D3D11ShaderResourceBindings shaderResources = { }; }; struct D3D11ContextStatePS { Com shader = nullptr; D3D11SamplerBindings samplers = { }; - D3D11ShaderResourceBindings shaderResources = { }; D3D11UnorderedAccessBindings unorderedAccessViews = { }; }; @@ -121,7 +130,6 @@ namespace dxvk { struct D3D11ContextStateCS { Com shader = nullptr; D3D11SamplerBindings samplers = { }; - D3D11ShaderResourceBindings shaderResources = { }; D3D11UnorderedAccessBindings unorderedAccessViews = { }; DxvkBindingSet uavMask = { }; @@ -222,6 +230,7 @@ namespace dxvk { D3D11ContextStatePR pr; D3D11CbvBindings cbv; + D3D11SrvBindings srv; }; } \ No newline at end of file