From 8813773d1df679d1a747a91e6be7afcdaa5b42c7 Mon Sep 17 00:00:00 2001 From: assiduous Date: Tue, 23 Feb 2021 19:39:00 -0800 Subject: Reworked ShaderResourceBindingVkImpl and ShaderResourceBindingD3D12Impl: moved common functionality to ShaderResourceBindingBase --- .../include/ShaderResourceBindingVkImpl.hpp | 15 +--- .../src/PipelineResourceSignatureVkImpl.cpp | 2 +- .../src/ShaderResourceBindingVkImpl.cpp | 86 ++++------------------ 3 files changed, 17 insertions(+), 86 deletions(-) (limited to 'Graphics/GraphicsEngineVulkan') diff --git a/Graphics/GraphicsEngineVulkan/include/ShaderResourceBindingVkImpl.hpp b/Graphics/GraphicsEngineVulkan/include/ShaderResourceBindingVkImpl.hpp index e2026a67..2ea6de99 100644 --- a/Graphics/GraphicsEngineVulkan/include/ShaderResourceBindingVkImpl.hpp +++ b/Graphics/GraphicsEngineVulkan/include/ShaderResourceBindingVkImpl.hpp @@ -51,8 +51,7 @@ public: using TBase = ShaderResourceBindingBase; ShaderResourceBindingVkImpl(IReferenceCounters* pRefCounters, - PipelineResourceSignatureVkImpl* pPRS, - bool IsDeviceInternal); + PipelineResourceSignatureVkImpl* pPRS); ~ShaderResourceBindingVkImpl(); IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_ShaderResourceBindingVk, TBase) @@ -69,26 +68,14 @@ public: /// Implementation of IShaderResourceBinding::GetVariableByIndex() in Vulkan backend. virtual IShaderResourceVariable* DILIGENT_CALL_TYPE GetVariableByIndex(SHADER_TYPE ShaderType, Uint32 Index) override final; - /// Implementation of IShaderResourceBinding::InitializeStaticResources() in Vulkan backend. - virtual void DILIGENT_CALL_TYPE InitializeStaticResources(const IPipelineState* pPipelineState) override final; - /// Implementation of IShaderResourceBinding::InitializeStaticResourcesWithSignature() in Vulkan backend. virtual void DILIGENT_CALL_TYPE InitializeStaticResourcesWithSignature(const IPipelineResourceSignature* pResourceSignature) override final; ShaderResourceCacheVk& GetResourceCache() { return m_ShaderResourceCache; } - bool StaticResourcesInitialized() const { return m_bStaticResourcesInitialized; } - private: void Destruct(); - std::array m_ShaderVarIndex = {-1, -1, -1, -1, -1, -1}; - static_assert(MAX_SHADERS_IN_PIPELINE == 6, "Please update the initializer list above"); - - bool m_bStaticResourcesInitialized = false; - - const Uint8 m_NumShaders = 0; - ShaderResourceCacheVk m_ShaderResourceCache; ShaderVariableManagerVk* m_pShaderVarMgrs = nullptr; }; diff --git a/Graphics/GraphicsEngineVulkan/src/PipelineResourceSignatureVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/PipelineResourceSignatureVkImpl.cpp index cedeaddc..bea6d341 100644 --- a/Graphics/GraphicsEngineVulkan/src/PipelineResourceSignatureVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/PipelineResourceSignatureVkImpl.cpp @@ -713,7 +713,7 @@ void PipelineResourceSignatureVkImpl::CreateShaderResourceBinding(IShaderResourc bool InitStaticResources) { auto& SRBAllocator = m_pDevice->GetSRBAllocator(); - auto pResBindingVk = NEW_RC_OBJ(SRBAllocator, "ShaderResourceBindingVkImpl instance", ShaderResourceBindingVkImpl)(this, false); + auto pResBindingVk = NEW_RC_OBJ(SRBAllocator, "ShaderResourceBindingVkImpl instance", ShaderResourceBindingVkImpl)(this); if (InitStaticResources) pResBindingVk->InitializeStaticResources(nullptr); pResBindingVk->QueryInterface(IID_ShaderResourceBinding, reinterpret_cast(ppShaderResourceBinding)); diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp index ca029922..eab0534f 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp @@ -36,27 +36,24 @@ namespace Diligent { ShaderResourceBindingVkImpl::ShaderResourceBindingVkImpl(IReferenceCounters* pRefCounters, - PipelineResourceSignatureVkImpl* pPRS, - bool IsDeviceInternal) : + PipelineResourceSignatureVkImpl* pPRS) : // clang-format off TBase { pRefCounters, - pPRS, - IsDeviceInternal + pPRS }, - m_ShaderResourceCache{ShaderResourceCacheVk::CacheContentType::SRB}, - m_NumShaders{static_cast(pPRS->GetNumActiveShaderStages())} + m_ShaderResourceCache{ShaderResourceCacheVk::CacheContentType::SRB} // clang-format on { try { - m_ShaderVarIndex.fill(-1); + const auto NumShaders = GetNumShaders(); FixedLinearAllocator MemPool{GetRawAllocator()}; - MemPool.AddSpace(m_NumShaders); + MemPool.AddSpace(NumShaders); MemPool.Reserve(); - m_pShaderVarMgrs = MemPool.ConstructArray(m_NumShaders, std::ref(*this), std::ref(m_ShaderResourceCache)); + m_pShaderVarMgrs = MemPool.ConstructArray(NumShaders, std::ref(*this), std::ref(m_ShaderResourceCache)); // The memory is now owned by ShaderResourceBindingVkImpl and will be freed by Destruct(). auto* Ptr = MemPool.ReleaseOwnership(); @@ -72,12 +69,12 @@ ShaderResourceBindingVkImpl::ShaderResourceBindingVkImpl(IReferenceCounters* auto& ResourceCacheDataAllocator = SRBMemAllocator.GetResourceCacheDataAllocator(0); pPRS->InitSRBResourceCache(m_ShaderResourceCache, ResourceCacheDataAllocator, pPRS->GetDesc().Name); - for (Uint32 s = 0; s < m_NumShaders; ++s) + for (Uint32 s = 0; s < NumShaders; ++s) { const auto ShaderType = pPRS->GetActiveShaderStageType(s); const auto ShaderInd = GetShaderTypePipelineIndex(ShaderType, pPRS->GetPipelineType()); - - m_ShaderVarIndex[ShaderInd] = static_cast(s); + const auto MgrInd = m_ActiveShaderStageIndex[ShaderInd]; + VERIFY_EXPR(MgrInd >= 0 && MgrInd < static_cast(NumShaders)); auto& VarDataAllocator = SRBMemAllocator.GetShaderVariableDataAllocator(s); @@ -85,7 +82,7 @@ ShaderResourceBindingVkImpl::ShaderResourceBindingVkImpl(IReferenceCounters* // Initialize vars manager to reference mutable and dynamic variables // Note that the cache has space for all variable types const SHADER_RESOURCE_VARIABLE_TYPE VarTypes[] = {SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC}; - m_pShaderVarMgrs[s].Initialize(*pPRS, VarDataAllocator, VarTypes, _countof(VarTypes), ShaderType); + m_pShaderVarMgrs[MgrInd].Initialize(*pPRS, VarDataAllocator, VarTypes, _countof(VarTypes), ShaderType); } } catch (...) @@ -105,7 +102,7 @@ void ShaderResourceBindingVkImpl::Destruct() if (m_pShaderVarMgrs != nullptr) { auto& SRBMemAllocator = GetSignature()->GetSRBMemoryAllocator(); - for (Uint32 s = 0; s < m_NumShaders; ++s) + for (Uint32 s = 0; s < GetNumShaders(); ++s) { auto& VarDataAllocator = SRBMemAllocator.GetShaderVariableDataAllocator(s); m_pShaderVarMgrs[s].DestroyVariables(VarDataAllocator); @@ -118,75 +115,22 @@ void ShaderResourceBindingVkImpl::Destruct() void ShaderResourceBindingVkImpl::BindResources(Uint32 ShaderFlags, IResourceMapping* pResMapping, Uint32 Flags) { - const auto PipelineType = GetPipelineType(); - for (Uint32 ShaderInd = 0; ShaderInd < m_ShaderVarIndex.size(); ++ShaderInd) - { - const auto VarMngrInd = m_ShaderVarIndex[ShaderInd]; - if (VarMngrInd >= 0) - { - // ShaderInd is the shader type pipeline index here - const auto ShaderType = GetShaderTypeFromPipelineIndex(ShaderInd, PipelineType); - if (ShaderFlags & ShaderType) - { - m_pShaderVarMgrs[VarMngrInd].BindResources(pResMapping, Flags); - } - } - } + BindResourcesImpl(ShaderFlags, pResMapping, Flags, m_pShaderVarMgrs); } Uint32 ShaderResourceBindingVkImpl::GetVariableCount(SHADER_TYPE ShaderType) const { - const auto VarMngrInd = GetVariableCountHelper(ShaderType, m_ShaderVarIndex); - if (VarMngrInd < 0) - return 0; - - auto& ShaderVarMgr = m_pShaderVarMgrs[VarMngrInd]; - return ShaderVarMgr.GetVariableCount(); + return GetVariableCountImpl(ShaderType, m_pShaderVarMgrs); } IShaderResourceVariable* ShaderResourceBindingVkImpl::GetVariableByName(SHADER_TYPE ShaderType, const Char* Name) { - const auto VarMngrInd = GetVariableByNameHelper(ShaderType, Name, m_ShaderVarIndex); - if (VarMngrInd < 0) - return nullptr; - - auto& ShaderVarMgr = m_pShaderVarMgrs[VarMngrInd]; - return ShaderVarMgr.GetVariable(Name); + return GetVariableByNameImpl(ShaderType, Name, m_pShaderVarMgrs); } IShaderResourceVariable* ShaderResourceBindingVkImpl::GetVariableByIndex(SHADER_TYPE ShaderType, Uint32 Index) { - const auto VarMngrInd = GetVariableByIndexHelper(ShaderType, Index, m_ShaderVarIndex); - if (VarMngrInd < 0) - return nullptr; - - const auto& ShaderVarMgr = m_pShaderVarMgrs[VarMngrInd]; - return ShaderVarMgr.GetVariable(Index); -} - -void ShaderResourceBindingVkImpl::InitializeStaticResources(const IPipelineState* pPipelineState) -{ - if (StaticResourcesInitialized()) - { - LOG_WARNING_MESSAGE("Static resources have already been initialized in this shader resource binding object. The operation will be ignored."); - return; - } - - if (pPipelineState == nullptr) - { - InitializeStaticResourcesWithSignature(nullptr); - } - else - { - auto* pSign = pPipelineState->GetResourceSignature(GetBindingIndex()); - if (pSign == nullptr) - { - LOG_ERROR_MESSAGE("Shader resource binding is not compatible with pipeline state."); - return; - } - - InitializeStaticResourcesWithSignature(pSign); - } + return GetVariableByIndexImpl(ShaderType, Index, m_pShaderVarMgrs); } void ShaderResourceBindingVkImpl::InitializeStaticResourcesWithSignature(const IPipelineResourceSignature* pResourceSignature) -- cgit v1.2.3