From 9b0a5dd39d261dc2ee02e9aac8d7176c71fecab5 Mon Sep 17 00:00:00 2001 From: assiduous Date: Tue, 16 Mar 2021 19:06:17 -0700 Subject: Few more updates to ShaderResourceCacheD3D11 --- .../include/ShaderResourceCacheD3D11.hpp | 125 +++++++++------------ .../src/ShaderResourceCacheD3D11.cpp | 102 ++++++----------- 2 files changed, 87 insertions(+), 140 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp index abc0b7e2..8989f6ef 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp @@ -32,6 +32,7 @@ #include #include +#include #include "MemoryAllocator.h" #include "ShaderResourceCacheCommon.hpp" @@ -180,37 +181,37 @@ public: __forceinline void SetCB(BindPointsD3D11 BindPoints, RefCntAutoPtr pBuffD3D11Impl) { auto* pd3d11Buff = pBuffD3D11Impl ? pBuffD3D11Impl->BufferD3D11Impl::GetD3D11Buffer() : nullptr; - SetD3D11ResourceInternal(BindPoints, std::move(pBuffD3D11Impl), pd3d11Buff); + SetD3D11ResourceInternal(BindPoints, std::move(pBuffD3D11Impl), pd3d11Buff); } __forceinline void SetTexSRV(BindPointsD3D11 BindPoints, RefCntAutoPtr pTexView) { auto* pd3d11SRV = pTexView ? static_cast(pTexView->TextureViewD3D11Impl::GetD3D11View()) : nullptr; - SetD3D11ResourceInternal(BindPoints, std::move(pTexView), pd3d11SRV); + SetD3D11ResourceInternal(BindPoints, std::move(pTexView), pd3d11SRV); } __forceinline void SetBufSRV(BindPointsD3D11 BindPoints, RefCntAutoPtr pBuffView) { auto* pd3d11SRV = pBuffView ? static_cast(pBuffView->BufferViewD3D11Impl::GetD3D11View()) : nullptr; - SetD3D11ResourceInternal(BindPoints, std::move(pBuffView), pd3d11SRV); + SetD3D11ResourceInternal(BindPoints, std::move(pBuffView), pd3d11SRV); } __forceinline void SetTexUAV(BindPointsD3D11 BindPoints, RefCntAutoPtr pTexView) { auto* pd3d11UAV = pTexView ? static_cast(pTexView->TextureViewD3D11Impl::GetD3D11View()) : nullptr; - SetD3D11ResourceInternal(BindPoints, std::move(pTexView), pd3d11UAV); + SetD3D11ResourceInternal(BindPoints, std::move(pTexView), pd3d11UAV); } __forceinline void SetBufUAV(BindPointsD3D11 BindPoints, RefCntAutoPtr pBuffView) { auto* pd3d11UAV = pBuffView ? static_cast(pBuffView->BufferViewD3D11Impl::GetD3D11View()) : nullptr; - SetD3D11ResourceInternal(BindPoints, std::move(pBuffView), pd3d11UAV); + SetD3D11ResourceInternal(BindPoints, std::move(pBuffView), pd3d11UAV); } __forceinline void SetSampler(BindPointsD3D11 BindPoints, SamplerD3D11Impl* pSampler) { auto* pd3d11Sampler = pSampler ? pSampler->SamplerD3D11Impl::GetD3D11SamplerState() : nullptr; - SetD3D11ResourceInternal(BindPoints, pSampler, pd3d11Sampler); + SetD3D11ResourceInternal(BindPoints, pSampler, pd3d11Sampler); } @@ -219,10 +220,8 @@ public: { const Uint32 ShaderInd = PlatformMisc::GetLSB(BindPoints.GetActiveBits()); VERIFY(BindPoints[ShaderInd] < GetResourceCount(ShaderInd), "Resource slot is out of range"); - CachedResourceTraits::CachedResourceType const* CachedResources; - D3D11ResourceType* const* pd3d11Resources; - GetConstResourceArrays(ShaderInd, CachedResources, pd3d11Resources); - return CachedResources[BindPoints[ShaderInd]]; + const auto ResArrays = GetConstResourceArrays(ShaderInd); + return ResArrays.first[BindPoints[ShaderInd]]; } template @@ -234,21 +233,16 @@ public: const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits); ActiveBits &= ~(1u << ShaderInd); - CachedResourceTraits::CachedResourceType const* pSrcCachedResources; - D3D11ResourceType* const* pd3d11SrcResources; - SrcCache.GetConstResourceArrays(ShaderInd, pSrcCachedResources, pd3d11SrcResources); - - CachedResourceTraits::CachedResourceType* pDstCachedResources; - D3D11ResourceType** pd3d11DstResources; - GetResourceArrays(ShaderInd, pDstCachedResources, pd3d11DstResources); + auto SrcResArrays = SrcCache.GetConstResourceArrays(ShaderInd); + auto DstResArrays = GetResourceArrays(ShaderInd); const Uint32 CacheOffset = BindPoints[ShaderInd]; VERIFY(CacheOffset < GetResourceCount(ShaderInd), "Index is out of range"); - if (!pSrcCachedResources[CacheOffset]) + if (!SrcResArrays.first[CacheOffset]) IsBound = false; - pDstCachedResources[CacheOffset] = pSrcCachedResources[CacheOffset]; - pd3d11DstResources[CacheOffset] = pd3d11SrcResources[CacheOffset]; + DstResArrays.first[CacheOffset] = SrcResArrays.first[CacheOffset]; + DstResArrays.second[CacheOffset] = SrcResArrays.second[CacheOffset]; } return IsBound; } @@ -263,10 +257,8 @@ public: ActiveBits &= ~(1u << ShaderInd); const Uint32 CacheOffset = BindPoints[ShaderInd]; - CachedResourceTraits::CachedResourceType const* CachedResources; - D3D11ResourceType* const* pd3d11Resources; - GetConstResourceArrays(ShaderInd, CachedResources, pd3d11Resources); - if (CacheOffset < GetResourceCount(ShaderInd) && CachedResources[CacheOffset]) + const auto ResArrays = GetConstResourceArrays(ShaderInd); + if (CacheOffset < GetResourceCount(ShaderInd) && ResArrays.first[CacheOffset]) { continue; } @@ -289,31 +281,6 @@ public: template __forceinline Uint32 GetResourceCount(Uint32 ShaderInd) const; - template - __forceinline void GetResourceArrays( - Uint32 ShaderInd, - typename CachedResourceTraits::CachedResourceType*& pResources, - D3D11ResourceType**& pd3d11Resources) const - { - static_assert(alignof(CachedResourceTraits::CachedResourceType) == alignof(D3D11ResourceType*), "Alignment mismatch, pointer to D3D11 resource may not be properly aligned"); - - pResources = reinterpret_cast::CachedResourceType*>(m_pResourceData.get() + GetResourceDataOffset(ShaderInd)); - pd3d11Resources = reinterpret_cast(pResources + GetResourceCount(ShaderInd)); - } - - - template - __forceinline void GetConstResourceArrays( - Uint32 ShaderInd, - typename CachedResourceTraits::CachedResourceType const*& pResources, - D3D11ResourceType* const*& pd3d11Resources) const - { - static_assert(alignof(CachedResourceTraits::CachedResourceType) == alignof(D3D11ResourceType*), "Alignment mismatch, pointer to D3D11 resource may not be properly aligned"); - - pResources = reinterpret_cast::CachedResourceType const*>(m_pResourceData.get() + GetResourceDataOffset(ShaderInd)); - pd3d11Resources = reinterpret_cast(pResources + GetResourceCount(ShaderInd)); - } - bool IsInitialized() const { return m_IsInitialized; } ResourceCacheContentType GetContentType() const { return m_ContentType; } @@ -352,7 +319,29 @@ private: template __forceinline Uint32 GetResourceDataOffset(Uint32 ShaderInd) const; - template + template + __forceinline std::pair::CachedResourceType*, D3D11ResourceType**> + GetResourceArrays(Uint32 ShaderInd) const + { + static_assert(alignof(CachedResourceTraits::CachedResourceType) == alignof(D3D11ResourceType*), "Alignment mismatch, pointer to D3D11 resource may not be properly aligned"); + + using CachedResourceType = CachedResourceTraits::CachedResourceType; + const auto DataOffset = GetResourceDataOffset(ShaderInd); + const auto ResCount = GetResourceCount(ShaderInd); + auto* const pResources = reinterpret_cast(m_pResourceData.get() + DataOffset); + auto* const pd3d11Resources = reinterpret_cast(pResources + ResCount); + return std::make_pair(pResources, pd3d11Resources); + } + + template + __forceinline std::pair::CachedResourceType*, D3D11ResourceType* const*> + GetConstResourceArrays(Uint32 ShaderInd) const + { + const auto ResArrays = GetResourceArrays(ShaderInd); + return std::make_pair(ResArrays.first, ResArrays.second); + } + + template __forceinline void SetD3D11ResourceInternal(BindPointsD3D11 BindPoints, TSrcResourceType pResource, TD3D11ResourceType* pd3d11Resource) { VERIFY(pResource != nullptr && pd3d11Resource != nullptr || pResource == nullptr && pd3d11Resource == nullptr, @@ -366,11 +355,9 @@ private: const Uint32 ResCount = GetResourceCount(ShaderInd); VERIFY(CacheOffset < ResCount, "Index is out of range"); - TCachedResourceType* Resources; - TD3D11ResourceType** d3d11ResArr; - GetResourceArrays(ShaderInd, Resources, d3d11ResArr); - Resources[CacheOffset].Set(pResource); - d3d11ResArr[CacheOffset] = pd3d11Resource; + auto ResArrays = GetResourceArrays(ShaderInd); + ResArrays.first[CacheOffset].Set(pResource); + ResArrays.second[CacheOffset] = pd3d11Resource; } } @@ -498,21 +485,18 @@ ShaderResourceCacheD3D11::MinMaxSlot ShaderResourceCacheD3D11::BindResources( D3D11ResourceType* CommittedD3D11Resources[], Uint8& Binding) const { - CachedResourceTraits::CachedResourceType const* CachedResources; - D3D11ResourceType* const* pd3d11Resources; - GetConstResourceArrays(ShaderInd, CachedResources, pd3d11Resources); + const auto ResCount = GetResourceCount(ShaderInd); + const auto ResArrays = GetConstResourceArrays(ShaderInd); MinMaxSlot Slots; - - const auto ResCount = GetResourceCount(ShaderInd); for (Uint32 res = 0; res < ResCount; ++res) { const Uint32 Slot = Binding++; - if (CommittedD3D11Resources[Slot] != pd3d11Resources[res]) + if (CommittedD3D11Resources[Slot] != ResArrays.second[res]) Slots.Add(Slot); - VERIFY_EXPR(pd3d11Resources[res] != nullptr); - CommittedD3D11Resources[Slot] = pd3d11Resources[res]; + VERIFY_EXPR(ResArrays.second[res] != nullptr); + CommittedD3D11Resources[Slot] = ResArrays.second[res]; } return Slots; @@ -525,22 +509,19 @@ ShaderResourceCacheD3D11::MinMaxSlot ShaderResourceCacheD3D11::BindResourceViews ID3D11Resource* CommittedD3D11Resources[], Uint8& Binding) const { - CachedResourceTraits::CachedResourceType const* CachedResources; - D3D11ResourceViewType* const* pd3d11Views; - GetConstResourceArrays(ShaderInd, CachedResources, pd3d11Views); + const auto ResCount = GetResourceCount(ShaderInd); + const auto ResArrays = GetConstResourceArrays(ShaderInd); MinMaxSlot Slots; - - const auto ResCount = GetResourceCount(ShaderInd); for (Uint32 res = 0; res < ResCount; ++res) { const Uint32 Slot = Binding++; - if (CommittedD3D11Views[Slot] != pd3d11Views[res]) + if (CommittedD3D11Views[Slot] != ResArrays.second[res]) Slots.Add(Slot); - VERIFY_EXPR(pd3d11Views[res] != nullptr); - CommittedD3D11Resources[Slot] = CachedResources[res].pd3d11Resource; - CommittedD3D11Views[Slot] = pd3d11Views[res]; + VERIFY_EXPR(ResArrays.second[res] != nullptr); + CommittedD3D11Resources[Slot] = ResArrays.first[res].pd3d11Resource; + CommittedD3D11Views[Slot] = ResArrays.second[res]; } return Slots; diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp index 866298fa..744484df 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp @@ -112,41 +112,33 @@ void ShaderResourceCacheD3D11::Initialize(const TBindingsPerStage& ResCount, IMe const auto CBCount = GetCBCount(ShaderInd); if (CBCount != 0) { - CachedCB* CBs = nullptr; - ID3D11Buffer** d3d11CBs = nullptr; - GetResourceArrays(ShaderInd, CBs, d3d11CBs); + auto CBArrays = GetResourceArrays(ShaderInd); for (Uint32 cb = 0; cb < CBCount; ++cb) - new (CBs + cb) CachedCB{}; + new (CBArrays.first + cb) CachedCB{}; } const auto SRVCount = GetSRVCount(ShaderInd); if (SRVCount != 0) { - CachedResource* SRVResources = nullptr; - ID3D11ShaderResourceView** d3d11SRVs = nullptr; - GetResourceArrays(ShaderInd, SRVResources, d3d11SRVs); + auto SRVArrays = GetResourceArrays(ShaderInd); for (Uint32 srv = 0; srv < SRVCount; ++srv) - new (SRVResources + srv) CachedResource{}; + new (SRVArrays.first + srv) CachedResource{}; } const auto SamplerCount = GetSamplerCount(ShaderInd); if (SamplerCount != 0) { - CachedSampler* Samplers = nullptr; - ID3D11SamplerState** d3d11Samplers = nullptr; - GetResourceArrays(ShaderInd, Samplers, d3d11Samplers); + auto SamArrays = GetResourceArrays(ShaderInd); for (Uint32 sam = 0; sam < SamplerCount; ++sam) - new (Samplers + sam) CachedSampler{}; + new (SamArrays.first + sam) CachedSampler{}; } const auto UAVCount = GetUAVCount(ShaderInd); if (UAVCount != 0) { - CachedResource* UAVResources = nullptr; - ID3D11UnorderedAccessView** d3d11UAVs = nullptr; - GetResourceArrays(ShaderInd, UAVResources, d3d11UAVs); + auto UAVArrays = GetResourceArrays(ShaderInd); for (Uint32 uav = 0; uav < UAVCount; ++uav) - new (UAVResources + uav) CachedResource{}; + new (UAVArrays.first + uav) CachedResource{}; } } @@ -163,41 +155,33 @@ ShaderResourceCacheD3D11::~ShaderResourceCacheD3D11() const auto CBCount = GetCBCount(ShaderInd); if (CBCount != 0) { - CachedCB* CBs = nullptr; - ID3D11Buffer** d3d11CBs = nullptr; - GetResourceArrays(ShaderInd, CBs, d3d11CBs); + auto CBArrays = GetResourceArrays(ShaderInd); for (size_t cb = 0; cb < CBCount; ++cb) - CBs[cb].~CachedCB(); + CBArrays.first[cb].~CachedCB(); } const auto SRVCount = GetSRVCount(ShaderInd); if (SRVCount != 0) { - CachedResource* SRVResources = nullptr; - ID3D11ShaderResourceView** d3d11SRVs = nullptr; - GetResourceArrays(ShaderInd, SRVResources, d3d11SRVs); + auto SRVArrays = GetResourceArrays(ShaderInd); for (size_t srv = 0; srv < SRVCount; ++srv) - SRVResources[srv].~CachedResource(); + SRVArrays.first[srv].~CachedResource(); } const auto SamplerCount = GetSamplerCount(ShaderInd); if (SamplerCount != 0) { - CachedSampler* Samplers = nullptr; - ID3D11SamplerState** d3d11Samplers = nullptr; - GetResourceArrays(ShaderInd, Samplers, d3d11Samplers); + auto SamArrays = GetResourceArrays(ShaderInd); for (size_t sam = 0; sam < SamplerCount; ++sam) - Samplers[sam].~CachedSampler(); + SamArrays.first[sam].~CachedSampler(); } const auto UAVCount = GetUAVCount(ShaderInd); if (UAVCount != 0) { - CachedResource* UAVResources = nullptr; - ID3D11UnorderedAccessView** d3d11UAVs = nullptr; - GetResourceArrays(ShaderInd, UAVResources, d3d11UAVs); + auto UAVArrays = GetResourceArrays(ShaderInd); for (size_t uav = 0; uav < UAVCount; ++uav) - UAVResources[uav].~CachedResource(); + UAVArrays.first[uav].~CachedResource(); } } m_Offsets = {}; @@ -262,25 +246,16 @@ void ShaderResourceCacheD3D11::DvpVerifyCacheConsistency() for (Uint32 ShaderInd = 0; ShaderInd < NumShaderTypes; ++ShaderInd) { - CachedCB* CBs = nullptr; - ID3D11Buffer** d3d11CBs = nullptr; - CachedResource* SRVResources = nullptr; - ID3D11ShaderResourceView** d3d11SRVs = nullptr; - CachedSampler* Samplers = nullptr; - ID3D11SamplerState** d3d11Samplers = nullptr; - CachedResource* UAVResources = nullptr; - ID3D11UnorderedAccessView** d3d11UAVs = nullptr; - - GetResourceArrays(ShaderInd, CBs, d3d11CBs); - GetResourceArrays(ShaderInd, SRVResources, d3d11SRVs); - GetResourceArrays(ShaderInd, Samplers, d3d11Samplers); - GetResourceArrays(ShaderInd, UAVResources, d3d11UAVs); + const auto CBArrays = GetResourceArrays(ShaderInd); + const auto SRVArrays = GetResourceArrays(ShaderInd); + const auto SamArrays = GetResourceArrays(ShaderInd); + const auto UAVArrays = GetResourceArrays(ShaderInd); auto CBCount = GetCBCount(ShaderInd); for (size_t cb = 0; cb < CBCount; ++cb) { - auto& pBuff = CBs[cb].pBuff; - auto* pd3d11Buff = d3d11CBs[cb]; + auto& pBuff = CBArrays.first[cb].pBuff; + auto* pd3d11Buff = CBArrays.second[cb]; VERIFY(pBuff == nullptr && pd3d11Buff == nullptr || pBuff != nullptr && pd3d11Buff != nullptr, "CB resource and d3d11 buffer must be set/unset atomically"); if (pBuff != nullptr && pd3d11Buff != nullptr) { @@ -291,24 +266,24 @@ void ShaderResourceCacheD3D11::DvpVerifyCacheConsistency() auto SRVCount = GetSRVCount(ShaderInd); for (size_t srv = 0; srv < SRVCount; ++srv) { - auto& Res = SRVResources[srv]; - auto* pd3d11SRV = d3d11SRVs[srv]; + auto& Res = SRVArrays.first[srv]; + auto* pd3d11SRV = SRVArrays.second[srv]; DvpVerifyResource(Res, pd3d11SRV, "SRV"); } auto UAVCount = GetUAVCount(ShaderInd); for (size_t uav = 0; uav < UAVCount; ++uav) { - auto& Res = UAVResources[uav]; - auto* pd3d11UAV = d3d11UAVs[uav]; + auto& Res = UAVArrays.first[uav]; + auto* pd3d11UAV = UAVArrays.second[uav]; DvpVerifyResource(Res, pd3d11UAV, "UAV"); } auto SamplerCount = GetSamplerCount(ShaderInd); for (size_t sam = 0; sam < SamplerCount; ++sam) { - auto& pSampler = Samplers[sam].pSampler; - auto* pd3d11Sampler = d3d11Samplers[sam]; + auto& pSampler = SamArrays.first[sam].pSampler; + auto* pd3d11Sampler = SamArrays.second[sam]; VERIFY(pSampler == nullptr && pd3d11Sampler == nullptr || pSampler != nullptr && pd3d11Sampler != nullptr, "CB resource and d3d11 buffer must be set/unset atomically"); if (pSampler != nullptr && pd3d11Sampler != nullptr) { @@ -339,13 +314,10 @@ void ShaderResourceCacheD3D11::TransitionResources(DeviceContextD3D11Impl& Ctx, if (CBCount == 0) continue; - CachedCB* CBs; - ID3D11Buffer** d3d11CBs; - GetResourceArrays(ShaderInd, CBs, d3d11CBs); - + auto CBArrays = GetResourceArrays(ShaderInd); for (Uint32 i = 0; i < CBCount; ++i) { - if (auto* pBuffer = CBs[i].pBuff.RawPtr()) + if (auto* pBuffer = CBArrays.first[i].pBuff.RawPtr()) { if (pBuffer->IsInKnownState() && !pBuffer->CheckState(RESOURCE_STATE_CONSTANT_BUFFER)) { @@ -374,13 +346,10 @@ void ShaderResourceCacheD3D11::TransitionResources(DeviceContextD3D11Impl& Ctx, if (SRVCount == 0) continue; - CachedResource* SRVResources; - ID3D11ShaderResourceView** d3d11SRVs; - GetResourceArrays(ShaderInd, SRVResources, d3d11SRVs); - + auto SRVArrays = GetResourceArrays(ShaderInd); for (Uint32 i = 0; i < SRVCount; ++i) { - auto& SRVRes = SRVResources[i]; + auto& SRVRes = SRVArrays.first[i]; if (auto* pTexture = SRVRes.pTexture) { if (pTexture->IsInKnownState() && !pTexture->CheckAnyState(RESOURCE_STATE_SHADER_RESOURCE | RESOURCE_STATE_INPUT_ATTACHMENT)) @@ -431,13 +400,10 @@ void ShaderResourceCacheD3D11::TransitionResources(DeviceContextD3D11Impl& Ctx, if (UAVCount == 0) continue; - CachedResource* UAVResources; - ID3D11UnorderedAccessView** d3d11UAVs; - GetResourceArrays(ShaderInd, UAVResources, d3d11UAVs); - + auto UAVArrays = GetResourceArrays(ShaderInd); for (Uint32 i = 0; i < UAVCount; ++i) { - auto& UAVRes = UAVResources[i]; + auto& UAVRes = UAVArrays.first[i]; if (auto* pTexture = UAVRes.pTexture) { if (pTexture->IsInKnownState() && !pTexture->CheckState(RESOURCE_STATE_UNORDERED_ACCESS)) -- cgit v1.2.3