From 6e54a0840da780c3b208bc72294aa33eecc64738 Mon Sep 17 00:00:00 2001 From: assiduous Date: Wed, 10 Mar 2021 20:58:49 -0800 Subject: Moved duplicate shader variable functionality to ShaderVariableBase --- .../include/ShaderResourceVariableBase.hpp | 48 ++++++++- .../include/ShaderVariableManagerD3D12.hpp | 37 +------ .../src/ShaderVariableManagerD3D12.cpp | 11 +- .../include/ShaderVariableManagerGL.hpp | 118 +++------------------ .../src/ShaderVariableManagerGL.cpp | 18 ++-- .../include/ShaderVariableManagerVk.hpp | 44 +------- .../src/ShaderVariableManagerVk.cpp | 13 +-- 7 files changed, 80 insertions(+), 209 deletions(-) (limited to 'Graphics') diff --git a/Graphics/GraphicsEngine/include/ShaderResourceVariableBase.hpp b/Graphics/GraphicsEngine/include/ShaderResourceVariableBase.hpp index 7b8115a9..e35cdb96 100644 --- a/Graphics/GraphicsEngine/include/ShaderResourceVariableBase.hpp +++ b/Graphics/GraphicsEngine/include/ShaderResourceVariableBase.hpp @@ -493,12 +493,14 @@ std::string GetShaderGroupName(const ShaderVectorType& Shaders) } /// Base implementation of a shader variable -template struct ShaderVariableBase : public ResourceVariableBaseInterface { - ShaderVariableBase(VarManagerType& ParentManager) : - m_ParentManager{ParentManager} + ShaderVariableBase(VarManagerType& ParentManager, Uint32 ResIndex) : + m_ParentManager{ParentManager}, + m_ResIndex{ResIndex} { } @@ -530,7 +532,39 @@ struct ShaderVariableBase : public ResourceVariableBaseInterface return m_ParentManager.GetOwner().GetReferenceCounters(); } - template + virtual void DILIGENT_CALL_TYPE Set(IDeviceObject* pObject) override final + { + static_cast(this)->BindResource(pObject, 0); + } + + virtual void DILIGENT_CALL_TYPE SetArray(IDeviceObject* const* ppObjects, + Uint32 FirstElement, + Uint32 NumElements) override final + { + const auto& Desc = GetDesc(); + VerifyAndCorrectSetArrayArguments(Desc.Name, Desc.ArraySize, FirstElement, NumElements); + for (Uint32 elem = 0; elem < NumElements; ++elem) + static_cast(this)->BindResource(ppObjects[elem], FirstElement + elem); + } + + virtual SHADER_RESOURCE_VARIABLE_TYPE DILIGENT_CALL_TYPE GetType() const override final + { + return GetDesc().VarType; + } + + virtual void DILIGENT_CALL_TYPE GetResourceDesc(ShaderResourceDesc& ResourceDesc) const override final + { + const auto& Desc = GetDesc(); + ResourceDesc.Name = Desc.Name; + ResourceDesc.Type = Desc.ResourceType; + ResourceDesc.ArraySize = Desc.ArraySize; + } + + virtual Uint32 DILIGENT_CALL_TYPE GetIndex() const override final + { + return m_ParentManager.GetVariableIndex(*static_cast(this)); + } + void BindResources(IResourceMapping* pResourceMapping, Uint32 Flags) { auto* const pThis = static_cast(this); @@ -564,8 +598,14 @@ struct ShaderVariableBase : public ResourceVariableBaseInterface } } + const PipelineResourceDesc& GetDesc() const { return m_ParentManager.GetResourceDesc(m_ResIndex); } + protected: + // Variable manager that owns this variable VarManagerType& m_ParentManager; + + // Resource index in pipeline resource signature m_Desc.Resources[] + const Uint32 m_ResIndex; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3D12/include/ShaderVariableManagerD3D12.hpp b/Graphics/GraphicsEngineD3D12/include/ShaderVariableManagerD3D12.hpp index a067592e..4b53fe89 100644 --- a/Graphics/GraphicsEngineD3D12/include/ShaderVariableManagerD3D12.hpp +++ b/Graphics/GraphicsEngineD3D12/include/ShaderVariableManagerD3D12.hpp @@ -120,6 +120,8 @@ public: private: friend ShaderVariableD3D12Impl; + friend ShaderVariableBase; + using ResourceAttribs = PipelineResourceAttribsD3D12; Uint32 GetVariableIndex(const ShaderVariableD3D12Impl& Variable); @@ -151,14 +153,13 @@ private: }; // sizeof(ShaderVariableD3D12Impl) == 24 (x64) -class ShaderVariableD3D12Impl final : public ShaderVariableBase +class ShaderVariableD3D12Impl final : public ShaderVariableBase { public: - using TBase = ShaderVariableBase; + using TBase = ShaderVariableBase; ShaderVariableD3D12Impl(ShaderVariableManagerD3D12& ParentManager, Uint32 ResIndex) : - TBase{ParentManager}, - m_ResIndex{ResIndex} + TBase{ParentManager, ResIndex} {} // clang-format off @@ -181,31 +182,6 @@ public: } } - virtual SHADER_RESOURCE_VARIABLE_TYPE DILIGENT_CALL_TYPE GetType() const override final - { - return GetDesc().VarType; - } - - virtual void DILIGENT_CALL_TYPE Set(IDeviceObject* pObject) override final - { - BindResource(pObject, 0); - } - - virtual void DILIGENT_CALL_TYPE SetArray(IDeviceObject* const* ppObjects, Uint32 FirstElement, Uint32 NumElements) override final; - - virtual void DILIGENT_CALL_TYPE GetResourceDesc(ShaderResourceDesc& ResourceDesc) const override final - { - const auto& Desc = GetDesc(); - ResourceDesc.Name = Desc.Name; - ResourceDesc.Type = Desc.ResourceType; - ResourceDesc.ArraySize = Desc.ArraySize; - } - - virtual Uint32 DILIGENT_CALL_TYPE GetIndex() const override final - { - return m_ParentManager.GetVariableIndex(*this); - } - virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final { return m_ParentManager.IsBound(ArrayIndex, m_ResIndex); @@ -233,9 +209,6 @@ private: { return m_ParentManager.GetResourceAttribs(m_ResIndex); } - -private: - const Uint32 m_ResIndex; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3D12/src/ShaderVariableManagerD3D12.cpp b/Graphics/GraphicsEngineD3D12/src/ShaderVariableManagerD3D12.cpp index a47583a1..2d335863 100644 --- a/Graphics/GraphicsEngineD3D12/src/ShaderVariableManagerD3D12.cpp +++ b/Graphics/GraphicsEngineD3D12/src/ShaderVariableManagerD3D12.cpp @@ -198,19 +198,10 @@ void ShaderVariableManagerD3D12::BindResources(IResourceMapping* pResourceMappin for (Uint32 v = 0; v < m_NumVariables; ++v) { - m_pVariables[v].BindResources(pResourceMapping, Flags); + m_pVariables[v].BindResources(pResourceMapping, Flags); } } -void ShaderVariableD3D12Impl::SetArray(IDeviceObject* const* ppObjects, Uint32 FirstElement, Uint32 NumElements) -{ - const auto& ResDesc = GetDesc(); - VerifyAndCorrectSetArrayArguments(ResDesc.Name, ResDesc.ArraySize, FirstElement, NumElements); - - for (Uint32 Elem = 0; Elem < NumElements; ++Elem) - BindResource(ppObjects[Elem], FirstElement + Elem); -} - namespace { diff --git a/Graphics/GraphicsEngineOpenGL/include/ShaderVariableManagerGL.hpp b/Graphics/GraphicsEngineOpenGL/include/ShaderVariableManagerGL.hpp index e028f566..cadf1eee 100644 --- a/Graphics/GraphicsEngineOpenGL/include/ShaderVariableManagerGL.hpp +++ b/Graphics/GraphicsEngineOpenGL/include/ShaderVariableManagerGL.hpp @@ -63,7 +63,7 @@ namespace Diligent class PipelineResourceSignatureGLImpl; -// sizeof(ShaderVariableManagerGL) == 48 (x64, msvc, Release) +// sizeof(ShaderVariableManagerGL) == 40 (x64, msvc, Release) class ShaderVariableManagerGL { public: @@ -99,68 +99,28 @@ public: // These two methods can't be implemented in the header because they depend on PipelineResourceSignatureGLImpl const PipelineResourceDesc& GetResourceDesc(Uint32 Index) const; - const ResourceAttribs& GetAttribs(Uint32 Index) const; + const ResourceAttribs& GetResourceAttribs(Uint32 Index) const; - - struct GLVariableBase : public ShaderVariableBase + template + struct GLVariableBase : public ShaderVariableBase { public: - using TBase = ShaderVariableBase; GLVariableBase(ShaderVariableManagerGL& ParentLayout, Uint32 ResIndex) : - TBase{ParentLayout}, - m_ResIndex{ResIndex} + ShaderVariableBase{ParentLayout, ResIndex} {} - const PipelineResourceDesc& GetDesc() const { return m_ParentManager.GetResourceDesc(m_ResIndex); } - const ResourceAttribs& GetAttribs() const { return m_ParentManager.GetAttribs(m_ResIndex); } - - virtual SHADER_RESOURCE_VARIABLE_TYPE DILIGENT_CALL_TYPE GetType() const override final - { - return GetDesc().VarType; - } - - virtual void DILIGENT_CALL_TYPE GetResourceDesc(ShaderResourceDesc& ResourceDesc) const override final - { - const auto& Desc = GetDesc(); - ResourceDesc.Name = Desc.Name; - ResourceDesc.Type = Desc.ResourceType; - ResourceDesc.ArraySize = Desc.ArraySize; - } - - virtual Uint32 DILIGENT_CALL_TYPE GetIndex() const override final - { - return m_ParentManager.GetVariableIndex(*this); - } - - private: - const Uint32 m_ResIndex; + const ResourceAttribs& GetAttribs() const { return this->m_ParentManager.GetResourceAttribs(this->m_ResIndex); } }; - struct UniformBuffBindInfo final : GLVariableBase + struct UniformBuffBindInfo final : GLVariableBase { UniformBuffBindInfo(ShaderVariableManagerGL& ParentLayout, Uint32 ResIndex) : - GLVariableBase{ParentLayout, ResIndex} + GLVariableBase{ParentLayout, ResIndex} {} - // Non-virtual function void BindResource(IDeviceObject* pObject, Uint32 ArrayIndex); - virtual void DILIGENT_CALL_TYPE Set(IDeviceObject* pObject) override final - { - BindResource(pObject, 0); - } - - virtual void DILIGENT_CALL_TYPE SetArray(IDeviceObject* const* ppObjects, - Uint32 FirstElement, - Uint32 NumElements) override final - { - const auto& Desc = GetDesc(); - VerifyAndCorrectSetArrayArguments(Desc.Name, Desc.ArraySize, FirstElement, NumElements); - for (Uint32 elem = 0; elem < NumElements; ++elem) - BindResource(ppObjects[elem], FirstElement + elem); - } - virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final { VERIFY_EXPR(ArrayIndex < GetDesc().ArraySize); @@ -169,30 +129,14 @@ public: }; - struct TextureBindInfo final : GLVariableBase + struct TextureBindInfo final : GLVariableBase { TextureBindInfo(ShaderVariableManagerGL& ParentLayout, Uint32 ResIndex) : - GLVariableBase{ParentLayout, ResIndex} + GLVariableBase{ParentLayout, ResIndex} {} - // Non-virtual function void BindResource(IDeviceObject* pObject, Uint32 ArrayIndex); - virtual void DILIGENT_CALL_TYPE Set(IDeviceObject* pObject) override final - { - BindResource(pObject, 0); - } - - virtual void DILIGENT_CALL_TYPE SetArray(IDeviceObject* const* ppObjects, - Uint32 FirstElement, - Uint32 NumElements) override final - { - const auto& Desc = GetDesc(); - VerifyAndCorrectSetArrayArguments(Desc.Name, Desc.ArraySize, FirstElement, NumElements); - for (Uint32 elem = 0; elem < NumElements; ++elem) - BindResource(ppObjects[elem], FirstElement + elem); - } - virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final { const auto& Desc = GetDesc(); @@ -203,30 +147,14 @@ public: }; - struct ImageBindInfo final : GLVariableBase + struct ImageBindInfo final : GLVariableBase { ImageBindInfo(ShaderVariableManagerGL& ParentLayout, Uint32 ResIndex) : - GLVariableBase{ParentLayout, ResIndex} + GLVariableBase{ParentLayout, ResIndex} {} - // Provide non-virtual function void BindResource(IDeviceObject* pObject, Uint32 ArrayIndex); - virtual void DILIGENT_CALL_TYPE Set(IDeviceObject* pObject) override final - { - BindResource(pObject, 0); - } - - virtual void DILIGENT_CALL_TYPE SetArray(IDeviceObject* const* ppObjects, - Uint32 FirstElement, - Uint32 NumElements) override final - { - const auto& Desc = GetDesc(); - VerifyAndCorrectSetArrayArguments(Desc.Name, Desc.ArraySize, FirstElement, NumElements); - for (Uint32 elem = 0; elem < NumElements; ++elem) - BindResource(ppObjects[elem], FirstElement + elem); - } - virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final { const auto& Desc = GetDesc(); @@ -237,30 +165,14 @@ public: }; - struct StorageBufferBindInfo final : GLVariableBase + struct StorageBufferBindInfo final : GLVariableBase { StorageBufferBindInfo(ShaderVariableManagerGL& ParentLayout, Uint32 ResIndex) : - GLVariableBase{ParentLayout, ResIndex} + GLVariableBase{ParentLayout, ResIndex} {} - // Non-virtual function void BindResource(IDeviceObject* pObject, Uint32 ArrayIndex); - virtual void DILIGENT_CALL_TYPE Set(IDeviceObject* pObject) override final - { - BindResource(pObject, 0); - } - - virtual void DILIGENT_CALL_TYPE SetArray(IDeviceObject* const* ppObjects, - Uint32 FirstElement, - Uint32 NumElements) override final - { - const auto& Desc = GetDesc(); - VerifyAndCorrectSetArrayArguments(Desc.Name, Desc.ArraySize, FirstElement, NumElements); - for (Uint32 elem = 0; elem < NumElements; ++elem) - BindResource(ppObjects[elem], FirstElement + elem); - } - virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final { VERIFY_EXPR(ArrayIndex < GetDesc().ArraySize); @@ -297,7 +209,7 @@ public: return reinterpret_cast(reinterpret_cast(m_ResourceBuffer) + Offset)[ResIndex]; } - Uint32 GetVariableIndex(const GLVariableBase& Var) const; + Uint32 GetVariableIndex(const IShaderResourceVariable& Var) const; private: struct ResourceCounters diff --git a/Graphics/GraphicsEngineOpenGL/src/ShaderVariableManagerGL.cpp b/Graphics/GraphicsEngineOpenGL/src/ShaderVariableManagerGL.cpp index 4294f2e8..1ad70b17 100644 --- a/Graphics/GraphicsEngineOpenGL/src/ShaderVariableManagerGL.cpp +++ b/Graphics/GraphicsEngineOpenGL/src/ShaderVariableManagerGL.cpp @@ -97,7 +97,7 @@ void ShaderVariableManagerGL::Initialize(const PipelineResourceSignatureGLImpl& m_pDbgAllocator = &Allocator; #endif - auto Counters = CountResources(Signature, AllowedVarTypes, NumAllowedTypes, ShaderType); + const auto Counters = CountResources(Signature, AllowedVarTypes, NumAllowedTypes, ShaderType); m_pSignature = &Signature; @@ -254,7 +254,7 @@ void ShaderVariableManagerGL::TextureBindInfo::BindResource(IDeviceObject* pView RESOURCE_DIM_UNDEFINED, false, CachedTexSampler.pView.RawPtr()); if (ImmutableSamplerAssigned && ResourceCache.GetContentType() == ResourceCacheContentType::SRB) { - VERIFY(CachedTexSampler.pSampler != nullptr, "Immutable samplers must be initialized in the SRB cache by PipelineResourceSignatureGLImpl::InitializeSRBResourceCache!"); + VERIFY(CachedTexSampler.pSampler != nullptr, "Immutable samplers must be initialized in the SRB cache by PipelineResourceSignatureGLImpl::InitSRBResourceCache!"); } if (Desc.ResourceType == SHADER_RESOURCE_TYPE_INPUT_ATTACHMENT) { @@ -403,16 +403,16 @@ void ShaderVariableManagerGL::BindResources(IResourceMapping* pResourceMapping, HandleResources( [&](UniformBuffBindInfo& ub) { - ub.BindResources(pResourceMapping, Flags); + ub.BindResources(pResourceMapping, Flags); }, [&](TextureBindInfo& tex) { - tex.BindResources(pResourceMapping, Flags); + tex.BindResources(pResourceMapping, Flags); }, [&](ImageBindInfo& img) { - img.BindResources(pResourceMapping, Flags); + img.BindResources(pResourceMapping, Flags); }, [&](StorageBufferBindInfo& ssbo) { - ssbo.BindResources(pResourceMapping, Flags); + ssbo.BindResources(pResourceMapping, Flags); }); } @@ -505,7 +505,7 @@ IShaderResourceVariable* ShaderVariableManagerGL::GetVariable(Uint32 Index) cons class ShaderVariableIndexLocator { public: - ShaderVariableIndexLocator(const ShaderVariableManagerGL& _Layout, const ShaderVariableManagerGL::GLVariableBase& Variable) : + ShaderVariableIndexLocator(const ShaderVariableManagerGL& _Layout, const IShaderResourceVariable& Variable) : // clang-format off Layout {_Layout}, VarOffset(reinterpret_cast(&Variable) - reinterpret_cast(_Layout.m_ResourceBuffer)) @@ -542,7 +542,7 @@ private: }; -Uint32 ShaderVariableManagerGL::GetVariableIndex(const GLVariableBase& Var) const +Uint32 ShaderVariableManagerGL::GetVariableIndex(const IShaderResourceVariable& Var) const { if (!m_ResourceBuffer) { @@ -574,7 +574,7 @@ const PipelineResourceDesc& ShaderVariableManagerGL::GetResourceDesc(Uint32 Inde return m_pSignature->GetResourceDesc(Index); } -const ShaderVariableManagerGL::ResourceAttribs& ShaderVariableManagerGL::GetAttribs(Uint32 Index) const +const ShaderVariableManagerGL::ResourceAttribs& ShaderVariableManagerGL::GetResourceAttribs(Uint32 Index) const { VERIFY_EXPR(m_pSignature); return m_pSignature->GetResourceAttribs(Index); diff --git a/Graphics/GraphicsEngineVulkan/include/ShaderVariableManagerVk.hpp b/Graphics/GraphicsEngineVulkan/include/ShaderVariableManagerVk.hpp index 06db6075..3869e64d 100644 --- a/Graphics/GraphicsEngineVulkan/include/ShaderVariableManagerVk.hpp +++ b/Graphics/GraphicsEngineVulkan/include/ShaderVariableManagerVk.hpp @@ -115,6 +115,8 @@ public: private: friend ShaderVariableVkImpl; + friend ShaderVariableBase; + using ResourceAttribs = PipelineResourceAttribsVk; Uint32 GetVariableIndex(const ShaderVariableVkImpl& Variable); @@ -146,15 +148,14 @@ private: }; // sizeof(ShaderVariableVkImpl) == 24 (x64) -class ShaderVariableVkImpl final : public ShaderVariableBase +class ShaderVariableVkImpl final : public ShaderVariableBase { public: - using TBase = ShaderVariableBase; + using TBase = ShaderVariableBase; ShaderVariableVkImpl(ShaderVariableManagerVk& ParentManager, Uint32 ResIndex) : - TBase{ParentManager}, - m_ResIndex{ResIndex} + TBase{ParentManager, ResIndex} {} // clang-format off @@ -164,43 +165,11 @@ public: ShaderVariableVkImpl& operator= (ShaderVariableVkImpl&&) = delete; // clang-format on - virtual SHADER_RESOURCE_VARIABLE_TYPE DILIGENT_CALL_TYPE GetType() const override final - { - return GetDesc().VarType; - } - - virtual void DILIGENT_CALL_TYPE Set(IDeviceObject* pObject) override final - { - BindResource(pObject, 0); - } - - virtual void DILIGENT_CALL_TYPE SetArray(IDeviceObject* const* ppObjects, - Uint32 FirstElement, - Uint32 NumElements) override final; - - virtual void DILIGENT_CALL_TYPE GetResourceDesc(ShaderResourceDesc& ResourceDesc) const override final - { - const auto& Desc = GetDesc(); - ResourceDesc.Name = Desc.Name; - ResourceDesc.Type = Desc.ResourceType; - ResourceDesc.ArraySize = Desc.ArraySize; - } - - virtual Uint32 DILIGENT_CALL_TYPE GetIndex() const override final - { - return m_ParentManager.GetVariableIndex(*this); - } - virtual bool DILIGENT_CALL_TYPE IsBound(Uint32 ArrayIndex) const override final { return m_ParentManager.IsBound(ArrayIndex, m_ResIndex); } - const PipelineResourceDesc& GetDesc() const - { - return m_ParentManager.GetResourceDesc(m_ResIndex); - } - void BindResource(IDeviceObject* pObj, Uint32 ArrayIndex) const { return m_ParentManager.BindResource(pObj, ArrayIndex, m_ResIndex); @@ -210,9 +179,6 @@ private: using ResourceAttribs = PipelineResourceAttribsVk; const ResourceAttribs& GetAttribs() const { return m_ParentManager.GetAttribs(m_ResIndex); } - -private: - const Uint32 m_ResIndex; // Index in Signatures' m_Desc.Resources }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderVariableManagerVk.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderVariableManagerVk.cpp index a159b8b9..5f4bd8d6 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderVariableManagerVk.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderVariableManagerVk.cpp @@ -198,21 +198,10 @@ void ShaderVariableManagerVk::BindResources(IResourceMapping* pResourceMapping, for (Uint32 v = 0; v < m_NumVariables; ++v) { - m_pVariables[v].BindResources(pResourceMapping, Flags); + m_pVariables[v].BindResources(pResourceMapping, Flags); } } -void ShaderVariableVkImpl::SetArray(IDeviceObject* const* ppObjects, - Uint32 FirstElement, - Uint32 NumElements) -{ - const auto& ResDesc = GetDesc(); - VerifyAndCorrectSetArrayArguments(ResDesc.Name, ResDesc.ArraySize, FirstElement, NumElements); - - for (Uint32 Elem = 0; Elem < NumElements; ++Elem) - BindResource(ppObjects[Elem], FirstElement + Elem); -} - namespace { -- cgit v1.2.3