diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2019-03-02 22:16:17 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2019-03-02 22:16:17 +0000 |
| commit | a32b0a27713a7e5414923a7b50f31bb6830e45e8 (patch) | |
| tree | 26a49b14652f2b76c481b560780ae7016717d642 /Graphics/GraphicsEngineVulkan | |
| parent | Updated ShaderVariableVk: not creating variables for immutable samplers + cod... (diff) | |
| download | DiligentCore-a32b0a27713a7e5414923a7b50f31bb6830e45e8.tar.gz DiligentCore-a32b0a27713a7e5414923a7b50f31bb6830e45e8.zip | |
Updated ShaderVariableVk and ShaderResourceBindingVkImpl
Diffstat (limited to 'Graphics/GraphicsEngineVulkan')
7 files changed, 63 insertions, 64 deletions
diff --git a/Graphics/GraphicsEngineVulkan/include/DescriptorPoolManager.h b/Graphics/GraphicsEngineVulkan/include/DescriptorPoolManager.h index 24c3e157..bcb0a919 100644 --- a/Graphics/GraphicsEngineVulkan/include/DescriptorPoolManager.h +++ b/Graphics/GraphicsEngineVulkan/include/DescriptorPoolManager.h @@ -41,6 +41,7 @@ class RenderDeviceVkImpl; // This class manages descriptor set allocation. // The class destructor calls DescriptorSetAllocator::FreeDescriptorSet() that moves // the set into the release queue. +// sizeof(DescriptorSetAllocation) == 32 (x64) class DescriptorSetAllocation { public: diff --git a/Graphics/GraphicsEngineVulkan/include/ShaderResourceBindingVkImpl.h b/Graphics/GraphicsEngineVulkan/include/ShaderResourceBindingVkImpl.h index b3933747..5719a1e4 100644 --- a/Graphics/GraphicsEngineVulkan/include/ShaderResourceBindingVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/ShaderResourceBindingVkImpl.h @@ -48,7 +48,7 @@ public: ShaderResourceBindingVkImpl(IReferenceCounters* pRefCounters, class PipelineStateVkImpl* pPSO, bool IsPSOInternal); ~ShaderResourceBindingVkImpl(); - virtual void QueryInterface( const Diligent::INTERFACE_ID& IID, IObject** ppInterface )override final; + virtual void QueryInterface( const INTERFACE_ID& IID, IObject** ppInterface )override final; virtual void BindResources(Uint32 ShaderFlags, IResourceMapping* pResMapping, Uint32 Flags)override final; @@ -66,8 +66,9 @@ public: private: - ShaderResourceCacheVk m_ShaderResourceCache; + ShaderResourceCacheVk m_ShaderResourceCache; ShaderVariableManagerVk* m_pShaderVarMgrs = nullptr; + // Shader variable manager index in m_pShaderVarMgrs[] array for every shader stage Int8 m_ResourceLayoutIndex[6] = {-1, -1, -1, -1, -1, -1}; bool m_bStaticResourcesInitialized = false; diff --git a/Graphics/GraphicsEngineVulkan/include/ShaderResourceCacheVk.h b/Graphics/GraphicsEngineVulkan/include/ShaderResourceCacheVk.h index 21f97b04..0843939f 100644 --- a/Graphics/GraphicsEngineVulkan/include/ShaderResourceCacheVk.h +++ b/Graphics/GraphicsEngineVulkan/include/ShaderResourceCacheVk.h @@ -109,7 +109,7 @@ public: VkDescriptorImageInfo GetSamplerDescriptorWriteInfo() const; }; - // sizeof(DescriptorSet) == 40 (x64, msvc, Release) + // sizeof(DescriptorSet) == 48 (x64, msvc, Release) class DescriptorSet { public: @@ -147,11 +147,12 @@ public: m_DescriptorSetAllocation = std::move(Allocation); } - const Uint32 m_NumResources = 0; +/* 0 */ const Uint32 m_NumResources = 0; private: - Resource* const m_pResources = nullptr; - DescriptorSetAllocation m_DescriptorSetAllocation; +/* 8 */ Resource* const m_pResources = nullptr; +/*16 */ DescriptorSetAllocation m_DescriptorSetAllocation; +/*48 */ // End of structure }; inline DescriptorSet& GetDescriptorSet(Uint32 Index) diff --git a/Graphics/GraphicsEngineVulkan/include/ShaderVariableVk.h b/Graphics/GraphicsEngineVulkan/include/ShaderVariableVk.h index bb555881..74dac81d 100644 --- a/Graphics/GraphicsEngineVulkan/include/ShaderVariableVk.h +++ b/Graphics/GraphicsEngineVulkan/include/ShaderVariableVk.h @@ -68,17 +68,16 @@ class ShaderVariableVkImpl; class ShaderVariableManagerVk { public: - ShaderVariableManagerVk(IObject &Owner) : - m_Owner(Owner) - {} + ShaderVariableManagerVk(IObject& Owner, + const ShaderResourceLayoutVk& SrcLayout, + IMemoryAllocator& Allocator, + const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, + Uint32 NumAllowedTypes, + ShaderResourceCacheVk& ResourceCache); + ~ShaderVariableManagerVk(); - void Initialize(const ShaderResourceLayoutVk& Layout, - IMemoryAllocator& Allocator, - const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes, - ShaderResourceCacheVk& ResourceCache); - void Destroy(IMemoryAllocator& Allocator); + void DestroyVariables(IMemoryAllocator& Allocator); ShaderVariableVkImpl* GetVariable(const Char* Name); ShaderVariableVkImpl* GetVariable(Uint32 Index); @@ -102,8 +101,8 @@ private: // static resource layout owned by the same PSO object), or by SRB object (in which case // m_pResourceLayout points to corresponding layout in pipeline state). Since SRB keeps strong // reference to PSO, the layout is guaranteed to be alive while SRB is alive - const ShaderResourceLayoutVk* m_pResourceLayout= nullptr; - ShaderResourceCacheVk* m_pResourceCache = nullptr; + const ShaderResourceLayoutVk& m_ResourceLayout; + ShaderResourceCacheVk& m_ResourceCache; // Memory is allocated through the allocator provided by the pipeline state. If allocation granularity > 1, fixed block // memory allocator is used. This ensures that all resources from different shader resource bindings reside in @@ -112,7 +111,7 @@ private: Uint32 m_NumVariables = 0; #ifdef _DEBUG - IMemoryAllocator* m_pDbgAllocator = nullptr; + IMemoryAllocator& m_DbgAllocator; #endif }; @@ -167,15 +166,13 @@ public: virtual void Set(IDeviceObject* pObject)override final { - VERIFY_EXPR(m_ParentManager.m_pResourceCache != nullptr); - m_Resource.BindResource(pObject, 0, *m_ParentManager.m_pResourceCache); + m_Resource.BindResource(pObject, 0, m_ParentManager.m_ResourceCache); } virtual void SetArray(IDeviceObject* const* ppObjects, Uint32 FirstElement, Uint32 NumElements)override final { - VERIFY_EXPR(m_ParentManager.m_pResourceCache != nullptr); for (Uint32 Elem = 0; Elem < NumElements; ++Elem) - m_Resource.BindResource(ppObjects[Elem], FirstElement + Elem, *m_ParentManager.m_pResourceCache); + m_Resource.BindResource(ppObjects[Elem], FirstElement + Elem, m_ParentManager.m_ResourceCache); } virtual Uint32 GetArraySize()const override final diff --git a/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp index fe05a3bd..5cb00c8a 100644 --- a/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp @@ -176,8 +176,7 @@ PipelineStateVkImpl :: PipelineStateVkImpl(IReferenceCounters* pRefCounters auto* pStaticResCache = new (m_StaticResCaches + s) ShaderResourceCacheVk(ShaderResourceCacheVk::DbgCacheContentType::StaticShaderResources); pStaticResLayout->InitializeStaticResourceLayout(ShaderResources[s], ShaderResLayoutAllocator, PipelineDesc.ResourceLayout, m_StaticResCaches[s]); - auto* pStaticVarMgr = new (m_StaticVarsMgrs + s) ShaderVariableManagerVk(*this); - pStaticVarMgr->Initialize(*pStaticResLayout, GetRawAllocator(), nullptr, 0, *pStaticResCache); + new (m_StaticVarsMgrs + s) ShaderVariableManagerVk(*this, *pStaticResLayout, GetRawAllocator(), nullptr, 0, *pStaticResCache); } ShaderResourceLayoutVk::Initialize(pDeviceVk, m_NumShaders, m_ShaderResourceLayouts, ShaderResources.data(), GetRawAllocator(), PipelineDesc.ResourceLayout, ShaderSPIRVs.data(), m_PipelineLayout); @@ -461,7 +460,7 @@ PipelineStateVkImpl::~PipelineStateVkImpl() for (Uint32 s=0; s < m_NumShaders; ++s) { m_StaticResCaches[s].~ShaderResourceCacheVk(); - m_StaticVarsMgrs[s].Destroy(GetRawAllocator()); + m_StaticVarsMgrs[s].DestroyVariables(GetRawAllocator()); m_StaticVarsMgrs[s].~ShaderVariableManagerVk(); } RawAllocator.Free(m_ShaderResourceLayouts); diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp index 3077a5dd..e7f0ac89 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp @@ -21,7 +21,6 @@ * of the possibility of such damages. */ -#include <array> #include "pch.h" #include "ShaderResourceBindingVkImpl.h" #include "PipelineStateVkImpl.h" @@ -31,8 +30,8 @@ namespace Diligent { -ShaderResourceBindingVkImpl::ShaderResourceBindingVkImpl( IReferenceCounters* pRefCounters, PipelineStateVkImpl* pPSO, bool IsPSOInternal) : - TBase( pRefCounters, pPSO, IsPSOInternal ), +ShaderResourceBindingVkImpl::ShaderResourceBindingVkImpl(IReferenceCounters* pRefCounters, PipelineStateVkImpl* pPSO, bool IsPSOInternal) : + TBase (pRefCounters, pPSO, IsPSOInternal), m_ShaderResourceCache(ShaderResourceCacheVk::DbgCacheContentType::SRBResources) { auto* ppShaders = pPSO->GetShaders(); @@ -44,30 +43,27 @@ ShaderResourceBindingVkImpl::ShaderResourceBindingVkImpl( IReferenceCounters* pR auto& ResourceCacheDataAllocator = pPSO->GetSRBMemoryAllocator().GetResourceCacheDataAllocator(0); pPSO->GetPipelineLayout().InitResourceCache(pRenderDeviceVkImpl, m_ShaderResourceCache, ResourceCacheDataAllocator, pPSO->GetDesc().Name); - auto *pVarMgrsRawMem = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderVariableManagerVk", m_NumShaders * sizeof(ShaderVariableManagerVk)); + auto* pVarMgrsRawMem = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderVariableManagerVk", m_NumShaders * sizeof(ShaderVariableManagerVk)); m_pShaderVarMgrs = reinterpret_cast<ShaderVariableManagerVk*>(pVarMgrsRawMem); for (Uint32 s = 0; s < m_NumShaders; ++s) { - auto *pShader = ppShaders[s]; + auto* pShader = ppShaders[s]; auto ShaderType = pShader->GetDesc().ShaderType; auto ShaderInd = GetShaderTypeIndex(ShaderType); - - auto &VarDataAllocator = pPSO->GetSRBMemoryAllocator().GetShaderVariableDataAllocator(s); + m_ResourceLayoutIndex[ShaderInd] = static_cast<Int8>(s); + + auto& VarDataAllocator = pPSO->GetSRBMemoryAllocator().GetShaderVariableDataAllocator(s); - const auto &SrcLayout = pPSO->GetShaderResLayout(s); + const auto& SrcLayout = pPSO->GetShaderResLayout(s); // Use source layout to initialize resource memory in the cache SrcLayout.InitializeResourceMemoryInCache(m_ShaderResourceCache); // Create shader variable manager in place - new (m_pShaderVarMgrs + s) ShaderVariableManagerVk(*this); - // Initialize vars manager to reference mutable and dynamic variables // Note that the cache has space for all variable types - std::array<SHADER_RESOURCE_VARIABLE_TYPE, 2> VarTypes = {{SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC}}; - m_pShaderVarMgrs[s].Initialize(SrcLayout, VarDataAllocator, VarTypes.data(), static_cast<Uint32>(VarTypes.size()), m_ShaderResourceCache); - - m_ResourceLayoutIndex[ShaderInd] = static_cast<Int8>(s); + SHADER_RESOURCE_VARIABLE_TYPE VarTypes[] = {SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC}; + new (m_pShaderVarMgrs + s) ShaderVariableManagerVk(*this, SrcLayout, VarDataAllocator, VarTypes, _countof(VarTypes), m_ShaderResourceCache); } } @@ -76,8 +72,8 @@ ShaderResourceBindingVkImpl::~ShaderResourceBindingVkImpl() PipelineStateVkImpl* pPSO = ValidatedCast<PipelineStateVkImpl>(m_pPSO); for(Uint32 s = 0; s < m_NumShaders; ++s) { - auto &VarDataAllocator = pPSO->GetSRBMemoryAllocator().GetShaderVariableDataAllocator(s); - m_pShaderVarMgrs[s].Destroy(VarDataAllocator); + auto& VarDataAllocator = pPSO->GetSRBMemoryAllocator().GetShaderVariableDataAllocator(s); + m_pShaderVarMgrs[s].DestroyVariables(VarDataAllocator); m_pShaderVarMgrs[s].~ShaderVariableManagerVk(); } @@ -101,13 +97,14 @@ void ShaderResourceBindingVkImpl::BindResources(Uint32 ShaderFlags, IResourceMap } } -IShaderResourceVariable* ShaderResourceBindingVkImpl::GetVariable(SHADER_TYPE ShaderType, const char *Name) +IShaderResourceVariable* ShaderResourceBindingVkImpl::GetVariable(SHADER_TYPE ShaderType, const char* Name) { auto ShaderInd = GetShaderTypeIndex(ShaderType); auto ResLayoutInd = m_ResourceLayoutIndex[ShaderInd]; if (ResLayoutInd < 0) { - LOG_WARNING_MESSAGE("Unable to find mutable/dynamic variable '", Name, "': shader stage ", GetShaderTypeLiteralName(ShaderType), " is inactive"); + LOG_WARNING_MESSAGE("Unable to find mutable/dynamic variable '", Name, "': shader stage ", GetShaderTypeLiteralName(ShaderType), + " is inactive in Pipeline State '", m_pPSO->GetDesc().Name, "'."); return nullptr; } return m_pShaderVarMgrs[ResLayoutInd].GetVariable(Name); @@ -119,7 +116,8 @@ Uint32 ShaderResourceBindingVkImpl::GetVariableCount(SHADER_TYPE ShaderType) con auto ResLayoutInd = m_ResourceLayoutIndex[ShaderInd]; if (ResLayoutInd < 0) { - LOG_WARNING_MESSAGE("Unable to get the number of mutable/dynamic variables: shader stage ", GetShaderTypeLiteralName(ShaderType), " is inactive"); + LOG_WARNING_MESSAGE("Unable to get the number of mutable/dynamic variables: shader stage ", GetShaderTypeLiteralName(ShaderType), + " is inactive in Pipeline State '", m_pPSO->GetDesc().Name, "'."); return 0; } return m_pShaderVarMgrs[ResLayoutInd].GetVariableCount(); @@ -131,7 +129,8 @@ IShaderResourceVariable* ShaderResourceBindingVkImpl::GetVariable(SHADER_TYPE Sh auto ResLayoutInd = m_ResourceLayoutIndex[ShaderInd]; if (ResLayoutInd < 0) { - LOG_ERROR("Unable to get mutable/dynamic variable at index ", Index, ": shader stage ", GetShaderTypeLiteralName(ShaderType), " is inactive"); + LOG_WARNING_MESSAGE("Unable to get mutable/dynamic variable at index ", Index, ": shader stage ", GetShaderTypeLiteralName(ShaderType), + " is inactive in Pipeline State '", m_pPSO->GetDesc().Name, "'."); return nullptr; } return m_pShaderVarMgrs[ResLayoutInd].GetVariable(Index); @@ -151,7 +150,9 @@ void ShaderResourceBindingVkImpl::InitializeStaticResources(const IPipelineState } else { - DEV_CHECK_ERR(pPipelineState->IsCompatibleWith(GetPipelineState()), "The pipeline state is not compatible with this SRB"); + DEV_CHECK_ERR(pPipelineState->IsCompatibleWith(GetPipelineState()), "The pipeline state '", pPipelineState->GetDesc().Name, "' " + "is not compatible with the pipeline state '", m_pPSO->GetDesc().Name, "' this SRB was created from and cannot be " + "used to initialize static resources."); } auto* pPSOVK = ValidatedCast<const PipelineStateVkImpl>(pPipelineState); diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderVariableVk.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderVariableVk.cpp index 6698dd53..d005dc26 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderVariableVk.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderVariableVk.cpp @@ -61,18 +61,19 @@ size_t ShaderVariableManagerVk::GetRequiredMemorySize(const ShaderResourceLayout } // Creates shader variable for every resource from SrcLayout whose type is one AllowedVarTypes -void ShaderVariableManagerVk::Initialize(const ShaderResourceLayoutVk& SrcLayout, - IMemoryAllocator& Allocator, - const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes, - ShaderResourceCacheVk& ResourceCache) -{ - m_pResourceLayout = &SrcLayout; - m_pResourceCache = &ResourceCache; +ShaderVariableManagerVk::ShaderVariableManagerVk(IObject& Owner, + const ShaderResourceLayoutVk& SrcLayout, + IMemoryAllocator& Allocator, + const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, + Uint32 NumAllowedTypes, + ShaderResourceCacheVk& ResourceCache) : + m_Owner(Owner), + m_ResourceLayout(SrcLayout), + m_ResourceCache(ResourceCache) #ifdef _DEBUG - m_pDbgAllocator = &Allocator; + , m_DbgAllocator(Allocator) #endif - +{ const Uint32 AllowedTypeBits = GetAllowedTypeBits(AllowedVarTypes, NumAllowedTypes); VERIFY_EXPR(m_NumVariables == 0); auto MemSize = GetRequiredMemorySize(SrcLayout, AllowedVarTypes, NumAllowedTypes, m_NumVariables); @@ -108,12 +109,12 @@ void ShaderVariableManagerVk::Initialize(const ShaderResourceLayoutVk& ShaderVariableManagerVk::~ShaderVariableManagerVk() { - VERIFY(m_pVariables == nullptr, "Destroy() has not been called"); + VERIFY(m_pVariables == nullptr, "DestroyVariables() has not been called"); } -void ShaderVariableManagerVk::Destroy(IMemoryAllocator &Allocator) +void ShaderVariableManagerVk::DestroyVariables(IMemoryAllocator& Allocator) { - VERIFY(m_pDbgAllocator == &Allocator, "Incosistent alloctor"); + VERIFY(&m_DbgAllocator == &Allocator, "Incosistent alloctor"); if (m_pVariables != nullptr) { @@ -174,8 +175,6 @@ Uint32 ShaderVariableManagerVk::GetVariableIndex(const ShaderVariableVkImpl& Var void ShaderVariableManagerVk::BindResources(IResourceMapping* pResourceMapping, Uint32 Flags) { - VERIFY_EXPR(m_pResourceCache != nullptr); - if (!pResourceMapping) { LOG_ERROR_MESSAGE( "Failed to bind resources: resource mapping is null" ); @@ -199,7 +198,7 @@ void ShaderVariableManagerVk::BindResources(IResourceMapping* pResourceMapping, for (Uint32 ArrInd = 0; ArrInd < Res.SpirvAttribs.ArraySize; ++ArrInd) { - if ( (Flags & BIND_SHADER_RESOURCES_KEEP_EXISTING) && Res.IsBound(ArrInd, *m_pResourceCache) ) + if ( (Flags & BIND_SHADER_RESOURCES_KEEP_EXISTING) && Res.IsBound(ArrInd, m_ResourceCache) ) continue; const auto* VarName = Res.SpirvAttribs.Name; @@ -207,11 +206,11 @@ void ShaderVariableManagerVk::BindResources(IResourceMapping* pResourceMapping, pResourceMapping->GetResource( VarName, &pObj, ArrInd ); if (pObj) { - Res.BindResource(pObj, ArrInd, *m_pResourceCache); + Res.BindResource(pObj, ArrInd, m_ResourceCache); } else { - if ( (Flags & BIND_SHADER_RESOURCES_VERIFY_ALL_RESOLVED) && !Res.IsBound(ArrInd, *m_pResourceCache) ) + if ( (Flags & BIND_SHADER_RESOURCES_VERIFY_ALL_RESOLVED) && !Res.IsBound(ArrInd, m_ResourceCache) ) LOG_ERROR_MESSAGE( "Unable to bind resource to shader variable '", Res.SpirvAttribs.GetPrintName(ArrInd), "': resource is not found in the resource mapping. " "Do not use BIND_SHADER_RESOURCES_VERIFY_ALL_RESOLVED flag to suppress the message if this is not an issue." ); } |
