diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2018-06-22 05:52:21 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2018-06-22 05:52:21 +0000 |
| commit | 53dd8552c725a90c284a706cd69a822600159cd1 (patch) | |
| tree | 1f5f82f86f557de7e62b3dff52e12758d0cb3644 /Graphics/GraphicsEngineVulkan | |
| parent | Improved memory allocation for shader variables and resource cache in Vk backend (diff) | |
| download | DiligentCore-53dd8552c725a90c284a706cd69a822600159cd1.tar.gz DiligentCore-53dd8552c725a90c284a706cd69a822600159cd1.zip | |
Added SRBMemoryAllocator
Diffstat (limited to 'Graphics/GraphicsEngineVulkan')
3 files changed, 21 insertions, 46 deletions
diff --git a/Graphics/GraphicsEngineVulkan/include/PipelineStateVkImpl.h b/Graphics/GraphicsEngineVulkan/include/PipelineStateVkImpl.h index 3e3a2134..a96017e7 100644 --- a/Graphics/GraphicsEngineVulkan/include/PipelineStateVkImpl.h +++ b/Graphics/GraphicsEngineVulkan/include/PipelineStateVkImpl.h @@ -34,6 +34,7 @@ #include "PipelineLayout.h" #include "ShaderResourceLayoutVk.h" #include "FixedBlockMemoryAllocator.h" +#include "SRBMemoryAllocator.h" #include "VulkanUtilities/VulkanObjectWrappers.h" #include "VulkanUtilities/VulkanCommandBuffer.h" #include "PipelineLayout.h" @@ -84,14 +85,9 @@ public: return m_ShaderResourceLayouts[ShaderInd]; } - IMemoryAllocator& GetResourceCacheDataAllocator() + SRBMemoryAllocator& GetSRBMemoryAllocator() { - return m_ResourceCacheDataAllocator != nullptr ? *m_ResourceCacheDataAllocator : GetRawAllocator(); - } - IMemoryAllocator& GetShaderVariableDataAllocator(Uint32 ActiveShaderInd) - { - VERIFY_EXPR(ActiveShaderInd < m_NumShaders); - return m_VariableDataAllocators != nullptr ? m_VariableDataAllocators[ActiveShaderInd] : GetRawAllocator(); + return m_SRBMemAllocator; } IShaderVariable *GetDummyShaderVar(){return &m_DummyVar;} @@ -104,9 +100,8 @@ private: ShaderResourceLayoutVk* m_ShaderResourceLayouts = nullptr; - // Use separate fixed-block allocator allocator for every shader stage - FixedBlockMemoryAllocator* m_VariableDataAllocators = nullptr; - FixedBlockMemoryAllocator* m_ResourceCacheDataAllocator = nullptr; + // SRB memory allocator must be declared before m_pDefaultShaderResBinding + SRBMemoryAllocator m_SRBMemAllocator; std::array<VulkanUtilities::ShaderModuleWrapper, MaxShadersInPipeline> m_ShaderModules; diff --git a/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp index 91baace8..7014d51b 100644 --- a/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp @@ -129,6 +129,7 @@ PipelineStateVkImpl :: PipelineStateVkImpl(IReferenceCounters* pRefCounters const PipelineStateDesc& PipelineDesc) : TPipelineStateBase(pRefCounters, pDeviceVk, PipelineDesc), m_DummyVar(*this), + m_SRBMemAllocator(GetRawAllocator()), m_pDefaultShaderResBinding(nullptr, STDDeleter<ShaderResourceBindingVkImpl, FixedBlockMemoryAllocator>(pDeviceVk->GetSRBAllocator()) ) { const auto& LogicalDevice = pDeviceVk->GetLogicalDevice(); @@ -151,25 +152,19 @@ PipelineStateVkImpl :: PipelineStateVkImpl(IReferenceCounters* pRefCounters if (PipelineDesc.SRBAllocationGranularity > 1) { + std::array<size_t, MaxShadersInPipeline> ShaderVariableDataSizes = {}; + for (Uint32 s = 0; s < m_NumShaders; ++s) { - auto* pVarAllocatorsRawMem = ALLOCATE(ShaderResLayoutAllocator, "Raw memory for m_VariableDataAllocators", sizeof(FixedBlockMemoryAllocator) * m_NumShaders); - m_VariableDataAllocators = reinterpret_cast<FixedBlockMemoryAllocator*>(pVarAllocatorsRawMem); - for (Uint32 s = 0; s < m_NumShaders; ++s) - { - std::array<SHADER_VARIABLE_TYPE, 2> AllowedVarTypes = { SHADER_VARIABLE_TYPE_STATIC, SHADER_VARIABLE_TYPE_MUTABLE }; - Uint32 UnusedNumVars = 0; - auto RequiredMemSize = ShaderVariableManagerVk::GetRequiredMemorySize(m_ShaderResourceLayouts[s], AllowedVarTypes.data(), static_cast<Uint32>(AllowedVarTypes.size()), UnusedNumVars); - new(m_VariableDataAllocators + s)FixedBlockMemoryAllocator(GetRawAllocator(), RequiredMemSize, PipelineDesc.SRBAllocationGranularity); - } + std::array<SHADER_VARIABLE_TYPE, 2> AllowedVarTypes = { SHADER_VARIABLE_TYPE_STATIC, SHADER_VARIABLE_TYPE_MUTABLE }; + Uint32 UnusedNumVars = 0; + ShaderVariableDataSizes[s] = ShaderVariableManagerVk::GetRequiredMemorySize(m_ShaderResourceLayouts[s], AllowedVarTypes.data(), static_cast<Uint32>(AllowedVarTypes.size()), UnusedNumVars); } - { - Uint32 NumSets = 0; - auto DescriptorSetSizes = m_PipelineLayout.GetDescriptorSetSizes(NumSets); - m_ResourceCacheDataAllocator = reinterpret_cast<FixedBlockMemoryAllocator*>(ALLOCATE(ShaderResLayoutAllocator, "Raw memory for m_ResourceCacheDataAllocator", sizeof(FixedBlockMemoryAllocator))); - auto CacheMemorySize = ShaderResourceCacheVk::GetRequiredMemorySize(NumSets, DescriptorSetSizes.data()); - new(m_ResourceCacheDataAllocator) FixedBlockMemoryAllocator(GetRawAllocator(), CacheMemorySize, PipelineDesc.SRBAllocationGranularity); - } + Uint32 NumSets = 0; + auto DescriptorSetSizes = m_PipelineLayout.GetDescriptorSetSizes(NumSets); + auto CacheMemorySize = ShaderResourceCacheVk::GetRequiredMemorySize(NumSets, DescriptorSetSizes.data()); + + m_SRBMemAllocator.Initialize(PipelineDesc.SRBAllocationGranularity, m_NumShaders, ShaderVariableDataSizes.data(), 1, &CacheMemorySize); } // Create shader modules and initialize shader stages @@ -414,27 +409,11 @@ PipelineStateVkImpl::~PipelineStateVkImpl() } } - // Default SRB must be destroyed before shader variable and - // shader resource cache memory allocators + // Default SRB must be destroyed before SRB allocators m_pDefaultShaderResBinding.reset(); auto& RawAllocator = GetRawAllocator(); - if (m_ResourceCacheDataAllocator != nullptr) - { - m_ResourceCacheDataAllocator->~FixedBlockMemoryAllocator(); - RawAllocator.Free(m_ResourceCacheDataAllocator); - } - - if (m_VariableDataAllocators != nullptr) - { - for (Uint32 s=0; s < m_NumShaders; ++s) - { - m_VariableDataAllocators[s].~FixedBlockMemoryAllocator(); - } - RawAllocator.Free(m_VariableDataAllocators); - } - for (Uint32 s=0; s < m_NumShaders; ++s) { m_ShaderResourceLayouts[s].~ShaderResourceLayoutVk(); diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp index 6e26bcb3..4e8e4edf 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderResourceBindingVkImpl.cpp @@ -41,7 +41,8 @@ ShaderResourceBindingVkImpl::ShaderResourceBindingVkImpl( IReferenceCounters* pR auto *pRenderDeviceVkImpl = pPSO->GetDevice<RenderDeviceVkImpl>(); // This will only allocate memory and initialize descriptor sets in the resource cache // Resources will be initialized by InitializeResourceMemoryInCache() - pPSO->GetPipelineLayout().InitResourceCache(pRenderDeviceVkImpl, m_ShaderResourceCache, pPSO->GetResourceCacheDataAllocator()); + auto& ResourceCacheDataAllocator = pPSO->GetSRBMemoryAllocator().GetResourceCacheDataAllocator(0); + pPSO->GetPipelineLayout().InitResourceCache(pRenderDeviceVkImpl, m_ShaderResourceCache, ResourceCacheDataAllocator); auto *pVarMgrsRawMem = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderVariableManagerVk", m_NumShaders * sizeof(ShaderVariableManagerVk)); m_pShaderVarMgrs = reinterpret_cast<ShaderVariableManagerVk*>(pVarMgrsRawMem); @@ -52,7 +53,7 @@ ShaderResourceBindingVkImpl::ShaderResourceBindingVkImpl( IReferenceCounters* pR auto ShaderType = pShader->GetDesc().ShaderType; auto ShaderInd = GetShaderTypeIndex(ShaderType); - auto &VarDataAllocator = pPSO->GetShaderVariableDataAllocator(s); + auto &VarDataAllocator = pPSO->GetSRBMemoryAllocator().GetShaderVariableDataAllocator(s); const auto &SrcLayout = pPSO->GetShaderResLayout(s); // Use source layout to initialize resource memory in the cache @@ -75,7 +76,7 @@ ShaderResourceBindingVkImpl::~ShaderResourceBindingVkImpl() PipelineStateVkImpl* pPSO = ValidatedCast<PipelineStateVkImpl>(m_pPSO); for(Uint32 s = 0; s < m_NumShaders; ++s) { - auto &VarDataAllocator = pPSO->GetShaderVariableDataAllocator(s); + auto &VarDataAllocator = pPSO->GetSRBMemoryAllocator().GetShaderVariableDataAllocator(s); m_pShaderVarMgrs[s].Destroy(VarDataAllocator); m_pShaderVarMgrs[s].~ShaderVariableManagerVk(); } |
