summaryrefslogtreecommitdiffstats
path: root/Graphics/GraphicsEngineD3D11
diff options
context:
space:
mode:
authorEgor Yusov <egor.yusov@gmail.com>2018-06-22 16:14:43 +0000
committerEgor Yusov <egor.yusov@gmail.com>2018-06-22 16:14:43 +0000
commitf0c5879b396a9d04d98f3e68dcbeeb4b05e0f430 (patch)
tree0534e03e92cdc4155953d0347db008beb70f88e1 /Graphics/GraphicsEngineD3D11
parentImproved shader resource binding memory allocation in D3D12 backend: replaced... (diff)
downloadDiligentCore-f0c5879b396a9d04d98f3e68dcbeeb4b05e0f430.tar.gz
DiligentCore-f0c5879b396a9d04d98f3e68dcbeeb4b05e0f430.zip
Improved SRB data allocation in D3D11 backend; removed AdaptiveFixedBlockAllocator
Diffstat (limited to 'Graphics/GraphicsEngineD3D11')
-rw-r--r--Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.h37
-rw-r--r--Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.h3
-rw-r--r--Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.h4
-rw-r--r--Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp40
-rw-r--r--Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp17
-rw-r--r--Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp23
-rw-r--r--Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp18
7 files changed, 77 insertions, 65 deletions
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<ID3D11DepthStencilState> m_pd3d11DepthStencilState;
CComPtr<ID3D11InputLayout> 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<const ShaderResourcesD3D11>& 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 <array>
#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<ShaderResourceBindingD3D11Impl, FixedBlockMemoryAllocator>(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<size_t, MaxShadersInPipeline> ShaderResLayoutDataSizes = {};
+ std::array<size_t, MaxShadersInPipeline> ShaderResCacheDataSizes = {};
+ for (Uint32 s = 0; s < m_NumShaders; ++s)
+ {
+ auto* pShader = ValidatedCast<ShaderD3D11Impl>(m_ppShaders[s]);
+ const auto& ShaderResources = *pShader->GetResources();
+ std::array<SHADER_VARIABLE_TYPE, 2> AllowedVarTypes = { SHADER_VARIABLE_TYPE_MUTABLE, SHADER_VARIABLE_TYPE_DYNAMIC };
+ ShaderResLayoutDataSizes[s] = ShaderResourceLayoutD3D11::GetRequiredMemorySize(ShaderResources, AllowedVarTypes.data(), static_cast<Uint32>(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<Int32>(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<PipelineStateD3D11Impl>(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<const ShaderResourcesD3D11>& pSrcResources,
const SHADER_VARIABLE_TYPE* VarTypes,
Uint32 NumVarTypes,
@@ -111,7 +125,9 @@ void ShaderResourceLayoutD3D11::Initialize(const std::shared_ptr<const ShaderRes
m_BuffUAVsOffset = m_TexUAVsOffset + static_cast<Uint16>( NumTexUAVs * sizeof(TexUAVBindInfo) );
m_BuffSRVsOffset = m_BuffUAVsOffset + static_cast<Uint16>( 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);