From 2250c36e3570a949104a3a49b02546a3dd8f1917 Mon Sep 17 00:00:00 2001 From: assiduous Date: Wed, 7 Oct 2020 14:57:33 -0700 Subject: Updated PipelineState[D3D11,D3D12,Vk]Impl to allocate single chunk of memory for resource layout, resource cache and var managers objects --- .../GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index 39a6ca4a..fb93f90d 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -132,8 +132,16 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR UNEXPECTED(GetPipelineTypeString(m_Desc.PipelineType), " pipelines are not supported by Direct3D11 backend"); } - m_pStaticResourceLayouts = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11", ShaderResourceLayoutD3D11, m_NumShaders); - m_pStaticResourceCaches = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceCacheD3D11", ShaderResourceCacheD3D11, m_NumShaders); + // clang-format off + static_assert((sizeof(ShaderResourceLayoutD3D11) % sizeof(void*)) == 0, "sizeof(ShaderResourceLayoutD3D11) is expected to be a multiple of sizeof(void*)"); + static_assert((sizeof(ShaderResourceCacheD3D11) % sizeof(void*)) == 0, "sizeof(ShaderResourceCacheD3D11) is expected to be a multiple of sizeof(void*)"); + // clang-format on + const auto MemSize = (sizeof(ShaderResourceLayoutD3D11) + sizeof(ShaderResourceCacheD3D11)) * m_NumShaders; + auto* const pRawMem = + ALLOCATE_RAW(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11 and ShaderResourceCacheD3D11 arrays", MemSize); + + m_pStaticResourceLayouts = reinterpret_cast(pRawMem); + m_pStaticResourceCaches = reinterpret_cast(m_pStaticResourceLayouts + m_NumShaders); const auto& ResourceLayout = m_Desc.ResourceLayout; @@ -236,13 +244,14 @@ PipelineStateD3D11Impl::~PipelineStateD3D11Impl() m_pStaticResourceCaches[s].Destroy(GetRawAllocator()); m_pStaticResourceCaches[s].~ShaderResourceCacheD3D11(); } - GetRawAllocator().Free(m_pStaticResourceCaches); for (Uint32 l = 0; l < m_NumShaders; ++l) { m_pStaticResourceLayouts[l].~ShaderResourceLayoutD3D11(); } - GetRawAllocator().Free(m_pStaticResourceLayouts); + // m_pStaticResourceLayouts and m_pStaticResourceCaches are allocated in contiguous chunks of memory. + auto* pRawMem = m_pStaticResourceLayouts; + GetRawAllocator().Free(pRawMem); } IMPLEMENT_QUERY_INTERFACE(PipelineStateD3D11Impl, IID_PipelineStateD3D11, TPipelineStateBase) -- cgit v1.2.3 From 2b396d236ab33dfe9c0defbe401d354ed3fb34f9 Mon Sep 17 00:00:00 2001 From: azhirnov Date: Thu, 8 Oct 2020 21:45:01 +0300 Subject: removed strong references to shaders in PSO --- .../include/PipelineStateD3D11Impl.hpp | 15 ++++- .../src/DeviceContextD3D11Impl.cpp | 10 ++-- .../src/PipelineStateD3D11Impl.cpp | 64 +++++++++++++++------- .../src/ShaderResourceBindingD3D11Impl.cpp | 9 ++- 4 files changed, 66 insertions(+), 32 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp index 02f922a6..7acb15f0 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp @@ -36,6 +36,7 @@ #include "ShaderResourceLayoutD3D11.hpp" #include "SRBMemoryAllocator.hpp" #include "RenderDeviceD3D11Impl.hpp" +#include "ShaderD3D11Impl.hpp" namespace Diligent { @@ -117,16 +118,19 @@ public: const ShaderResourceLayoutD3D11& GetStaticResourceLayout(Uint32 s) const { - VERIFY_EXPR(s < m_NumShaders); + VERIFY_EXPR(s < GetNumShaderTypes()); return m_pStaticResourceLayouts[s]; } ShaderResourceCacheD3D11& GetStaticResourceCache(Uint32 s) { - VERIFY_EXPR(s < m_NumShaders); + VERIFY_EXPR(s < GetNumShaderTypes()); return m_pStaticResourceCaches[s]; } + const ShaderD3D11Impl* GetShaderByType(SHADER_TYPE ShaderType) const; + const ShaderD3D11Impl* GetShader(Uint32 Index) const; + void SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache, Uint32 ShaderInd) const; private: @@ -135,6 +139,13 @@ private: CComPtr m_pd3d11DepthStencilState; CComPtr m_pd3d11InputLayout; + RefCntAutoPtr m_pVS; + RefCntAutoPtr m_pPS; + RefCntAutoPtr m_pGS; + RefCntAutoPtr m_pDS; + RefCntAutoPtr m_pHS; + RefCntAutoPtr m_pCS; + // The caches are indexed by the shader order in the PSO, not shader index ShaderResourceCacheD3D11* m_pStaticResourceCaches = nullptr; ShaderResourceLayoutD3D11* m_pStaticResourceLayouts = nullptr; diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index 6af8b502..cd942aed 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -179,9 +179,9 @@ void DeviceContextD3D11Impl::TransitionAndCommitShaderResources(IPipelineState* { #ifdef DILIGENT_DEVELOPMENT bool ResourcesPresent = false; - for (Uint32 s = 0; s < pPipelineStateD3D11->GetNumShaders(); ++s) + for (Uint32 s = 0; s < pPipelineStateD3D11->GetNumShaderTypes(); ++s) { - auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); + auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); if (pShaderD3D11->GetD3D11Resources()->GetTotalResources() > 0) ResourcesPresent = true; } @@ -206,7 +206,7 @@ void DeviceContextD3D11Impl::TransitionAndCommitShaderResources(IPipelineState* #endif auto NumShaders = pShaderResBindingD3D11->GetNumActiveShaders(); - VERIFY(NumShaders == pPipelineStateD3D11->GetNumShaders(), "Number of active shaders in shader resource binding is not consistent with the number of shaders in the pipeline state"); + VERIFY(NumShaders == pPipelineStateD3D11->GetNumShaderTypes(), "Number of active shaders in shader resource binding is not consistent with the number of shaders in the pipeline state"); #ifdef DILIGENT_DEVELOPMENT { @@ -233,7 +233,7 @@ void DeviceContextD3D11Impl::TransitionAndCommitShaderResources(IPipelineState* const auto ShaderTypeInd = GetShaderTypeIndex(ShaderType); #ifdef DILIGENT_DEVELOPMENT - auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); + auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); VERIFY_EXPR(ShaderType == pShaderD3D11->GetDesc().ShaderType); #endif @@ -380,7 +380,7 @@ void DeviceContextD3D11Impl::TransitionAndCommitShaderResources(IPipelineState* const auto ShaderTypeInd = GetShaderTypeIndex(ShaderType); #ifdef DILIGENT_DEVELOPMENT - auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); + auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); VERIFY_EXPR(ShaderType == pShaderD3D11->GetDesc().ShaderType); #endif diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index fb93f90d..0e312740 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -31,7 +31,6 @@ #include "RenderDeviceD3D11Impl.hpp" #include "ShaderResourceBindingD3D11Impl.hpp" #include "EngineMemory.h" -#include "ShaderD3D11Impl.hpp" namespace Diligent { @@ -136,24 +135,24 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR static_assert((sizeof(ShaderResourceLayoutD3D11) % sizeof(void*)) == 0, "sizeof(ShaderResourceLayoutD3D11) is expected to be a multiple of sizeof(void*)"); static_assert((sizeof(ShaderResourceCacheD3D11) % sizeof(void*)) == 0, "sizeof(ShaderResourceCacheD3D11) is expected to be a multiple of sizeof(void*)"); // clang-format on - const auto MemSize = (sizeof(ShaderResourceLayoutD3D11) + sizeof(ShaderResourceCacheD3D11)) * m_NumShaders; + const auto MemSize = (sizeof(ShaderResourceLayoutD3D11) + sizeof(ShaderResourceCacheD3D11)) * GetNumShaderTypes(); auto* const pRawMem = ALLOCATE_RAW(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11 and ShaderResourceCacheD3D11 arrays", MemSize); m_pStaticResourceLayouts = reinterpret_cast(pRawMem); - m_pStaticResourceCaches = reinterpret_cast(m_pStaticResourceLayouts + m_NumShaders); + m_pStaticResourceCaches = reinterpret_cast(m_pStaticResourceLayouts + GetNumShaderTypes()); const auto& ResourceLayout = m_Desc.ResourceLayout; #ifdef DILIGENT_DEVELOPMENT { const ShaderResources* pResources[MAX_SHADERS_IN_PIPELINE] = {}; - for (Uint32 s = 0; s < m_NumShaders; ++s) + for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) { - auto* pShader = GetShader(s); + auto* pShader = GetShader(s); pResources[s] = &(*pShader->GetD3D11Resources()); } - ShaderResources::DvpVerifyResourceLayout(ResourceLayout, pResources, m_NumShaders, + ShaderResources::DvpVerifyResourceLayout(ResourceLayout, pResources, GetNumShaderTypes(), (CreateInfo.Flags & PSO_CREATE_FLAG_IGNORE_MISSING_VARIABLES) == 0, (CreateInfo.Flags & PSO_CREATE_FLAG_IGNORE_MISSING_STATIC_SAMPLERS) == 0); } @@ -162,9 +161,9 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR decltype(m_StaticSamplers) StaticSamplers(STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")); std::array ShaderResLayoutDataSizes = {}; std::array ShaderResCacheDataSizes = {}; - for (Uint32 s = 0; s < m_NumShaders; ++s) + for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) { - const auto* pShader = GetShader(s); + const auto* pShader = GetShader(s); const auto& ShaderDesc = pShader->GetDesc(); const auto& ShaderResources = *pShader->GetD3D11Resources(); VERIFY_EXPR(ShaderDesc.ShaderType == ShaderResources.GetShaderType()); @@ -222,14 +221,14 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR if (m_Desc.SRBAllocationGranularity > 1) { - m_SRBMemAllocator.Initialize(m_Desc.SRBAllocationGranularity, m_NumShaders, ShaderResLayoutDataSizes.data(), m_NumShaders, ShaderResCacheDataSizes.data()); + m_SRBMemAllocator.Initialize(m_Desc.SRBAllocationGranularity, GetNumShaderTypes(), ShaderResLayoutDataSizes.data(), GetNumShaderTypes(), ShaderResCacheDataSizes.data()); } m_StaticSamplers.reserve(StaticSamplers.size()); for (auto& Sam : StaticSamplers) m_StaticSamplers.emplace_back(std::move(Sam)); - for (Uint32 s = 0; s < m_NumShaders; ++s) + for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) { // Initialize static samplers in the static resource cache to avoid warning messages SetStaticSamplers(m_pStaticResourceCaches[s], s); @@ -239,13 +238,13 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR PipelineStateD3D11Impl::~PipelineStateD3D11Impl() { - for (Uint32 s = 0; s < m_NumShaders; ++s) + for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) { m_pStaticResourceCaches[s].Destroy(GetRawAllocator()); m_pStaticResourceCaches[s].~ShaderResourceCacheD3D11(); } - for (Uint32 l = 0; l < m_NumShaders; ++l) + for (Uint32 l = 0; l < GetNumShaderTypes(); ++l) { m_pStaticResourceLayouts[l].~ShaderResourceLayoutD3D11(); } @@ -298,13 +297,13 @@ bool PipelineStateD3D11Impl::IsCompatibleWith(const IPipelineState* pPSO) const if (m_ShaderResourceLayoutHash != pPSOD3D11->m_ShaderResourceLayoutHash) return false; - if (m_NumShaders != pPSOD3D11->m_NumShaders) + if (GetNumShaderTypes() != pPSOD3D11->GetNumShaderTypes()) return false; - for (Uint32 s = 0; s < m_NumShaders; ++s) + for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) { - auto* pShader0 = GetShader(s); - auto* pShader1 = pPSOD3D11->GetShader(s); + auto* pShader0 = GetShader(s); + auto* pShader1 = pPSOD3D11->GetShader(s); if (pShader0->GetDesc().ShaderType != pShader1->GetDesc().ShaderType) return false; const auto& Res0 = *pShader0->GetD3D11Resources(); @@ -361,7 +360,7 @@ ID3D11ComputeShader* PipelineStateD3D11Impl::GetD3D11ComputeShader() void PipelineStateD3D11Impl::BindStaticResources(Uint32 ShaderFlags, IResourceMapping* pResourceMapping, Uint32 Flags) { - for (Uint32 s = 0; s < m_NumShaders; ++s) + for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) { auto& StaticResLayout = m_pStaticResourceLayouts[s]; if ((ShaderFlags & StaticResLayout.GetShaderType()) != 0) @@ -375,7 +374,7 @@ Uint32 PipelineStateD3D11Impl::GetStaticVariableCount(SHADER_TYPE ShaderType) co if (LayoutInd < 0) return 0; - VERIFY_EXPR(static_cast(LayoutInd) <= m_NumShaders); + VERIFY_EXPR(static_cast(LayoutInd) <= GetNumShaderTypes()); return m_pStaticResourceLayouts[LayoutInd].GetTotalResourceCount(); } @@ -385,7 +384,7 @@ IShaderResourceVariable* PipelineStateD3D11Impl::GetStaticVariableByName(SHADER_ if (LayoutInd < 0) return nullptr; - VERIFY_EXPR(static_cast(LayoutInd) <= m_NumShaders); + VERIFY_EXPR(static_cast(LayoutInd) <= GetNumShaderTypes()); return m_pStaticResourceLayouts[LayoutInd].GetShaderVariable(Name); } @@ -395,7 +394,7 @@ IShaderResourceVariable* PipelineStateD3D11Impl::GetStaticVariableByIndex(SHADER if (LayoutInd < 0) return nullptr; - VERIFY_EXPR(static_cast(LayoutInd) <= m_NumShaders); + VERIFY_EXPR(static_cast(LayoutInd) <= GetNumShaderTypes()); return m_pStaticResourceLayouts[LayoutInd].GetShaderVariable(Index); } @@ -414,4 +413,29 @@ void PipelineStateD3D11Impl::SetStaticSamplers(ShaderResourceCacheD3D11& Resourc } } +const ShaderD3D11Impl* PipelineStateD3D11Impl::GetShaderByType(SHADER_TYPE ShaderType) const +{ + switch (ShaderType) + { + // clang-format off + case SHADER_TYPE_VERTEX: return m_pVS; + case SHADER_TYPE_PIXEL: return m_pPS; + case SHADER_TYPE_GEOMETRY: return m_pGS; + case SHADER_TYPE_HULL: return m_pHS; + case SHADER_TYPE_DOMAIN: return m_pDS; + case SHADER_TYPE_COMPUTE: return m_pCS; + default: UNEXPECTED("unsupported shader type"); return nullptr; + // clang-format on + } +} + +const ShaderD3D11Impl* PipelineStateD3D11Impl::GetShader(Uint32 Index) const +{ + if (Index < GetNumShaderTypes()) + return GetShaderByType(GetShaderTypes()[Index]); + + UNEXPECTED("Shader index is out of range"); + return nullptr; +} + } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp index 62fd2df7..17083bbc 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp @@ -50,7 +50,7 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl(IReferenceCounter // clang-format on { m_ResourceLayoutIndex.fill(-1); - m_NumActiveShaders = static_cast(pPSO->GetNumShaders()); + m_NumActiveShaders = static_cast(pPSO->GetNumShaderTypes()); // clang-format off m_pResourceLayouts = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11", ShaderResourceLayoutD3D11, m_NumActiveShaders); @@ -62,7 +62,7 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl(IReferenceCounter // Reserve memory for resource layouts for (Uint8 s = 0; s < m_NumActiveShaders; ++s) { - auto* pShaderD3D11 = pPSO->GetShader(s); + auto* pShaderD3D11 = pPSO->GetShader(s); auto& SRBMemAllocator = pPSO->GetSRBMemoryAllocator(); auto& ResCacheDataAllocator = SRBMemAllocator.GetResourceCacheDataAllocator(s); @@ -151,14 +151,13 @@ void ShaderResourceBindingD3D11Impl::InitializeStaticResources(const IPipelineSt } const auto* pPSOD3D11 = ValidatedCast(pPipelineState); - auto ppShaders = pPSOD3D11->GetShaders(); - auto NumShaders = pPSOD3D11->GetNumShaders(); + auto NumShaders = pPSOD3D11->GetNumShaderTypes(); VERIFY_EXPR(NumShaders == m_NumActiveShaders); for (Uint32 shader = 0; shader < NumShaders; ++shader) { const auto& StaticResLayout = pPSOD3D11->GetStaticResourceLayout(shader); - auto* pShaderD3D11 = ValidatedCast(ppShaders[shader]); + auto* pShaderD3D11 = pPSOD3D11->GetShader(shader); #ifdef DILIGENT_DEVELOPMENT if (!StaticResLayout.dvpVerifyBindings()) { -- cgit v1.2.3 From 91aac63f651da1079be93d2fe722cd10e4e15570 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sat, 10 Oct 2020 19:28:54 -0700 Subject: A number of corrections for PSO refactoring --- .../include/PipelineStateD3D11Impl.hpp | 4 +- .../src/DeviceContextD3D11Impl.cpp | 4 +- .../src/PipelineStateD3D11Impl.cpp | 45 +++++++++++++--------- .../src/ShaderResourceBindingD3D11Impl.cpp | 4 +- 4 files changed, 32 insertions(+), 25 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp index 7acb15f0..1d836ab3 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp @@ -118,13 +118,13 @@ public: const ShaderResourceLayoutD3D11& GetStaticResourceLayout(Uint32 s) const { - VERIFY_EXPR(s < GetNumShaderTypes()); + VERIFY_EXPR(s < GetNumShaderStages()); return m_pStaticResourceLayouts[s]; } ShaderResourceCacheD3D11& GetStaticResourceCache(Uint32 s) { - VERIFY_EXPR(s < GetNumShaderTypes()); + VERIFY_EXPR(s < GetNumShaderStages()); return m_pStaticResourceCaches[s]; } diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index cd942aed..75ce9094 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -179,7 +179,7 @@ void DeviceContextD3D11Impl::TransitionAndCommitShaderResources(IPipelineState* { #ifdef DILIGENT_DEVELOPMENT bool ResourcesPresent = false; - for (Uint32 s = 0; s < pPipelineStateD3D11->GetNumShaderTypes(); ++s) + for (Uint32 s = 0; s < pPipelineStateD3D11->GetNumShaderStages(); ++s) { auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); if (pShaderD3D11->GetD3D11Resources()->GetTotalResources() > 0) @@ -206,7 +206,7 @@ void DeviceContextD3D11Impl::TransitionAndCommitShaderResources(IPipelineState* #endif auto NumShaders = pShaderResBindingD3D11->GetNumActiveShaders(); - VERIFY(NumShaders == pPipelineStateD3D11->GetNumShaderTypes(), "Number of active shaders in shader resource binding is not consistent with the number of shaders in the pipeline state"); + VERIFY(NumShaders == pPipelineStateD3D11->GetNumShaderStages(), "Number of active shaders in shader resource binding is not consistent with the number of shaders in the pipeline state"); #ifdef DILIGENT_DEVELOPMENT { diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index 0e312740..4893f97e 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -131,28 +131,35 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR UNEXPECTED(GetPipelineTypeString(m_Desc.PipelineType), " pipelines are not supported by Direct3D11 backend"); } + // We do not really need ShaderStages, but ExtractShaders() also initializes + // shader stage types and shader stage counter. + std::vector> ShaderStages; + ExtractShaders(ShaderStages); + VERIFY_EXPR(GetNumShaderStages() == ShaderStages.size()); + // clang-format off static_assert((sizeof(ShaderResourceLayoutD3D11) % sizeof(void*)) == 0, "sizeof(ShaderResourceLayoutD3D11) is expected to be a multiple of sizeof(void*)"); static_assert((sizeof(ShaderResourceCacheD3D11) % sizeof(void*)) == 0, "sizeof(ShaderResourceCacheD3D11) is expected to be a multiple of sizeof(void*)"); // clang-format on - const auto MemSize = (sizeof(ShaderResourceLayoutD3D11) + sizeof(ShaderResourceCacheD3D11)) * GetNumShaderTypes(); + + const auto MemSize = (sizeof(ShaderResourceLayoutD3D11) + sizeof(ShaderResourceCacheD3D11)) * GetNumShaderStages(); auto* const pRawMem = ALLOCATE_RAW(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11 and ShaderResourceCacheD3D11 arrays", MemSize); m_pStaticResourceLayouts = reinterpret_cast(pRawMem); - m_pStaticResourceCaches = reinterpret_cast(m_pStaticResourceLayouts + GetNumShaderTypes()); + m_pStaticResourceCaches = reinterpret_cast(m_pStaticResourceLayouts + GetNumShaderStages()); const auto& ResourceLayout = m_Desc.ResourceLayout; #ifdef DILIGENT_DEVELOPMENT { const ShaderResources* pResources[MAX_SHADERS_IN_PIPELINE] = {}; - for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) + for (Uint32 s = 0; s < GetNumShaderStages(); ++s) { auto* pShader = GetShader(s); pResources[s] = &(*pShader->GetD3D11Resources()); } - ShaderResources::DvpVerifyResourceLayout(ResourceLayout, pResources, GetNumShaderTypes(), + ShaderResources::DvpVerifyResourceLayout(ResourceLayout, pResources, GetNumShaderStages(), (CreateInfo.Flags & PSO_CREATE_FLAG_IGNORE_MISSING_VARIABLES) == 0, (CreateInfo.Flags & PSO_CREATE_FLAG_IGNORE_MISSING_STATIC_SAMPLERS) == 0); } @@ -161,7 +168,7 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR decltype(m_StaticSamplers) StaticSamplers(STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")); std::array ShaderResLayoutDataSizes = {}; std::array ShaderResCacheDataSizes = {}; - for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) + for (Uint32 s = 0; s < GetNumShaderStages(); ++s) { const auto* pShader = GetShader(s); const auto& ShaderDesc = pShader->GetDesc(); @@ -221,14 +228,14 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR if (m_Desc.SRBAllocationGranularity > 1) { - m_SRBMemAllocator.Initialize(m_Desc.SRBAllocationGranularity, GetNumShaderTypes(), ShaderResLayoutDataSizes.data(), GetNumShaderTypes(), ShaderResCacheDataSizes.data()); + m_SRBMemAllocator.Initialize(m_Desc.SRBAllocationGranularity, GetNumShaderStages(), ShaderResLayoutDataSizes.data(), GetNumShaderStages(), ShaderResCacheDataSizes.data()); } m_StaticSamplers.reserve(StaticSamplers.size()); for (auto& Sam : StaticSamplers) m_StaticSamplers.emplace_back(std::move(Sam)); - for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) + for (Uint32 s = 0; s < GetNumShaderStages(); ++s) { // Initialize static samplers in the static resource cache to avoid warning messages SetStaticSamplers(m_pStaticResourceCaches[s], s); @@ -238,19 +245,19 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR PipelineStateD3D11Impl::~PipelineStateD3D11Impl() { - for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) + for (Uint32 s = 0; s < GetNumShaderStages(); ++s) { m_pStaticResourceCaches[s].Destroy(GetRawAllocator()); m_pStaticResourceCaches[s].~ShaderResourceCacheD3D11(); } - for (Uint32 l = 0; l < GetNumShaderTypes(); ++l) + for (Uint32 l = 0; l < GetNumShaderStages(); ++l) { m_pStaticResourceLayouts[l].~ShaderResourceLayoutD3D11(); } // m_pStaticResourceLayouts and m_pStaticResourceCaches are allocated in contiguous chunks of memory. - auto* pRawMem = m_pStaticResourceLayouts; - GetRawAllocator().Free(pRawMem); + if (auto* pRawMem = m_pStaticResourceLayouts) + GetRawAllocator().Free(pRawMem); } IMPLEMENT_QUERY_INTERFACE(PipelineStateD3D11Impl, IID_PipelineStateD3D11, TPipelineStateBase) @@ -297,10 +304,10 @@ bool PipelineStateD3D11Impl::IsCompatibleWith(const IPipelineState* pPSO) const if (m_ShaderResourceLayoutHash != pPSOD3D11->m_ShaderResourceLayoutHash) return false; - if (GetNumShaderTypes() != pPSOD3D11->GetNumShaderTypes()) + if (GetNumShaderStages() != pPSOD3D11->GetNumShaderStages()) return false; - for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) + for (Uint32 s = 0; s < GetNumShaderStages(); ++s) { auto* pShader0 = GetShader(s); auto* pShader1 = pPSOD3D11->GetShader(s); @@ -360,7 +367,7 @@ ID3D11ComputeShader* PipelineStateD3D11Impl::GetD3D11ComputeShader() void PipelineStateD3D11Impl::BindStaticResources(Uint32 ShaderFlags, IResourceMapping* pResourceMapping, Uint32 Flags) { - for (Uint32 s = 0; s < GetNumShaderTypes(); ++s) + for (Uint32 s = 0; s < GetNumShaderStages(); ++s) { auto& StaticResLayout = m_pStaticResourceLayouts[s]; if ((ShaderFlags & StaticResLayout.GetShaderType()) != 0) @@ -374,7 +381,7 @@ Uint32 PipelineStateD3D11Impl::GetStaticVariableCount(SHADER_TYPE ShaderType) co if (LayoutInd < 0) return 0; - VERIFY_EXPR(static_cast(LayoutInd) <= GetNumShaderTypes()); + VERIFY_EXPR(static_cast(LayoutInd) <= GetNumShaderStages()); return m_pStaticResourceLayouts[LayoutInd].GetTotalResourceCount(); } @@ -384,7 +391,7 @@ IShaderResourceVariable* PipelineStateD3D11Impl::GetStaticVariableByName(SHADER_ if (LayoutInd < 0) return nullptr; - VERIFY_EXPR(static_cast(LayoutInd) <= GetNumShaderTypes()); + VERIFY_EXPR(static_cast(LayoutInd) <= GetNumShaderStages()); return m_pStaticResourceLayouts[LayoutInd].GetShaderVariable(Name); } @@ -394,7 +401,7 @@ IShaderResourceVariable* PipelineStateD3D11Impl::GetStaticVariableByIndex(SHADER if (LayoutInd < 0) return nullptr; - VERIFY_EXPR(static_cast(LayoutInd) <= GetNumShaderTypes()); + VERIFY_EXPR(static_cast(LayoutInd) <= GetNumShaderStages()); return m_pStaticResourceLayouts[LayoutInd].GetShaderVariable(Index); } @@ -431,8 +438,8 @@ const ShaderD3D11Impl* PipelineStateD3D11Impl::GetShaderByType(SHADER_TYPE Shade const ShaderD3D11Impl* PipelineStateD3D11Impl::GetShader(Uint32 Index) const { - if (Index < GetNumShaderTypes()) - return GetShaderByType(GetShaderTypes()[Index]); + if (Index < GetNumShaderStages()) + return GetShaderByType(GetShaderStageType(Index)); UNEXPECTED("Shader index is out of range"); return nullptr; diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp index 17083bbc..88c7b77f 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp @@ -50,7 +50,7 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl(IReferenceCounter // clang-format on { m_ResourceLayoutIndex.fill(-1); - m_NumActiveShaders = static_cast(pPSO->GetNumShaderTypes()); + m_NumActiveShaders = static_cast(pPSO->GetNumShaderStages()); // clang-format off m_pResourceLayouts = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11", ShaderResourceLayoutD3D11, m_NumActiveShaders); @@ -151,7 +151,7 @@ void ShaderResourceBindingD3D11Impl::InitializeStaticResources(const IPipelineSt } const auto* pPSOD3D11 = ValidatedCast(pPipelineState); - auto NumShaders = pPSOD3D11->GetNumShaderTypes(); + auto NumShaders = pPSOD3D11->GetNumShaderStages(); VERIFY_EXPR(NumShaders == m_NumActiveShaders); for (Uint32 shader = 0; shader < NumShaders; ++shader) -- cgit v1.2.3 From b3fec8bd40e80115086281bce74774617aa95e23 Mon Sep 17 00:00:00 2001 From: assiduous Date: Wed, 14 Oct 2020 01:04:05 -0700 Subject: Added buffer mode validation when binding buffer views --- Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp index fd6c7299..63b34f44 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp @@ -553,6 +553,7 @@ void ShaderResourceLayoutD3D11::BuffSRVBindInfo::BindResource(IDeviceObject* pVi { auto& CachedSRV = ResourceCache.GetSRV(m_Attribs.BindPoint + ArrayIndex); VerifyResourceViewBinding(m_Attribs, GetType(), ArrayIndex, pView, pViewD3D11.RawPtr(), {BUFFER_VIEW_SHADER_RESOURCE}, CachedSRV.pView.RawPtr(), m_ParentResLayout.GetShaderName()); + VerifyBufferViewModeD3D(pViewD3D11.RawPtr(), m_Attribs, m_ParentResLayout.GetShaderName()); } #endif ResourceCache.SetBufSRV(m_Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11)); @@ -593,6 +594,7 @@ void ShaderResourceLayoutD3D11::BuffUAVBindInfo::BindResource(IDeviceObject* pVi { auto& CachedUAV = ResourceCache.GetUAV(m_Attribs.BindPoint + ArrayIndex); VerifyResourceViewBinding(m_Attribs, GetType(), ArrayIndex, pView, pViewD3D11.RawPtr(), {BUFFER_VIEW_UNORDERED_ACCESS}, CachedUAV.pView.RawPtr(), m_ParentResLayout.GetShaderName()); + VerifyBufferViewModeD3D(pViewD3D11.RawPtr(), m_Attribs, m_ParentResLayout.GetShaderName()); } #endif ResourceCache.SetBufUAV(m_Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11)); -- cgit v1.2.3 From 259a9d5897937dcaaca1b4b294bb8d5250371e76 Mon Sep 17 00:00:00 2001 From: azhirnov Date: Thu, 15 Oct 2020 20:55:38 +0300 Subject: Added GraphicsPipelineCreateInfo and ComputePipelineCreateInfo instead of single PipelineCreateInfo. Some optimizations for dynamic memory allocations in PipelineState. --- .../include/PipelineStateD3D11Impl.hpp | 17 +- .../include/RenderDeviceD3D11Impl.hpp | 10 +- .../src/DeviceContextD3D11Impl.cpp | 10 +- .../src/PipelineStateD3D11Impl.cpp | 229 ++++++++++++--------- .../src/RenderDeviceD3D11Impl.cpp | 13 +- 5 files changed, 173 insertions(+), 106 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp index 1d836ab3..99062706 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp @@ -49,9 +49,12 @@ class PipelineStateD3D11Impl final : public PipelineStateBase; - PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, - class RenderDeviceD3D11Impl* pDeviceD3D11, - const PipelineStateCreateInfo& CreateInfo); + PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, + class RenderDeviceD3D11Impl* pDeviceD3D11, + const GraphicsPipelineStateCreateInfo& CreateInfo); + PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, + class RenderDeviceD3D11Impl* pDeviceD3D11, + const ComputePipelineStateCreateInfo& CreateInfo); ~PipelineStateD3D11Impl(); virtual void DILIGENT_CALL_TYPE QueryInterface(const INTERFACE_ID& IID, IObject** ppInterface) override final; @@ -134,6 +137,10 @@ public: void SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache, Uint32 ShaderInd) const; private: + void InitResourceLayouts(RenderDeviceD3D11Impl* pRenderDeviceD3D11, + const PipelineStateCreateInfo& CreateInfo, + const std::vector>& ShaderStages); + CComPtr m_pd3d11BlendState; CComPtr m_pd3d11RasterizerState; CComPtr m_pd3d11DepthStencilState; @@ -147,8 +154,8 @@ private: RefCntAutoPtr m_pCS; // The caches are indexed by the shader order in the PSO, not shader index - ShaderResourceCacheD3D11* m_pStaticResourceCaches = nullptr; - ShaderResourceLayoutD3D11* m_pStaticResourceLayouts = nullptr; + ShaderResourceCacheD3D11* m_pStaticResourceCaches = nullptr; // [m_NumShaderStages] + ShaderResourceLayoutD3D11* m_pStaticResourceLayouts = nullptr; // [m_NumShaderStages] // SRB memory allocator must be defined before the default shader res binding SRBMemoryAllocator m_SRBMemAllocator; diff --git a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.hpp index 8003615e..1a1e01af 100644 --- a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.hpp @@ -69,9 +69,13 @@ public: virtual void DILIGENT_CALL_TYPE CreateSampler(const SamplerDesc& SamplerDesc, ISampler** ppSampler) override final; - /// Implementation of IRenderDevice::CreatePipelineState() in Direct3D11 backend. - virtual void DILIGENT_CALL_TYPE CreatePipelineState(const PipelineStateCreateInfo& PSOCreateInfo, - IPipelineState** ppPipelineState) override final; + /// Implementation of IRenderDevice::CreateGraphicsPipelineState() in Direct3D11 backend. + virtual void DILIGENT_CALL_TYPE CreateGraphicsPipelineState(const GraphicsPipelineStateCreateInfo& PSOCreateInfo, + IPipelineState** ppPipelineState) override final; + + /// Implementation of IRenderDevice::CreateComputePipelineState() in Direct3D11 backend. + virtual void DILIGENT_CALL_TYPE CreateComputePipelineState(const ComputePipelineStateCreateInfo& PSOCreateInfo, + IPipelineState** ppPipelineState) override final; /// Implementation of IRenderDevice::CreateFence() in Direct3D11 backend. virtual void DILIGENT_CALL_TYPE CreateFence(const FenceDesc& Desc, diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index 75ce9094..39560453 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -75,7 +75,7 @@ void DeviceContextD3D11Impl::SetPipelineState(IPipelineState* pPipelineState) TDeviceContextBase::SetPipelineState(pPipelineStateD3D11, 0 /*Dummy*/); auto& Desc = pPipelineStateD3D11->GetDesc(); - if (Desc.IsComputePipeline()) + if (Desc.PipelineType == PIPELINE_TYPE_COMPUTE) { auto* pd3d11CS = pPipelineStateD3D11->GetD3D11ComputeShader(); if (pd3d11CS == nullptr) @@ -106,7 +106,9 @@ void DeviceContextD3D11Impl::SetPipelineState(IPipelineState* pPipelineState) COMMIT_SHADER(DS, DomainShader); #undef COMMIT_SHADER - m_pd3d11DeviceContext->OMSetBlendState(pPipelineStateD3D11->GetD3D11BlendState(), m_BlendFactors, Desc.GraphicsPipeline.SampleMask); + auto& GraphicsPipeline = pPipelineStateD3D11->GetGraphicsPipelineDesc(); + + m_pd3d11DeviceContext->OMSetBlendState(pPipelineStateD3D11->GetD3D11BlendState(), m_BlendFactors, GraphicsPipeline.SampleMask); m_pd3d11DeviceContext->RSSetState(pPipelineStateD3D11->GetD3D11RasterizerState()); m_pd3d11DeviceContext->OMSetDepthStencilState(pPipelineStateD3D11->GetD3D11DepthStencilState(), m_StencilRef); @@ -119,7 +121,7 @@ void DeviceContextD3D11Impl::SetPipelineState(IPipelineState* pPipelineState) m_CommittedD3D11InputLayout = pd3d11InputLayout; } - auto PrimTopology = Desc.GraphicsPipeline.PrimitiveTopology; + auto PrimTopology = GraphicsPipeline.PrimitiveTopology; if (m_CommittedPrimitiveTopology != PrimTopology) { m_CommittedPrimitiveTopology = PrimTopology; @@ -673,7 +675,7 @@ void DeviceContextD3D11Impl::SetBlendFactors(const float* pBlendFactors) ID3D11BlendState* pd3d11BS = nullptr; if (m_pPipelineState) { - SampleMask = m_pPipelineState->GetDesc().GraphicsPipeline.SampleMask; + SampleMask = m_pPipelineState->GetGraphicsPipelineDesc().SampleMask; pd3d11BS = m_pPipelineState->GetD3D11BlendState(); } m_pd3d11DeviceContext->OMSetBlendState(pd3d11BS, m_BlendFactors, SampleMask); diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index 4893f97e..d20e0c32 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -35,9 +35,9 @@ namespace Diligent { -PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, - RenderDeviceD3D11Impl* pRenderDeviceD3D11, - const PipelineStateCreateInfo& CreateInfo) : +PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, + RenderDeviceD3D11Impl* pRenderDeviceD3D11, + const GraphicsPipelineStateCreateInfo& CreateInfo) : // clang-format off TPipelineStateBase { @@ -51,28 +51,33 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR { m_ResourceLayoutIndex.fill(-1); - if (m_Desc.IsComputePipeline()) - { - auto* pCS = ValidatedCast(m_Desc.ComputePipeline.pCS); - m_pCS = pCS; - if (m_pCS == nullptr) - { - LOG_ERROR_AND_THROW("Compute shader is null"); - } + // We do not really need ShaderStages, but ExtractShaders() also initializes + // shader stage types and shader stage counter. + std::vector> ShaderStages; + ExtractShaders(CreateInfo, ShaderStages); - if (m_pCS && m_pCS->GetDesc().ShaderType != SHADER_TYPE_COMPUTE) - { - LOG_ERROR_AND_THROW(GetShaderTypeLiteralName(SHADER_TYPE_COMPUTE), " shader is expeceted while ", GetShaderTypeLiteralName(m_pCS->GetDesc().ShaderType), " provided"); - } - m_ShaderResourceLayoutHash = pCS->GetD3D11Resources()->GetHash(); - } - else if (m_Desc.PipelineType == PIPELINE_TYPE_GRAPHICS) - { + // Memory must be released if an exception is thrown. + LinearAllocator MemPool{GetRawAllocator()}; + + MemPool.AddRequiredSize(GetNumShaderStages()); + MemPool.AddRequiredSize(GetNumShaderStages()); + + ValidateAndReserveSpace(CreateInfo, MemPool); + + MemPool.Reserve(); + + m_pStaticResourceLayouts = MemPool.Allocate(GetNumShaderStages()); + m_pStaticResourceCaches = MemPool.Allocate(GetNumShaderStages()); + + InitGraphicsPipeline(CreateInfo, MemPool); + InitResourceLayouts(pRenderDeviceD3D11, CreateInfo, ShaderStages); + + auto& GraphicsPipeline = GetGraphicsPipelineDesc(); #define INIT_SHADER(ShortName, ExpectedType) \ do \ { \ - auto* pShader = ValidatedCast(m_Desc.GraphicsPipeline.p##ShortName); \ + auto* pShader = ValidatedCast(CreateInfo.p##ShortName); \ m_p##ShortName = pShader; \ if (m_p##ShortName && m_p##ShortName->GetDesc().ShaderType != ExpectedType) \ { \ @@ -82,81 +87,140 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR HashCombine(m_ShaderResourceLayoutHash, pShader->GetD3D11Resources()->GetHash()); \ } while (false) - INIT_SHADER(VS, SHADER_TYPE_VERTEX); - INIT_SHADER(PS, SHADER_TYPE_PIXEL); - INIT_SHADER(GS, SHADER_TYPE_GEOMETRY); - INIT_SHADER(DS, SHADER_TYPE_DOMAIN); - INIT_SHADER(HS, SHADER_TYPE_HULL); + INIT_SHADER(VS, SHADER_TYPE_VERTEX); + INIT_SHADER(PS, SHADER_TYPE_PIXEL); + INIT_SHADER(GS, SHADER_TYPE_GEOMETRY); + INIT_SHADER(DS, SHADER_TYPE_DOMAIN); + INIT_SHADER(HS, SHADER_TYPE_HULL); #undef INIT_SHADER - if (m_pVS == nullptr) - { - LOG_ERROR_AND_THROW("Vertex shader is null"); - } + if (m_pVS == nullptr) + { + LOG_ERROR_AND_THROW("Vertex shader is null"); + } - auto* pDeviceD3D11 = pRenderDeviceD3D11->GetD3D11Device(); + auto* pDeviceD3D11 = pRenderDeviceD3D11->GetD3D11Device(); - D3D11_BLEND_DESC D3D11BSDesc = {}; - BlendStateDesc_To_D3D11_BLEND_DESC(m_Desc.GraphicsPipeline.BlendDesc, D3D11BSDesc); - CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateBlendState(&D3D11BSDesc, &m_pd3d11BlendState), - "Failed to create D3D11 blend state object"); + D3D11_BLEND_DESC D3D11BSDesc = {}; + BlendStateDesc_To_D3D11_BLEND_DESC(GraphicsPipeline.BlendDesc, D3D11BSDesc); + CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateBlendState(&D3D11BSDesc, &m_pd3d11BlendState), + "Failed to create D3D11 blend state object"); - D3D11_RASTERIZER_DESC D3D11RSDesc = {}; - RasterizerStateDesc_To_D3D11_RASTERIZER_DESC(m_Desc.GraphicsPipeline.RasterizerDesc, D3D11RSDesc); - CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateRasterizerState(&D3D11RSDesc, &m_pd3d11RasterizerState), - "Failed to create D3D11 rasterizer state"); + D3D11_RASTERIZER_DESC D3D11RSDesc = {}; + RasterizerStateDesc_To_D3D11_RASTERIZER_DESC(GraphicsPipeline.RasterizerDesc, D3D11RSDesc); + CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateRasterizerState(&D3D11RSDesc, &m_pd3d11RasterizerState), + "Failed to create D3D11 rasterizer state"); - D3D11_DEPTH_STENCIL_DESC D3D11DSSDesc = {}; - DepthStencilStateDesc_To_D3D11_DEPTH_STENCIL_DESC(m_Desc.GraphicsPipeline.DepthStencilDesc, D3D11DSSDesc); - CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateDepthStencilState(&D3D11DSSDesc, &m_pd3d11DepthStencilState), - "Failed to create D3D11 depth stencil state"); + D3D11_DEPTH_STENCIL_DESC D3D11DSSDesc = {}; + DepthStencilStateDesc_To_D3D11_DEPTH_STENCIL_DESC(GraphicsPipeline.DepthStencilDesc, D3D11DSSDesc); + CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateDepthStencilState(&D3D11DSSDesc, &m_pd3d11DepthStencilState), + "Failed to create D3D11 depth stencil state"); - // Create input layout - const auto& InputLayout = m_Desc.GraphicsPipeline.InputLayout; - if (InputLayout.NumElements > 0) - { - std::vector> d311InputElements(STD_ALLOCATOR_RAW_MEM(D3D11_INPUT_ELEMENT_DESC, GetRawAllocator(), "Allocator for vector")); - LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(InputLayout, d311InputElements); + // Create input layout + const auto& InputLayout = GraphicsPipeline.InputLayout; + if (InputLayout.NumElements > 0) + { + std::vector> d311InputElements(STD_ALLOCATOR_RAW_MEM(D3D11_INPUT_ELEMENT_DESC, GetRawAllocator(), "Allocator for vector")); + LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(InputLayout, d311InputElements); - ID3DBlob* pVSByteCode = m_pVS.RawPtr()->GetBytecode(); - if (!pVSByteCode) - LOG_ERROR_AND_THROW("Vertex Shader byte code does not exist"); + ID3DBlob* pVSByteCode = m_pVS.RawPtr()->GetBytecode(); + if (!pVSByteCode) + LOG_ERROR_AND_THROW("Vertex Shader byte code does not exist"); - CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateInputLayout(d311InputElements.data(), static_cast(d311InputElements.size()), pVSByteCode->GetBufferPointer(), pVSByteCode->GetBufferSize(), &m_pd3d11InputLayout), - "Failed to create the Direct3D11 input layout"); - } + CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateInputLayout(d311InputElements.data(), static_cast(d311InputElements.size()), pVSByteCode->GetBufferPointer(), pVSByteCode->GetBufferSize(), &m_pd3d11InputLayout), + "Failed to create the Direct3D11 input layout"); } - else + + void* Ptr = MemPool.Release(); + VERIFY_EXPR(Ptr == m_pStaticResourceLayouts); +} + +PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, + RenderDeviceD3D11Impl* pRenderDeviceD3D11, + const ComputePipelineStateCreateInfo& CreateInfo) : + // clang-format off + TPipelineStateBase { - UNEXPECTED(GetPipelineTypeString(m_Desc.PipelineType), " pipelines are not supported by Direct3D11 backend"); - } + pRefCounters, + pRenderDeviceD3D11, + CreateInfo.PSODesc + }, + m_SRBMemAllocator{GetRawAllocator()}, + m_StaticSamplers (STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")) +// clang-format on +{ + m_ResourceLayoutIndex.fill(-1); // We do not really need ShaderStages, but ExtractShaders() also initializes // shader stage types and shader stage counter. std::vector> ShaderStages; - ExtractShaders(ShaderStages); - VERIFY_EXPR(GetNumShaderStages() == ShaderStages.size()); + ExtractShaders(CreateInfo, ShaderStages); - // clang-format off - static_assert((sizeof(ShaderResourceLayoutD3D11) % sizeof(void*)) == 0, "sizeof(ShaderResourceLayoutD3D11) is expected to be a multiple of sizeof(void*)"); - static_assert((sizeof(ShaderResourceCacheD3D11) % sizeof(void*)) == 0, "sizeof(ShaderResourceCacheD3D11) is expected to be a multiple of sizeof(void*)"); - // clang-format on + // Memory must be released if an exception is thrown. + LinearAllocator MemPool{GetRawAllocator()}; + + MemPool.AddRequiredSize(GetNumShaderStages()); + MemPool.AddRequiredSize(GetNumShaderStages()); + + ValidateAndReserveSpace(CreateInfo, MemPool); + + MemPool.Reserve(); - const auto MemSize = (sizeof(ShaderResourceLayoutD3D11) + sizeof(ShaderResourceCacheD3D11)) * GetNumShaderStages(); - auto* const pRawMem = - ALLOCATE_RAW(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11 and ShaderResourceCacheD3D11 arrays", MemSize); + m_pStaticResourceLayouts = MemPool.Allocate(GetNumShaderStages()); + m_pStaticResourceCaches = MemPool.Allocate(GetNumShaderStages()); - m_pStaticResourceLayouts = reinterpret_cast(pRawMem); - m_pStaticResourceCaches = reinterpret_cast(m_pStaticResourceLayouts + GetNumShaderStages()); + InitComputePipeline(CreateInfo, MemPool); + InitResourceLayouts(pRenderDeviceD3D11, CreateInfo, ShaderStages); + auto* pCS = ValidatedCast(CreateInfo.pCS); + m_pCS = pCS; + if (m_pCS == nullptr) + { + LOG_ERROR_AND_THROW("Compute shader is null"); + } + + if (m_pCS && m_pCS->GetDesc().ShaderType != SHADER_TYPE_COMPUTE) + { + LOG_ERROR_AND_THROW(GetShaderTypeLiteralName(SHADER_TYPE_COMPUTE), " shader is expeceted while ", GetShaderTypeLiteralName(m_pCS->GetDesc().ShaderType), " provided"); + } + m_ShaderResourceLayoutHash = pCS->GetD3D11Resources()->GetHash(); + + void* Ptr = MemPool.Release(); + VERIFY_EXPR(Ptr == m_pStaticResourceLayouts); +} + +PipelineStateD3D11Impl::~PipelineStateD3D11Impl() +{ + for (Uint32 s = 0; s < GetNumShaderStages(); ++s) + { + m_pStaticResourceCaches[s].Destroy(GetRawAllocator()); + m_pStaticResourceCaches[s].~ShaderResourceCacheD3D11(); + } + + for (Uint32 l = 0; l < GetNumShaderStages(); ++l) + { + m_pStaticResourceLayouts[l].~ShaderResourceLayoutD3D11(); + } + // m_pStaticResourceLayouts and m_pStaticResourceCaches are allocated in contiguous chunks of memory. + if (auto* pRawMem = m_pStaticResourceLayouts) + GetRawAllocator().Free(pRawMem); +} + +IMPLEMENT_QUERY_INTERFACE(PipelineStateD3D11Impl, IID_PipelineStateD3D11, TPipelineStateBase) + + +void PipelineStateD3D11Impl::InitResourceLayouts(RenderDeviceD3D11Impl* pRenderDeviceD3D11, + const PipelineStateCreateInfo& CreateInfo, + const std::vector>& ShaderStages) +{ const auto& ResourceLayout = m_Desc.ResourceLayout; #ifdef DILIGENT_DEVELOPMENT { const ShaderResources* pResources[MAX_SHADERS_IN_PIPELINE] = {}; - for (Uint32 s = 0; s < GetNumShaderStages(); ++s) + for (Uint32 s = 0; s < ShaderStages.size(); ++s) { - auto* pShader = GetShader(s); + auto* pShader = ShaderStages[s].second; pResources[s] = &(*pShader->GetD3D11Resources()); } ShaderResources::DvpVerifyResourceLayout(ResourceLayout, pResources, GetNumShaderStages(), @@ -168,9 +232,9 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR decltype(m_StaticSamplers) StaticSamplers(STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")); std::array ShaderResLayoutDataSizes = {}; std::array ShaderResCacheDataSizes = {}; - for (Uint32 s = 0; s < GetNumShaderStages(); ++s) + for (Uint32 s = 0; s < ShaderStages.size(); ++s) { - const auto* pShader = GetShader(s); + const auto* pShader = ShaderStages[s].second; const auto& ShaderDesc = pShader->GetDesc(); const auto& ShaderResources = *pShader->GetD3D11Resources(); VERIFY_EXPR(ShaderDesc.ShaderType == ShaderResources.GetShaderType()); @@ -242,27 +306,6 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR } } - -PipelineStateD3D11Impl::~PipelineStateD3D11Impl() -{ - for (Uint32 s = 0; s < GetNumShaderStages(); ++s) - { - m_pStaticResourceCaches[s].Destroy(GetRawAllocator()); - m_pStaticResourceCaches[s].~ShaderResourceCacheD3D11(); - } - - for (Uint32 l = 0; l < GetNumShaderStages(); ++l) - { - m_pStaticResourceLayouts[l].~ShaderResourceLayoutD3D11(); - } - // m_pStaticResourceLayouts and m_pStaticResourceCaches are allocated in contiguous chunks of memory. - if (auto* pRawMem = m_pStaticResourceLayouts) - GetRawAllocator().Free(pRawMem); -} - -IMPLEMENT_QUERY_INTERFACE(PipelineStateD3D11Impl, IID_PipelineStateD3D11, TPipelineStateBase) - - ID3D11BlendState* PipelineStateD3D11Impl::GetD3D11BlendState() { return m_pd3d11BlendState; diff --git a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp index 76020ed8..537d9db2 100644 --- a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp @@ -385,7 +385,18 @@ void RenderDeviceD3D11Impl::CreateSampler(const SamplerDesc& SamplerDesc, ISampl }); } -void RenderDeviceD3D11Impl::CreatePipelineState(const PipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState) +void RenderDeviceD3D11Impl::CreateGraphicsPipelineState(const GraphicsPipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState) +{ + CreateDeviceObject("Pipeline state", PSOCreateInfo.PSODesc, ppPipelineState, + [&]() // + { + PipelineStateD3D11Impl* pPipelineStateD3D11(NEW_RC_OBJ(m_PSOAllocator, "PipelineStateD3D11Impl instance", PipelineStateD3D11Impl)(this, PSOCreateInfo)); + pPipelineStateD3D11->QueryInterface(IID_PipelineState, reinterpret_cast(ppPipelineState)); + OnCreateDeviceObject(pPipelineStateD3D11); + }); +} + +void RenderDeviceD3D11Impl::CreateComputePipelineState(const ComputePipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState) { CreateDeviceObject("Pipeline state", PSOCreateInfo.PSODesc, ppPipelineState, [&]() // -- cgit v1.2.3 From e7b18160a758b30dd6b701b4aa6f43c9c2061ccf Mon Sep 17 00:00:00 2001 From: assiduous Date: Sat, 17 Oct 2020 09:56:34 -0700 Subject: All backends: added resource dimension validation when setting shader variables --- .../src/ShaderResourceLayoutD3D11.cpp | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp index 63b34f44..723e190a 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp @@ -429,7 +429,7 @@ void ShaderResourceLayoutD3D11::ConstBuffBindInfo::BindResource(IDeviceObject* p // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type - RefCntAutoPtr pBuffD3D11Impl(pBuffer, IID_BufferD3D11); + RefCntAutoPtr pBuffD3D11Impl{pBuffer, IID_BufferD3D11}; #ifdef DILIGENT_DEVELOPMENT { auto& CachedCB = m_ParentResLayout.m_ResourceCache.GetCB(m_Attribs.BindPoint + ArrayIndex); @@ -448,11 +448,12 @@ void ShaderResourceLayoutD3D11::TexSRVBindInfo::BindResource(IDeviceObject* pVie // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type - RefCntAutoPtr pViewD3D11(pView, IID_TextureViewD3D11); + RefCntAutoPtr pViewD3D11{pView, IID_TextureViewD3D11}; #ifdef DILIGENT_DEVELOPMENT { auto& CachedSRV = ResourceCache.GetSRV(m_Attribs.BindPoint + ArrayIndex); - VerifyResourceViewBinding(m_Attribs, GetType(), ArrayIndex, pView, pViewD3D11.RawPtr(), {TEXTURE_VIEW_SHADER_RESOURCE}, CachedSRV.pView.RawPtr(), m_ParentResLayout.GetShaderName()); + VerifyResourceViewBinding(m_Attribs, GetType(), ArrayIndex, pView, pViewD3D11.RawPtr(), {TEXTURE_VIEW_SHADER_RESOURCE}, + CachedSRV.pView.RawPtr(), m_ParentResLayout.GetShaderName()); } #endif @@ -505,7 +506,7 @@ void ShaderResourceLayoutD3D11::SamplerBindInfo::BindResource(IDeviceObject* pSa // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type - RefCntAutoPtr pSamplerD3D11(pSampler, IID_SamplerD3D11); + RefCntAutoPtr pSamplerD3D11{pSampler, IID_SamplerD3D11}; #ifdef DILIGENT_DEVELOPMENT if (pSampler && !pSamplerD3D11) @@ -548,11 +549,12 @@ void ShaderResourceLayoutD3D11::BuffSRVBindInfo::BindResource(IDeviceObject* pVi // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type - RefCntAutoPtr pViewD3D11(pView, IID_BufferViewD3D11); + RefCntAutoPtr pViewD3D11{pView, IID_BufferViewD3D11}; #ifdef DILIGENT_DEVELOPMENT { auto& CachedSRV = ResourceCache.GetSRV(m_Attribs.BindPoint + ArrayIndex); - VerifyResourceViewBinding(m_Attribs, GetType(), ArrayIndex, pView, pViewD3D11.RawPtr(), {BUFFER_VIEW_SHADER_RESOURCE}, CachedSRV.pView.RawPtr(), m_ParentResLayout.GetShaderName()); + VerifyResourceViewBinding(m_Attribs, GetType(), ArrayIndex, pView, pViewD3D11.RawPtr(), {BUFFER_VIEW_SHADER_RESOURCE}, + CachedSRV.pView.RawPtr(), m_ParentResLayout.GetShaderName()); VerifyBufferViewModeD3D(pViewD3D11.RawPtr(), m_Attribs, m_ParentResLayout.GetShaderName()); } #endif @@ -569,11 +571,12 @@ void ShaderResourceLayoutD3D11::TexUAVBindInfo::BindResource(IDeviceObject* pVie // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type - RefCntAutoPtr pViewD3D11(pView, IID_TextureViewD3D11); + RefCntAutoPtr pViewD3D11{pView, IID_TextureViewD3D11}; #ifdef DILIGENT_DEVELOPMENT { auto& CachedUAV = ResourceCache.GetUAV(m_Attribs.BindPoint + ArrayIndex); - VerifyResourceViewBinding(m_Attribs, GetType(), ArrayIndex, pView, pViewD3D11.RawPtr(), {TEXTURE_VIEW_UNORDERED_ACCESS}, CachedUAV.pView.RawPtr(), m_ParentResLayout.GetShaderName()); + VerifyResourceViewBinding(m_Attribs, GetType(), ArrayIndex, pView, pViewD3D11.RawPtr(), {TEXTURE_VIEW_UNORDERED_ACCESS}, + CachedUAV.pView.RawPtr(), m_ParentResLayout.GetShaderName()); } #endif ResourceCache.SetTexUAV(m_Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11)); @@ -589,11 +592,12 @@ void ShaderResourceLayoutD3D11::BuffUAVBindInfo::BindResource(IDeviceObject* pVi // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type - RefCntAutoPtr pViewD3D11(pView, IID_BufferViewD3D11); + RefCntAutoPtr pViewD3D11{pView, IID_BufferViewD3D11}; #ifdef DILIGENT_DEVELOPMENT { auto& CachedUAV = ResourceCache.GetUAV(m_Attribs.BindPoint + ArrayIndex); - VerifyResourceViewBinding(m_Attribs, GetType(), ArrayIndex, pView, pViewD3D11.RawPtr(), {BUFFER_VIEW_UNORDERED_ACCESS}, CachedUAV.pView.RawPtr(), m_ParentResLayout.GetShaderName()); + VerifyResourceViewBinding(m_Attribs, GetType(), ArrayIndex, pView, pViewD3D11.RawPtr(), {BUFFER_VIEW_UNORDERED_ACCESS}, + CachedUAV.pView.RawPtr(), m_ParentResLayout.GetShaderName()); VerifyBufferViewModeD3D(pViewD3D11.RawPtr(), m_Attribs, m_ParentResLayout.GetShaderName()); } #endif -- cgit v1.2.3 From 645ef7e425d6ff4ee64b782d27767e0a5732be50 Mon Sep 17 00:00:00 2001 From: assiduous Date: Sun, 18 Oct 2020 13:06:48 -0700 Subject: A number of fixes for PSO creation refactoring (API240075) --- .../include/PipelineStateD3D11Impl.hpp | 6 +- .../include/RenderDeviceD3D11Impl.hpp | 3 + .../src/DeviceContextD3D11Impl.cpp | 4 +- .../src/PipelineStateD3D11Impl.cpp | 93 +++++++++------------- .../src/RenderDeviceD3D11Impl.cpp | 41 +++++----- 5 files changed, 68 insertions(+), 79 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp index 99062706..eb184710 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp @@ -137,8 +137,10 @@ public: void SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache, Uint32 ShaderInd) const; private: - void InitResourceLayouts(RenderDeviceD3D11Impl* pRenderDeviceD3D11, - const PipelineStateCreateInfo& CreateInfo, + template + LinearAllocator InitInternalObjects(const PSOCreateInfoType& CreateInfo); + + void InitResourceLayouts(const PipelineStateCreateInfo& CreateInfo, const std::vector>& ShaderStages); CComPtr m_pd3d11BlendState; diff --git a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.hpp index 1a1e01af..c2a1285c 100644 --- a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.hpp @@ -124,6 +124,9 @@ public: Uint64 GetCommandQueueMask() const { return Uint64{1}; } private: + template + void CreatePipelineState(const PSOCreateInfoType& PSOCreateInfo, IPipelineState** ppPipelineState); + virtual void TestTextureFormat(TEXTURE_FORMAT TexFormat) override final; EngineD3D11CreateInfo m_EngineAttribs; diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index 39560453..7ee1ccec 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -106,7 +106,7 @@ void DeviceContextD3D11Impl::SetPipelineState(IPipelineState* pPipelineState) COMMIT_SHADER(DS, DomainShader); #undef COMMIT_SHADER - auto& GraphicsPipeline = pPipelineStateD3D11->GetGraphicsPipelineDesc(); + const auto& GraphicsPipeline = pPipelineStateD3D11->GetGraphicsPipelineDesc(); m_pd3d11DeviceContext->OMSetBlendState(pPipelineStateD3D11->GetD3D11BlendState(), m_BlendFactors, GraphicsPipeline.SampleMask); m_pd3d11DeviceContext->RSSetState(pPipelineStateD3D11->GetD3D11RasterizerState()); @@ -673,7 +673,7 @@ void DeviceContextD3D11Impl::SetBlendFactors(const float* pBlendFactors) { Uint32 SampleMask = 0xFFFFFFFF; ID3D11BlendState* pd3d11BS = nullptr; - if (m_pPipelineState) + if (m_pPipelineState && m_pPipelineState->GetDesc().IsAnyGraphicsPipeline()) { SampleMask = m_pPipelineState->GetGraphicsPipelineDesc().SampleMask; pd3d11BS = m_pPipelineState->GetD3D11BlendState(); diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index d20e0c32..292a9323 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -35,42 +35,49 @@ namespace Diligent { -PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, - RenderDeviceD3D11Impl* pRenderDeviceD3D11, - const GraphicsPipelineStateCreateInfo& CreateInfo) : - // clang-format off - TPipelineStateBase - { - pRefCounters, - pRenderDeviceD3D11, - CreateInfo.PSODesc - }, - m_SRBMemAllocator{GetRawAllocator()}, - m_StaticSamplers (STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")) -// clang-format on +template +LinearAllocator PipelineStateD3D11Impl::InitInternalObjects(const PSOCreateInfoType& CreateInfo) { m_ResourceLayoutIndex.fill(-1); - // We do not really need ShaderStages, but ExtractShaders() also initializes - // shader stage types and shader stage counter. std::vector> ShaderStages; ExtractShaders(CreateInfo, ShaderStages); // Memory must be released if an exception is thrown. LinearAllocator MemPool{GetRawAllocator()}; - MemPool.AddRequiredSize(GetNumShaderStages()); - MemPool.AddRequiredSize(GetNumShaderStages()); + MemPool.AddSpace(GetNumShaderStages()); + MemPool.AddSpace(GetNumShaderStages()); - ValidateAndReserveSpace(CreateInfo, MemPool); + ReserveSpaceForPipelineDesc(CreateInfo, MemPool); MemPool.Reserve(); m_pStaticResourceLayouts = MemPool.Allocate(GetNumShaderStages()); m_pStaticResourceCaches = MemPool.Allocate(GetNumShaderStages()); - InitGraphicsPipeline(CreateInfo, MemPool); - InitResourceLayouts(pRenderDeviceD3D11, CreateInfo, ShaderStages); + InitializePipelineDesc(CreateInfo, MemPool); + InitResourceLayouts(CreateInfo, ShaderStages); + + return MemPool; +} + + +PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, + RenderDeviceD3D11Impl* pRenderDeviceD3D11, + const GraphicsPipelineStateCreateInfo& CreateInfo) : + // clang-format off + TPipelineStateBase + { + pRefCounters, + pRenderDeviceD3D11, + CreateInfo + }, + m_SRBMemAllocator{GetRawAllocator()}, + m_StaticSamplers (STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")) +// clang-format on +{ + auto MemPool = InitInternalObjects(CreateInfo); auto& GraphicsPipeline = GetGraphicsPipelineDesc(); @@ -131,7 +138,7 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* "Failed to create the Direct3D11 input layout"); } - void* Ptr = MemPool.Release(); + auto* Ptr = MemPool.Release(); VERIFY_EXPR(Ptr == m_pStaticResourceLayouts); } @@ -143,49 +150,27 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* { pRefCounters, pRenderDeviceD3D11, - CreateInfo.PSODesc + CreateInfo }, m_SRBMemAllocator{GetRawAllocator()}, m_StaticSamplers (STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")) // clang-format on { - m_ResourceLayoutIndex.fill(-1); - - // We do not really need ShaderStages, but ExtractShaders() also initializes - // shader stage types and shader stage counter. - std::vector> ShaderStages; - ExtractShaders(CreateInfo, ShaderStages); - - // Memory must be released if an exception is thrown. - LinearAllocator MemPool{GetRawAllocator()}; - - MemPool.AddRequiredSize(GetNumShaderStages()); - MemPool.AddRequiredSize(GetNumShaderStages()); - - ValidateAndReserveSpace(CreateInfo, MemPool); - - MemPool.Reserve(); - - m_pStaticResourceLayouts = MemPool.Allocate(GetNumShaderStages()); - m_pStaticResourceCaches = MemPool.Allocate(GetNumShaderStages()); - - InitComputePipeline(CreateInfo, MemPool); - InitResourceLayouts(pRenderDeviceD3D11, CreateInfo, ShaderStages); + auto MemPool = InitInternalObjects(CreateInfo); - auto* pCS = ValidatedCast(CreateInfo.pCS); - m_pCS = pCS; + m_pCS = ValidatedCast(CreateInfo.pCS); if (m_pCS == nullptr) { LOG_ERROR_AND_THROW("Compute shader is null"); } - if (m_pCS && m_pCS->GetDesc().ShaderType != SHADER_TYPE_COMPUTE) + if (m_pCS->GetDesc().ShaderType != SHADER_TYPE_COMPUTE) { LOG_ERROR_AND_THROW(GetShaderTypeLiteralName(SHADER_TYPE_COMPUTE), " shader is expeceted while ", GetShaderTypeLiteralName(m_pCS->GetDesc().ShaderType), " provided"); } - m_ShaderResourceLayoutHash = pCS->GetD3D11Resources()->GetHash(); + m_ShaderResourceLayoutHash = m_pCS->GetD3D11Resources()->GetHash(); - void* Ptr = MemPool.Release(); + auto* Ptr = MemPool.Release(); VERIFY_EXPR(Ptr == m_pStaticResourceLayouts); } @@ -209,8 +194,7 @@ PipelineStateD3D11Impl::~PipelineStateD3D11Impl() IMPLEMENT_QUERY_INTERFACE(PipelineStateD3D11Impl, IID_PipelineStateD3D11, TPipelineStateBase) -void PipelineStateD3D11Impl::InitResourceLayouts(RenderDeviceD3D11Impl* pRenderDeviceD3D11, - const PipelineStateCreateInfo& CreateInfo, +void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& CreateInfo, const std::vector>& ShaderStages) { const auto& ResourceLayout = m_Desc.ResourceLayout; @@ -269,7 +253,7 @@ void PipelineStateD3D11Impl::InitResourceLayouts(RenderDeviceD3D11Impl* { const auto& SrcStaticSamplerInfo = ResourceLayout.StaticSamplers[SrcStaticSamplerInd]; RefCntAutoPtr pStaticSampler; - pRenderDeviceD3D11->CreateSampler(SrcStaticSamplerInfo.Desc, &pStaticSampler); + GetDevice()->CreateSampler(SrcStaticSamplerInfo.Desc, &pStaticSampler); StaticSamplers.emplace_back(SamplerAttribs, std::move(pStaticSampler)); } } @@ -328,9 +312,8 @@ ID3D11InputLayout* PipelineStateD3D11Impl::GetD3D11InputLayout() void PipelineStateD3D11Impl::CreateShaderResourceBinding(IShaderResourceBinding** ppShaderResourceBinding, bool InitStaticResources) { - auto* pRenderDeviceD3D11 = ValidatedCast(GetDevice()); - auto& SRBAllocator = pRenderDeviceD3D11->GetSRBAllocator(); - auto pShaderResBinding = NEW_RC_OBJ(SRBAllocator, "ShaderResourceBindingD3D11Impl instance", ShaderResourceBindingD3D11Impl)(this, false); + auto& SRBAllocator = GetDevice()->GetSRBAllocator(); + auto pShaderResBinding = NEW_RC_OBJ(SRBAllocator, "ShaderResourceBindingD3D11Impl instance", ShaderResourceBindingD3D11Impl)(this, false); if (InitStaticResources) pShaderResBinding->InitializeStaticResources(nullptr); pShaderResBinding->QueryInterface(IID_ShaderResourceBinding, reinterpret_cast(static_cast(ppShaderResourceBinding))); diff --git a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp index 537d9db2..94847a51 100644 --- a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp @@ -254,7 +254,7 @@ void RenderDeviceD3D11Impl::CreateBufferFromD3DResource(ID3D11Buffer* pd3d11Buff CreateDeviceObject("buffer", BuffDesc, ppBuffer, [&]() // { - BufferD3D11Impl* pBufferD3D11(NEW_RC_OBJ(m_BufObjAllocator, "BufferD3D11Impl instance", BufferD3D11Impl)(m_BuffViewObjAllocator, this, BuffDesc, InitialState, pd3d11Buffer)); + BufferD3D11Impl* pBufferD3D11{NEW_RC_OBJ(m_BufObjAllocator, "BufferD3D11Impl instance", BufferD3D11Impl)(m_BuffViewObjAllocator, this, BuffDesc, InitialState, pd3d11Buffer)}; pBufferD3D11->QueryInterface(IID_Buffer, reinterpret_cast(ppBuffer)); pBufferD3D11->CreateDefaultViews(); OnCreateDeviceObject(pBufferD3D11); @@ -266,7 +266,7 @@ void RenderDeviceD3D11Impl::CreateBuffer(const BufferDesc& BuffDesc, const Buffe CreateDeviceObject("buffer", BuffDesc, ppBuffer, [&]() // { - BufferD3D11Impl* pBufferD3D11(NEW_RC_OBJ(m_BufObjAllocator, "BufferD3D11Impl instance", BufferD3D11Impl)(m_BuffViewObjAllocator, this, BuffDesc, pBuffData)); + BufferD3D11Impl* pBufferD3D11{NEW_RC_OBJ(m_BufObjAllocator, "BufferD3D11Impl instance", BufferD3D11Impl)(m_BuffViewObjAllocator, this, BuffDesc, pBuffData)}; pBufferD3D11->QueryInterface(IID_Buffer, reinterpret_cast(ppBuffer)); pBufferD3D11->CreateDefaultViews(); OnCreateDeviceObject(pBufferD3D11); @@ -278,7 +278,7 @@ void RenderDeviceD3D11Impl::CreateShader(const ShaderCreateInfo& ShaderCI, IShad CreateDeviceObject("shader", ShaderCI.Desc, ppShader, [&]() // { - ShaderD3D11Impl* pShaderD3D11(NEW_RC_OBJ(m_ShaderObjAllocator, "ShaderD3D11Impl instance", ShaderD3D11Impl)(this, ShaderCI)); + ShaderD3D11Impl* pShaderD3D11{NEW_RC_OBJ(m_ShaderObjAllocator, "ShaderD3D11Impl instance", ShaderD3D11Impl)(this, ShaderCI)}; pShaderD3D11->QueryInterface(IID_Shader, reinterpret_cast(ppShader)); OnCreateDeviceObject(pShaderD3D11); @@ -295,7 +295,7 @@ void RenderDeviceD3D11Impl::CreateTexture1DFromD3DResource(ID3D11Texture1D* pd3d CreateDeviceObject("texture", TexDesc, ppTexture, [&]() // { - TextureBaseD3D11* pTextureD3D11 = NEW_RC_OBJ(m_TexObjAllocator, "Texture1D_D3D11 instance", Texture1D_D3D11)(m_TexViewObjAllocator, this, InitialState, pd3d11Texture); + TextureBaseD3D11* pTextureD3D11{NEW_RC_OBJ(m_TexObjAllocator, "Texture1D_D3D11 instance", Texture1D_D3D11)(m_TexViewObjAllocator, this, InitialState, pd3d11Texture)}; pTextureD3D11->QueryInterface(IID_Texture, reinterpret_cast(ppTexture)); pTextureD3D11->CreateDefaultViews(); OnCreateDeviceObject(pTextureD3D11); @@ -312,7 +312,7 @@ void RenderDeviceD3D11Impl::CreateTexture2DFromD3DResource(ID3D11Texture2D* pd3d CreateDeviceObject("texture", TexDesc, ppTexture, [&]() // { - TextureBaseD3D11* pTextureD3D11 = NEW_RC_OBJ(m_TexObjAllocator, "Texture2D_D3D11 instance", Texture2D_D3D11)(m_TexViewObjAllocator, this, InitialState, pd3d11Texture); + TextureBaseD3D11* pTextureD3D11{NEW_RC_OBJ(m_TexObjAllocator, "Texture2D_D3D11 instance", Texture2D_D3D11)(m_TexViewObjAllocator, this, InitialState, pd3d11Texture)}; pTextureD3D11->QueryInterface(IID_Texture, reinterpret_cast(ppTexture)); pTextureD3D11->CreateDefaultViews(); OnCreateDeviceObject(pTextureD3D11); @@ -329,7 +329,7 @@ void RenderDeviceD3D11Impl::CreateTexture3DFromD3DResource(ID3D11Texture3D* pd3d CreateDeviceObject("texture", TexDesc, ppTexture, [&]() // { - TextureBaseD3D11* pTextureD3D11 = NEW_RC_OBJ(m_TexObjAllocator, "Texture3D_D3D11 instance", Texture3D_D3D11)(m_TexViewObjAllocator, this, InitialState, pd3d11Texture); + TextureBaseD3D11* pTextureD3D11{NEW_RC_OBJ(m_TexObjAllocator, "Texture3D_D3D11 instance", Texture3D_D3D11)(m_TexViewObjAllocator, this, InitialState, pd3d11Texture)}; pTextureD3D11->QueryInterface(IID_Texture, reinterpret_cast(ppTexture)); pTextureD3D11->CreateDefaultViews(); OnCreateDeviceObject(pTextureD3D11); @@ -377,7 +377,7 @@ void RenderDeviceD3D11Impl::CreateSampler(const SamplerDesc& SamplerDesc, ISampl m_SamplersRegistry.Find(SamplerDesc, reinterpret_cast(ppSampler)); if (*ppSampler == nullptr) { - SamplerD3D11Impl* pSamplerD3D11(NEW_RC_OBJ(m_SamplerObjAllocator, "SamplerD3D11Impl instance", SamplerD3D11Impl)(this, SamplerDesc)); + SamplerD3D11Impl* pSamplerD3D11{NEW_RC_OBJ(m_SamplerObjAllocator, "SamplerD3D11Impl instance", SamplerD3D11Impl)(this, SamplerDesc)}; pSamplerD3D11->QueryInterface(IID_Sampler, reinterpret_cast(ppSampler)); OnCreateDeviceObject(pSamplerD3D11); m_SamplersRegistry.Add(SamplerDesc, *ppSampler); @@ -385,26 +385,27 @@ void RenderDeviceD3D11Impl::CreateSampler(const SamplerDesc& SamplerDesc, ISampl }); } -void RenderDeviceD3D11Impl::CreateGraphicsPipelineState(const GraphicsPipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState) +template +void RenderDeviceD3D11Impl::CreatePipelineState(const PSOCreateInfoType& PSOCreateInfo, IPipelineState** ppPipelineState) { CreateDeviceObject("Pipeline state", PSOCreateInfo.PSODesc, ppPipelineState, [&]() // { - PipelineStateD3D11Impl* pPipelineStateD3D11(NEW_RC_OBJ(m_PSOAllocator, "PipelineStateD3D11Impl instance", PipelineStateD3D11Impl)(this, PSOCreateInfo)); + PipelineStateD3D11Impl* pPipelineStateD3D11{NEW_RC_OBJ(m_PSOAllocator, "PipelineStateD3D11Impl instance", PipelineStateD3D11Impl)(this, PSOCreateInfo)}; pPipelineStateD3D11->QueryInterface(IID_PipelineState, reinterpret_cast(ppPipelineState)); OnCreateDeviceObject(pPipelineStateD3D11); }); } + +void RenderDeviceD3D11Impl::CreateGraphicsPipelineState(const GraphicsPipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState) +{ + CreatePipelineState(PSOCreateInfo, ppPipelineState); +} + void RenderDeviceD3D11Impl::CreateComputePipelineState(const ComputePipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState) { - CreateDeviceObject("Pipeline state", PSOCreateInfo.PSODesc, ppPipelineState, - [&]() // - { - PipelineStateD3D11Impl* pPipelineStateD3D11(NEW_RC_OBJ(m_PSOAllocator, "PipelineStateD3D11Impl instance", PipelineStateD3D11Impl)(this, PSOCreateInfo)); - pPipelineStateD3D11->QueryInterface(IID_PipelineState, reinterpret_cast(ppPipelineState)); - OnCreateDeviceObject(pPipelineStateD3D11); - }); + CreatePipelineState(PSOCreateInfo, ppPipelineState); } void RenderDeviceD3D11Impl::CreateFence(const FenceDesc& Desc, IFence** ppFence) @@ -412,7 +413,7 @@ void RenderDeviceD3D11Impl::CreateFence(const FenceDesc& Desc, IFence** ppFence) CreateDeviceObject("Fence", Desc, ppFence, [&]() // { - FenceD3D11Impl* pFenceD3D11(NEW_RC_OBJ(m_FenceAllocator, "FenceD3D11Impl instance", FenceD3D11Impl)(this, Desc)); + FenceD3D11Impl* pFenceD3D11{NEW_RC_OBJ(m_FenceAllocator, "FenceD3D11Impl instance", FenceD3D11Impl)(this, Desc)}; pFenceD3D11->QueryInterface(IID_Fence, reinterpret_cast(ppFence)); OnCreateDeviceObject(pFenceD3D11); }); @@ -423,7 +424,7 @@ void RenderDeviceD3D11Impl::CreateQuery(const QueryDesc& Desc, IQuery** ppQuery) CreateDeviceObject("Query", Desc, ppQuery, [&]() // { - QueryD3D11Impl* pQueryD3D11(NEW_RC_OBJ(m_QueryAllocator, "QueryD3D11Impl instance", QueryD3D11Impl)(this, Desc)); + QueryD3D11Impl* pQueryD3D11{NEW_RC_OBJ(m_QueryAllocator, "QueryD3D11Impl instance", QueryD3D11Impl)(this, Desc)}; pQueryD3D11->QueryInterface(IID_Query, reinterpret_cast(ppQuery)); OnCreateDeviceObject(pQueryD3D11); }); @@ -434,7 +435,7 @@ void RenderDeviceD3D11Impl::CreateRenderPass(const RenderPassDesc& Desc, IRender CreateDeviceObject("RenderPass", Desc, ppRenderPass, [&]() // { - RenderPassD3D11Impl* pRenderPassD3D11(NEW_RC_OBJ(m_RenderPassAllocator, "RenderPassD3D11Impl instance", RenderPassD3D11Impl)(this, Desc)); + RenderPassD3D11Impl* pRenderPassD3D11{NEW_RC_OBJ(m_RenderPassAllocator, "RenderPassD3D11Impl instance", RenderPassD3D11Impl)(this, Desc)}; pRenderPassD3D11->QueryInterface(IID_RenderPass, reinterpret_cast(ppRenderPass)); OnCreateDeviceObject(pRenderPassD3D11); }); @@ -445,7 +446,7 @@ void RenderDeviceD3D11Impl::CreateFramebuffer(const FramebufferDesc& Desc, IFram CreateDeviceObject("Framebuffer", Desc, ppFramebuffer, [&]() // { - FramebufferD3D11Impl* pFramebufferD3D11(NEW_RC_OBJ(m_FramebufferAllocator, "FramebufferD3D11Impl instance", FramebufferD3D11Impl)(this, Desc)); + FramebufferD3D11Impl* pFramebufferD3D11{NEW_RC_OBJ(m_FramebufferAllocator, "FramebufferD3D11Impl instance", FramebufferD3D11Impl)(this, Desc)}; pFramebufferD3D11->QueryInterface(IID_Framebuffer, reinterpret_cast(ppFramebuffer)); OnCreateDeviceObject(pFramebufferD3D11); }); -- cgit v1.2.3 From a520bf4f9748d20e432bcf7994be3148d0b75d54 Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 19 Oct 2020 10:21:30 -0700 Subject: Renamed static sampler to immutable sampler (API240076) --- .../include/PipelineStateD3D11Impl.hpp | 12 +++--- .../src/PipelineStateD3D11Impl.cpp | 47 +++++++++++----------- .../src/ShaderResourceBindingD3D11Impl.cpp | 2 +- .../src/ShaderResourceLayoutD3D11.cpp | 42 +++++++++---------- 4 files changed, 52 insertions(+), 51 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp index eb184710..0fa62606 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp @@ -134,7 +134,7 @@ public: const ShaderD3D11Impl* GetShaderByType(SHADER_TYPE ShaderType) const; const ShaderD3D11Impl* GetShader(Uint32 Index) const; - void SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache, Uint32 ShaderInd) const; + void SetImmutableSamplers(ShaderResourceCacheD3D11& ResourceCache, Uint32 ShaderInd) const; private: template @@ -166,20 +166,20 @@ private: // indexed by the shader type pipeline index (returned by GetShaderTypePipelineIndex) std::array m_ResourceLayoutIndex = {-1, -1, -1, -1, -1}; - std::array m_StaticSamplerOffsets = {}; - struct StaticSamplerInfo + std::array m_ImmutableSamplerOffsets = {}; + struct ImmutableSamplerInfo { const D3DShaderResourceAttribs& Attribs; RefCntAutoPtr pSampler; - StaticSamplerInfo(const D3DShaderResourceAttribs& _Attribs, - RefCntAutoPtr _pSampler) : + ImmutableSamplerInfo(const D3DShaderResourceAttribs& _Attribs, + RefCntAutoPtr _pSampler) : // clang-format off Attribs {_Attribs}, pSampler {std::move(_pSampler)} // clang-format on {} }; - std::vector> m_StaticSamplers; + std::vector> m_ImmutableSamplers; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index 292a9323..722318f2 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -74,7 +74,7 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* CreateInfo }, m_SRBMemAllocator{GetRawAllocator()}, - m_StaticSamplers (STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")) + m_ImmutableSamplers (STD_ALLOCATOR_RAW_MEM(ImmutableSamplerInfo, GetRawAllocator(), "Allocator for vector")) // clang-format on { auto MemPool = InitInternalObjects(CreateInfo); @@ -153,7 +153,7 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* CreateInfo }, m_SRBMemAllocator{GetRawAllocator()}, - m_StaticSamplers (STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")) + m_ImmutableSamplers(STD_ALLOCATOR_RAW_MEM(ImmutableSamplerInfo, GetRawAllocator(), "Allocator for vector")) // clang-format on { auto MemPool = InitInternalObjects(CreateInfo); @@ -209,11 +209,11 @@ void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& } ShaderResources::DvpVerifyResourceLayout(ResourceLayout, pResources, GetNumShaderStages(), (CreateInfo.Flags & PSO_CREATE_FLAG_IGNORE_MISSING_VARIABLES) == 0, - (CreateInfo.Flags & PSO_CREATE_FLAG_IGNORE_MISSING_STATIC_SAMPLERS) == 0); + (CreateInfo.Flags & PSO_CREATE_FLAG_IGNORE_MISSING_IMMUTABLE_SAMPLERS) == 0); } #endif - decltype(m_StaticSamplers) StaticSamplers(STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector")); + decltype(m_ImmutableSamplers) ImmutableSamplers(STD_ALLOCATOR_RAW_MEM(ImmutableSamplerInfo, GetRawAllocator(), "Allocator for vector")); std::array ShaderResLayoutDataSizes = {}; std::array ShaderResCacheDataSizes = {}; for (Uint32 s = 0; s < ShaderStages.size(); ++s) @@ -243,21 +243,22 @@ void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& }; // clang-format on - // Initialize static samplers + // Initialize immutable samplers for (Uint32 sam = 0; sam < ShaderResources.GetNumSamplers(); ++sam) { - const auto& SamplerAttribs = ShaderResources.GetSampler(sam); - constexpr bool LogStaticSamplerArrayError = true; - auto SrcStaticSamplerInd = ShaderResources.FindStaticSampler(SamplerAttribs, ResourceLayout, LogStaticSamplerArrayError); - if (SrcStaticSamplerInd >= 0) + const auto& SamplerAttribs = ShaderResources.GetSampler(sam); + constexpr bool LogImtblSamplerArrayError = true; + auto SrcImtblSamplerInd = ShaderResources.FindImmutableSampler(SamplerAttribs, ResourceLayout, LogImtblSamplerArrayError); + if (SrcImtblSamplerInd >= 0) { - const auto& SrcStaticSamplerInfo = ResourceLayout.StaticSamplers[SrcStaticSamplerInd]; - RefCntAutoPtr pStaticSampler; - GetDevice()->CreateSampler(SrcStaticSamplerInfo.Desc, &pStaticSampler); - StaticSamplers.emplace_back(SamplerAttribs, std::move(pStaticSampler)); + const auto& SrcImtblSamplerInfo = ResourceLayout.ImmutableSamplers[SrcImtblSamplerInd]; + + RefCntAutoPtr pImbtlSampler; + GetDevice()->CreateSampler(SrcImtblSamplerInfo.Desc, &pImbtlSampler); + ImmutableSamplers.emplace_back(SamplerAttribs, std::move(pImbtlSampler)); } } - m_StaticSamplerOffsets[s + 1] = static_cast(StaticSamplers.size()); + m_ImmutableSamplerOffsets[s + 1] = static_cast(ImmutableSamplers.size()); if (m_Desc.SRBAllocationGranularity > 1) { @@ -279,14 +280,14 @@ void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& m_SRBMemAllocator.Initialize(m_Desc.SRBAllocationGranularity, GetNumShaderStages(), ShaderResLayoutDataSizes.data(), GetNumShaderStages(), ShaderResCacheDataSizes.data()); } - m_StaticSamplers.reserve(StaticSamplers.size()); - for (auto& Sam : StaticSamplers) - m_StaticSamplers.emplace_back(std::move(Sam)); + m_ImmutableSamplers.reserve(ImmutableSamplers.size()); + for (auto& ImtblSam : ImmutableSamplers) + m_ImmutableSamplers.emplace_back(std::move(ImtblSam)); for (Uint32 s = 0; s < GetNumShaderStages(); ++s) { - // Initialize static samplers in the static resource cache to avoid warning messages - SetStaticSamplers(m_pStaticResourceCaches[s], s); + // Initialize immutable samplers in the static resource cache to avoid warning messages + SetImmutableSamplers(m_pStaticResourceCaches[s], s); } } @@ -431,15 +432,15 @@ IShaderResourceVariable* PipelineStateD3D11Impl::GetStaticVariableByIndex(SHADER return m_pStaticResourceLayouts[LayoutInd].GetShaderVariable(Index); } -void PipelineStateD3D11Impl::SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache, Uint32 ShaderInd) const +void PipelineStateD3D11Impl::SetImmutableSamplers(ShaderResourceCacheD3D11& ResourceCache, Uint32 ShaderInd) const { auto NumCachedSamplers = ResourceCache.GetSamplerCount(); - for (Uint32 s = m_StaticSamplerOffsets[ShaderInd]; s < m_StaticSamplerOffsets[ShaderInd + 1]; ++s) + for (Uint32 s = m_ImmutableSamplerOffsets[ShaderInd]; s < m_ImmutableSamplerOffsets[ShaderInd + 1]; ++s) { - auto& SamplerInfo = m_StaticSamplers[s]; + auto& SamplerInfo = m_ImmutableSamplers[s]; const auto& SamAttribs = SamplerInfo.Attribs; auto* pSamplerD3D11Impl = SamplerInfo.pSampler.RawPtr(); - // Limiting EndBindPoint is required when initializing static samplers in a Shader's static cache + // Limiting EndBindPoint is required when initializing immutable samplers in a Shader's static cache auto EndBindPoint = std::min(static_cast(SamAttribs.BindPoint) + SamAttribs.BindCount, NumCachedSamplers); for (Uint32 BindPoint = SamAttribs.BindPoint; BindPoint < EndBindPoint; ++BindPoint) ResourceCache.SetSampler(BindPoint, pSamplerD3D11Impl); diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp index 88c7b77f..5e95db97 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp @@ -177,7 +177,7 @@ void ShaderResourceBindingD3D11Impl::InitializeStaticResources(const IPipelineSt } #endif StaticResLayout.CopyResources(m_pBoundResourceCaches[shader]); - pPSOD3D11->SetStaticSamplers(m_pBoundResourceCaches[shader], shader); + pPSOD3D11->SetImmutableSamplers(m_pBoundResourceCaches[shader], shader); } m_bIsStaticResourcesBound = true; diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp index 723e190a..c1e6e65a 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp @@ -86,9 +86,9 @@ size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, Uint32 NumAllowedTypes) { - // Skip static samplers as they are initialized directly in the resource cache by the PSO - constexpr bool CountStaticSamplers = false; - auto ResCounters = SrcResources.CountResources(ResourceLayout, AllowedVarTypes, NumAllowedTypes, CountStaticSamplers); + // Skip immutable samplers as they are initialized directly in the resource cache by the PSO + constexpr bool CountImtblSamplers = false; + auto ResCounters = SrcResources.CountResources(ResourceLayout, AllowedVarTypes, NumAllowedTypes, CountImtblSamplers); // clang-format off auto MemSize = ResCounters.NumCBs * sizeof(ConstBuffBindInfo) + ResCounters.NumTexSRVs * sizeof(TexSRVBindInfo) + @@ -120,9 +120,9 @@ ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& const auto AllowedTypeBits = GetAllowedTypeBits(VarTypes, NumVarTypes); // Count total number of resources of allowed types - // Skip static samplers as they are initialized directly in the resource cache by the PSO - constexpr bool CountStaticSamplers = false; - auto ResCounters = m_pResources->CountResources(ResourceLayout, VarTypes, NumVarTypes, CountStaticSamplers); + // Skip immutable samplers as they are initialized directly in the resource cache by the PSO + constexpr bool CountImtblSamplers = false; + auto ResCounters = m_pResources->CountResources(ResourceLayout, VarTypes, NumVarTypes, CountImtblSamplers); // Initialize offsets size_t CurrentOffset = 0; @@ -192,12 +192,12 @@ ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& auto VarType = m_pResources->FindVariableType(Sampler, ResourceLayout); if (IsAllowedType(VarType, AllowedTypeBits)) { - // Constructor of PipelineStateD3D11Impl initializes static samplers and will log the error, if any - constexpr bool LogStaticSamplerArrayError = false; - auto StaticSamplerInd = m_pResources->FindStaticSampler(Sampler, ResourceLayout, LogStaticSamplerArrayError); - if (StaticSamplerInd >= 0) + // Constructor of PipelineStateD3D11Impl initializes immutable samplers and will log the error, if any + constexpr bool LogImtblSamplerArrayError = false; + auto ImtblSamplerInd = m_pResources->FindImmutableSampler(Sampler, ResourceLayout, LogImtblSamplerArrayError); + if (ImtblSamplerInd >= 0) { - // Skip static samplers as they are initialized directly in the resource cache by the PSO + // Skip immutble samplers as they are initialized directly in the resource cache by the PSO return; } // Initialize current sampler in place, increment sampler counter @@ -241,10 +241,10 @@ ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& AssignedSamplerIndex = TexSRVBindInfo::InvalidSamplerIndex; #ifdef DILIGENT_DEBUG // Shader error will be logged by the PipelineStateD3D11Impl - constexpr bool LogStaticSamplerArrayError = false; - if (m_pResources->FindStaticSampler(AssignedSamplerAttribs, ResourceLayout, LogStaticSamplerArrayError) < 0) + constexpr bool LogImtblSamplerArrayError = false; + if (m_pResources->FindImmutableSampler(AssignedSamplerAttribs, ResourceLayout, LogImtblSamplerArrayError) < 0) { - UNEXPECTED("Unable to find non-static sampler assigned to texture SRV '", TexSRV.Name, "'."); + UNEXPECTED("Unable to find non-immutable sampler assigned to texture SRV '", TexSRV.Name, "'."); } #endif } @@ -252,10 +252,10 @@ ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& { #ifdef DILIGENT_DEBUG // Shader error will be logged by the PipelineStateD3D11Impl - constexpr bool LogStaticSamplerArrayError = false; - if (m_pResources->FindStaticSampler(AssignedSamplerAttribs, ResourceLayout, LogStaticSamplerArrayError) >= 0) + constexpr bool LogImtblSamplerArrayError = false; + if (m_pResources->FindImmutableSampler(AssignedSamplerAttribs, ResourceLayout, LogImtblSamplerArrayError) >= 0) { - UNEXPECTED("Static sampler '", AssignedSamplerAttribs.Name, "' is assigned to texture SRV '", TexSRV.Name, "'."); + UNEXPECTED("Immutable sampler '", AssignedSamplerAttribs.Name, "' is assigned to texture SRV '", TexSRV.Name, "'."); } #endif } @@ -412,7 +412,7 @@ void ShaderResourceLayoutD3D11::CopyResources(ShaderResourceCacheD3D11& DstCache [&](const SamplerBindInfo& sam) // { - //VERIFY(!sam.IsStaticSampler, "Variables are not created for static samplers"); + //VERIFY(!sam.IsImmutableSampler, "Variables are not created for immutable samplers"); for (auto SamSlot = sam.m_Attribs.BindPoint; SamSlot < sam.m_Attribs.BindPoint + sam.m_Attribs.BindCount; ++SamSlot) { VERIFY_EXPR(SamSlot < m_ResourceCache.GetSamplerCount() && SamSlot < DstCache.GetSamplerCount()); @@ -460,7 +460,7 @@ void ShaderResourceLayoutD3D11::TexSRVBindInfo::BindResource(IDeviceObject* pVie if (ValidSamplerAssigned()) { auto& Sampler = m_ParentResLayout.GetResource(SamplerIndex); - //VERIFY(!Sampler.IsStaticSampler, "Static samplers are not assigned to texture SRVs as they are initialized directly in the shader resource cache"); + //VERIFY(!Sampler.IsImmutableSampler, "Immutable samplers are not assigned to texture SRVs as they are initialized directly in the shader resource cache"); VERIFY_EXPR(Sampler.m_Attribs.BindCount == m_Attribs.BindCount || Sampler.m_Attribs.BindCount == 1); auto SamplerBindPoint = Sampler.m_Attribs.BindPoint + (Sampler.m_Attribs.BindCount != 1 ? ArrayIndex : 0); @@ -502,7 +502,7 @@ void ShaderResourceLayoutD3D11::SamplerBindInfo::BindResource(IDeviceObject* pSa "Array index (", ArrayIndex, ") is out of range for variable '", m_Attribs.Name, "'. Max allowed index: ", m_Attribs.BindCount - 1); auto& ResourceCache = m_ParentResLayout.m_ResourceCache; - //VERIFY(!IsStaticSampler, "Cannot bind sampler to a static sampler"); + //VERIFY(!IsImmutableSampler, "Cannot bind sampler to an immutable sampler"); // We cannot use ValidatedCast<> here as the resource retrieved from the // resource mapping can be of wrong type @@ -737,7 +737,7 @@ IShaderResourceVariable* ShaderResourceLayoutD3D11::GetShaderVariable(const Char if (!m_pResources->IsUsingCombinedTextureSamplers()) { - // Static samplers are never created in the resource layout + // Immutable samplers are never created in the resource layout if (auto* pSampler = GetResourceByName(Name)) return pSampler; } -- cgit v1.2.3 From e5f36378de4a7258fcc9d6ca7a349df1136b7444 Mon Sep 17 00:00:00 2001 From: assiduous Date: Mon, 19 Oct 2020 12:08:40 -0700 Subject: Renamed USAGE_STATIC to USAGE_IMMUTABLE (API240077) --- Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.hpp | 4 ++-- Graphics/GraphicsEngineD3D11/src/BufferD3D11Impl.cpp | 4 ++-- Graphics/GraphicsEngineD3D11/src/TextureBaseD3D11.cpp | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.hpp b/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.hpp index 5a87cfd7..96de94f9 100644 --- a/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.hpp +++ b/Graphics/GraphicsEngineD3D11/include/D3D11TypeConversions.hpp @@ -79,7 +79,7 @@ inline D3D11_USAGE UsageToD3D11Usage(USAGE Usage) switch (Usage) { // clang-format off - case USAGE_STATIC: return D3D11_USAGE_IMMUTABLE; + case USAGE_IMMUTABLE: return D3D11_USAGE_IMMUTABLE; case USAGE_DEFAULT: return D3D11_USAGE_DEFAULT; case USAGE_DYNAMIC: return D3D11_USAGE_DYNAMIC; case USAGE_STAGING: return D3D11_USAGE_STAGING; @@ -93,7 +93,7 @@ inline USAGE D3D11UsageToUsage(D3D11_USAGE D3D11Usage) switch (D3D11Usage) { // clang-format off - case D3D11_USAGE_IMMUTABLE: return USAGE_STATIC; + case D3D11_USAGE_IMMUTABLE: return USAGE_IMMUTABLE; case D3D11_USAGE_DEFAULT: return USAGE_DEFAULT; case D3D11_USAGE_DYNAMIC: return USAGE_DYNAMIC; case D3D11_USAGE_STAGING: return USAGE_STAGING; diff --git a/Graphics/GraphicsEngineD3D11/src/BufferD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/BufferD3D11Impl.cpp index de7c843d..16e77806 100644 --- a/Graphics/GraphicsEngineD3D11/src/BufferD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/BufferD3D11Impl.cpp @@ -62,8 +62,8 @@ BufferD3D11Impl::BufferD3D11Impl(IReferenceCounters* pRefCounters, LOG_ERROR_AND_THROW("Unified resources are not supported in Direct3D11"); } - if (m_Desc.Usage == USAGE_STATIC) - VERIFY(pBuffData != nullptr && pBuffData->pData != nullptr, "Initial data must not be null for static buffers"); + if (m_Desc.Usage == USAGE_IMMUTABLE) + VERIFY(pBuffData != nullptr && pBuffData->pData != nullptr, "Initial data must not be null for immutable buffers"); if (m_Desc.BindFlags & BIND_UNIFORM_BUFFER) { diff --git a/Graphics/GraphicsEngineD3D11/src/TextureBaseD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/TextureBaseD3D11.cpp index 7a744b57..0e9b217b 100644 --- a/Graphics/GraphicsEngineD3D11/src/TextureBaseD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/TextureBaseD3D11.cpp @@ -51,8 +51,8 @@ TextureBaseD3D11::TextureBaseD3D11(IReferenceCounters* pRefCounters, } // clang-format on { - if (m_Desc.Usage == USAGE_STATIC && (pInitData == nullptr || pInitData->pSubResources == nullptr)) - LOG_ERROR_AND_THROW("Static textures must be initialized with data at creation time: pInitData can't be null"); + if (m_Desc.Usage == USAGE_IMMUTABLE && (pInitData == nullptr || pInitData->pSubResources == nullptr)) + LOG_ERROR_AND_THROW("Immutable textures must be initialized with data at creation time: pInitData can't be null"); SetState(RESOURCE_STATE_UNDEFINED); } -- cgit v1.2.3 From 13b0b54987db2684e46e337d2e5be5b81366083b Mon Sep 17 00:00:00 2001 From: assiduous Date: Tue, 20 Oct 2020 13:26:21 -0700 Subject: Improved exception safety of pipeline state object construction --- .../include/PipelineStateD3D11Impl.hpp | 4 +- .../include/ShaderResourceCacheD3D11.hpp | 2 +- .../include/ShaderResourceLayoutD3D11.hpp | 23 ++- .../src/PipelineStateD3D11Impl.cpp | 210 ++++++++++++--------- .../src/ShaderResourceBindingD3D11Impl.cpp | 23 +-- .../src/ShaderResourceCacheD3D11.cpp | 93 +++++---- .../src/ShaderResourceLayoutD3D11.cpp | 23 +-- 7 files changed, 201 insertions(+), 177 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp index 0fa62606..520e9bb0 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp @@ -138,11 +138,13 @@ public: private: template - LinearAllocator InitInternalObjects(const PSOCreateInfoType& CreateInfo); + void InitInternalObjects(const PSOCreateInfoType& CreateInfo); void InitResourceLayouts(const PipelineStateCreateInfo& CreateInfo, const std::vector>& ShaderStages); + void Destruct(); + CComPtr m_pd3d11BlendState; CComPtr m_pd3d11RasterizerState; CComPtr m_pd3d11DepthStencilState; diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp index dc70eec1..7aa7126d 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp @@ -51,7 +51,7 @@ namespace Diligent class ShaderResourceCacheD3D11 { public: - ShaderResourceCacheD3D11() + ShaderResourceCacheD3D11() noexcept {} ~ShaderResourceCacheD3D11(); diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.hpp b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.hpp index 4b4d13b2..9b2c2d9b 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.hpp +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceLayoutD3D11.hpp @@ -47,14 +47,19 @@ namespace Diligent class ShaderResourceLayoutD3D11 { public: - ShaderResourceLayoutD3D11(IObject& Owner, - std::shared_ptr pSrcResources, - const PipelineResourceLayoutDesc& ResourceLayout, - const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, - Uint32 NumVarTypes, - ShaderResourceCacheD3D11& ResourceCache, - IMemoryAllocator& ResCacheDataAllocator, - IMemoryAllocator& ResLayoutDataAllocator); + ShaderResourceLayoutD3D11(IObject& Owner, + ShaderResourceCacheD3D11& ResourceCache) noexcept : + m_Owner{Owner}, + m_ResourceCache{ResourceCache} + { + } + + void Initialize(std::shared_ptr pSrcResources, + const PipelineResourceLayoutDesc& ResourceLayout, + const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, + Uint32 NumVarTypes, + IMemoryAllocator& ResCacheDataAllocator, + IMemoryAllocator& ResLayoutDataAllocator); ~ShaderResourceLayoutD3D11(); // clang-format off @@ -68,7 +73,7 @@ public: static size_t GetRequiredMemorySize(const ShaderResourcesD3D11& SrcResources, const PipelineResourceLayoutDesc& ResourceLayout, const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes); + Uint32 NumAllowedTypes) noexcept; void CopyResources(ShaderResourceCacheD3D11& DstCache) const; diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index 722318f2..108f149c 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -36,30 +36,42 @@ namespace Diligent { template -LinearAllocator PipelineStateD3D11Impl::InitInternalObjects(const PSOCreateInfoType& CreateInfo) +void PipelineStateD3D11Impl::InitInternalObjects(const PSOCreateInfoType& CreateInfo) { m_ResourceLayoutIndex.fill(-1); std::vector> ShaderStages; ExtractShaders(CreateInfo, ShaderStages); - // Memory must be released if an exception is thrown. + const auto NumShaderStages = GetNumShaderStages(); + VERIFY_EXPR(NumShaderStages > 0 && NumShaderStages == ShaderStages.size()); + LinearAllocator MemPool{GetRawAllocator()}; - MemPool.AddSpace(GetNumShaderStages()); - MemPool.AddSpace(GetNumShaderStages()); + MemPool.AddSpace(NumShaderStages); + MemPool.AddSpace(NumShaderStages); ReserveSpaceForPipelineDesc(CreateInfo, MemPool); MemPool.Reserve(); - m_pStaticResourceLayouts = MemPool.Allocate(GetNumShaderStages()); - m_pStaticResourceCaches = MemPool.Allocate(GetNumShaderStages()); + m_pStaticResourceCaches = MemPool.ConstructArray(NumShaderStages); + + // The memory is now owned by PipelineStateD3D11Impl and will be freed by Destruct(). + auto* Ptr = MemPool.ReleaseOwnership(); + VERIFY_EXPR(Ptr == m_pStaticResourceCaches); + (void)Ptr; + + m_pStaticResourceLayouts = MemPool.Allocate(NumShaderStages); + for (Uint32 i = 0; i < NumShaderStages; ++i) + new (m_pStaticResourceLayouts + i) ShaderResourceLayoutD3D11{*this, m_pStaticResourceCaches[i]}; // noexcept InitializePipelineDesc(CreateInfo, MemPool); - InitResourceLayouts(CreateInfo, ShaderStages); - return MemPool; + // It is important to construct all objects before initializing them because if an exception is thrown, + // destructors will be called for all objects + + InitResourceLayouts(CreateInfo, ShaderStages); } @@ -77,9 +89,11 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* m_ImmutableSamplers (STD_ALLOCATOR_RAW_MEM(ImmutableSamplerInfo, GetRawAllocator(), "Allocator for vector")) // clang-format on { - auto MemPool = InitInternalObjects(CreateInfo); + try + { + InitInternalObjects(CreateInfo); - auto& GraphicsPipeline = GetGraphicsPipelineDesc(); + auto& GraphicsPipeline = GetGraphicsPipelineDesc(); #define INIT_SHADER(ShortName, ExpectedType) \ do \ @@ -94,52 +108,55 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* HashCombine(m_ShaderResourceLayoutHash, pShader->GetD3D11Resources()->GetHash()); \ } while (false) - INIT_SHADER(VS, SHADER_TYPE_VERTEX); - INIT_SHADER(PS, SHADER_TYPE_PIXEL); - INIT_SHADER(GS, SHADER_TYPE_GEOMETRY); - INIT_SHADER(DS, SHADER_TYPE_DOMAIN); - INIT_SHADER(HS, SHADER_TYPE_HULL); + INIT_SHADER(VS, SHADER_TYPE_VERTEX); + INIT_SHADER(PS, SHADER_TYPE_PIXEL); + INIT_SHADER(GS, SHADER_TYPE_GEOMETRY); + INIT_SHADER(DS, SHADER_TYPE_DOMAIN); + INIT_SHADER(HS, SHADER_TYPE_HULL); #undef INIT_SHADER - if (m_pVS == nullptr) - { - LOG_ERROR_AND_THROW("Vertex shader is null"); - } + if (m_pVS == nullptr) + { + LOG_ERROR_AND_THROW("Vertex shader is null"); + } - auto* pDeviceD3D11 = pRenderDeviceD3D11->GetD3D11Device(); + auto* pDeviceD3D11 = pRenderDeviceD3D11->GetD3D11Device(); - D3D11_BLEND_DESC D3D11BSDesc = {}; - BlendStateDesc_To_D3D11_BLEND_DESC(GraphicsPipeline.BlendDesc, D3D11BSDesc); - CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateBlendState(&D3D11BSDesc, &m_pd3d11BlendState), - "Failed to create D3D11 blend state object"); + D3D11_BLEND_DESC D3D11BSDesc = {}; + BlendStateDesc_To_D3D11_BLEND_DESC(GraphicsPipeline.BlendDesc, D3D11BSDesc); + CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateBlendState(&D3D11BSDesc, &m_pd3d11BlendState), + "Failed to create D3D11 blend state object"); - D3D11_RASTERIZER_DESC D3D11RSDesc = {}; - RasterizerStateDesc_To_D3D11_RASTERIZER_DESC(GraphicsPipeline.RasterizerDesc, D3D11RSDesc); - CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateRasterizerState(&D3D11RSDesc, &m_pd3d11RasterizerState), - "Failed to create D3D11 rasterizer state"); + D3D11_RASTERIZER_DESC D3D11RSDesc = {}; + RasterizerStateDesc_To_D3D11_RASTERIZER_DESC(GraphicsPipeline.RasterizerDesc, D3D11RSDesc); + CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateRasterizerState(&D3D11RSDesc, &m_pd3d11RasterizerState), + "Failed to create D3D11 rasterizer state"); - D3D11_DEPTH_STENCIL_DESC D3D11DSSDesc = {}; - DepthStencilStateDesc_To_D3D11_DEPTH_STENCIL_DESC(GraphicsPipeline.DepthStencilDesc, D3D11DSSDesc); - CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateDepthStencilState(&D3D11DSSDesc, &m_pd3d11DepthStencilState), - "Failed to create D3D11 depth stencil state"); + D3D11_DEPTH_STENCIL_DESC D3D11DSSDesc = {}; + DepthStencilStateDesc_To_D3D11_DEPTH_STENCIL_DESC(GraphicsPipeline.DepthStencilDesc, D3D11DSSDesc); + CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateDepthStencilState(&D3D11DSSDesc, &m_pd3d11DepthStencilState), + "Failed to create D3D11 depth stencil state"); - // Create input layout - const auto& InputLayout = GraphicsPipeline.InputLayout; - if (InputLayout.NumElements > 0) - { - std::vector> d311InputElements(STD_ALLOCATOR_RAW_MEM(D3D11_INPUT_ELEMENT_DESC, GetRawAllocator(), "Allocator for vector")); - LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(InputLayout, d311InputElements); + // Create input layout + const auto& InputLayout = GraphicsPipeline.InputLayout; + if (InputLayout.NumElements > 0) + { + std::vector> d311InputElements(STD_ALLOCATOR_RAW_MEM(D3D11_INPUT_ELEMENT_DESC, GetRawAllocator(), "Allocator for vector")); + LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(InputLayout, d311InputElements); - ID3DBlob* pVSByteCode = m_pVS.RawPtr()->GetBytecode(); - if (!pVSByteCode) - LOG_ERROR_AND_THROW("Vertex Shader byte code does not exist"); + ID3DBlob* pVSByteCode = m_pVS.RawPtr()->GetBytecode(); + if (!pVSByteCode) + LOG_ERROR_AND_THROW("Vertex Shader byte code does not exist"); - CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateInputLayout(d311InputElements.data(), static_cast(d311InputElements.size()), pVSByteCode->GetBufferPointer(), pVSByteCode->GetBufferSize(), &m_pd3d11InputLayout), - "Failed to create the Direct3D11 input layout"); + CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateInputLayout(d311InputElements.data(), static_cast(d311InputElements.size()), pVSByteCode->GetBufferPointer(), pVSByteCode->GetBufferSize(), &m_pd3d11InputLayout), + "Failed to create the Direct3D11 input layout"); + } + } + catch (...) + { + Destruct(); + throw; } - - auto* Ptr = MemPool.Release(); - VERIFY_EXPR(Ptr == m_pStaticResourceLayouts); } PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, @@ -156,38 +173,55 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* m_ImmutableSamplers(STD_ALLOCATOR_RAW_MEM(ImmutableSamplerInfo, GetRawAllocator(), "Allocator for vector")) // clang-format on { - auto MemPool = InitInternalObjects(CreateInfo); - - m_pCS = ValidatedCast(CreateInfo.pCS); - if (m_pCS == nullptr) + try { - LOG_ERROR_AND_THROW("Compute shader is null"); - } + InitInternalObjects(CreateInfo); - if (m_pCS->GetDesc().ShaderType != SHADER_TYPE_COMPUTE) + m_pCS = ValidatedCast(CreateInfo.pCS); + if (m_pCS == nullptr) + { + LOG_ERROR_AND_THROW("Compute shader is null"); + } + + if (m_pCS->GetDesc().ShaderType != SHADER_TYPE_COMPUTE) + { + LOG_ERROR_AND_THROW(GetShaderTypeLiteralName(SHADER_TYPE_COMPUTE), " shader is expeceted while ", GetShaderTypeLiteralName(m_pCS->GetDesc().ShaderType), " provided"); + } + m_ShaderResourceLayoutHash = m_pCS->GetD3D11Resources()->GetHash(); + } + catch (...) { - LOG_ERROR_AND_THROW(GetShaderTypeLiteralName(SHADER_TYPE_COMPUTE), " shader is expeceted while ", GetShaderTypeLiteralName(m_pCS->GetDesc().ShaderType), " provided"); + Destruct(); + throw; } - m_ShaderResourceLayoutHash = m_pCS->GetD3D11Resources()->GetHash(); - - auto* Ptr = MemPool.Release(); - VERIFY_EXPR(Ptr == m_pStaticResourceLayouts); } PipelineStateD3D11Impl::~PipelineStateD3D11Impl() { - for (Uint32 s = 0; s < GetNumShaderStages(); ++s) + Destruct(); +} + +void PipelineStateD3D11Impl::Destruct() +{ + if (m_pStaticResourceLayouts != nullptr) { - m_pStaticResourceCaches[s].Destroy(GetRawAllocator()); - m_pStaticResourceCaches[s].~ShaderResourceCacheD3D11(); + for (Uint32 l = 0; l < GetNumShaderStages(); ++l) + { + m_pStaticResourceLayouts[l].~ShaderResourceLayoutD3D11(); + } } - for (Uint32 l = 0; l < GetNumShaderStages(); ++l) + if (m_pStaticResourceCaches != nullptr) { - m_pStaticResourceLayouts[l].~ShaderResourceLayoutD3D11(); + for (Uint32 s = 0; s < GetNumShaderStages(); ++s) + { + m_pStaticResourceCaches[s].Destroy(GetRawAllocator()); + m_pStaticResourceCaches[s].~ShaderResourceCacheD3D11(); + } } - // m_pStaticResourceLayouts and m_pStaticResourceCaches are allocated in contiguous chunks of memory. - if (auto* pRawMem = m_pStaticResourceLayouts) + + // All subobjects are allocated in contiguous chunks of memory. + if (auto* pRawMem = m_pStaticResourceCaches) GetRawAllocator().Free(pRawMem); } @@ -213,42 +247,36 @@ void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& } #endif - decltype(m_ImmutableSamplers) ImmutableSamplers(STD_ALLOCATOR_RAW_MEM(ImmutableSamplerInfo, GetRawAllocator(), "Allocator for vector")); + decltype(m_ImmutableSamplers) ImmutableSamplers{STD_ALLOCATOR_RAW_MEM(ImmutableSamplerInfo, GetRawAllocator(), "Allocator for vector")}; + std::array ShaderResLayoutDataSizes = {}; std::array ShaderResCacheDataSizes = {}; for (Uint32 s = 0; s < ShaderStages.size(); ++s) { - const auto* pShader = ShaderStages[s].second; - const auto& ShaderDesc = pShader->GetDesc(); - const auto& ShaderResources = *pShader->GetD3D11Resources(); - VERIFY_EXPR(ShaderDesc.ShaderType == ShaderResources.GetShaderType()); + const auto* pShader = ShaderStages[s].second; + const auto& ShaderDesc = pShader->GetDesc(); + const auto& Resources = *pShader->GetD3D11Resources(); + VERIFY_EXPR(ShaderDesc.ShaderType == Resources.GetShaderType()); - new (m_pStaticResourceCaches + s) ShaderResourceCacheD3D11; - // Do not initialize the cache as this will be performed by the resource layout + // The cache will be initialized by the resource layout // Shader resource layout will only contain dynamic and mutable variables const SHADER_RESOURCE_VARIABLE_TYPE StaticVarTypes[] = {SHADER_RESOURCE_VARIABLE_TYPE_STATIC}; - // clang-format off - new (m_pStaticResourceLayouts + s) - ShaderResourceLayoutD3D11 - { - *this, - pShader->GetD3D11Resources(), - m_Desc.ResourceLayout, - StaticVarTypes, - _countof(StaticVarTypes), - m_pStaticResourceCaches[s], - GetRawAllocator(), - GetRawAllocator() - }; - // clang-format on + m_pStaticResourceLayouts[s].Initialize( + pShader->GetD3D11Resources(), + m_Desc.ResourceLayout, + StaticVarTypes, + _countof(StaticVarTypes), + GetRawAllocator(), + GetRawAllocator() // + ); // Initialize immutable samplers - for (Uint32 sam = 0; sam < ShaderResources.GetNumSamplers(); ++sam) + for (Uint32 sam = 0; sam < Resources.GetNumSamplers(); ++sam) { - const auto& SamplerAttribs = ShaderResources.GetSampler(sam); + const auto& SamplerAttribs = Resources.GetSampler(sam); constexpr bool LogImtblSamplerArrayError = true; - auto SrcImtblSamplerInd = ShaderResources.FindImmutableSampler(SamplerAttribs, ResourceLayout, LogImtblSamplerArrayError); + auto SrcImtblSamplerInd = Resources.FindImmutableSampler(SamplerAttribs, ResourceLayout, LogImtblSamplerArrayError); if (SrcImtblSamplerInd >= 0) { const auto& SrcImtblSamplerInfo = ResourceLayout.ImmutableSamplers[SrcImtblSamplerInd]; @@ -267,8 +295,8 @@ void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC // }; - ShaderResLayoutDataSizes[s] = ShaderResourceLayoutD3D11::GetRequiredMemorySize(ShaderResources, ResourceLayout, SRBVarTypes, _countof(SRBVarTypes)); - ShaderResCacheDataSizes[s] = ShaderResourceCacheD3D11::GetRequriedMemorySize(ShaderResources); + ShaderResLayoutDataSizes[s] = ShaderResourceLayoutD3D11::GetRequiredMemorySize(Resources, ResourceLayout, SRBVarTypes, _countof(SRBVarTypes)); + ShaderResCacheDataSizes[s] = ShaderResourceCacheD3D11::GetRequriedMemorySize(Resources); } auto ShaderInd = GetShaderTypePipelineIndex(ShaderDesc.ShaderType, m_Desc.PipelineType); diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp index 5e95db97..f7ce059b 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp @@ -77,20 +77,15 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl(IReferenceCounter // Shader resource layout will only contain dynamic and mutable variables // http://diligentgraphics.com/diligent-engine/architecture/d3d11/shader-resource-cache#Shader-Resource-Cache-Initialization SHADER_RESOURCE_VARIABLE_TYPE VarTypes[] = {SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC}; - // clang-format off - new (m_pResourceLayouts + s) - ShaderResourceLayoutD3D11 - { - *this, - pShaderD3D11->GetD3D11Resources(), - PSODesc.ResourceLayout, - VarTypes, - _countof(VarTypes), - m_pBoundResourceCaches[s], - ResCacheDataAllocator, - ResLayoutDataAllocator - }; - // clang-format on + new (m_pResourceLayouts + s) ShaderResourceLayoutD3D11{*this, m_pBoundResourceCaches[s]}; + m_pResourceLayouts[s].Initialize( + pShaderD3D11->GetD3D11Resources(), + PSODesc.ResourceLayout, + VarTypes, + _countof(VarTypes), + ResCacheDataAllocator, + ResLayoutDataAllocator // + ); const auto ShaderType = pShaderD3D11->GetDesc().ShaderType; const auto ShaderInd = GetShaderTypePipelineIndex(ShaderType, PSODesc.PipelineType); diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp index 28086285..3aaf82b7 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp @@ -148,61 +148,60 @@ void ShaderResourceCacheD3D11::Initialize(Uint32 CBCount, Uint32 SRVCount, Uint3 void ShaderResourceCacheD3D11::Destroy(IMemoryAllocator& MemAllocator) { - VERIFY(IsInitialized(), "Resource cache is not initialized"); + if (!IsInitialized()) + return; + VERIFY(m_pdbgMemoryAllocator == &MemAllocator, "The allocator does not match the one used to create resources"); - if (IsInitialized()) + // Explicitly destory all objects + auto CBCount = GetCBCount(); + if (CBCount != 0) { - // Explicitly destory all objects - auto CBCount = GetCBCount(); - if (CBCount != 0) - { - CachedCB* CBs = nullptr; - ID3D11Buffer** d3d11CBs = nullptr; - GetCBArrays(CBs, d3d11CBs); - for (size_t cb = 0; cb < CBCount; ++cb) - CBs[cb].~CachedCB(); - } + CachedCB* CBs = nullptr; + ID3D11Buffer** d3d11CBs = nullptr; + GetCBArrays(CBs, d3d11CBs); + for (size_t cb = 0; cb < CBCount; ++cb) + CBs[cb].~CachedCB(); + } - auto SRVCount = GetSRVCount(); - if (SRVCount != 0) - { - CachedResource* SRVResources = nullptr; - ID3D11ShaderResourceView** d3d11SRVs = nullptr; - GetSRVArrays(SRVResources, d3d11SRVs); - for (size_t srv = 0; srv < SRVCount; ++srv) - SRVResources[srv].~CachedResource(); - } + auto SRVCount = GetSRVCount(); + if (SRVCount != 0) + { + CachedResource* SRVResources = nullptr; + ID3D11ShaderResourceView** d3d11SRVs = nullptr; + GetSRVArrays(SRVResources, d3d11SRVs); + for (size_t srv = 0; srv < SRVCount; ++srv) + SRVResources[srv].~CachedResource(); + } - auto SamplerCount = GetSamplerCount(); - if (SamplerCount != 0) - { - CachedSampler* Samplers = nullptr; - ID3D11SamplerState** d3d11Samplers = nullptr; - GetSamplerArrays(Samplers, d3d11Samplers); - for (size_t sam = 0; sam < SamplerCount; ++sam) - Samplers[sam].~CachedSampler(); - } + auto SamplerCount = GetSamplerCount(); + if (SamplerCount != 0) + { + CachedSampler* Samplers = nullptr; + ID3D11SamplerState** d3d11Samplers = nullptr; + GetSamplerArrays(Samplers, d3d11Samplers); + for (size_t sam = 0; sam < SamplerCount; ++sam) + Samplers[sam].~CachedSampler(); + } - auto UAVCount = GetUAVCount(); - if (UAVCount != 0) - { - CachedResource* UAVResources = nullptr; - ID3D11UnorderedAccessView** d3d11UAVs = nullptr; - GetUAVArrays(UAVResources, d3d11UAVs); - for (size_t uav = 0; uav < UAVCount; ++uav) - UAVResources[uav].~CachedResource(); - } + auto UAVCount = GetUAVCount(); + if (UAVCount != 0) + { + CachedResource* UAVResources = nullptr; + ID3D11UnorderedAccessView** d3d11UAVs = nullptr; + GetUAVArrays(UAVResources, d3d11UAVs); + for (size_t uav = 0; uav < UAVCount; ++uav) + UAVResources[uav].~CachedResource(); + } - m_SRVOffset = InvalidResourceOffset; - m_SamplerOffset = InvalidResourceOffset; - m_UAVOffset = InvalidResourceOffset; - m_MemoryEndOffset = InvalidResourceOffset; + m_SRVOffset = InvalidResourceOffset; + m_SamplerOffset = InvalidResourceOffset; + m_UAVOffset = InvalidResourceOffset; + m_MemoryEndOffset = InvalidResourceOffset; - if (m_pResourceData != nullptr) - MemAllocator.Free(m_pResourceData); - m_pResourceData = nullptr; - } + if (m_pResourceData != nullptr) + MemAllocator.Free(m_pResourceData); + m_pResourceData = nullptr; } ShaderResourceCacheD3D11::~ShaderResourceCacheD3D11() diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp index c1e6e65a..f7c1958d 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp @@ -84,7 +84,7 @@ ShaderResourceLayoutD3D11::~ShaderResourceLayoutD3D11() size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D11& SrcResources, const PipelineResourceLayoutDesc& ResourceLayout, const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes) + Uint32 NumAllowedTypes) noexcept { // Skip immutable samplers as they are initialized directly in the resource cache by the PSO constexpr bool CountImtblSamplers = false; @@ -101,20 +101,15 @@ size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D } -ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& Owner, - std::shared_ptr pSrcResources, - const PipelineResourceLayoutDesc& ResourceLayout, - const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, - Uint32 NumVarTypes, - ShaderResourceCacheD3D11& ResourceCache, - IMemoryAllocator& ResCacheDataAllocator, - IMemoryAllocator& ResLayoutDataAllocator) : - // clang-format off - m_Owner {Owner}, - m_pResources {std::move(pSrcResources)}, - m_ResourceCache {ResourceCache} -// clang-format on +void ShaderResourceLayoutD3D11::Initialize(std::shared_ptr pSrcResources, + const PipelineResourceLayoutDesc& ResourceLayout, + const SHADER_RESOURCE_VARIABLE_TYPE* VarTypes, + Uint32 NumVarTypes, + IMemoryAllocator& ResCacheDataAllocator, + IMemoryAllocator& ResLayoutDataAllocator) { + m_pResources = std::move(pSrcResources); + // http://diligentgraphics.com/diligent-engine/architecture/d3d11/shader-resource-layout#Shader-Resource-Layout-Initialization const auto AllowedTypeBits = GetAllowedTypeBits(VarTypes, NumVarTypes); -- cgit v1.2.3 From 0c7e8e2efc95362cbc0998558bb41789a3943037 Mon Sep 17 00:00:00 2001 From: assiduous Date: Tue, 20 Oct 2020 14:06:33 -0700 Subject: Improved exception safety of SRB object creation --- .../include/ShaderResourceBindingD3D11Impl.hpp | 2 + .../src/ShaderResourceBindingD3D11Impl.cpp | 134 +++++++++++++-------- 2 files changed, 87 insertions(+), 49 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.hpp index eb97dfdc..21437e0f 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceBindingD3D11Impl.hpp @@ -100,6 +100,8 @@ public: } private: + void Destruct(); + // The caches are indexed by the shader order in the PSO, not shader index ShaderResourceCacheD3D11* m_pBoundResourceCaches = nullptr; ShaderResourceLayoutD3D11* m_pResourceLayouts = nullptr; diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp index f7ce059b..46833c81 100644 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp @@ -31,6 +31,7 @@ #include "DeviceContextD3D11Impl.hpp" #include "RenderDeviceD3D11Impl.hpp" #include "ShaderD3D11Impl.hpp" +#include "LinearAllocator.hpp" namespace Diligent { @@ -49,69 +50,104 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl(IReferenceCounter m_bIsStaticResourcesBound{false} // clang-format on { - m_ResourceLayoutIndex.fill(-1); - m_NumActiveShaders = static_cast(pPSO->GetNumShaderStages()); + try + { + m_ResourceLayoutIndex.fill(-1); + m_NumActiveShaders = static_cast(pPSO->GetNumShaderStages()); - // clang-format off - m_pResourceLayouts = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11", ShaderResourceLayoutD3D11, m_NumActiveShaders); - m_pBoundResourceCaches = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceCacheD3D11", ShaderResourceCacheD3D11, m_NumActiveShaders); - // clang-format on + LinearAllocator MemPool{GetRawAllocator()}; + MemPool.AddSpace(m_NumActiveShaders); + MemPool.AddSpace(m_NumActiveShaders); + + MemPool.Reserve(); + + m_pBoundResourceCaches = MemPool.ConstructArray(m_NumActiveShaders); + + // The memory is now owned by ShaderResourceBindingD3D11Impl and will be freed by Destruct(). + auto* Ptr = MemPool.ReleaseOwnership(); + VERIFY_EXPR(Ptr == m_pBoundResourceCaches); + (void)Ptr; - const auto& PSODesc = pPSO->GetDesc(); + m_pResourceLayouts = MemPool.Allocate(m_NumActiveShaders); + for (Uint8 s = 0; s < m_NumActiveShaders; ++s) + new (m_pResourceLayouts + s) ShaderResourceLayoutD3D11{*this, m_pBoundResourceCaches[s]}; // noexcept - // Reserve memory for resource layouts - for (Uint8 s = 0; s < m_NumActiveShaders; ++s) + // It is important to construct all objects before initializing them because if an exception is thrown, + // destructors will be called for all objects + + const auto& PSODesc = pPSO->GetDesc(); + + // Reserve memory for resource layouts + for (Uint8 s = 0; s < m_NumActiveShaders; ++s) + { + auto* pShaderD3D11 = pPSO->GetShader(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->GetD3D11Resources(); + 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 + SHADER_RESOURCE_VARIABLE_TYPE VarTypes[] = {SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC}; + m_pResourceLayouts[s].Initialize( + pShaderD3D11->GetD3D11Resources(), + PSODesc.ResourceLayout, + VarTypes, + _countof(VarTypes), + ResCacheDataAllocator, + ResLayoutDataAllocator // + ); + + const auto ShaderType = pShaderD3D11->GetDesc().ShaderType; + const auto ShaderInd = GetShaderTypePipelineIndex(ShaderType, PSODesc.PipelineType); + VERIFY_EXPR(ShaderType == m_pResourceLayouts[s].GetShaderType()); + m_ShaderTypes[s] = ShaderType; + + m_ResourceLayoutIndex[ShaderInd] = s; + } + } + catch (...) { - auto* pShaderD3D11 = pPSO->GetShader(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->GetD3D11Resources(); - new (m_pBoundResourceCaches + s) ShaderResourceCacheD3D11; - 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 - SHADER_RESOURCE_VARIABLE_TYPE VarTypes[] = {SHADER_RESOURCE_VARIABLE_TYPE_MUTABLE, SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC}; - new (m_pResourceLayouts + s) ShaderResourceLayoutD3D11{*this, m_pBoundResourceCaches[s]}; - m_pResourceLayouts[s].Initialize( - pShaderD3D11->GetD3D11Resources(), - PSODesc.ResourceLayout, - VarTypes, - _countof(VarTypes), - ResCacheDataAllocator, - ResLayoutDataAllocator // - ); - - const auto ShaderType = pShaderD3D11->GetDesc().ShaderType; - const auto ShaderInd = GetShaderTypePipelineIndex(ShaderType, PSODesc.PipelineType); - VERIFY_EXPR(ShaderType == m_pResourceLayouts[s].GetShaderType()); - m_ShaderTypes[s] = ShaderType; - - m_ResourceLayoutIndex[ShaderInd] = s; + Destruct(); + throw; } } ShaderResourceBindingD3D11Impl::~ShaderResourceBindingD3D11Impl() { - auto* pPSOD3D11Impl = ValidatedCast(m_pPSO); - for (Uint32 s = 0; s < m_NumActiveShaders; ++s) + Destruct(); +} + +void ShaderResourceBindingD3D11Impl::Destruct() +{ + if (m_pResourceLayouts != nullptr) + { + for (Int32 l = 0; l < m_NumActiveShaders; ++l) + { + m_pResourceLayouts[l].~ShaderResourceLayoutD3D11(); + } + } + + if (m_pBoundResourceCaches != nullptr) { - auto& Allocator = pPSOD3D11Impl->GetSRBMemoryAllocator().GetResourceCacheDataAllocator(s); - m_pBoundResourceCaches[s].Destroy(Allocator); - m_pBoundResourceCaches[s].~ShaderResourceCacheD3D11(); + auto& SRBMemAllocator = m_pPSO->GetSRBMemoryAllocator(); + for (Uint32 s = 0; s < m_NumActiveShaders; ++s) + { + auto& Allocator = SRBMemAllocator.GetResourceCacheDataAllocator(s); + m_pBoundResourceCaches[s].Destroy(Allocator); + m_pBoundResourceCaches[s].~ShaderResourceCacheD3D11(); + } } - GetRawAllocator().Free(m_pBoundResourceCaches); - for (Int32 l = 0; l < m_NumActiveShaders; ++l) + if (void* pRawMem = m_pBoundResourceCaches) { - m_pResourceLayouts[l].~ShaderResourceLayoutD3D11(); + GetRawAllocator().Free(pRawMem); } - GetRawAllocator().Free(m_pResourceLayouts); } IMPLEMENT_QUERY_INTERFACE(ShaderResourceBindingD3D11Impl, IID_ShaderResourceBindingD3D11, TBase) -- cgit v1.2.3 From b9d1a84943f61d1b48ffb1847cd0b3f7b8c60fb2 Mon Sep 17 00:00:00 2001 From: assiduous Date: Tue, 20 Oct 2020 18:05:02 -0700 Subject: Added ShaderResourceQueries device feature and EngineGLCreateInfo::ForceNonSeparablePrograms parameter (API 240078) --- Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp index 94847a51..8d45e547 100644 --- a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp @@ -176,7 +176,7 @@ RenderDeviceD3D11Impl::RenderDeviceD3D11Impl(IReferenceCounters* pRefCo #undef UNSUPPORTED_FEATURE #if defined(_MSC_VER) && defined(_WIN64) - static_assert(sizeof(DeviceFeatures) == 30, "Did you add a new feature to DeviceFeatures? Please handle its satus here."); + static_assert(sizeof(DeviceFeatures) == 31, "Did you add a new feature to DeviceFeatures? Please handle its satus here."); #endif auto& TexCaps = m_DeviceCaps.TexCaps; -- cgit v1.2.3