diff options
| author | azhirnov <zh1dron@gmail.com> | 2020-10-21 22:15:55 +0000 |
|---|---|---|
| committer | azhirnov <zh1dron@gmail.com> | 2020-10-25 10:50:53 +0000 |
| commit | 42217cc1ec7f81c028dcc5c1f845a2fc3b3da497 (patch) | |
| tree | 2efcb7c0fdeae582ecb5c8d13c7bedd593ab9a3c /Graphics/GraphicsEngineD3D11 | |
| parent | Fixed Mac/iOS build (diff) | |
| parent | Updated Metal testing environment (diff) | |
| download | DiligentCore-42217cc1ec7f81c028dcc5c1f845a2fc3b3da497.tar.gz DiligentCore-42217cc1ec7f81c028dcc5c1f845a2fc3b3da497.zip | |
Merge branch 'master' into ray_tracing
# Conflicts:
# Graphics/GraphicsEngine/interface/PipelineState.h
# Graphics/GraphicsEngineVulkan/include/VulkanTypeConversions.hpp
# Graphics/GraphicsEngineVulkan/src/PipelineLayout.cpp
# Graphics/GraphicsEngineVulkan/src/VulkanTypeConversions.cpp
Diffstat (limited to 'Graphics/GraphicsEngineD3D11')
14 files changed, 489 insertions, 314 deletions
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/include/PipelineStateD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp index 02f922a6..520e9bb0 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 { @@ -48,9 +49,12 @@ class PipelineStateD3D11Impl final : public PipelineStateBase<IPipelineStateD3D1 public: using TPipelineStateBase = PipelineStateBase<IPipelineStateD3D11, RenderDeviceD3D11Impl>; - 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; @@ -117,27 +121,45 @@ public: const ShaderResourceLayoutD3D11& GetStaticResourceLayout(Uint32 s) const { - VERIFY_EXPR(s < m_NumShaders); + VERIFY_EXPR(s < GetNumShaderStages()); return m_pStaticResourceLayouts[s]; } ShaderResourceCacheD3D11& GetStaticResourceCache(Uint32 s) { - VERIFY_EXPR(s < m_NumShaders); + VERIFY_EXPR(s < GetNumShaderStages()); return m_pStaticResourceCaches[s]; } - void SetStaticSamplers(ShaderResourceCacheD3D11& ResourceCache, Uint32 ShaderInd) const; + const ShaderD3D11Impl* GetShaderByType(SHADER_TYPE ShaderType) const; + const ShaderD3D11Impl* GetShader(Uint32 Index) const; + + void SetImmutableSamplers(ShaderResourceCacheD3D11& ResourceCache, Uint32 ShaderInd) const; private: + template <typename PSOCreateInfoType> + void InitInternalObjects(const PSOCreateInfoType& CreateInfo); + + void InitResourceLayouts(const PipelineStateCreateInfo& CreateInfo, + const std::vector<std::pair<SHADER_TYPE, ShaderD3D11Impl*>>& ShaderStages); + + void Destruct(); + CComPtr<ID3D11BlendState> m_pd3d11BlendState; CComPtr<ID3D11RasterizerState> m_pd3d11RasterizerState; CComPtr<ID3D11DepthStencilState> m_pd3d11DepthStencilState; CComPtr<ID3D11InputLayout> m_pd3d11InputLayout; + RefCntAutoPtr<ShaderD3D11Impl> m_pVS; + RefCntAutoPtr<ShaderD3D11Impl> m_pPS; + RefCntAutoPtr<ShaderD3D11Impl> m_pGS; + RefCntAutoPtr<ShaderD3D11Impl> m_pDS; + RefCntAutoPtr<ShaderD3D11Impl> m_pHS; + RefCntAutoPtr<ShaderD3D11Impl> 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; @@ -146,20 +168,20 @@ private: // indexed by the shader type pipeline index (returned by GetShaderTypePipelineIndex) std::array<Int8, MAX_SHADERS_IN_PIPELINE> m_ResourceLayoutIndex = {-1, -1, -1, -1, -1}; - std::array<Uint16, MAX_SHADERS_IN_PIPELINE + 1> m_StaticSamplerOffsets = {}; - struct StaticSamplerInfo + std::array<Uint16, MAX_SHADERS_IN_PIPELINE + 1> m_ImmutableSamplerOffsets = {}; + struct ImmutableSamplerInfo { const D3DShaderResourceAttribs& Attribs; RefCntAutoPtr<ISampler> pSampler; - StaticSamplerInfo(const D3DShaderResourceAttribs& _Attribs, - RefCntAutoPtr<ISampler> _pSampler) : + ImmutableSamplerInfo(const D3DShaderResourceAttribs& _Attribs, + RefCntAutoPtr<ISampler> _pSampler) : // clang-format off Attribs {_Attribs}, pSampler {std::move(_pSampler)} // clang-format on {} }; - std::vector<StaticSamplerInfo, STDAllocatorRawMem<StaticSamplerInfo>> m_StaticSamplers; + std::vector<ImmutableSamplerInfo, STDAllocatorRawMem<ImmutableSamplerInfo>> m_ImmutableSamplers; }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/RenderDeviceD3D11Impl.hpp index 2d279b86..8565935d 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, @@ -132,6 +136,9 @@ public: Uint64 GetCommandQueueMask() const { return Uint64{1}; } private: + template <typename PSOCreateInfoType> + void CreatePipelineState(const PSOCreateInfoType& PSOCreateInfo, IPipelineState** ppPipelineState); + virtual void TestTextureFormat(TEXTURE_FORMAT TexFormat) override final; EngineD3D11CreateInfo m_EngineAttribs; 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/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<const ShaderResourcesD3D11> 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<const ShaderResourcesD3D11> 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/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/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index c396b15c..9286f3f8 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); + const 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; @@ -179,9 +181,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->GetNumShaderStages(); ++s) { - auto* pShaderD3D11 = pPipelineStateD3D11->GetShader<ShaderD3D11Impl>(s); + auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); if (pShaderD3D11->GetD3D11Resources()->GetTotalResources() > 0) ResourcesPresent = true; } @@ -206,7 +208,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->GetNumShaderStages(), "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 +235,7 @@ void DeviceContextD3D11Impl::TransitionAndCommitShaderResources(IPipelineState* const auto ShaderTypeInd = GetShaderTypeIndex(ShaderType); #ifdef DILIGENT_DEVELOPMENT - auto* pShaderD3D11 = pPipelineStateD3D11->GetShader<ShaderD3D11Impl>(s); + auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); VERIFY_EXPR(ShaderType == pShaderD3D11->GetDesc().ShaderType); #endif @@ -380,7 +382,7 @@ void DeviceContextD3D11Impl::TransitionAndCommitShaderResources(IPipelineState* const auto ShaderTypeInd = GetShaderTypeIndex(ShaderType); #ifdef DILIGENT_DEVELOPMENT - auto* pShaderD3D11 = pPipelineStateD3D11->GetShader<ShaderD3D11Impl>(s); + auto* pShaderD3D11 = pPipelineStateD3D11->GetShader(s); VERIFY_EXPR(ShaderType == pShaderD3D11->GetDesc().ShaderType); #endif @@ -671,9 +673,9 @@ 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->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 39a6ca4a..108f149c 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -31,49 +31,74 @@ #include "RenderDeviceD3D11Impl.hpp" #include "ShaderResourceBindingD3D11Impl.hpp" #include "EngineMemory.h" -#include "ShaderD3D11Impl.hpp" namespace Diligent { -PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, - RenderDeviceD3D11Impl* pRenderDeviceD3D11, - const PipelineStateCreateInfo& CreateInfo) : +template <typename PSOCreateInfoType> +void PipelineStateD3D11Impl::InitInternalObjects(const PSOCreateInfoType& CreateInfo) +{ + m_ResourceLayoutIndex.fill(-1); + + std::vector<std::pair<SHADER_TYPE, ShaderD3D11Impl*>> ShaderStages; + ExtractShaders<ShaderD3D11Impl>(CreateInfo, ShaderStages); + + const auto NumShaderStages = GetNumShaderStages(); + VERIFY_EXPR(NumShaderStages > 0 && NumShaderStages == ShaderStages.size()); + + LinearAllocator MemPool{GetRawAllocator()}; + + MemPool.AddSpace<ShaderResourceCacheD3D11>(NumShaderStages); + MemPool.AddSpace<ShaderResourceLayoutD3D11>(NumShaderStages); + + ReserveSpaceForPipelineDesc(CreateInfo, MemPool); + + MemPool.Reserve(); + + m_pStaticResourceCaches = MemPool.ConstructArray<ShaderResourceCacheD3D11>(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<ShaderResourceLayoutD3D11>(NumShaderStages); + for (Uint32 i = 0; i < NumShaderStages; ++i) + new (m_pStaticResourceLayouts + i) ShaderResourceLayoutD3D11{*this, m_pStaticResourceCaches[i]}; // noexcept + + InitializePipelineDesc(CreateInfo, 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); +} + + +PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, + RenderDeviceD3D11Impl* pRenderDeviceD3D11, + const GraphicsPipelineStateCreateInfo& CreateInfo) : // clang-format off TPipelineStateBase { pRefCounters, pRenderDeviceD3D11, - CreateInfo.PSODesc + CreateInfo }, m_SRBMemAllocator{GetRawAllocator()}, - m_StaticSamplers (STD_ALLOCATOR_RAW_MEM(StaticSamplerInfo, GetRawAllocator(), "Allocator for vector<StaticSamplerInfo>")) + m_ImmutableSamplers (STD_ALLOCATOR_RAW_MEM(ImmutableSamplerInfo, GetRawAllocator(), "Allocator for vector<ImmutableSamplerInfo>")) // clang-format on { - m_ResourceLayoutIndex.fill(-1); - - if (m_Desc.IsComputePipeline()) + try { - auto* pCS = ValidatedCast<ShaderD3D11Impl>(m_Desc.ComputePipeline.pCS); - m_pCS = pCS; - if (m_pCS == nullptr) - { - LOG_ERROR_AND_THROW("Compute shader is null"); - } + InitInternalObjects(CreateInfo); - 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) - { + auto& GraphicsPipeline = GetGraphicsPipelineDesc(); #define INIT_SHADER(ShortName, ExpectedType) \ do \ { \ - auto* pShader = ValidatedCast<ShaderD3D11Impl>(m_Desc.GraphicsPipeline.p##ShortName); \ + auto* pShader = ValidatedCast<ShaderD3D11Impl>(CreateInfo.p##ShortName); \ m_p##ShortName = pShader; \ if (m_p##ShortName && m_p##ShortName->GetDesc().ShaderType != ExpectedType) \ { \ @@ -98,22 +123,22 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR auto* pDeviceD3D11 = pRenderDeviceD3D11->GetD3D11Device(); D3D11_BLEND_DESC D3D11BSDesc = {}; - BlendStateDesc_To_D3D11_BLEND_DESC(m_Desc.GraphicsPipeline.BlendDesc, 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); + 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); + 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; + const auto& InputLayout = GraphicsPipeline.InputLayout; if (InputLayout.NumElements > 0) { std::vector<D3D11_INPUT_ELEMENT_DESC, STDAllocatorRawMem<D3D11_INPUT_ELEMENT_DESC>> d311InputElements(STD_ALLOCATOR_RAW_MEM(D3D11_INPUT_ELEMENT_DESC, GetRawAllocator(), "Allocator for vector<D3D11_INPUT_ELEMENT_DESC>")); @@ -127,75 +152,141 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR "Failed to create the Direct3D11 input layout"); } } - else + catch (...) + { + Destruct(); + throw; + } +} + +PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pRefCounters, + RenderDeviceD3D11Impl* pRenderDeviceD3D11, + const ComputePipelineStateCreateInfo& CreateInfo) : + // clang-format off + TPipelineStateBase + { + pRefCounters, + pRenderDeviceD3D11, + CreateInfo + }, + m_SRBMemAllocator{GetRawAllocator()}, + m_ImmutableSamplers(STD_ALLOCATOR_RAW_MEM(ImmutableSamplerInfo, GetRawAllocator(), "Allocator for vector<ImmutableSamplerInfo>")) +// clang-format on +{ + try + { + InitInternalObjects(CreateInfo); + + m_pCS = ValidatedCast<ShaderD3D11Impl>(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 (...) { - UNEXPECTED(GetPipelineTypeString(m_Desc.PipelineType), " pipelines are not supported by Direct3D11 backend"); + Destruct(); + throw; } +} - m_pStaticResourceLayouts = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceLayoutD3D11", ShaderResourceLayoutD3D11, m_NumShaders); - m_pStaticResourceCaches = ALLOCATE(GetRawAllocator(), "Raw memory for ShaderResourceCacheD3D11", ShaderResourceCacheD3D11, m_NumShaders); +PipelineStateD3D11Impl::~PipelineStateD3D11Impl() +{ + Destruct(); +} +void PipelineStateD3D11Impl::Destruct() +{ + if (m_pStaticResourceLayouts != nullptr) + { + for (Uint32 l = 0; l < GetNumShaderStages(); ++l) + { + m_pStaticResourceLayouts[l].~ShaderResourceLayoutD3D11(); + } + } + + if (m_pStaticResourceCaches != nullptr) + { + for (Uint32 s = 0; s < GetNumShaderStages(); ++s) + { + m_pStaticResourceCaches[s].Destroy(GetRawAllocator()); + m_pStaticResourceCaches[s].~ShaderResourceCacheD3D11(); + } + } + + // All subobjects are allocated in contiguous chunks of memory. + if (auto* pRawMem = m_pStaticResourceCaches) + GetRawAllocator().Free(pRawMem); +} + +IMPLEMENT_QUERY_INTERFACE(PipelineStateD3D11Impl, IID_PipelineStateD3D11, TPipelineStateBase) + + +void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& CreateInfo, + const std::vector<std::pair<SHADER_TYPE, ShaderD3D11Impl*>>& ShaderStages) +{ 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 < ShaderStages.size(); ++s) { - auto* pShader = GetShader<const ShaderD3D11Impl>(s); + auto* pShader = ShaderStages[s].second; pResources[s] = &(*pShader->GetD3D11Resources()); } - ShaderResources::DvpVerifyResourceLayout(ResourceLayout, pResources, m_NumShaders, + 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<StaticSamplerInfo>")); + decltype(m_ImmutableSamplers) ImmutableSamplers{STD_ALLOCATOR_RAW_MEM(ImmutableSamplerInfo, GetRawAllocator(), "Allocator for vector<ImmutableSamplerInfo>")}; + std::array<size_t, MAX_SHADERS_IN_PIPELINE> ShaderResLayoutDataSizes = {}; std::array<size_t, MAX_SHADERS_IN_PIPELINE> ShaderResCacheDataSizes = {}; - for (Uint32 s = 0; s < m_NumShaders; ++s) + for (Uint32 s = 0; s < ShaderStages.size(); ++s) { - const auto* pShader = GetShader<const ShaderD3D11Impl>(s); - 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 - - // Initialize static samplers - for (Uint32 sam = 0; sam < ShaderResources.GetNumSamplers(); ++sam) + m_pStaticResourceLayouts[s].Initialize( + pShader->GetD3D11Resources(), + m_Desc.ResourceLayout, + StaticVarTypes, + _countof(StaticVarTypes), + GetRawAllocator(), + GetRawAllocator() // + ); + + // Initialize immutable samplers + for (Uint32 sam = 0; sam < Resources.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 = Resources.GetSampler(sam); + constexpr bool LogImtblSamplerArrayError = true; + auto SrcImtblSamplerInd = Resources.FindImmutableSampler(SamplerAttribs, ResourceLayout, LogImtblSamplerArrayError); + if (SrcImtblSamplerInd >= 0) { - const auto& SrcStaticSamplerInfo = ResourceLayout.StaticSamplers[SrcStaticSamplerInd]; - RefCntAutoPtr<ISampler> pStaticSampler; - pRenderDeviceD3D11->CreateSampler(SrcStaticSamplerInfo.Desc, &pStaticSampler); - StaticSamplers.emplace_back(SamplerAttribs, std::move(pStaticSampler)); + const auto& SrcImtblSamplerInfo = ResourceLayout.ImmutableSamplers[SrcImtblSamplerInd]; + + RefCntAutoPtr<ISampler> pImbtlSampler; + GetDevice()->CreateSampler(SrcImtblSamplerInfo.Desc, &pImbtlSampler); + ImmutableSamplers.emplace_back(SamplerAttribs, std::move(pImbtlSampler)); } } - m_StaticSamplerOffsets[s + 1] = static_cast<Uint16>(StaticSamplers.size()); + m_ImmutableSamplerOffsets[s + 1] = static_cast<Uint16>(ImmutableSamplers.size()); if (m_Desc.SRBAllocationGranularity > 1) { @@ -204,8 +295,8 @@ PipelineStateD3D11Impl::PipelineStateD3D11Impl(IReferenceCounters* pR 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); @@ -214,40 +305,20 @@ 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, 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 < m_NumShaders; ++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); + // Initialize immutable samplers in the static resource cache to avoid warning messages + SetImmutableSamplers(m_pStaticResourceCaches[s], s); } } - -PipelineStateD3D11Impl::~PipelineStateD3D11Impl() -{ - for (Uint32 s = 0; s < m_NumShaders; ++s) - { - 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); -} - -IMPLEMENT_QUERY_INTERFACE(PipelineStateD3D11Impl, IID_PipelineStateD3D11, TPipelineStateBase) - - ID3D11BlendState* PipelineStateD3D11Impl::GetD3D11BlendState() { return m_pd3d11BlendState; @@ -270,9 +341,8 @@ ID3D11InputLayout* PipelineStateD3D11Impl::GetD3D11InputLayout() void PipelineStateD3D11Impl::CreateShaderResourceBinding(IShaderResourceBinding** ppShaderResourceBinding, bool InitStaticResources) { - auto* pRenderDeviceD3D11 = ValidatedCast<RenderDeviceD3D11Impl>(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<IObject**>(static_cast<IShaderResourceBinding**>(ppShaderResourceBinding))); @@ -289,13 +359,13 @@ bool PipelineStateD3D11Impl::IsCompatibleWith(const IPipelineState* pPSO) const if (m_ShaderResourceLayoutHash != pPSOD3D11->m_ShaderResourceLayoutHash) return false; - if (m_NumShaders != pPSOD3D11->m_NumShaders) + if (GetNumShaderStages() != pPSOD3D11->GetNumShaderStages()) return false; - for (Uint32 s = 0; s < m_NumShaders; ++s) + for (Uint32 s = 0; s < GetNumShaderStages(); ++s) { - auto* pShader0 = GetShader<const ShaderD3D11Impl>(s); - auto* pShader1 = pPSOD3D11->GetShader<const ShaderD3D11Impl>(s); + auto* pShader0 = GetShader(s); + auto* pShader1 = pPSOD3D11->GetShader(s); if (pShader0->GetDesc().ShaderType != pShader1->GetDesc().ShaderType) return false; const auto& Res0 = *pShader0->GetD3D11Resources(); @@ -352,7 +422,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 < GetNumShaderStages(); ++s) { auto& StaticResLayout = m_pStaticResourceLayouts[s]; if ((ShaderFlags & StaticResLayout.GetShaderType()) != 0) @@ -366,7 +436,7 @@ Uint32 PipelineStateD3D11Impl::GetStaticVariableCount(SHADER_TYPE ShaderType) co if (LayoutInd < 0) return 0; - VERIFY_EXPR(static_cast<Uint32>(LayoutInd) <= m_NumShaders); + VERIFY_EXPR(static_cast<Uint32>(LayoutInd) <= GetNumShaderStages()); return m_pStaticResourceLayouts[LayoutInd].GetTotalResourceCount(); } @@ -376,7 +446,7 @@ IShaderResourceVariable* PipelineStateD3D11Impl::GetStaticVariableByName(SHADER_ if (LayoutInd < 0) return nullptr; - VERIFY_EXPR(static_cast<Uint32>(LayoutInd) <= m_NumShaders); + VERIFY_EXPR(static_cast<Uint32>(LayoutInd) <= GetNumShaderStages()); return m_pStaticResourceLayouts[LayoutInd].GetShaderVariable(Name); } @@ -386,23 +456,48 @@ IShaderResourceVariable* PipelineStateD3D11Impl::GetStaticVariableByIndex(SHADER if (LayoutInd < 0) return nullptr; - VERIFY_EXPR(static_cast<Uint32>(LayoutInd) <= m_NumShaders); + VERIFY_EXPR(static_cast<Uint32>(LayoutInd) <= GetNumShaderStages()); 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<SamplerD3D11Impl>(); - // 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<Uint32>(SamAttribs.BindPoint) + SamAttribs.BindCount, NumCachedSamplers); for (Uint32 BindPoint = SamAttribs.BindPoint; BindPoint < EndBindPoint; ++BindPoint) ResourceCache.SetSampler(BindPoint, pSamplerD3D11Impl); } } +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 < GetNumShaderStages()) + return GetShaderByType(GetShaderStageType(Index)); + + UNEXPECTED("Shader index is out of range"); + return nullptr; +} + } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp index d0d4957b..06cda6ad 100644 --- a/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/RenderDeviceD3D11Impl.cpp @@ -177,7 +177,7 @@ RenderDeviceD3D11Impl::RenderDeviceD3D11Impl(IReferenceCounters* pRefCo #undef UNSUPPORTED_FEATURE #if defined(_MSC_VER) && defined(_WIN64) - static_assert(sizeof(DeviceFeatures) == 31, "Did you add a new feature to DeviceFeatures? Please handle its satus here."); + static_assert(sizeof(DeviceFeatures) == 32, "Did you add a new feature to DeviceFeatures? Please handle its satus here."); #endif auto& TexCaps = m_DeviceCaps.TexCaps; @@ -255,7 +255,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<IObject**>(ppBuffer)); pBufferD3D11->CreateDefaultViews(); OnCreateDeviceObject(pBufferD3D11); @@ -267,7 +267,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<IObject**>(ppBuffer)); pBufferD3D11->CreateDefaultViews(); OnCreateDeviceObject(pBufferD3D11); @@ -279,7 +279,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<IObject**>(ppShader)); OnCreateDeviceObject(pShaderD3D11); @@ -296,7 +296,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<IObject**>(ppTexture)); pTextureD3D11->CreateDefaultViews(); OnCreateDeviceObject(pTextureD3D11); @@ -313,7 +313,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<IObject**>(ppTexture)); pTextureD3D11->CreateDefaultViews(); OnCreateDeviceObject(pTextureD3D11); @@ -330,7 +330,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<IObject**>(ppTexture)); pTextureD3D11->CreateDefaultViews(); OnCreateDeviceObject(pTextureD3D11); @@ -378,7 +378,7 @@ void RenderDeviceD3D11Impl::CreateSampler(const SamplerDesc& SamplerDesc, ISampl m_SamplersRegistry.Find(SamplerDesc, reinterpret_cast<IDeviceObject**>(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<IObject**>(ppSampler)); OnCreateDeviceObject(pSamplerD3D11); m_SamplersRegistry.Add(SamplerDesc, *ppSampler); @@ -386,23 +386,35 @@ void RenderDeviceD3D11Impl::CreateSampler(const SamplerDesc& SamplerDesc, ISampl }); } -void RenderDeviceD3D11Impl::CreatePipelineState(const PipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState) +template <typename PSOCreateInfoType> +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<IObject**>(ppPipelineState)); OnCreateDeviceObject(pPipelineStateD3D11); }); } + +void RenderDeviceD3D11Impl::CreateGraphicsPipelineState(const GraphicsPipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState) +{ + CreatePipelineState(PSOCreateInfo, ppPipelineState); +} + +void RenderDeviceD3D11Impl::CreateComputePipelineState(const ComputePipelineStateCreateInfo& PSOCreateInfo, IPipelineState** ppPipelineState) +{ + CreatePipelineState(PSOCreateInfo, ppPipelineState); +} + 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<IObject**>(ppFence)); OnCreateDeviceObject(pFenceD3D11); }); @@ -413,7 +425,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<IObject**>(ppQuery)); OnCreateDeviceObject(pQueryD3D11); }); @@ -424,7 +436,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<IObject**>(ppRenderPass)); OnCreateDeviceObject(pRenderPassD3D11); }); @@ -435,7 +447,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<IObject**>(ppFramebuffer)); OnCreateDeviceObject(pFramebufferD3D11); }); diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceBindingD3D11Impl.cpp index 62fd2df7..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,74 +50,104 @@ ShaderResourceBindingD3D11Impl::ShaderResourceBindingD3D11Impl(IReferenceCounter m_bIsStaticResourcesBound{false} // clang-format on { - m_ResourceLayoutIndex.fill(-1); - m_NumActiveShaders = static_cast<Uint8>(pPSO->GetNumShaders()); + try + { + m_ResourceLayoutIndex.fill(-1); + m_NumActiveShaders = static_cast<Uint8>(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<ShaderResourceCacheD3D11>(m_NumActiveShaders); + MemPool.AddSpace<ShaderResourceLayoutD3D11>(m_NumActiveShaders); - const auto& PSODesc = pPSO->GetDesc(); + MemPool.Reserve(); - // Reserve memory for resource layouts - for (Uint8 s = 0; s < m_NumActiveShaders; ++s) - { - auto* pShaderD3D11 = pPSO->GetShader<ShaderD3D11Impl>(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}; - // clang-format off - new (m_pResourceLayouts + s) - ShaderResourceLayoutD3D11 - { - *this, + m_pBoundResourceCaches = MemPool.ConstructArray<ShaderResourceCacheD3D11>(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; + + m_pResourceLayouts = MemPool.Allocate<ShaderResourceLayoutD3D11>(m_NumActiveShaders); + for (Uint8 s = 0; s < m_NumActiveShaders; ++s) + new (m_pResourceLayouts + s) ShaderResourceLayoutD3D11{*this, m_pBoundResourceCaches[s]}; // noexcept + + // 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), - m_pBoundResourceCaches[s], ResCacheDataAllocator, - ResLayoutDataAllocator - }; - // clang-format on + 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; + 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; + m_ResourceLayoutIndex[ShaderInd] = s; + } + } + catch (...) + { + Destruct(); + throw; } } ShaderResourceBindingD3D11Impl::~ShaderResourceBindingD3D11Impl() { - auto* pPSOD3D11Impl = ValidatedCast<PipelineStateD3D11Impl>(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) @@ -151,14 +182,13 @@ void ShaderResourceBindingD3D11Impl::InitializeStaticResources(const IPipelineSt } const auto* pPSOD3D11 = ValidatedCast<const PipelineStateD3D11Impl>(pPipelineState); - auto ppShaders = pPSOD3D11->GetShaders(); - auto NumShaders = pPSOD3D11->GetNumShaders(); + auto NumShaders = pPSOD3D11->GetNumShaderStages(); VERIFY_EXPR(NumShaders == m_NumActiveShaders); for (Uint32 shader = 0; shader < NumShaders; ++shader) { const auto& StaticResLayout = pPSOD3D11->GetStaticResourceLayout(shader); - auto* pShaderD3D11 = ValidatedCast<ShaderD3D11Impl>(ppShaders[shader]); + auto* pShaderD3D11 = pPSOD3D11->GetShader(shader); #ifdef DILIGENT_DEVELOPMENT if (!StaticResLayout.dvpVerifyBindings()) { @@ -178,7 +208,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/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 fd6c7299..f7c1958d 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceLayoutD3D11.cpp @@ -84,11 +84,11 @@ ShaderResourceLayoutD3D11::~ShaderResourceLayoutD3D11() size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D11& SrcResources,
const PipelineResourceLayoutDesc& ResourceLayout,
const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes,
- Uint32 NumAllowedTypes)
+ Uint32 NumAllowedTypes) noexcept
{
- // 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) +
@@ -101,28 +101,23 @@ size_t ShaderResourceLayoutD3D11::GetRequiredMemorySize(const ShaderResourcesD3D }
-ShaderResourceLayoutD3D11::ShaderResourceLayoutD3D11(IObject& Owner,
- std::shared_ptr<const ShaderResourcesD3D11> 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<const ShaderResourcesD3D11> 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);
// 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 +187,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 +236,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 +247,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 +407,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());
@@ -429,7 +424,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<BufferD3D11Impl> pBuffD3D11Impl(pBuffer, IID_BufferD3D11);
+ RefCntAutoPtr<BufferD3D11Impl> pBuffD3D11Impl{pBuffer, IID_BufferD3D11};
#ifdef DILIGENT_DEVELOPMENT
{
auto& CachedCB = m_ParentResLayout.m_ResourceCache.GetCB(m_Attribs.BindPoint + ArrayIndex);
@@ -448,18 +443,19 @@ 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<TextureViewD3D11Impl> pViewD3D11(pView, IID_TextureViewD3D11);
+ RefCntAutoPtr<TextureViewD3D11Impl> 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
if (ValidSamplerAssigned())
{
auto& Sampler = m_ParentResLayout.GetResource<SamplerBindInfo>(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);
@@ -501,11 +497,11 @@ 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
- RefCntAutoPtr<SamplerD3D11Impl> pSamplerD3D11(pSampler, IID_SamplerD3D11);
+ RefCntAutoPtr<SamplerD3D11Impl> pSamplerD3D11{pSampler, IID_SamplerD3D11};
#ifdef DILIGENT_DEVELOPMENT
if (pSampler && !pSamplerD3D11)
@@ -548,11 +544,13 @@ 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<BufferViewD3D11Impl> pViewD3D11(pView, IID_BufferViewD3D11);
+ RefCntAutoPtr<BufferViewD3D11Impl> 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
ResourceCache.SetBufSRV(m_Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11));
@@ -568,11 +566,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<TextureViewD3D11Impl> pViewD3D11(pView, IID_TextureViewD3D11);
+ RefCntAutoPtr<TextureViewD3D11Impl> 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));
@@ -588,11 +587,13 @@ 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<BufferViewD3D11Impl> pViewD3D11(pView, IID_BufferViewD3D11);
+ RefCntAutoPtr<BufferViewD3D11Impl> 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
ResourceCache.SetBufUAV(m_Attribs.BindPoint + ArrayIndex, std::move(pViewD3D11));
@@ -731,7 +732,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<SamplerBindInfo>(Name))
return pSampler;
}
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); } |
