diff options
| author | assiduous <assiduous@diligentgraphics.com> | 2020-10-20 20:26:21 +0000 |
|---|---|---|
| committer | assiduous <assiduous@diligentgraphics.com> | 2020-10-20 20:26:21 +0000 |
| commit | 13b0b54987db2684e46e337d2e5be5b81366083b (patch) | |
| tree | 40f2f3c7b180c7728bc8676cd32614e16fac17a0 /Graphics/GraphicsEngineD3D11 | |
| parent | Renamed USAGE_STATIC to USAGE_IMMUTABLE (API240077) (diff) | |
| download | DiligentCore-13b0b54987db2684e46e337d2e5be5b81366083b.tar.gz DiligentCore-13b0b54987db2684e46e337d2e5be5b81366083b.zip | |
Improved exception safety of pipeline state object construction
Diffstat (limited to 'Graphics/GraphicsEngineD3D11')
7 files changed, 201 insertions, 177 deletions
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 <typename PSOCreateInfoType> - LinearAllocator InitInternalObjects(const PSOCreateInfoType& CreateInfo); + 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; 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/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 <typename PSOCreateInfoType> -LinearAllocator PipelineStateD3D11Impl::InitInternalObjects(const PSOCreateInfoType& CreateInfo) +void PipelineStateD3D11Impl::InitInternalObjects(const PSOCreateInfoType& CreateInfo) { m_ResourceLayoutIndex.fill(-1); std::vector<std::pair<SHADER_TYPE, ShaderD3D11Impl*>> ShaderStages; ExtractShaders<ShaderD3D11Impl>(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<ShaderResourceLayoutD3D11>(GetNumShaderStages()); - MemPool.AddSpace<ShaderResourceCacheD3D11>(GetNumShaderStages()); + MemPool.AddSpace<ShaderResourceCacheD3D11>(NumShaderStages); + MemPool.AddSpace<ShaderResourceLayoutD3D11>(NumShaderStages); ReserveSpaceForPipelineDesc(CreateInfo, MemPool); MemPool.Reserve(); - m_pStaticResourceLayouts = MemPool.Allocate<ShaderResourceLayoutD3D11>(GetNumShaderStages()); - m_pStaticResourceCaches = MemPool.Allocate<ShaderResourceCacheD3D11>(GetNumShaderStages()); + 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); - 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<ImmutableSamplerInfo>")) // 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<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>")); - LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(InputLayout, d311InputElements); + // Create input layout + 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>")); + LayoutElements_To_D3D11_INPUT_ELEMENT_DESCs(InputLayout, d311InputElements); - ID3DBlob* pVSByteCode = m_pVS.RawPtr<ShaderD3D11Impl>()->GetBytecode(); - if (!pVSByteCode) - LOG_ERROR_AND_THROW("Vertex Shader byte code does not exist"); + ID3DBlob* pVSByteCode = m_pVS.RawPtr<ShaderD3D11Impl>()->GetBytecode(); + if (!pVSByteCode) + LOG_ERROR_AND_THROW("Vertex Shader byte code does not exist"); - CHECK_D3D_RESULT_THROW(pDeviceD3D11->CreateInputLayout(d311InputElements.data(), static_cast<UINT>(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<UINT>(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<ImmutableSamplerInfo>")) // clang-format on { - auto MemPool = InitInternalObjects(CreateInfo); - - m_pCS = ValidatedCast<ShaderD3D11Impl>(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<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 (...) { - 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<ImmutableSamplerInfo>")); + 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 < 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<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);
|
