diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2019-03-05 03:17:32 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2019-03-05 03:17:32 +0000 |
| commit | c72191a465edd01905181aa47cc57e47fcc29b9e (patch) | |
| tree | 1a51acaf618fe32a23f3b3b0d020e15293b64a77 /Graphics/GraphicsEngineD3D12 | |
| parent | Minor interface improvement (diff) | |
| download | DiligentCore-c72191a465edd01905181aa47cc57e47fcc29b9e.tar.gz DiligentCore-c72191a465edd01905181aa47cc57e47fcc29b9e.zip | |
Completed D3D12 back-end refactor to comply with the new API
Diffstat (limited to 'Graphics/GraphicsEngineD3D12')
12 files changed, 265 insertions, 117 deletions
diff --git a/Graphics/GraphicsEngineD3D12/include/PipelineStateD3D12Impl.h b/Graphics/GraphicsEngineD3D12/include/PipelineStateD3D12Impl.h index 35cd4e7e..64dfdc2b 100644 --- a/Graphics/GraphicsEngineD3D12/include/PipelineStateD3D12Impl.h +++ b/Graphics/GraphicsEngineD3D12/include/PipelineStateD3D12Impl.h @@ -33,6 +33,7 @@ #include "ShaderResourceLayoutD3D12.h" #include "SRBMemoryAllocator.h" #include "RenderDeviceD3D12Impl.h" +#include "ShaderVariableD3D12.h" namespace Diligent { @@ -49,12 +50,20 @@ public: virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; - virtual ID3D12PipelineState *GetD3D12PipelineState()const override final{return m_pd3d12PSO;} - + virtual void BindStaticResources(IResourceMapping* pResourceMapping, Uint32 Flags)override final; + + virtual Uint32 GetStaticVariableCount(SHADER_TYPE ShaderType) const override final; + + virtual IShaderResourceVariable* GetStaticShaderVariable(SHADER_TYPE ShaderType, const Char* Name) override final; + + virtual IShaderResourceVariable* GetStaticShaderVariable(SHADER_TYPE ShaderType, Uint32 Index) override final; + virtual void CreateShaderResourceBinding( IShaderResourceBinding **ppShaderResourceBinding, bool InitStaticResources )override final; virtual bool IsCompatibleWith(const IPipelineState *pPSO)const override final; + virtual ID3D12PipelineState *GetD3D12PipelineState()const override final{return m_pd3d12PSO;} + virtual ID3D12RootSignature *GetD3D12RootSignature()const override final{return m_RootSig.GetD3D12RootSignature(); } ShaderResourceCacheD3D12* CommitAndTransitionShaderResources(IShaderResourceBinding* pShaderResourceBinding, @@ -70,7 +79,19 @@ public: VERIFY_EXPR(ShaderInd < m_NumShaders); return m_pShaderResourceLayouts[ShaderInd]; } + + const ShaderResourceLayoutD3D12& GetStaticShaderResLayout(Uint32 ShaderInd)const + { + VERIFY_EXPR(ShaderInd < m_NumShaders); + return m_pShaderResourceLayouts[m_NumShaders + ShaderInd]; + } + ShaderResourceCacheD3D12& GetStaticShaderResCache(Uint32 ShaderInd)const + { + VERIFY_EXPR(ShaderInd < m_NumShaders); + return m_pStaticResourceCaches[ShaderInd]; + } + bool dbgContainsShaderResources()const; SRBMemoryAllocator& GetSRBMemoryAllocator() @@ -87,7 +108,11 @@ private: // Must be defined before default SRB SRBMemoryAllocator m_SRBMemAllocator; - ShaderResourceLayoutD3D12* m_pShaderResourceLayouts; + ShaderResourceLayoutD3D12* m_pShaderResourceLayouts = nullptr; + ShaderResourceCacheD3D12* m_pStaticResourceCaches = nullptr; + ShaderVariableManagerD3D12* m_pStaticVarManagers = nullptr; + // Resource layout index in m_ResourceLayouts[] array for every shader stage + Int8 m_ResourceLayoutIndex[6] = {-1, -1, -1, -1, -1, -1}; }; } diff --git a/Graphics/GraphicsEngineD3D12/include/RootSignature.h b/Graphics/GraphicsEngineD3D12/include/RootSignature.h index ebe2083e..1d312399 100644 --- a/Graphics/GraphicsEngineD3D12/include/RootSignature.h +++ b/Graphics/GraphicsEngineD3D12/include/RootSignature.h @@ -288,9 +288,9 @@ class RootSignature public: RootSignature(); - void AllocateStaticSamplers(IShader* const *ppShaders, Uint32 NumShaders); + void AllocateStaticSamplers(const PipelineResourceLayoutDesc& ResourceLayout); - void Finalize(ID3D12Device *pd3d12Device); + void Finalize(ID3D12Device* pd3d12Device); ID3D12RootSignature* GetD3D12RootSignature()const{return m_pd3d12RootSignature;} @@ -305,6 +305,7 @@ public: void AllocateResourceSlot(SHADER_TYPE ShaderType, const D3DShaderResourceAttribs& ShaderResAttribs, + SHADER_RESOURCE_VARIABLE_TYPE VariableType, D3D12_DESCRIPTOR_RANGE_TYPE RangeType, Uint32& RootIndex, Uint32& OffsetFromTableStart); diff --git a/Graphics/GraphicsEngineD3D12/include/ShaderD3D12Impl.h b/Graphics/GraphicsEngineD3D12/include/ShaderD3D12Impl.h index 674beafb..9227287e 100644 --- a/Graphics/GraphicsEngineD3D12/include/ShaderD3D12Impl.h +++ b/Graphics/GraphicsEngineD3D12/include/ShaderD3D12Impl.h @@ -52,6 +52,16 @@ public: virtual void QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface)override final; + virtual Uint32 GetResourceCount()const override final + { + return m_pShaderResources->GetTotalResources(); + } + + virtual ShaderResourceDesc GetResource(Uint32 Index)const override final + { + return m_pShaderResources->GetShaderResourceDesc(Index); + } + ID3DBlob* GetShaderByteCode(){return m_pShaderByteCode;} const std::shared_ptr<const ShaderResourcesD3D12>& GetShaderResources()const { return m_pShaderResources; } diff --git a/Graphics/GraphicsEngineD3D12/include/ShaderResourceBindingD3D12Impl.h b/Graphics/GraphicsEngineD3D12/include/ShaderResourceBindingD3D12Impl.h index b45514da..18f03df1 100644 --- a/Graphics/GraphicsEngineD3D12/include/ShaderResourceBindingD3D12Impl.h +++ b/Graphics/GraphicsEngineD3D12/include/ShaderResourceBindingD3D12Impl.h @@ -74,9 +74,9 @@ private: ShaderResourceCacheD3D12 m_ShaderResourceCache; ShaderVariableManagerD3D12* m_pShaderVarMgrs = nullptr; // Resource layout index in m_ResourceLayouts[] array for every shader stage - Int8 m_ResourceLayoutIndex[6] = {-1, -1, -1, -1, -1, -1}; + Int8 m_ResourceLayoutIndex[6] = {-1, -1, -1, -1, -1, -1}; bool m_bStaticResourcesInitialized = false; - const Uint8 m_NumShaders = 0; + const Uint8 m_NumShaders = 0; }; } diff --git a/Graphics/GraphicsEngineD3D12/include/ShaderResourceLayoutD3D12.h b/Graphics/GraphicsEngineD3D12/include/ShaderResourceLayoutD3D12.h index 2ae5c927..26ed260e 100644 --- a/Graphics/GraphicsEngineD3D12/include/ShaderResourceLayoutD3D12.h +++ b/Graphics/GraphicsEngineD3D12/include/ShaderResourceLayoutD3D12.h @@ -108,7 +108,21 @@ namespace Diligent class ShaderResourceLayoutD3D12 final { public: - ShaderResourceLayoutD3D12(IObject& Owner); + + // There are two modes a layout can be constructed: + // - initialize static resource layout and initialize shader resource cache to hold static resources + // - initialize reference layouts that address all types of resources (static, mutable, dynamic). + // Root indices and descriptor table offsets are assigned during the initialization; + // no shader resource cache is provided + ShaderResourceLayoutD3D12(IObject& Owner, + ID3D12Device* pd3d12Device, + const PipelineResourceLayoutDesc& ResourceLayout, + std::shared_ptr<const ShaderResourcesD3D12> pSrcResources, + IMemoryAllocator& LayoutDataAllocator, + const SHADER_RESOURCE_VARIABLE_TYPE* const VarTypes, + Uint32 NumAllowedTypes, + ShaderResourceCacheD3D12* pResourceCache, + class RootSignature* pRootSig); ShaderResourceLayoutD3D12 (const ShaderResourceLayoutD3D12&) = delete; ShaderResourceLayoutD3D12 (ShaderResourceLayoutD3D12&&) = delete; @@ -117,20 +131,6 @@ public: ~ShaderResourceLayoutD3D12(); - // The method is called to - // - initialize static resource layout and initialize shader resource cache to hold static resources - // - initialize reference layouts that address all types of resources (static, mutable, dynamic). - // Root indices and descriptor table offsets are assigned during the initialization; - // no shader resource cache is provided - void Initialize(ID3D12Device* pd3d12Device, - const PipelineResourceLayoutDesc& ResourceLayout, - std::shared_ptr<const ShaderResourcesD3D12> pSrcResources, - IMemoryAllocator& LayoutDataAllocator, - const SHADER_RESOURCE_VARIABLE_TYPE* const VarTypes, - Uint32 NumAllowedTypes, - ShaderResourceCacheD3D12* pResourceCache, - class RootSignature* pRootSig); - // sizeof(D3D12Resource) == 24 (x64) struct D3D12Resource final { @@ -327,8 +327,8 @@ private: std::array<Uint16, SHADER_RESOURCE_VARIABLE_TYPE_NUM_TYPES + 1> m_CbvSrvUavOffsets = {}; std::array<Uint16, SHADER_RESOURCE_VARIABLE_TYPE_NUM_TYPES + 1> m_SamplersOffsets = {}; - CComPtr<ID3D12Device> m_pd3d12Device; IObject& m_Owner; + CComPtr<ID3D12Device> m_pd3d12Device; // We must use shared_ptr to reference ShaderResources instance, because // there may be multiple objects referencing the same set of resources std::shared_ptr<const ShaderResourcesD3D12> m_pResources; diff --git a/Graphics/GraphicsEngineD3D12/include/ShaderVariableD3D12.h b/Graphics/GraphicsEngineD3D12/include/ShaderVariableD3D12.h index 0b6d2762..85dc30c7 100644 --- a/Graphics/GraphicsEngineD3D12/include/ShaderVariableD3D12.h +++ b/Graphics/GraphicsEngineD3D12/include/ShaderVariableD3D12.h @@ -70,16 +70,14 @@ class ShaderVariableD3D12Impl; class ShaderVariableManagerD3D12 { public: - ShaderVariableManagerD3D12(IObject &Owner) : - m_Owner(Owner) - {} + ShaderVariableManagerD3D12(IObject& Owner, + const ShaderResourceLayoutD3D12& Layout, + IMemoryAllocator& Allocator, + const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, + Uint32 NumAllowedTypes, + ShaderResourceCacheD3D12& ResourceCache); ~ShaderVariableManagerD3D12(); - void Initialize(const ShaderResourceLayoutD3D12& Layout, - IMemoryAllocator& Allocator, - const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes, - ShaderResourceCacheD3D12& ResourceCache); void Destroy(IMemoryAllocator& Allocator); ShaderVariableD3D12Impl* GetVariable(const Char* Name); @@ -99,13 +97,14 @@ private: Uint32 GetVariableIndex(const ShaderVariableD3D12Impl& Variable); - IObject& m_Owner; - // Variable mgr is owned by either Shader object (in which case m_pResourceLayout points to - // static resource layout owned by the same shader object), or by SRB object (in which case + IObject& m_Owner; + + // Variable mgr is owned by either PSO object (in which case m_pResourceLayout points to + // 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 be alive while SRB is alive - const ShaderResourceLayoutD3D12* m_pResourceLayout= nullptr; - ShaderResourceCacheD3D12* m_pResourceCache = nullptr; + const ShaderResourceLayoutD3D12& m_ResourceLayout; + ShaderResourceCacheD3D12& 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 @@ -114,7 +113,7 @@ private: Uint32 m_NumVariables = 0; #ifdef _DEBUG - IMemoryAllocator* m_pDbgAllocator = nullptr; + IMemoryAllocator& m_DbgAllocator; #endif }; @@ -168,15 +167,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/GraphicsEngineD3D12/src/PipelineStateD3D12Impl.cpp b/Graphics/GraphicsEngineD3D12/src/PipelineStateD3D12Impl.cpp index 82c3f587..91835158 100644 --- a/Graphics/GraphicsEngineD3D12/src/PipelineStateD3D12Impl.cpp +++ b/Graphics/GraphicsEngineD3D12/src/PipelineStateD3D12Impl.cpp @@ -67,17 +67,87 @@ PipelineStateD3D12Impl :: PipelineStateD3D12Impl(IReferenceCounters* pRefCo m_SRBMemAllocator(GetRawAllocator()) { auto pd3d12Device = pDeviceD3D12->GetD3D12Device(); - - m_RootSig.AllocateStaticSamplers( GetShaders(), GetNumShaders() ); + const auto& ResourceLayout = m_Desc.ResourceLayout; + m_RootSig.AllocateStaticSamplers(ResourceLayout); + + { + auto& ShaderResLayoutAllocator = GetRawAllocator(); + auto* pShaderResLayoutRawMem = ALLOCATE(ShaderResLayoutAllocator, "Raw memory for ShaderResourceLayoutD3D12", sizeof(ShaderResourceLayoutD3D12) * m_NumShaders * 2); + m_pShaderResourceLayouts = reinterpret_cast<ShaderResourceLayoutD3D12*>(pShaderResLayoutRawMem); + } + + { + auto& ShaderResCacheAllocator = GetRawAllocator(); + auto* pShaderResCacheRawMem = ALLOCATE(ShaderResCacheAllocator, "Raw memory for ShaderResourceCacheD3D12", sizeof(ShaderResourceCacheD3D12) * m_NumShaders); + m_pStaticResourceCaches = reinterpret_cast<ShaderResourceCacheD3D12*>(pShaderResCacheRawMem); + } + + { + auto& ShaderVarMgrAllocator = GetRawAllocator(); + auto* pStaticVarsMgrRawMem = ALLOCATE(ShaderVarMgrAllocator, "Raw memory for ShaderVariableManagerD3D12", sizeof(ShaderVariableManagerD3D12) * m_NumShaders); + m_pStaticVarManagers = reinterpret_cast<ShaderVariableManagerD3D12*>(pStaticVarsMgrRawMem); + } + +#ifdef DEVELOPMENT + { + const ShaderResources* pResources[MaxShadersInPipeline] = {}; + for (Uint32 s = 0; s < m_NumShaders; ++s) + { + const auto* pShader = GetShader<const ShaderD3D12Impl>(s); + pResources[s] = &(*pShader->GetShaderResources()); + } + ShaderResources::DvpVerifyResourceLayout(ResourceLayout, pResources, m_NumShaders); + } +#endif - auto& ShaderResLayoutAllocator = GetRawAllocator(); - auto* pShaderResLayoutRawMem = ALLOCATE(ShaderResLayoutAllocator, "Raw memory for ShaderResourceLayoutD3D12", sizeof(ShaderResourceLayoutD3D12) * m_NumShaders); - m_pShaderResourceLayouts = reinterpret_cast<ShaderResourceLayoutD3D12*>(pShaderResLayoutRawMem); for (Uint32 s=0; s < m_NumShaders; ++s) { auto* pShaderD3D12 = GetShader<ShaderD3D12Impl>(s); - new (m_pShaderResourceLayouts+s) ShaderResourceLayoutD3D12(*this); - m_pShaderResourceLayouts[s].Initialize(pDeviceD3D12->GetD3D12Device(), pShaderD3D12->GetShaderResources(), GetRawAllocator(), nullptr, 0, nullptr, &m_RootSig); + auto ShaderType = pShaderD3D12->GetDesc().ShaderType; + auto ShaderInd = GetShaderTypeIndex(ShaderType); + m_ResourceLayoutIndex[ShaderInd] = static_cast<Int8>(s); + + new (m_pShaderResourceLayouts+s) + ShaderResourceLayoutD3D12 + { + *this, + pDeviceD3D12->GetD3D12Device(), + ResourceLayout, + pShaderD3D12->GetShaderResources(), + GetRawAllocator(), + nullptr, + 0, + nullptr, + &m_RootSig + }; + + new (m_pStaticResourceCaches+s) ShaderResourceCacheD3D12{ShaderResourceCacheD3D12::DbgCacheContentType::StaticShaderResources}; + + const SHADER_RESOURCE_VARIABLE_TYPE StaticVarType[] = {SHADER_RESOURCE_VARIABLE_TYPE_STATIC}; + new (m_pShaderResourceLayouts + m_NumShaders + s) + ShaderResourceLayoutD3D12 + { + *this, + pDeviceD3D12->GetD3D12Device(), + ResourceLayout, + pShaderD3D12->GetShaderResources(), + GetRawAllocator(), + StaticVarType, + _countof(StaticVarType), + m_pStaticResourceCaches+s, + nullptr + }; + + new (m_pStaticVarManagers+s) + ShaderVariableManagerD3D12 + { + *this, + GetStaticShaderResLayout(s), + GetRawAllocator(), + nullptr, + 0, + GetStaticShaderResCache(s) + }; } m_RootSig.Finalize(pd3d12Device); @@ -221,8 +291,14 @@ PipelineStateD3D12Impl::~PipelineStateD3D12Impl() auto& ShaderResLayoutAllocator = GetRawAllocator(); for(Uint32 s = 0; s < m_NumShaders; ++s) { + m_pStaticVarManagers[s].Destroy(GetRawAllocator()); + m_pStaticVarManagers[s].~ShaderVariableManagerD3D12(); + m_pStaticResourceCaches [s].~ShaderResourceCacheD3D12(); m_pShaderResourceLayouts[s].~ShaderResourceLayoutD3D12(); + m_pShaderResourceLayouts[m_NumShaders+s].~ShaderResourceLayoutD3D12(); } + ShaderResLayoutAllocator.Free(m_pStaticVarManagers); + ShaderResLayoutAllocator.Free(m_pStaticResourceCaches); ShaderResLayoutAllocator.Free(m_pShaderResourceLayouts); // D3D12 object can only be destroyed when it is no longer used by the GPU @@ -366,4 +442,39 @@ bool PipelineStateD3D12Impl::dbgContainsShaderResources()const return false; } +void PipelineStateD3D12Impl::BindStaticResources(IResourceMapping* pResourceMapping, Uint32 Flags) +{ + for (Uint32 s=0; s < m_NumShaders; ++s) + { + m_pStaticVarManagers[s].BindResources(pResourceMapping, Flags); + } +} + +Uint32 PipelineStateD3D12Impl::GetStaticVariableCount(SHADER_TYPE ShaderType) const +{ + const auto LayoutInd = m_ResourceLayoutIndex[GetShaderTypeIndex(ShaderType)]; + if (LayoutInd < 0) + return 0; + + return m_pStaticVarManagers[LayoutInd].GetVariableCount(); +} + +IShaderResourceVariable* PipelineStateD3D12Impl::GetStaticShaderVariable(SHADER_TYPE ShaderType, const Char* Name) +{ + const auto LayoutInd = m_ResourceLayoutIndex[GetShaderTypeIndex(ShaderType)]; + if (LayoutInd < 0) + return nullptr; + + return m_pStaticVarManagers[LayoutInd].GetVariable(Name); +} + +IShaderResourceVariable* PipelineStateD3D12Impl::GetStaticShaderVariable(SHADER_TYPE ShaderType, Uint32 Index) +{ + const auto LayoutInd = m_ResourceLayoutIndex[GetShaderTypeIndex(ShaderType)]; + if (LayoutInd < 0) + return nullptr; + + return m_pStaticVarManagers[LayoutInd].GetVariable(Index); +} + } diff --git a/Graphics/GraphicsEngineD3D12/src/RenderDeviceD3D12Impl.cpp b/Graphics/GraphicsEngineD3D12/src/RenderDeviceD3D12Impl.cpp index 202b729a..4640daad 100644 --- a/Graphics/GraphicsEngineD3D12/src/RenderDeviceD3D12Impl.cpp +++ b/Graphics/GraphicsEngineD3D12/src/RenderDeviceD3D12Impl.cpp @@ -318,12 +318,12 @@ void RenderDeviceD3D12Impl :: CreateBuffer(const BufferDesc& BuffDesc, const Buf } -void RenderDeviceD3D12Impl :: CreateShader(const ShaderCreationAttribs& ShaderCreationAttribs, IShader** ppShader) +void RenderDeviceD3D12Impl :: CreateShader(const ShaderCreateInfo& ShaderCI, IShader** ppShader) { - CreateDeviceObject( "shader", ShaderCreationAttribs.Desc, ppShader, + CreateDeviceObject( "shader", ShaderCI.Desc, ppShader, [&]() { - ShaderD3D12Impl *pShaderD3D12( NEW_RC_OBJ(m_ShaderObjAllocator, "ShaderD3D12Impl instance", ShaderD3D12Impl)(this, ShaderCreationAttribs ) ); + ShaderD3D12Impl *pShaderD3D12( NEW_RC_OBJ(m_ShaderObjAllocator, "ShaderD3D12Impl instance", ShaderD3D12Impl)(this, ShaderCI ) ); pShaderD3D12->QueryInterface( IID_Shader, reinterpret_cast<IObject**>(ppShader) ); OnCreateDeviceObject( pShaderD3D12 ); diff --git a/Graphics/GraphicsEngineD3D12/src/RootSignature.cpp b/Graphics/GraphicsEngineD3D12/src/RootSignature.cpp index 64da64c9..b5809eb5 100644 --- a/Graphics/GraphicsEngineD3D12/src/RootSignature.cpp +++ b/Graphics/GraphicsEngineD3D12/src/RootSignature.cpp @@ -267,8 +267,8 @@ void RootSignature::InitStaticSampler(SHADER_TYPE ShaderType StreqSuff(SamplerName, StSmplr.SamplerDesc.SamplerOrTextureName, SamplerSuffix)) { StSmplr.ShaderRegister = SamplerAttribs.BindPoint; - StSmplr.ArraySize = SamplerAttribs.BindCount; - StSmplr.RegisterSpace = 0; + StSmplr.ArraySize = SamplerAttribs.BindCount; + StSmplr.RegisterSpace = 0; SamplerFound = true; break; } @@ -283,6 +283,7 @@ void RootSignature::InitStaticSampler(SHADER_TYPE ShaderType // http://diligentgraphics.com/diligent-engine/architecture/d3d12/shader-resource-layout#Initializing-Shader-Resource-Layouts-and-Root-Signature-in-a-Pipeline-State-Object void RootSignature::AllocateResourceSlot(SHADER_TYPE ShaderType, const D3DShaderResourceAttribs& ShaderResAttribs, + SHADER_RESOURCE_VARIABLE_TYPE VariableType, D3D12_DESCRIPTOR_RANGE_TYPE RangeType, Uint32& RootIndex, // Output parameter Uint32& OffsetFromTableStart // Output parameter @@ -299,12 +300,12 @@ void RootSignature::AllocateResourceSlot(SHADER_TYPE ShaderT OffsetFromTableStart = 0; // Add new root view to existing root parameters - m_RootParams.AddRootView(D3D12_ROOT_PARAMETER_TYPE_CBV, RootIndex, ShaderResAttribs.BindPoint, ShaderVisibility, ShaderResAttribs.GetVariableType()); + m_RootParams.AddRootView(D3D12_ROOT_PARAMETER_TYPE_CBV, RootIndex, ShaderResAttribs.BindPoint, ShaderVisibility, VariableType); } else { // Use the same table for static and mutable resources. Treat both as static - auto RootTableType = (ShaderResAttribs.GetVariableType() == SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC) ? SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC : SHADER_RESOURCE_VARIABLE_TYPE_STATIC; + auto RootTableType = (VariableType == SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC) ? SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC : SHADER_RESOURCE_VARIABLE_TYPE_STATIC; auto TableIndKey = ShaderInd * SHADER_RESOURCE_VARIABLE_TYPE_NUM_TYPES + RootTableType; // Get the table array index (this is not the root index!) auto& RootTableArrayInd = (( RangeType == D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER ) ? m_SamplerRootTablesMap : m_SrvCbvUavRootTablesMap)[ TableIndKey ]; @@ -411,23 +412,22 @@ void RootSignature::dbgVerifyRootParameters()const } #endif -void RootSignature::AllocateStaticSamplers(IShader* const* ppShaders, Uint32 NumShaders) +void RootSignature::AllocateStaticSamplers(const PipelineResourceLayoutDesc& ResourceLayout) { - Uint32 TotalSamplers = 0; - for(Uint32 s=0;s < NumShaders; ++s) - TotalSamplers += ppShaders[s]->GetDesc().NumStaticSamplers; - if (TotalSamplers > 0) + if (ResourceLayout.NumStaticSamplers > 0) { - m_StaticSamplers.reserve(TotalSamplers); - for(Uint32 sh=0;sh < NumShaders; ++sh) + m_StaticSamplers.reserve(ResourceLayout.NumStaticSamplers); + for(Uint32 sam=0; sam < ResourceLayout.NumStaticSamplers; ++sam) { - const auto& Desc = ppShaders[sh]->GetDesc(); - for(Uint32 sam=0; sam < Desc.NumStaticSamplers; ++sam) + const auto& StSamDesc = ResourceLayout.StaticSamplers[sam]; + Uint32 ShaderStages = StSamDesc.ShaderStages; + while (ShaderStages != 0) { - m_StaticSamplers.emplace_back(Desc.StaticSamplers[sam], GetShaderVisibility(Desc.ShaderType)); + auto Stage = ShaderStages & ~(ShaderStages-1); + m_StaticSamplers.emplace_back(StSamDesc, GetShaderVisibility(static_cast<SHADER_TYPE>(Stage))); + ShaderStages &= ~Stage; } } - VERIFY_EXPR(m_StaticSamplers.size() == TotalSamplers); } } diff --git a/Graphics/GraphicsEngineD3D12/src/ShaderResourceBindingD3D12Impl.cpp b/Graphics/GraphicsEngineD3D12/src/ShaderResourceBindingD3D12Impl.cpp index b8d46dbb..644eb1ff 100644 --- a/Graphics/GraphicsEngineD3D12/src/ShaderResourceBindingD3D12Impl.cpp +++ b/Graphics/GraphicsEngineD3D12/src/ShaderResourceBindingD3D12Impl.cpp @@ -52,15 +52,22 @@ ShaderResourceBindingD3D12Impl::ShaderResourceBindingD3D12Impl(IReferenceCounter auto ShaderType = pShader->GetDesc().ShaderType; auto ShaderInd = GetShaderTypeIndex(ShaderType); - // Create shader variable manager in place - new (m_pShaderVarMgrs + s) ShaderVariableManagerD3D12(*this); - auto& VarDataAllocator = pPSO->GetSRBMemoryAllocator().GetShaderVariableDataAllocator(s); // http://diligentgraphics.com/diligent-engine/architecture/d3d12/shader-resource-layout#Initializing-Resource-Layouts-in-a-Shader-Resource-Binding-Object - std::array<SHADER_RESOURCE_VARIABLE_TYPE, 2> AllowedVarTypes = { SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC }; + const SHADER_RESOURCE_VARIABLE_TYPE AllowedVarTypes[] = { SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC }; const auto& SrcLayout = pPSO->GetShaderResLayout(s); - m_pShaderVarMgrs[s].Initialize(SrcLayout, VarDataAllocator, AllowedVarTypes.data(), static_cast<Uint32>(AllowedVarTypes.size()), m_ShaderResourceCache); + // Create shader variable manager in place + new (m_pShaderVarMgrs + s) + ShaderVariableManagerD3D12 + { + *this, + SrcLayout, + VarDataAllocator, + AllowedVarTypes, + _countof(AllowedVarTypes), + m_ShaderResourceCache + }; m_ResourceLayoutIndex[ShaderInd] = static_cast<Int8>(s); } @@ -173,23 +180,22 @@ void ShaderResourceBindingD3D12Impl::InitializeStaticResources(const IPipelineSt auto* pPSO12 = ValidatedCast<const PipelineStateD3D12Impl>(pPSO); auto NumShaders = pPSO12->GetNumShaders(); - auto ppShaders = pPSO12->GetShaders(); // Copy static resources for (Uint32 s = 0; s < NumShaders; ++s) { - auto* pShader = ValidatedCast<ShaderD3D12Impl>( ppShaders[s] ); + const auto& ShaderResLayout = pPSO12->GetShaderResLayout(s); + auto& StaticResLayout = pPSO12->GetStaticShaderResLayout(s); + auto& StaticResCache = pPSO12->GetStaticShaderResCache(s); #ifdef DEVELOPMENT - if (!pShader->DvpVerifyStaticResourceBindings()) + if (!StaticResLayout.dvpVerifyBindings(StaticResCache)) { + auto* pShader = pPSO12->GetShader<ShaderD3D12Impl>(s); LOG_ERROR_MESSAGE("Static resources in SRB of PSO '", pPSO12->GetDesc().Name, "' will not be successfully initialized " "because not all static resource bindings in shader '", pShader->GetDesc().Name, "' are valid. " "Please make sure you bind all static resources to the shader before calling InitializeStaticResources() " "directly or indirectly by passing InitStaticResources=true to CreateShaderResourceBinding() method."); } #endif - const auto& ShaderResLayout = pPSO12->GetShaderResLayout(s); - auto& StaticResLayout = pShader->GetStaticResLayout(); - auto& StaticResCache = pShader->GetStaticResCache(); StaticResLayout.CopyStaticResourceDesriptorHandles(StaticResCache, ShaderResLayout, m_ShaderResourceCache); } diff --git a/Graphics/GraphicsEngineD3D12/src/ShaderResourceLayoutD3D12.cpp b/Graphics/GraphicsEngineD3D12/src/ShaderResourceLayoutD3D12.cpp index ca133acc..de7458b6 100644 --- a/Graphics/GraphicsEngineD3D12/src/ShaderResourceLayoutD3D12.cpp +++ b/Graphics/GraphicsEngineD3D12/src/ShaderResourceLayoutD3D12.cpp @@ -38,11 +38,6 @@ namespace Diligent { -ShaderResourceLayoutD3D12::ShaderResourceLayoutD3D12(IObject& Owner) : - m_Owner(Owner) -{ -} - ShaderResourceLayoutD3D12::~ShaderResourceLayoutD3D12() { for(Uint32 r=0; r < GetTotalResourceCount(); ++r) @@ -111,18 +106,19 @@ void ShaderResourceLayoutD3D12::AllocateMemory(IMemoryAllocator& // http://diligentgraphics.com/diligent-engine/architecture/d3d12/shader-resource-layout#Initializing-Shader-Resource-Layouts-and-Root-Signature-in-a-Pipeline-State-Object // http://diligentgraphics.com/diligent-engine/architecture/d3d12/shader-resource-cache#Initializing-Shader-Resource-Layouts-in-a-Pipeline-State -void ShaderResourceLayoutD3D12::Initialize(ID3D12Device* pd3d12Device, - const PipelineResourceLayoutDesc& ResourceLayout, - std::shared_ptr<const ShaderResourcesD3D12> pSrcResources, - IMemoryAllocator& LayoutDataAllocator, - const SHADER_RESOURCE_VARIABLE_TYPE* const AllowedVarTypes, - Uint32 NumAllowedTypes, - ShaderResourceCacheD3D12* pResourceCache, - RootSignature* pRootSig) +ShaderResourceLayoutD3D12::ShaderResourceLayoutD3D12(IObject& Owner, + ID3D12Device* pd3d12Device, + const PipelineResourceLayoutDesc& ResourceLayout, + std::shared_ptr<const ShaderResourcesD3D12> pSrcResources, + IMemoryAllocator& LayoutDataAllocator, + const SHADER_RESOURCE_VARIABLE_TYPE* const AllowedVarTypes, + Uint32 NumAllowedTypes, + ShaderResourceCacheD3D12* pResourceCache, + RootSignature* pRootSig) : + m_Owner (Owner), + m_pd3d12Device (pd3d12Device), + m_pResources (std::move(pSrcResources)) { - m_pResources = std::move(pSrcResources); - m_pd3d12Device = pd3d12Device; - VERIFY_EXPR( (pResourceCache != nullptr) ^ (pRootSig != nullptr) ); const Uint32 AllowedTypeBits = GetAllowedTypeBits(AllowedVarTypes, NumAllowedTypes); @@ -155,10 +151,9 @@ void ShaderResourceLayoutD3D12::Initialize(ID3D12Device* if (IsAllowedType(VarType, AllowedTypeBits)) { ++CbvSrvUavCount[VarType]; - if (TexSRV.ValidSamplerAssigned()) + if (TexSRV.IsCombinedWithSampler()) { - auto SamplerId = TexSRV.GetSamplerId(); - const auto& SamplerAttribs = m_pResources->GetSampler(SamplerId); + const auto& SamplerAttribs = m_pResources->GetCombinedSampler(TexSRV); auto SamplerVarType = m_pResources->FindVariableType(SamplerAttribs, ResourceLayout); DEV_CHECK_ERR(SamplerVarType == VarType, "The type (", GetShaderVariableTypeLiteralName(VarType),") of texture SRV variable '", TexSRV.Name, @@ -203,7 +198,7 @@ void ShaderResourceLayoutD3D12::Initialize(ID3D12Device* D3D12_DESCRIPTOR_RANGE_TYPE DescriptorRangeType = GetDescriptorRangeType(ResType); if (pRootSig) { - pRootSig->AllocateResourceSlot(m_pResources->GetShaderType(), Attribs, DescriptorRangeType, RootIndex, Offset ); + pRootSig->AllocateResourceSlot(m_pResources->GetShaderType(), Attribs, VarType, DescriptorRangeType, RootIndex, Offset ); VERIFY(RootIndex <= D3D12Resource::MaxRootIndex, "Root index excceeds allowed limit"); } else @@ -268,9 +263,9 @@ void ShaderResourceLayoutD3D12::Initialize(ID3D12Device* VERIFY(CurrSampler[SHADER_RESOURCE_VARIABLE_TYPE_STATIC] + CurrSampler[SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE] + CurrSampler[SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC] == GetTotalSamplerCount(), "All samplers must be initialized before texture SRVs"); Uint32 SamplerId = D3D12Resource::InvalidSamplerId; - if (TexSRV.ValidSamplerAssigned()) + if (TexSRV.IsCombinedWithSampler()) { - const auto& SamplerAttribs = m_pResources->GetSampler(TexSRV.GetSamplerId()); + const auto& SamplerAttribs = m_pResources->GetCombinedSampler(TexSRV); auto SamplerVarType = m_pResources->FindVariableType(SamplerAttribs, ResourceLayout); DEV_CHECK_ERR(SamplerVarType == VarType, "The type (", GetShaderVariableTypeLiteralName(VarType),") of texture SRV variable '", TexSRV.Name, @@ -283,9 +278,9 @@ void ShaderResourceLayoutD3D12::Initialize(ID3D12Device* // Static samplers are never copied, and SamplerId == InvalidSamplerId #ifdef _DEBUG auto SamplerCount = GetTotalSamplerCount(); - for (SamplerId = 0; SamplerId < SamplerCount; ++SamplerId) + for (Uint32 s = 0; s < SamplerCount; ++s) { - const auto& Sampler = GetSampler(SamplerId); + const auto& Sampler = GetSampler(s); if (strcmp(Sampler.Attribs.Name, SamplerAttribs.Name) == 0) LOG_ERROR("Static sampler '", Sampler.Attribs.Name, "' was found among resources. This seems to be a bug"); } @@ -294,14 +289,16 @@ void ShaderResourceLayoutD3D12::Initialize(ID3D12Device* else { auto SamplerCount = GetTotalSamplerCount(); + bool SamplerFound = false; for (SamplerId = 0; SamplerId < SamplerCount; ++SamplerId) { const auto& Sampler = GetSampler(SamplerId); - if (strcmp(Sampler.Attribs.Name, SamplerAttribs.Name) == 0) + SamplerFound = strcmp(Sampler.Attribs.Name, SamplerAttribs.Name) == 0; + if (SamplerFound) break; } - if (SamplerId == SamplerCount) + if (!SamplerFound) { LOG_ERROR("Unable to find sampler '", SamplerAttribs.Name, "' assigned to texture SRV '", TexSRV.Name, "' in the list of already created resources. This seems to be a bug."); SamplerId = D3D12Resource::InvalidSamplerId; diff --git a/Graphics/GraphicsEngineD3D12/src/ShaderVariableD3D12.cpp b/Graphics/GraphicsEngineD3D12/src/ShaderVariableD3D12.cpp index 877a270d..3c14c461 100644 --- a/Graphics/GraphicsEngineD3D12/src/ShaderVariableD3D12.cpp +++ b/Graphics/GraphicsEngineD3D12/src/ShaderVariableD3D12.cpp @@ -50,18 +50,20 @@ size_t ShaderVariableManagerD3D12::GetRequiredMemorySize(const ShaderResourceLay } // Creates shader variable for every resource from SrcLayout whose type is one AllowedVarTypes -void ShaderVariableManagerD3D12::Initialize(const ShaderResourceLayoutD3D12& SrcLayout, - IMemoryAllocator& Allocator, - const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes, - ShaderResourceCacheD3D12& ResourceCache) -{ - m_pResourceLayout = &SrcLayout; - m_pResourceCache = &ResourceCache; +ShaderVariableManagerD3D12::ShaderVariableManagerD3D12(IObject& Owner, + const ShaderResourceLayoutD3D12& SrcLayout, + IMemoryAllocator& Allocator, + const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, + Uint32 NumAllowedTypes, + ShaderResourceCacheD3D12& 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); @@ -107,7 +109,7 @@ ShaderVariableManagerD3D12::~ShaderVariableManagerD3D12() void ShaderVariableManagerD3D12::Destroy(IMemoryAllocator &Allocator) { - VERIFY(m_pDbgAllocator == &Allocator, "Incosistent alloctor"); + VERIFY(&m_DbgAllocator == &Allocator, "Incosistent alloctor"); if(m_pVariables != nullptr) { @@ -167,7 +169,6 @@ Uint32 ShaderVariableManagerD3D12::GetVariableIndex(const ShaderVariableD3D12Imp void ShaderVariableManagerD3D12::BindResources( IResourceMapping* pResourceMapping, Uint32 Flags) { - VERIFY_EXPR(m_pResourceCache != nullptr); DEV_CHECK_ERR(pResourceMapping != nullptr, "Failed to bind resources: resource mapping is null"); if ( (Flags & BIND_SHADER_RESOURCES_UPDATE_ALL) == 0 ) @@ -183,7 +184,7 @@ void ShaderVariableManagerD3D12::BindResources( IResourceMapping* pResourceMappi for (Uint32 ArrInd = 0; ArrInd < Res.Attribs.BindCount; ++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; RefCntAutoPtr<IDeviceObject> pObj; @@ -192,11 +193,11 @@ void ShaderVariableManagerD3D12::BindResources( IResourceMapping* pResourceMappi if ( pObj ) { // Call non-virtual function - 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.Attribs.GetPrintName(ArrInd), "': resource is not found in the resource mapping" ); } } |
