From 834c6884fa0600527e0f116cf06e8a64d6b4a47f Mon Sep 17 00:00:00 2001 From: assiduous Date: Tue, 16 Mar 2021 15:02:04 -0700 Subject: Some updates to ShaderResourceCacheD3D11 --- .../include/ShaderResourceCacheD3D11.hpp | 58 ++++++++------ .../src/DeviceContextD3D11Impl.cpp | 48 ++++-------- .../src/ShaderResourceCacheD3D11.cpp | 88 +++++++++++----------- 3 files changed, 98 insertions(+), 96 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp index a4112f8a..23fc8654 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp @@ -504,28 +504,42 @@ public: ResourceCacheContentType GetContentType() const { return m_ContentType; } - void BindCBs(Uint32 ShaderInd, - ID3D11Buffer* CommittedD3D11CBs[D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT], - Uint8& Binding, - Uint32& MinSlot, - Uint32& MaxSlot) const; - void BindSRVs(Uint32 ShaderInd, - ID3D11ShaderResourceView* CommittedD3D11SRVs[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT], - ID3D11Resource* CommittedD3D11SRVResources[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT], - Uint8& Binding, - Uint32& MinSlot, - Uint32& MaxSlot) const; - void BindSamplers(Uint32 ShaderInd, - ID3D11SamplerState* CommittedD3D11Samplers[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT], - Uint8& Binding, - Uint32& MinSlot, - Uint32& MaxSlot) const; - void BindUAVs(Uint32 ShaderInd, - ID3D11UnorderedAccessView* CommittedD3D11UAVs[D3D11_PS_CS_UAV_REGISTER_COUNT], - ID3D11Resource* CommittedD3D11UAVResources[D3D11_PS_CS_UAV_REGISTER_COUNT], - Uint8& Binding, - Uint32& MinSlot, - Uint32& MaxSlot) const; + struct MinMaxSlot + { + UINT MinSlot = UINT_MAX; + UINT MaxSlot = 0; + + void Add(UINT Slot) + { + MinSlot = std::min(MinSlot, Slot); + + VERIFY_EXPR(Slot >= MaxSlot); + MaxSlot = Slot; + } + + explicit operator bool() const + { + return MinSlot <= MaxSlot; + } + }; + + MinMaxSlot BindCBs(Uint32 ShaderInd, + ID3D11Buffer* CommittedD3D11CBs[D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT], + Uint8& Binding) const; + + MinMaxSlot BindSRVs(Uint32 ShaderInd, + ID3D11ShaderResourceView* CommittedD3D11SRVs[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT], + ID3D11Resource* CommittedD3D11SRVResources[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT], + Uint8& Binding) const; + + MinMaxSlot BindSamplers(Uint32 ShaderInd, + ID3D11SamplerState* CommittedD3D11Samplers[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT], + Uint8& Binding) const; + + MinMaxSlot BindUAVs(Uint32 ShaderInd, + ID3D11UnorderedAccessView* CommittedD3D11UAVs[D3D11_PS_CS_UAV_REGISTER_COUNT], + ID3D11Resource* CommittedD3D11UAVResources[D3D11_PS_CS_UAV_REGISTER_COUNT], + Uint8& Binding) const; private: template diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index 40ce63c9..b35411e1 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -268,14 +268,6 @@ void DeviceContextD3D11Impl::BindCacheResources(const ShaderResourceCacheD3D11& const TBindingsPerStage& BaseBindings, SHADER_TYPE ActiveStages) { - struct MinMaxSlot - { - UINT MinSlot = UINT_MAX; - UINT MaxSlot = 0; - }; - using TMinMaxSlotPerStage = std::array; - using TBindings = TBindingsPerStage::value_type; - Uint8 ShaderIndices[NumShaderTypes] = {}; SHADER_TYPE ShaderTypes[NumShaderTypes] = {}; Uint32 ShaderCount = 0; @@ -294,16 +286,14 @@ void DeviceContextD3D11Impl::BindCacheResources(const ShaderResourceCacheD3D11& const auto ShaderInd = ShaderIndices[i]; auto* CommittedD3D11CBs = m_CommittedRes.D3D11CBs[ShaderInd]; Uint8 Binding = BaseBindings[Range][ShaderInd]; - Uint32 MinSlot = UINT_MAX; - Uint32 MaxSlot = 0; - ResourceCache.BindCBs(ShaderInd, CommittedD3D11CBs, Binding, MinSlot, MaxSlot); + auto Slots = ResourceCache.BindCBs(ShaderInd, CommittedD3D11CBs, Binding); - if (MinSlot != UINT_MAX) + if (Slots) { auto SetCBMethod = SetCBMethods[ShaderInd]; - (m_pd3d11DeviceContext->*SetCBMethod)(MinSlot, MaxSlot - MinSlot + 1, CommittedD3D11CBs + MinSlot); + (m_pd3d11DeviceContext->*SetCBMethod)(Slots.MinSlot, Slots.MaxSlot - Slots.MinSlot + 1, CommittedD3D11CBs + Slots.MinSlot); m_CommittedRes.NumCBs[ShaderInd] = std::max(m_CommittedRes.NumCBs[ShaderInd], Binding); - VERIFY_EXPR(MaxSlot < Binding); + VERIFY_EXPR(Slots.MaxSlot < Binding); } #ifdef VERIFY_CONTEXT_BINDINGS if (m_DebugFlags & D3D11_DEBUG_FLAG_VERIFY_COMMITTED_RESOURCE_RELEVANCE) @@ -320,16 +310,14 @@ void DeviceContextD3D11Impl::BindCacheResources(const ShaderResourceCacheD3D11& auto* CommittedD3D11SRVs = m_CommittedRes.D3D11SRVs[ShaderInd]; auto* CommittedD3D11SRVRes = m_CommittedRes.D3D11SRVResources[ShaderInd]; Uint8 Binding = BaseBindings[Range][ShaderInd]; - Uint32 MinSlot = UINT_MAX; - Uint32 MaxSlot = 0; - ResourceCache.BindSRVs(ShaderInd, CommittedD3D11SRVs, CommittedD3D11SRVRes, Binding, MinSlot, MaxSlot); + auto Slots = ResourceCache.BindSRVs(ShaderInd, CommittedD3D11SRVs, CommittedD3D11SRVRes, Binding); - if (MinSlot != UINT_MAX) + if (Slots) { auto SetSRVMethod = SetSRVMethods[ShaderInd]; - (m_pd3d11DeviceContext->*SetSRVMethod)(MinSlot, MaxSlot - MinSlot + 1, CommittedD3D11SRVs + MinSlot); + (m_pd3d11DeviceContext->*SetSRVMethod)(Slots.MinSlot, Slots.MaxSlot - Slots.MinSlot + 1, CommittedD3D11SRVs + Slots.MinSlot); m_CommittedRes.NumSRVs[ShaderInd] = std::max(m_CommittedRes.NumSRVs[ShaderInd], Binding); - VERIFY_EXPR(MaxSlot <= Binding); + VERIFY_EXPR(Slots.MaxSlot <= Binding); } #ifdef VERIFY_CONTEXT_BINDINGS if (m_DebugFlags & D3D11_DEBUG_FLAG_VERIFY_COMMITTED_RESOURCE_RELEVANCE) @@ -345,16 +333,14 @@ void DeviceContextD3D11Impl::BindCacheResources(const ShaderResourceCacheD3D11& const auto ShaderInd = ShaderIndices[i]; auto* CommittedD3D11Samplers = m_CommittedRes.D3D11Samplers[ShaderInd]; Uint8 Binding = BaseBindings[Range][ShaderInd]; - Uint32 MinSlot = UINT_MAX; - Uint32 MaxSlot = 0; - ResourceCache.BindSamplers(ShaderInd, CommittedD3D11Samplers, Binding, MinSlot, MaxSlot); + auto Slots = ResourceCache.BindSamplers(ShaderInd, CommittedD3D11Samplers, Binding); - if (MinSlot != UINT_MAX) + if (Slots) { auto SetSamplerMethod = SetSamplerMethods[ShaderInd]; - (m_pd3d11DeviceContext->*SetSamplerMethod)(MinSlot, MaxSlot - MinSlot + 1, CommittedD3D11Samplers + MinSlot); + (m_pd3d11DeviceContext->*SetSamplerMethod)(Slots.MinSlot, Slots.MaxSlot - Slots.MinSlot + 1, CommittedD3D11Samplers + Slots.MinSlot); m_CommittedRes.NumSamplers[ShaderInd] = std::max(m_CommittedRes.NumSamplers[ShaderInd], Binding); - VERIFY_EXPR(MaxSlot < Binding); + VERIFY_EXPR(Slots.MaxSlot < Binding); } #ifdef VERIFY_CONTEXT_BINDINGS if (m_DebugFlags & D3D11_DEBUG_FLAG_VERIFY_COMMITTED_RESOURCE_RELEVANCE) @@ -372,11 +358,9 @@ void DeviceContextD3D11Impl::BindCacheResources(const ShaderResourceCacheD3D11& auto* CommittedD3D11UAVs = m_CommittedRes.D3D11UAVs[ShaderInd]; auto* CommittedD3D11UAVRes = m_CommittedRes.D3D11UAVResources[ShaderInd]; Uint8 Binding = BaseBindings[Range][ShaderInd]; - Uint32 MinSlot = UINT_MAX; - Uint32 MaxSlot = 0; - ResourceCache.BindUAVs(ShaderInd, CommittedD3D11UAVs, CommittedD3D11UAVRes, Binding, MinSlot, MaxSlot); + auto Slots = ResourceCache.BindUAVs(ShaderInd, CommittedD3D11UAVs, CommittedD3D11UAVRes, Binding); - if (MinSlot != UINT_MAX) + if (Slots) { if (ShaderInd == PSInd) ClearPixelShaderUAVs = false; @@ -405,9 +389,9 @@ void DeviceContextD3D11Impl::BindCacheResources(const ShaderResourceCacheD3D11& { // This can only be CS auto SetUAVMethod = SetUAVMethods[ShaderInd]; - (m_pd3d11DeviceContext->*SetUAVMethod)(MinSlot, MaxSlot - MinSlot + 1, CommittedD3D11UAVs + MinSlot, nullptr); + (m_pd3d11DeviceContext->*SetUAVMethod)(Slots.MinSlot, Slots.MaxSlot - Slots.MinSlot + 1, CommittedD3D11UAVs + Slots.MinSlot, nullptr); m_CommittedRes.NumUAVs[ShaderInd] = std::max(m_CommittedRes.NumUAVs[ShaderInd], Binding); - VERIFY_EXPR(MaxSlot < Binding); + VERIFY_EXPR(Slots.MaxSlot < Binding); } else { diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp index 377f0990..974a15ea 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp @@ -474,104 +474,108 @@ void ShaderResourceCacheD3D11::TransitionResources(DeviceContextD3D11Impl& Ctx, } } -void ShaderResourceCacheD3D11::BindCBs(Uint32 ShaderInd, - ID3D11Buffer* CommittedD3D11CBs[D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT], - Uint8& Binding, - Uint32& MinSlot, - Uint32& MaxSlot) const +ShaderResourceCacheD3D11::MinMaxSlot ShaderResourceCacheD3D11::BindCBs( + Uint32 ShaderInd, + ID3D11Buffer* CommittedD3D11CBs[D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT], + Uint8& Binding) const { CachedCB const* CBs; ID3D11Buffer* const* d3d11CBs; GetConstCBArrays(ShaderInd, CBs, d3d11CBs); + MinMaxSlot Slots; + const auto CBCount = GetCBCount(ShaderInd); for (Uint32 cb = 0; cb < CBCount; ++cb) { - const Uint32 Slot = Binding++; - const bool IsNewCB = CommittedD3D11CBs[Slot] != d3d11CBs[cb]; - MinSlot = IsNewCB ? std::min(MinSlot, Slot) : MinSlot; - MaxSlot = IsNewCB ? Slot : MaxSlot; + const Uint32 Slot = Binding++; + if (CommittedD3D11CBs[Slot] != d3d11CBs[cb]) + Slots.Add(Slot); - VERIFY_EXPR(!IsNewCB || (Slot >= MinSlot && Slot <= MaxSlot)); VERIFY_EXPR(d3d11CBs[cb] != nullptr); CommittedD3D11CBs[Slot] = d3d11CBs[cb]; } + + return Slots; } -void ShaderResourceCacheD3D11::BindSRVs(Uint32 ShaderInd, - ID3D11ShaderResourceView* CommittedD3D11SRVs[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT], - ID3D11Resource* CommittedD3D11SRVResources[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT], - Uint8& Binding, - Uint32& MinSlot, - Uint32& MaxSlot) const +ShaderResourceCacheD3D11::MinMaxSlot ShaderResourceCacheD3D11::BindSRVs( + Uint32 ShaderInd, + ID3D11ShaderResourceView* CommittedD3D11SRVs[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT], + ID3D11Resource* CommittedD3D11SRVResources[D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT], + Uint8& Binding) const { CachedResource const* SRVResources; ID3D11ShaderResourceView* const* d3d11SRVs; GetConstSRVArrays(ShaderInd, SRVResources, d3d11SRVs); + MinMaxSlot Slots; + const auto SRVCount = GetSRVCount(ShaderInd); for (Uint32 srv = 0; srv < SRVCount; ++srv) { - const Uint32 Slot = Binding++; - const bool IsNewSRV = CommittedD3D11SRVs[Slot] != d3d11SRVs[srv]; - MinSlot = IsNewSRV ? std::min(MinSlot, Slot) : MinSlot; - MaxSlot = IsNewSRV ? Slot : MaxSlot; + const Uint32 Slot = Binding++; + if (CommittedD3D11SRVs[Slot] != d3d11SRVs[srv]) + Slots.Add(Slot); - VERIFY_EXPR(!IsNewSRV || (Slot >= MinSlot && Slot <= MaxSlot)); VERIFY_EXPR(d3d11SRVs[srv] != nullptr); CommittedD3D11SRVResources[Slot] = SRVResources[srv].pd3d11Resource; CommittedD3D11SRVs[Slot] = d3d11SRVs[srv]; } + + return Slots; } -void ShaderResourceCacheD3D11::BindSamplers(Uint32 ShaderInd, - ID3D11SamplerState* CommittedD3D11Samplers[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT], - Uint8& Binding, - Uint32& MinSlot, - Uint32& MaxSlot) const +ShaderResourceCacheD3D11::MinMaxSlot ShaderResourceCacheD3D11::BindSamplers( + Uint32 ShaderInd, + ID3D11SamplerState* CommittedD3D11Samplers[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT], + Uint8& Binding) const { CachedSampler const* Samplers; ID3D11SamplerState* const* d3d11Samplers; GetConstSamplerArrays(ShaderInd, Samplers, d3d11Samplers); + MinMaxSlot Slots; + const auto SamplerCount = GetSamplerCount(ShaderInd); for (Uint32 sam = 0; sam < SamplerCount; ++sam) { - const Uint32 Slot = Binding++; - const bool IsNewSam = CommittedD3D11Samplers[Slot] != d3d11Samplers[sam]; - MinSlot = IsNewSam ? std::min(MinSlot, Slot) : MinSlot; - MaxSlot = IsNewSam ? Slot : MaxSlot; + const Uint32 Slot = Binding++; + if (CommittedD3D11Samplers[Slot] != d3d11Samplers[sam]) + Slots.Add(Slot); - VERIFY_EXPR(!IsNewSam || (Slot >= MinSlot && Slot <= MaxSlot)); VERIFY_EXPR(d3d11Samplers[sam] != nullptr); CommittedD3D11Samplers[Slot] = d3d11Samplers[sam]; } + + return Slots; } -void ShaderResourceCacheD3D11::BindUAVs(Uint32 ShaderInd, - ID3D11UnorderedAccessView* CommittedD3D11UAVs[D3D11_PS_CS_UAV_REGISTER_COUNT], - ID3D11Resource* CommittedD3D11UAVResources[D3D11_PS_CS_UAV_REGISTER_COUNT], - Uint8& Binding, - Uint32& MinSlot, - Uint32& MaxSlot) const +ShaderResourceCacheD3D11::MinMaxSlot ShaderResourceCacheD3D11::BindUAVs( + Uint32 ShaderInd, + ID3D11UnorderedAccessView* CommittedD3D11UAVs[D3D11_PS_CS_UAV_REGISTER_COUNT], + ID3D11Resource* CommittedD3D11UAVResources[D3D11_PS_CS_UAV_REGISTER_COUNT], + Uint8& Binding) const { CachedResource const* UAVResources; ID3D11UnorderedAccessView* const* d3d11UAVs; GetConstUAVArrays(ShaderInd, UAVResources, d3d11UAVs); + MinMaxSlot Slots; + const auto UAVCount = GetUAVCount(ShaderInd); for (Uint32 uav = 0; uav < UAVCount; ++uav) { - const Uint32 Slot = Binding++; - const bool IsNewUAV = CommittedD3D11UAVs[Slot] != d3d11UAVs[uav]; - MinSlot = IsNewUAV ? std::min(MinSlot, Slot) : MinSlot; - MaxSlot = IsNewUAV ? Slot : MaxSlot; + const Uint32 Slot = Binding++; + if (CommittedD3D11UAVs[Slot] != d3d11UAVs[uav]) + Slots.Add(Slot); - VERIFY_EXPR(!IsNewUAV || (Slot >= MinSlot && Slot <= MaxSlot)); VERIFY_EXPR(d3d11UAVs[uav] != nullptr); CommittedD3D11UAVResources[Slot] = UAVResources[uav].pd3d11Resource; CommittedD3D11UAVs[Slot] = d3d11UAVs[uav]; } + + return Slots; } } // namespace Diligent -- cgit v1.2.3