From 3988fe1c2824220145f037a2e41f3e211a311127 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 7 Mar 2021 22:51:57 -0800 Subject: Unified implementations of SRB in D3D12, Vk and GL --- .../include/ShaderResourceBindingD3D12Impl.hpp | 16 --- .../src/ShaderResourceBindingD3D12Impl.cpp | 111 ++++----------------- 2 files changed, 22 insertions(+), 105 deletions(-) (limited to 'Graphics/GraphicsEngineD3D12') diff --git a/Graphics/GraphicsEngineD3D12/include/ShaderResourceBindingD3D12Impl.hpp b/Graphics/GraphicsEngineD3D12/include/ShaderResourceBindingD3D12Impl.hpp index 75009356..3749145f 100644 --- a/Graphics/GraphicsEngineD3D12/include/ShaderResourceBindingD3D12Impl.hpp +++ b/Graphics/GraphicsEngineD3D12/include/ShaderResourceBindingD3D12Impl.hpp @@ -55,22 +55,6 @@ public: ~ShaderResourceBindingD3D12Impl(); IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_ShaderResourceBindingD3D12, TBase) - - virtual void DILIGENT_CALL_TYPE BindResources(Uint32 ShaderFlags, IResourceMapping* pResMapping, Uint32 Flags) override; - - virtual IShaderResourceVariable* DILIGENT_CALL_TYPE GetVariableByName(SHADER_TYPE ShaderType, const char* Name) override; - - virtual Uint32 DILIGENT_CALL_TYPE GetVariableCount(SHADER_TYPE ShaderType) const override final; - - virtual IShaderResourceVariable* DILIGENT_CALL_TYPE GetVariableByIndex(SHADER_TYPE ShaderType, Uint32 Index) override final; - - ShaderResourceCacheD3D12& GetResourceCache() { return m_ShaderResourceCache; } - -private: - void Destruct(); - - ShaderResourceCacheD3D12 m_ShaderResourceCache; - ShaderVariableManagerD3D12* m_pShaderVarMgrs = nullptr; // [GetNumShaders()] }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3D12/src/ShaderResourceBindingD3D12Impl.cpp b/Graphics/GraphicsEngineD3D12/src/ShaderResourceBindingD3D12Impl.cpp index 2d10f17d..17c39480 100644 --- a/Graphics/GraphicsEngineD3D12/src/ShaderResourceBindingD3D12Impl.cpp +++ b/Graphics/GraphicsEngineD3D12/src/ShaderResourceBindingD3D12Impl.cpp @@ -38,103 +38,36 @@ namespace Diligent ShaderResourceBindingD3D12Impl::ShaderResourceBindingD3D12Impl(IReferenceCounters* pRefCounters, PipelineResourceSignatureD3D12Impl* pPRS) : - // clang-format off - TBase - { - pRefCounters, - pPRS - }, - m_ShaderResourceCache{ResourceCacheContentType::SRB} -// clang-format on + TBase{pRefCounters, pPRS} { - try - { - const auto NumShaders = GetNumShaders(); - - FixedLinearAllocator MemPool{GetRawAllocator()}; - MemPool.AddSpace(NumShaders); - MemPool.Reserve(); - // Constructor of ShaderVariableManagerD3D12 is noexcept, so we can safely construct all managers. - m_pShaderVarMgrs = MemPool.ConstructArray(NumShaders, std::ref(*this), std::ref(m_ShaderResourceCache)); - - // The memory is now owned by ShaderResourceBindingD3D12Impl and will be freed by Destruct(). - auto* Ptr = MemPool.ReleaseOwnership(); - VERIFY_EXPR(Ptr == m_pShaderVarMgrs); - (void)Ptr; - - // It is important to construct all objects before initializing them because if an exception is thrown, - // destructors will be called for all objects - - auto& SRBMemAllocator = pPRS->GetSRBMemoryAllocator(); - auto& ResourceCacheDataAllocator = SRBMemAllocator.GetResourceCacheDataAllocator(0); - pPRS->InitSRBResourceCache(m_ShaderResourceCache, ResourceCacheDataAllocator, pPRS->GetDesc().Name); - - for (Uint32 s = 0; s < NumShaders; ++s) - { - const auto ShaderType = pPRS->GetActiveShaderStageType(s); - const auto ShaderInd = GetShaderTypePipelineIndex(ShaderType, pPRS->GetPipelineType()); - const auto MgrInd = m_ActiveShaderStageIndex[ShaderInd]; - VERIFY_EXPR(MgrInd >= 0 && MgrInd < static_cast(NumShaders)); + auto& SRBMemAllocator = pPRS->GetSRBMemoryAllocator(); + auto& ResourceCacheDataAllocator = SRBMemAllocator.GetResourceCacheDataAllocator(0); + pPRS->InitSRBResourceCache(m_ShaderResourceCache, ResourceCacheDataAllocator, pPRS->GetDesc().Name); - auto& VarDataAllocator = SRBMemAllocator.GetShaderVariableDataAllocator(s); - - // It is important that initialization is separated from construction because it provides exception safety. - constexpr SHADER_RESOURCE_VARIABLE_TYPE AllowedVarTypes[] = {SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC}; - m_pShaderVarMgrs[MgrInd].Initialize( - *pPRS, - VarDataAllocator, - AllowedVarTypes, - _countof(AllowedVarTypes), - ShaderType // - ); - } - } - catch (...) + const auto NumShaders = GetNumShaders(); + for (Uint32 s = 0; s < NumShaders; ++s) { - Destruct(); - throw; + const auto ShaderType = pPRS->GetActiveShaderStageType(s); + const auto ShaderInd = GetShaderTypePipelineIndex(ShaderType, pPRS->GetPipelineType()); + const auto MgrInd = m_ActiveShaderStageIndex[ShaderInd]; + VERIFY_EXPR(MgrInd >= 0 && MgrInd < static_cast(NumShaders)); + + auto& VarDataAllocator = SRBMemAllocator.GetShaderVariableDataAllocator(s); + + // It is important that initialization is separated from construction because it provides exception safety. + constexpr SHADER_RESOURCE_VARIABLE_TYPE AllowedVarTypes[] = {SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC}; + m_pShaderVarMgrs[MgrInd].Initialize( + *pPRS, + VarDataAllocator, + AllowedVarTypes, + _countof(AllowedVarTypes), + ShaderType // + ); } } - ShaderResourceBindingD3D12Impl::~ShaderResourceBindingD3D12Impl() { - Destruct(); -} - -void ShaderResourceBindingD3D12Impl::Destruct() -{ - if (m_pShaderVarMgrs != nullptr) - { - auto& SRBMemAllocator = GetSignature()->GetSRBMemoryAllocator(); - for (Uint32 s = 0; s < GetNumShaders(); ++s) - { - auto& VarDataAllocator = SRBMemAllocator.GetShaderVariableDataAllocator(s); - m_pShaderVarMgrs[s].Destroy(VarDataAllocator); - m_pShaderVarMgrs[s].~ShaderVariableManagerD3D12(); - } - GetRawAllocator().Free(m_pShaderVarMgrs); - } -} - -void ShaderResourceBindingD3D12Impl::BindResources(Uint32 ShaderFlags, IResourceMapping* pResMapping, Uint32 Flags) -{ - BindResourcesImpl(ShaderFlags, pResMapping, Flags, m_pShaderVarMgrs); -} - -IShaderResourceVariable* ShaderResourceBindingD3D12Impl::GetVariableByName(SHADER_TYPE ShaderType, const char* Name) -{ - return GetVariableByNameImpl(ShaderType, Name, m_pShaderVarMgrs); -} - -Uint32 ShaderResourceBindingD3D12Impl::GetVariableCount(SHADER_TYPE ShaderType) const -{ - return GetVariableCountImpl(ShaderType, m_pShaderVarMgrs); -} - -IShaderResourceVariable* ShaderResourceBindingD3D12Impl::GetVariableByIndex(SHADER_TYPE ShaderType, Uint32 Index) -{ - return GetVariableByIndexImpl(ShaderType, Index, m_pShaderVarMgrs); } } // namespace Diligent -- cgit v1.2.3