From f0c5879b396a9d04d98f3e68dcbeeb4b05e0f430 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Fri, 22 Jun 2018 09:14:43 -0700 Subject: Improved SRB data allocation in D3D11 backend; removed AdaptiveFixedBlockAllocator --- .../include/PipelineStateD3D11Impl.h | 37 ++++---------------- .../include/ShaderResourceCacheD3D11.h | 3 ++ .../include/ShaderResourceLayoutD3D11.h | 4 +++ .../src/PipelineStateD3D11Impl.cpp | 40 +++++++++------------- .../src/ShaderResourceBindingD3D11Impl.cpp | 17 ++++----- .../src/ShaderResourceCacheD3D11.cpp | 23 +++++++++++++ .../src/ShaderResourceLayoutD3D11.cpp | 18 +++++++++- 7 files changed, 77 insertions(+), 65 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h index cb44157e..e3d1a461 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h @@ -30,7 +30,7 @@ #include "RenderDeviceD3D11.h" #include "PipelineStateBase.h" #include "ShaderD3D11Impl.h" -#include "AdaptiveFixedBlockAllocator.h" +#include "SRBMemoryAllocator.h" namespace Diligent { @@ -74,18 +74,12 @@ public: virtual bool IsCompatibleWith(const IPipelineState *pPSO)const override final; class ShaderResourceBindingD3D11Impl* GetDefaultResourceBinding(){return m_pDefaultShaderResBinding.get();} - IMemoryAllocator &GetResourceCacheDataAllocator(Uint32 ActiveShaderInd) - { - VERIFY_EXPR(ActiveShaderInd < m_NumShaders); - auto *pAllocator = m_Allocators.GetResourceCacheDataAllocator(ActiveShaderInd); - return pAllocator != nullptr ? *pAllocator : GetRawAllocator(); - } - IMemoryAllocator &GetShaderResLayoutDataAllocators(Uint32 ActiveShaderInd) + + SRBMemoryAllocator& GetSRBMemoryAllocator() { - VERIFY_EXPR(ActiveShaderInd < m_NumShaders); - auto *pAllocator = m_Allocators.GetShaderResLayoutDataAllocator(ActiveShaderInd); - return pAllocator != nullptr ? *pAllocator : GetRawAllocator(); + return m_SRBMemAllocator; } + IShaderVariable* GetDummyShaderVariable(){return &m_DummyShaderVar;} private: @@ -94,25 +88,8 @@ private: CComPtr m_pd3d11DepthStencilState; CComPtr m_pd3d11InputLayout; - class DataAllocators - { - public: - ~DataAllocators(); - void Init(size_t NumActiveShaders, Uint32 SRBAllocationGranularity); - AdaptiveFixedBlockAllocator* GetShaderResLayoutDataAllocator(Uint32 Ind) - { - VERIFY_EXPR(Ind < _countof(m_pShaderResLayoutDataAllocators)); - return m_pShaderResLayoutDataAllocators[Ind]; - } - AdaptiveFixedBlockAllocator* GetResourceCacheDataAllocator(Uint32 Ind) - { - VERIFY_EXPR(Ind < _countof(m_pResourceCacheDataAllocators)); - return m_pResourceCacheDataAllocators[Ind]; - } - private: - AdaptiveFixedBlockAllocator* m_pShaderResLayoutDataAllocators[5] = {}; // Use separate allocator for every shader stage - AdaptiveFixedBlockAllocator* m_pResourceCacheDataAllocators[5] = {}; // Use separate allocator for every shader stage - }m_Allocators; // Allocators must be defined before the default shader res binding + // SRB memory allocator must be defined before the default shader res binding + SRBMemoryAllocator m_SRBMemAllocator; // Do not use strong reference to avoid cyclic references // Must be declared after the data allocators diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.h b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.h index 38a3b50b..3a7495aa 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.h @@ -115,6 +115,9 @@ public: } }; + static size_t GetRequriedMemorySize(const class ShaderResourcesD3D11& Resources); + + void Initialize(const class ShaderResourcesD3D11& Resources, class IMemoryAllocator &MemAllocator); void Initialize(Int32 CBCount, Int32 SRVCount, Int32 SamplerCount, Int32 UAVCount, class IMemoryAllocator &MemAllocator); void Destroy(class IMemoryAllocator& MemAllocator); diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h index af4a60dc..2012015f 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h @@ -58,6 +58,10 @@ public: ShaderResourceLayoutD3D11 (ShaderResourceLayoutD3D11&&) = default; ShaderResourceLayoutD3D11& operator = (ShaderResourceLayoutD3D11&&) = delete; + static size_t GetRequiredMemorySize(const ShaderResourcesD3D11& SrcResources, + const SHADER_VARIABLE_TYPE* VarTypes, + Uint32 NumVarTypes); + void Initialize(const std::shared_ptr& pSrcResources, const SHADER_VARIABLE_TYPE* VarTypes, Uint32 NumVarTypes, diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index e96270b8..4241eb31 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -22,6 +22,7 @@ */ #include "pch.h" +#include #include "PipelineStateD3D11Impl.h" #include "RenderDeviceD3D11Impl.h" #include "ShaderResourceBindingD3D11Impl.h" @@ -34,6 +35,7 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun RenderDeviceD3D11Impl* pRenderDeviceD3D11, const PipelineStateDesc& PipelineDesc) : TPipelineStateBase(pRefCounters, pRenderDeviceD3D11, PipelineDesc), + m_SRBMemAllocator(GetRawAllocator()), m_pDefaultShaderResBinding( nullptr, STDDeleter(pRenderDeviceD3D11->GetSRBAllocator()) ), m_DummyShaderVar(*this) { @@ -112,35 +114,25 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCoun } if(PipelineDesc.SRBAllocationGranularity > 1) - m_Allocators.Init(m_NumShaders, PipelineDesc.SRBAllocationGranularity); + { + std::array ShaderResLayoutDataSizes = {}; + std::array ShaderResCacheDataSizes = {}; + for (Uint32 s = 0; s < m_NumShaders; ++s) + { + auto* pShader = ValidatedCast(m_ppShaders[s]); + const auto& ShaderResources = *pShader->GetResources(); + std::array AllowedVarTypes = { SHADER_VARIABLE_TYPE_MUTABLE, SHADER_VARIABLE_TYPE_DYNAMIC }; + ShaderResLayoutDataSizes[s] = ShaderResourceLayoutD3D11::GetRequiredMemorySize(ShaderResources, AllowedVarTypes.data(), static_cast(AllowedVarTypes.size())); + ShaderResCacheDataSizes[s] = ShaderResourceCacheD3D11::GetRequriedMemorySize(ShaderResources); + } + + m_SRBMemAllocator.Initialize(PipelineDesc.SRBAllocationGranularity, m_NumShaders, ShaderResLayoutDataSizes.data(), m_NumShaders, ShaderResCacheDataSizes.data()); + } auto &SRBAllocator = pRenderDeviceD3D11->GetSRBAllocator(); m_pDefaultShaderResBinding.reset( NEW_RC_OBJ(SRBAllocator, "ShaderResourceBindingD3D11Impl instance", ShaderResourceBindingD3D11Impl, this)(this, true) ); } -void PipelineStateD3D11Impl::DataAllocators::Init(size_t NumActiveShaders, Uint32 SRBAllocationGranularity) -{ - VERIFY_EXPR(NumActiveShaders <= _countof(m_pShaderResLayoutDataAllocators) ); - // Since this is not constructor, there is no need to - // handle exceptions. If exception is thrown, ~DataAllocators() - // will destroy all allocators that have been initialized - for(size_t stage = 0; stage < NumActiveShaders; ++stage) - { - m_pShaderResLayoutDataAllocators[stage] = NEW_POOL_OBJECT(AdaptiveFixedBlockAllocator, "Shader resource layout data allocator", GetRawAllocator(), SRBAllocationGranularity); - m_pResourceCacheDataAllocators[stage] = NEW_POOL_OBJECT(AdaptiveFixedBlockAllocator, "Shader resource cache data allocator", GetRawAllocator(), SRBAllocationGranularity); - } -} - -PipelineStateD3D11Impl::DataAllocators::~DataAllocators() -{ - for(size_t i=0; i < _countof(m_pShaderResLayoutDataAllocators); ++i) - if(m_pShaderResLayoutDataAllocators[i] != nullptr) - DESTROY_POOL_OBJECT(m_pShaderResLayoutDataAllocators[i]); - - for(size_t i=0; i < _countof(m_pResourceCacheDataAllocators); ++i) - if(m_pResourceCacheDataAllocators[i] != nullptr) - DESTROY_POOL_OBJECT(m_pResourceCacheDataAllocators[i]); -} PipelineStateD3D11Impl::~PipelineStateD3D11Impl() { diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp index 9259c563..5d1afaa5 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp @@ -56,18 +56,15 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl( IReferenceCounte auto ShaderInd = pShaderD3D11->GetShaderTypeIndex(); VERIFY_EXPR(static_cast(ShaderInd) == GetShaderTypeIndex(pShaderD3D11->GetDesc().ShaderType)); - auto &ResCacheDataAllocator = pPSO->GetResourceCacheDataAllocator(s); - auto &ResLayoutDataAllocator = pPSO->GetShaderResLayoutDataAllocators(s); + auto& SRBMemAllocator = pPSO->GetSRBMemoryAllocator(); + auto& ResCacheDataAllocator = SRBMemAllocator.GetResourceCacheDataAllocator(s); + auto& ResLayoutDataAllocator = SRBMemAllocator.GetShaderVariableDataAllocator(s); // Initialize resource cache to have enough space to contain all shader resources, including static ones // Static resources are copied before resources are committed - const auto &Resources = *pShaderD3D11->GetResources(); - auto CBCount = Resources.GetMaxCBBindPoint()+1; - auto SRVCount = Resources.GetMaxSRVBindPoint()+1; - auto SamplerCount = Resources.GetMaxSamplerBindPoint()+1; - auto UAVCount = Resources.GetMaxUAVBindPoint()+1; + const auto& Resources = *pShaderD3D11->GetResources(); new (m_pBoundResourceCaches+s) ShaderResourceCacheD3D11; - m_pBoundResourceCaches[s].Initialize(CBCount, SRVCount, SamplerCount, UAVCount, ResCacheDataAllocator); + m_pBoundResourceCaches[s].Initialize(Resources, ResCacheDataAllocator); // Shader resource layout will only contain dynamic and mutable variables // http://diligentgraphics.com/diligent-engine/architecture/d3d11/shader-resource-cache#Shader-Resource-Cache-Initialization @@ -87,7 +84,7 @@ ShaderResourceBindingD3D11Impl::~ShaderResourceBindingD3D11Impl() auto *pPSOD3D11Impl = ValidatedCast(m_pPSO); for (Uint32 s = 0; s < m_NumActiveShaders; ++s) { - auto &Allocator = pPSOD3D11Impl->GetResourceCacheDataAllocator(s); + auto& Allocator = pPSOD3D11Impl->GetSRBMemoryAllocator().GetResourceCacheDataAllocator(s); m_pBoundResourceCaches[s].Destroy(Allocator); m_pBoundResourceCaches[s].~ShaderResourceCacheD3D11(); } @@ -106,7 +103,7 @@ void ShaderResourceBindingD3D11Impl::BindResources(Uint32 ShaderFlags, IResource { for(Uint32 ResLayoutInd = 0; ResLayoutInd < m_NumActiveShaders; ++ResLayoutInd) { - auto &ResLayout = m_pResourceLayouts[ResLayoutInd]; + auto& ResLayout = m_pResourceLayouts[ResLayoutInd]; if(ShaderFlags & ResLayout.GetShaderType()) { ResLayout.BindResources(pResMapping, Flags, m_pBoundResourceCaches[ResLayoutInd]); diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp index 36f6645e..1564a9b7 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp @@ -32,6 +32,29 @@ namespace Diligent { + size_t ShaderResourceCacheD3D11::GetRequriedMemorySize(const ShaderResourcesD3D11& Resources) + { + auto CBCount = Resources.GetMaxCBBindPoint() + 1; + auto SRVCount = Resources.GetMaxSRVBindPoint() + 1; + auto SamplerCount = Resources.GetMaxSamplerBindPoint()+ 1; + auto UAVCount = Resources.GetMaxUAVBindPoint() + 1; + auto MemSize = + (sizeof(CachedCB) + sizeof(ID3D11Buffer*)) * CBCount + + (sizeof(CachedResource) + sizeof(ID3D11ShaderResourceView*)) * SRVCount + + (sizeof(CachedSampler) + sizeof(ID3D11SamplerState*)) * SamplerCount + + (sizeof(CachedResource) + sizeof(ID3D11UnorderedAccessView*)) * UAVCount; + return MemSize; + } + + void ShaderResourceCacheD3D11::Initialize(const ShaderResourcesD3D11& Resources, IMemoryAllocator& MemAllocator) + { + auto CBCount = Resources.GetMaxCBBindPoint() + 1; + auto SRVCount = Resources.GetMaxSRVBindPoint() + 1; + auto SamplerCount = Resources.GetMaxSamplerBindPoint()+ 1; + auto UAVCount = Resources.GetMaxUAVBindPoint() + 1; + Initialize(CBCount, SRVCount, SamplerCount, UAVCount, MemAllocator); + } + void ShaderResourceCacheD3D11::Initialize(Int32 CBCount, Int32 SRVCount, Int32 SamplerCount, Int32 UAVCount, IMemoryAllocator& MemAllocator) { // http://diligentgraphics.com/diligent-engine/architecture/d3d11/shader-resource-cache/ diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp index e1502f8b..ebfb6a18 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp @@ -79,6 +79,20 @@ ShaderResourceLayoutD3D11::~ShaderResourceLayoutD3D11() const D3DShaderResourceAttribs ShaderResourceLayoutD3D11::TexAndSamplerBindInfo::InvalidSamplerAttribs("Invalid sampler", D3DShaderResourceAttribs::InvalidBindPoint, 0, D3D_SIT_SAMPLER, SHADER_VARIABLE_TYPE_NUM_TYPES, D3D_SRV_DIMENSION_UNKNOWN, D3DShaderResourceAttribs::InvalidSamplerId, false); +size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D11& SrcResources, + const SHADER_VARIABLE_TYPE* VarTypes, + Uint32 NumVarTypes) +{ + Uint32 NumCBs, NumTexSRVs, NumTexUAVs, NumBufSRVs, NumBufUAVs, NumSamplers; + SrcResources.CountResources(VarTypes, NumVarTypes, NumCBs, NumTexSRVs, NumTexUAVs, NumBufSRVs, NumBufUAVs, NumSamplers); + auto MemSize = NumCBs * sizeof(ConstBuffBindInfo) + + NumTexSRVs * sizeof(TexAndSamplerBindInfo) + + NumTexUAVs * sizeof(TexUAVBindInfo) + + NumBufUAVs * sizeof(BuffUAVBindInfo) + + NumBufSRVs * sizeof(BuffSRVBindInfo); + return MemSize; +} + void ShaderResourceLayoutD3D11::Initialize(const std::shared_ptr& pSrcResources, const SHADER_VARIABLE_TYPE* VarTypes, Uint32 NumVarTypes, @@ -111,7 +125,9 @@ void ShaderResourceLayoutD3D11::Initialize(const std::shared_ptr( NumTexUAVs * sizeof(TexUAVBindInfo) ); m_BuffSRVsOffset = m_BuffUAVsOffset + static_cast( NumBufUAVs * sizeof(BuffUAVBindInfo) ); auto MemorySize = m_BuffSRVsOffset + NumBufSRVs * sizeof(BuffSRVBindInfo); - + + VERIFY_EXPR(MemorySize == GetRequiredMemorySize(*pSrcResources, VarTypes, NumVarTypes)); + if( MemorySize ) { auto *pRawMem = ALLOCATE(ResLayoutDataAllocator, "Raw memory buffer for shader resource layout resources", MemorySize); -- cgit v1.2.3