diff options
| author | assiduous <assiduous@diligentgraphics.com> | 2021-03-15 03:12:37 +0000 |
|---|---|---|
| committer | assiduous <assiduous@diligentgraphics.com> | 2021-03-19 00:38:23 +0000 |
| commit | 250b656204dee014074b093c6b60c179a25fbbf7 (patch) | |
| tree | 8d31b0c9d94917a952b70e2a7fb99a277ddada20 /Graphics/GraphicsEngineD3D11 | |
| parent | D3D11 backend: some header clean-up plus few minor updates (diff) | |
| download | DiligentCore-250b656204dee014074b093c6b60c179a25fbbf7.tar.gz DiligentCore-250b656204dee014074b093c6b60c179a25fbbf7.zip | |
Refactored D3D11 resource cache
Diffstat (limited to 'Graphics/GraphicsEngineD3D11')
10 files changed, 438 insertions, 912 deletions
diff --git a/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.hpp index 5c72ed1f..7dcde7a0 100644 --- a/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.hpp @@ -333,11 +333,67 @@ private: void BindShaderResources(); - using TBindingsPerStage = PipelineResourceSignatureD3D11Impl::TBindingsPerStage; - using TCommittedResources = ShaderResourceCacheD3D11::TCommittedResources; - using TMinMaxSlotPerStage = ShaderResourceCacheD3D11::TMinMaxSlotPerStage; + static constexpr int NumShaderTypes = BindPointsD3D11::NumShaderTypes; + struct TCommittedResources + { + // clang-format off + + /// An array of D3D11 constant buffers committed to D3D11 device context, + /// for each shader type. The context addref's all bound resources, so we do + /// not need to keep strong references. + ID3D11Buffer* D3D11CBs [NumShaderTypes][D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT] = {}; + + /// An array of D3D11 shader resource views committed to D3D11 device context, + /// for each shader type. The context addref's all bound resources, so we do + /// not need to keep strong references. + ID3D11ShaderResourceView* D3D11SRVs [NumShaderTypes][D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT] = {}; + + /// An array of D3D11 samplers committed to D3D11 device context, + /// for each shader type. The context addref's all bound resources, so we do + /// not need to keep strong references. + ID3D11SamplerState* D3D11Samplers[NumShaderTypes][D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT] = {}; + + /// An array of D3D11 UAVs committed to D3D11 device context, + /// for each shader type. The context addref's all bound resources, so we do + /// not need to keep strong references. + ID3D11UnorderedAccessView* D3D11UAVs [NumShaderTypes][D3D11_PS_CS_UAV_REGISTER_COUNT] = {}; + + /// An array of D3D11 resources commited as SRV to D3D11 device context, + /// for each shader type. The context addref's all bound resources, so we do + /// not need to keep strong references. + ID3D11Resource* D3D11SRVResources [NumShaderTypes][D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT] = {}; + + /// An array of D3D11 resources commited as UAV to D3D11 device context, + /// for each shader type. The context addref's all bound resources, so we do + /// not need to keep strong references. + ID3D11Resource* D3D11UAVResources [NumShaderTypes][D3D11_PS_CS_UAV_REGISTER_COUNT] = {}; + + Uint8 NumCBs [NumShaderTypes] = {}; + Uint8 NumSRVs [NumShaderTypes] = {}; + Uint8 NumSamplers[NumShaderTypes] = {}; + Uint8 NumUAVs [NumShaderTypes] = {}; + + // clang-format on + + void Clear() + { + *this = {}; + } + }; + + using TBindingsPerStage = PipelineResourceSignatureD3D11Impl::TBindingsPerStage; + struct MinMaxSlot + { + UINT MinSlot = UINT_MAX; + UINT MaxSlot = 0; + }; + using TMinMaxSlotPerStage = std::array<std::array<MinMaxSlot, D3D11_RESOURCE_RANGE_COUNT>, NumShaderTypes>; + - static constexpr auto NumShaderTypes = PipelineResourceSignatureD3D11Impl::NumShaderTypes; + void BindCacheResources(const ShaderResourceCacheD3D11& ResourceCache, + const TBindingsPerStage& Bindings, + TMinMaxSlotPerStage& MinMaxSlot, + SHADER_TYPE ActiveStages); #ifdef DILIGENT_DEVELOPMENT void DvpValidateCommittedShaderResources(); diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineResourceAttribsD3D11.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineResourceAttribsD3D11.hpp index 33dc96e4..1eb7875a 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineResourceAttribsD3D11.hpp +++ b/Graphics/GraphicsEngineD3D11/include/PipelineResourceAttribsD3D11.hpp @@ -38,16 +38,16 @@ namespace Diligent { -enum DESCRIPTOR_RANGE : Uint32 +enum D3D11_RESOURCE_RANGE : Uint32 { - DESCRIPTOR_RANGE_CBV = 0, - DESCRIPTOR_RANGE_SRV, - DESCRIPTOR_RANGE_SAMPLER, - DESCRIPTOR_RANGE_UAV, - DESCRIPTOR_RANGE_COUNT, - DESCRIPTOR_RANGE_UNKNOWN = ~0u + D3D11_RESOURCE_RANGE_CBV = 0, + D3D11_RESOURCE_RANGE_SRV, + D3D11_RESOURCE_RANGE_SAMPLER, + D3D11_RESOURCE_RANGE_UAV, + D3D11_RESOURCE_RANGE_COUNT, + D3D11_RESOURCE_RANGE_UNKNOWN = ~0u }; -DESCRIPTOR_RANGE ShaderResourceToDescriptorRange(SHADER_RESOURCE_TYPE Type); +D3D11_RESOURCE_RANGE ShaderResourceToDescriptorRange(SHADER_RESOURCE_TYPE Type); // sizeof(BindPointsD3D11) == 8, x64 @@ -122,7 +122,8 @@ public: static constexpr Uint32 InvalidSamplerInd = (1u << _SamplerIndBits) - 1; // clang-format off - const Uint32 CacheOffset : _CacheOffsetBits; // SRB and Signature has the same cache offsets for static resources. + const Uint32 CacheOffset : _CacheOffsetBits; // SRB and Signature have the same cache offsets for static resources + // (thanks to sorting variables by type, where all static vars go first). const Uint32 SamplerInd : _SamplerIndBits; // Index of the assigned sampler in m_Desc.Resources. const Uint32 ImtblSamplerAssigned : _SamplerAssignedBits; // Immutable sampler flag. BindPointsD3D11 BindPoints; diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineResourceSignatureD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineResourceSignatureD3D11Impl.hpp index 7deb0a11..4e30df64 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineResourceSignatureD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/PipelineResourceSignatureD3D11Impl.hpp @@ -103,7 +103,9 @@ public: } using TBindings = ShaderResourceCacheD3D11::TResourceCount; - using TBindingsPerStage = ShaderResourceCacheD3D11::TBindingsPerStage; + using TResourceCount = std::array<Uint8, D3D11_RESOURCE_RANGE_COUNT>; + using TBindingsPerStage = std::array<TResourceCount, NumShaderTypes>; + __forceinline void ShiftBindings(TBindingsPerStage& Bindings) const { diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp index fd238c16..ddd0f736 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp @@ -30,13 +30,18 @@ /// \file /// Declaration of Diligent::ShaderResourceCacheD3D11 class +#include <array> +#include <memory> + #include "MemoryAllocator.h" #include "ShaderResourceCacheCommon.hpp" +#include "PipelineResourceAttribsD3D11.hpp" + #include "TextureBaseD3D11.hpp" +#include "TextureViewD3D11Impl.hpp" #include "BufferD3D11Impl.hpp" #include "BufferViewD3D11Impl.hpp" #include "SamplerD3D11Impl.hpp" -#include "PipelineResourceAttribsD3D11.hpp" namespace Diligent { @@ -73,66 +78,8 @@ public: Verify }; // Transitions all resources in the cache - void TransitionResourceStates(DeviceContextD3D11Impl& Ctx, StateTransitionMode Mode); - - - static constexpr int NumShaderTypes = BindPointsD3D11::NumShaderTypes; - - struct TCommittedResources - { - // clang-format off - - /// An array of D3D11 constant buffers committed to D3D11 device context, - /// for each shader type. The context addref's all bound resources, so we do - /// not need to keep strong references. - ID3D11Buffer* D3D11CBs [NumShaderTypes][D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT] = {}; - - /// An array of D3D11 shader resource views committed to D3D11 device context, - /// for each shader type. The context addref's all bound resources, so we do - /// not need to keep strong references. - ID3D11ShaderResourceView* D3D11SRVs [NumShaderTypes][D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT] = {}; - - /// An array of D3D11 samplers committed to D3D11 device context, - /// for each shader type. The context addref's all bound resources, so we do - /// not need to keep strong references. - ID3D11SamplerState* D3D11Samplers[NumShaderTypes][D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT] = {}; - - /// An array of D3D11 UAVs committed to D3D11 device context, - /// for each shader type. The context addref's all bound resources, so we do - /// not need to keep strong references. - ID3D11UnorderedAccessView* D3D11UAVs [NumShaderTypes][D3D11_PS_CS_UAV_REGISTER_COUNT] = {}; - - /// An array of D3D11 resources commited as SRV to D3D11 device context, - /// for each shader type. The context addref's all bound resources, so we do - /// not need to keep strong references. - ID3D11Resource* D3D11SRVResources [NumShaderTypes][D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT] = {}; - - /// An array of D3D11 resources commited as UAV to D3D11 device context, - /// for each shader type. The context addref's all bound resources, so we do - /// not need to keep strong references. - ID3D11Resource* D3D11UAVResources [NumShaderTypes][D3D11_PS_CS_UAV_REGISTER_COUNT] = {}; - - Uint8 NumCBs [NumShaderTypes] = {}; - Uint8 NumSRVs [NumShaderTypes] = {}; - Uint8 NumSamplers[NumShaderTypes] = {}; - Uint8 NumUAVs [NumShaderTypes] = {}; - - // clang-format on - - void Clear(); - }; - - struct MinMaxSlot - { - UINT MinSlot = UINT_MAX; - UINT MaxSlot = 0; - }; - using TMinMaxSlotPerStage = std::array<std::array<MinMaxSlot, DESCRIPTOR_RANGE_COUNT>, NumShaderTypes>; - using TResourceCount = std::array<Uint8, DESCRIPTOR_RANGE_COUNT>; - using TBindingsPerStage = std::array<TResourceCount, NumShaderTypes>; - - void BindResources(DeviceContextD3D11Impl& Ctx, const TBindingsPerStage& BaseBindings, TMinMaxSlotPerStage& MinMaxSlot, TCommittedResources& CommittedRes, SHADER_TYPE ActiveStages) const; - + template <StateTransitionMode Mode> + void TransitionResourceStates(DeviceContextD3D11Impl& Ctx); /// Describes a resource associated with a cached constant buffer struct CachedCB @@ -152,7 +99,7 @@ public: struct CachedSampler { /// Strong reference to the sampler - RefCntAutoPtr<class SamplerD3D11Impl> pSampler; + RefCntAutoPtr<SamplerD3D11Impl> pSampler; private: friend class ShaderResourceCacheD3D11; @@ -187,8 +134,8 @@ public: { pBuffer = nullptr; // Avoid unnecessary virtual function calls - pTexture = pTexView ? ValidatedCast<TextureBaseD3D11>(pTexView->TextureViewD3D11Impl::GetTexture()) : nullptr; - pView.Attach(pTexView.Detach()); + pTexture = pTexView ? pTexView->GetTexture<TextureBaseD3D11>() : nullptr; + pView = std::move(pTexView); pd3d11Resource = pTexture ? pTexture->TextureBaseD3D11::GetD3D11Texture() : nullptr; } @@ -196,12 +143,13 @@ public: { pTexture = nullptr; // Avoid unnecessary virtual function calls - pBuffer = pBufView ? ValidatedCast<BufferD3D11Impl>(pBufView->BufferViewD3D11Impl::GetBuffer()) : nullptr; - pView.Attach(pBufView.Detach()); + pBuffer = pBufView ? pBufView->GetBuffer<BufferD3D11Impl>() : nullptr; + pView = std::move(pBufView); pd3d11Resource = pBuffer ? pBuffer->BufferD3D11Impl::GetD3D11Buffer() : nullptr; } }; + using TResourceCount = std::array<Uint8, D3D11_RESOURCE_RANGE_COUNT>; static size_t GetRequriedMemorySize(const TResourceCount& ResCount); void Initialize(const TResourceCount& ResCount, IMemoryAllocator& MemAllocator); @@ -249,7 +197,7 @@ public: ShaderResourceCacheD3D11::CachedCB* CBs; ID3D11Buffer** pd3d11CBs; BindPointsD3D11* bindPoints; - const_cast<ShaderResourceCacheD3D11*>(this)->GetCBArrays(CBs, pd3d11CBs, bindPoints); + GetCBArrays(CBs, pd3d11CBs, bindPoints); return CBs[CacheOffset]; } @@ -259,7 +207,7 @@ public: ShaderResourceCacheD3D11::CachedResource* SRVResources; ID3D11ShaderResourceView** pd3d11SRVs; BindPointsD3D11* bindPoints; - const_cast<ShaderResourceCacheD3D11*>(this)->GetSRVArrays(SRVResources, pd3d11SRVs, bindPoints); + GetSRVArrays(SRVResources, pd3d11SRVs, bindPoints); return SRVResources[CacheOffset]; } @@ -269,7 +217,7 @@ public: ShaderResourceCacheD3D11::CachedResource* UAVResources; ID3D11UnorderedAccessView** pd3d11UAVs; BindPointsD3D11* bindPoints; - const_cast<ShaderResourceCacheD3D11*>(this)->GetUAVArrays(UAVResources, pd3d11UAVs, bindPoints); + GetUAVArrays(UAVResources, pd3d11UAVs, bindPoints); return UAVResources[CacheOffset]; } @@ -279,7 +227,7 @@ public: ShaderResourceCacheD3D11::CachedSampler* Samplers; ID3D11SamplerState** pd3d11Samplers; BindPointsD3D11* bindPoints; - const_cast<ShaderResourceCacheD3D11*>(this)->GetSamplerArrays(Samplers, pd3d11Samplers, bindPoints); + GetSamplerArrays(Samplers, pd3d11Samplers, bindPoints); return Samplers[CacheOffset]; } @@ -353,34 +301,34 @@ public: __forceinline Uint32 GetUAVCount() const { return m_UAVCount; } // clang-format on - __forceinline void GetCBArrays(CachedCB*& CBs, ID3D11Buffer**& pd3d11CBs, BindPointsD3D11*& pBindPoints) + __forceinline void GetCBArrays(CachedCB*& CBs, ID3D11Buffer**& pd3d11CBs, BindPointsD3D11*& pBindPoints) const { VERIFY(alignof(CachedCB) == alignof(ID3D11Buffer*), "Alignment mismatch, pointer to D3D11 resource may not be properly aligned"); - CBs = reinterpret_cast<CachedCB*>(m_pResourceData + m_CBOffset); + CBs = reinterpret_cast<CachedCB*>(m_pResourceData.get() + m_CBOffset); pd3d11CBs = reinterpret_cast<ID3D11Buffer**>(CBs + GetCBCount()); pBindPoints = reinterpret_cast<BindPointsD3D11*>(pd3d11CBs + GetCBCount()); } - __forceinline void GetSRVArrays(CachedResource*& SRVResources, ID3D11ShaderResourceView**& d3d11SRVs, BindPointsD3D11*& pBindPoints) + __forceinline void GetSRVArrays(CachedResource*& SRVResources, ID3D11ShaderResourceView**& d3d11SRVs, BindPointsD3D11*& pBindPoints) const { VERIFY(alignof(CachedResource) == alignof(ID3D11ShaderResourceView*), "Alignment mismatch, pointer to D3D11 resource may not be properly aligned"); - SRVResources = reinterpret_cast<CachedResource*>(m_pResourceData + m_SRVOffset); + SRVResources = reinterpret_cast<CachedResource*>(m_pResourceData.get() + m_SRVOffset); d3d11SRVs = reinterpret_cast<ID3D11ShaderResourceView**>(SRVResources + GetSRVCount()); pBindPoints = reinterpret_cast<BindPointsD3D11*>(d3d11SRVs + GetSRVCount()); } - __forceinline void GetSamplerArrays(CachedSampler*& Samplers, ID3D11SamplerState**& pd3d11Samplers, BindPointsD3D11*& pBindPoints) + __forceinline void GetSamplerArrays(CachedSampler*& Samplers, ID3D11SamplerState**& pd3d11Samplers, BindPointsD3D11*& pBindPoints) const { VERIFY(alignof(CachedSampler) == alignof(ID3D11SamplerState*), "Alignment mismatch, pointer to D3D11 resource may not be properly aligned"); - Samplers = reinterpret_cast<CachedSampler*>(m_pResourceData + m_SamplerOffset); + Samplers = reinterpret_cast<CachedSampler*>(m_pResourceData.get() + m_SamplerOffset); pd3d11Samplers = reinterpret_cast<ID3D11SamplerState**>(Samplers + GetSamplerCount()); pBindPoints = reinterpret_cast<BindPointsD3D11*>(pd3d11Samplers + GetSamplerCount()); } - __forceinline void GetUAVArrays(CachedResource*& UAVResources, ID3D11UnorderedAccessView**& pd3d11UAVs, BindPointsD3D11*& pBindPoints) + __forceinline void GetUAVArrays(CachedResource*& UAVResources, ID3D11UnorderedAccessView**& pd3d11UAVs, BindPointsD3D11*& pBindPoints) const { VERIFY(alignof(CachedResource) == alignof(ID3D11UnorderedAccessView*), "Alignment mismatch, pointer to D3D11 resource may not be properly aligned"); - UAVResources = reinterpret_cast<CachedResource*>(m_pResourceData + m_UAVOffset); + UAVResources = reinterpret_cast<CachedResource*>(m_pResourceData.get() + m_UAVOffset); pd3d11UAVs = reinterpret_cast<ID3D11UnorderedAccessView**>(UAVResources + GetUAVCount()); pBindPoints = reinterpret_cast<BindPointsD3D11*>(pd3d11UAVs + GetUAVCount()); } @@ -388,7 +336,7 @@ public: __forceinline void GetConstCBArrays(CachedCB const*& CBs, ID3D11Buffer* const*& pd3d11CBs, BindPointsD3D11 const*& pBindPoints) const { VERIFY(alignof(CachedCB) == alignof(ID3D11Buffer*), "Alignment mismatch, pointer to D3D11 resource may not be properly aligned"); - CBs = reinterpret_cast<CachedCB const*>(m_pResourceData + m_CBOffset); + CBs = reinterpret_cast<CachedCB const*>(m_pResourceData.get() + m_CBOffset); pd3d11CBs = reinterpret_cast<ID3D11Buffer* const*>(CBs + GetCBCount()); pBindPoints = reinterpret_cast<BindPointsD3D11 const*>(pd3d11CBs + GetCBCount()); } @@ -396,7 +344,7 @@ public: __forceinline void GetConstSRVArrays(CachedResource const*& SRVResources, ID3D11ShaderResourceView* const*& d3d11SRVs, BindPointsD3D11 const*& pBindPoints) const { VERIFY(alignof(CachedResource) == alignof(ID3D11ShaderResourceView*), "Alignment mismatch, pointer to D3D11 resource may not be properly aligned"); - SRVResources = reinterpret_cast<CachedResource const*>(m_pResourceData + m_SRVOffset); + SRVResources = reinterpret_cast<CachedResource const*>(m_pResourceData.get() + m_SRVOffset); d3d11SRVs = reinterpret_cast<ID3D11ShaderResourceView* const*>(SRVResources + GetSRVCount()); pBindPoints = reinterpret_cast<BindPointsD3D11 const*>(d3d11SRVs + GetSRVCount()); } @@ -404,7 +352,7 @@ public: __forceinline void GetConstSamplerArrays(CachedSampler const*& Samplers, ID3D11SamplerState* const*& pd3d11Samplers, BindPointsD3D11 const*& pBindPoints) const { VERIFY(alignof(CachedSampler) == alignof(ID3D11SamplerState*), "Alignment mismatch, pointer to D3D11 resource may not be properly aligned"); - Samplers = reinterpret_cast<CachedSampler const*>(m_pResourceData + m_SamplerOffset); + Samplers = reinterpret_cast<CachedSampler const*>(m_pResourceData.get() + m_SamplerOffset); pd3d11Samplers = reinterpret_cast<ID3D11SamplerState* const*>(Samplers + GetSamplerCount()); pBindPoints = reinterpret_cast<BindPointsD3D11 const*>(pd3d11Samplers + GetSamplerCount()); } @@ -412,7 +360,7 @@ public: __forceinline void GetConstUAVArrays(CachedResource const*& UAVResources, ID3D11UnorderedAccessView* const*& pd3d11UAVs, BindPointsD3D11 const*& pBindPoints) const { VERIFY(alignof(CachedResource) == alignof(ID3D11UnorderedAccessView*), "Alignment mismatch, pointer to D3D11 resource may not be properly aligned"); - UAVResources = reinterpret_cast<CachedResource const*>(m_pResourceData + m_UAVOffset); + UAVResources = reinterpret_cast<CachedResource const*>(m_pResourceData.get() + m_UAVOffset); pd3d11UAVs = reinterpret_cast<ID3D11UnorderedAccessView* const*>(UAVResources + GetUAVCount()); pBindPoints = reinterpret_cast<BindPointsD3D11 const*>(pd3d11UAVs + GetUAVCount()); } @@ -440,22 +388,18 @@ private: d3d11ResArr[CacheOffset] = pd3d11Resource; } - template <typename THandleResource> - void ProcessResources(THandleResource HandleResource); + // Transitions or verifies the resource state. + template <StateTransitionMode Mode> + void TransitionResources(DeviceContextD3D11Impl& Ctx, const ID3D11Buffer* /*Selector*/) const; - // Transitions resource to the shader resource state required by Type member. - __forceinline void TransitionResource(DeviceContextD3D11Impl& Ctx, Uint32 Count, CachedCB* CBs, ID3D11Buffer** pd3d11CBs); - __forceinline void TransitionResource(DeviceContextD3D11Impl& Ctx, Uint32 Count, CachedResource* SRVResources, ID3D11ShaderResourceView** d3d11SRVs); - __forceinline void TransitionResource(DeviceContextD3D11Impl& Ctx, Uint32 Count, CachedSampler* Samplers, ID3D11SamplerState** pd3d11Samplers); - __forceinline void TransitionResource(DeviceContextD3D11Impl& Ctx, Uint32 Count, CachedResource* UAVResources, ID3D11UnorderedAccessView** pd3d11UAVs); + template <StateTransitionMode Mode> + void TransitionResources(DeviceContextD3D11Impl& Ctx, const ID3D11ShaderResourceView* /*Selector*/) const; -#ifdef DILIGENT_DEVELOPMENT - // Verifies that resource is in correct shader resource state required by Type member. - void DvpVerifyResourceState(DeviceContextD3D11Impl& Ctx, Uint32 Count, const CachedCB* CBs, ID3D11Buffer**); - void DvpVerifyResourceState(DeviceContextD3D11Impl& Ctx, Uint32 Count, const CachedResource* SRVResources, ID3D11ShaderResourceView**); - void DvpVerifyResourceState(DeviceContextD3D11Impl& Ctx, Uint32 Count, const CachedSampler* Samplers, ID3D11SamplerState**); - void DvpVerifyResourceState(DeviceContextD3D11Impl& Ctx, Uint32 Count, const CachedResource* UAVResources, ID3D11UnorderedAccessView**); -#endif + template <StateTransitionMode Mode> + void TransitionResources(DeviceContextD3D11Impl& Ctx, const ID3D11SamplerState* /*Selector*/) const; + + template <StateTransitionMode Mode> + void TransitionResources(DeviceContextD3D11Impl& Ctx, const ID3D11UnorderedAccessView* /*Selector*/) const; private: using OffsetType = Uint16; @@ -473,18 +417,20 @@ private: OffsetType m_UAVOffset = InvalidResourceOffset; static constexpr Uint32 _CBCountBits = 7; - static constexpr Uint32 _SVCountBits = 10; + static constexpr Uint32 _SRVCountBits = 10; static constexpr Uint32 _SampCountBits = 7; static constexpr Uint32 _UAVCountBits = 4; + static constexpr int NumShaderTypes = BindPointsD3D11::NumShaderTypes; + // clang-format off - static_assert((1U << _CBCountBits) >= (D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT * NumShaderTypes), "Number of constant buffers is too small"); - static_assert((1U << _SVCountBits) >= (D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT * NumShaderTypes), "Number of shader resources is too small"); - static_assert((1U << _SampCountBits) >= (D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT * NumShaderTypes), "Number of samplers is too small"); - static_assert((1U << _UAVCountBits) >= D3D11_PS_CS_UAV_REGISTER_COUNT, "Number of UAVs is too small"); + static_assert((1U << _CBCountBits) >= (D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT * NumShaderTypes), "Not enough bits to represent CB count"); + static_assert((1U << _SRVCountBits) >= (D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT * NumShaderTypes), "Not enough bits to represent SRV count"); + static_assert((1U << _SampCountBits) >= (D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT * NumShaderTypes), "Not enough bits to represent Sampler count"); + static_assert((1U << _UAVCountBits) >= D3D11_PS_CS_UAV_REGISTER_COUNT, "Not enough bits to represent UAV count"); Uint32 m_CBCount : _CBCountBits; - Uint32 m_SRVCount : _SVCountBits; + Uint32 m_SRVCount : _SRVCountBits; Uint32 m_SamplerCount : _SampCountBits; Uint32 m_UAVCount : _UAVCountBits; // clang-format on @@ -492,8 +438,11 @@ private: // Indicates what types of resources are stored in the cache const ResourceCacheContentType m_ContentType; - Uint8* m_pResourceData = nullptr; - IMemoryAllocator* m_pAllocator = nullptr; + std::unique_ptr<Uint8, STDDeleter<Uint8, IMemoryAllocator>> m_pResourceData; }; +// Instantiate templates +template void ShaderResourceCacheD3D11::TransitionResourceStates<ShaderResourceCacheD3D11::StateTransitionMode::Transition>(DeviceContextD3D11Impl& Ctx); +template void ShaderResourceCacheD3D11::TransitionResourceStates<ShaderResourceCacheD3D11::StateTransitionMode::Verify>(DeviceContextD3D11Impl& Ctx); + } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.hpp b/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.hpp index bd6e859b..5dc8f744 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.hpp +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourcesD3D11.hpp @@ -30,59 +30,6 @@ /// \file /// Declaration of Diligent::ShaderResourcesD3D11 class - -// ShaderResourcesD3D11 are created by ShaderD3D11Impl instances. They are then referenced by ShaderResourceLayoutD3D11 objects, which are in turn -// created by instances of ShaderResourceBindingsD3D11Impl and PipelineStateD3D11Impl -// -// _________________ -// | | -// | ShaderD3D11Impl | -// |_________________| -// | -// |shared_ptr -// ________V_____________ _____________________________________________________________________ -// | | unique_ptr | | | | | | | -// | ShaderResourcesD3D11 |--------------->| CBs | TexSRVs | TexUAVs | BufSRVs | BufUAVs | Samplers | -// |______________________| |________|___________|___________|___________|___________|____________| -// A A A A A A A -// | \ \ \ \ \ \ -// |shared_ptr Ref Ref Ref Ref Ref Ref -// ________|__________________ ____\_________\__________\__________\___________\_______ ___\______ -// | | unique_ptr | | | | | | | -// | ShaderResourceLayoutD3D11 |--------------->| CBs | TexSRVs | TexUAVs | BufSRVs | BufUAVs | Samplers | -// |___________________________| |________|___________|___________|___________|___________|__________| -// | A -// |_________________SamplerIndex_________________| -// -// -// One ShaderResourcesD3D11 instance can be referenced by multiple objects -// -// -// ____<m_pResourceLayouts>___ ________________________________ -// | | | | -// ----| ShaderResourceLayoutD3D11 |<-----| ShaderResourceBindingD3D11Impl | -// | |___________________________| |________________________________| -// | -// | -// _________________ ______________________ | ____<m_pResourceLayouts>___ ________________________________ -// | | shared_ptr | | shared_ptr| | | | | -// | ShaderD3D11Impl |--------------->| ShaderResourcesD3D11 |<---------------| ShaderResourceLayoutD3D11 |<-----| ShaderResourceBindingD3D11Impl | -// |_________________| |______________________| | |___________________________| |________________________________| -// A | -// | | -// _____<StaticResLayout>_____ | | ____<m_pResourceLayouts>___ ________________________________ -// | | shared_ptr | | | | | | -// | ShaderResourceLayoutD3D11 |------------------- ----| ShaderResourceLayoutD3D11 |<-----| ShaderResourceBindingD3D11Impl | -// |___________________________| |___________________________| |________________________________| -// A -// ___________|______________ -// | | -// | PipelineStateD3D11Impl | -// |__________________________| -// - -#include <vector> - #include "ShaderResources.hpp" namespace Diligent @@ -111,17 +58,6 @@ public: __forceinline Int32 GetMaxUAVBindPoint() const { return m_MaxUAVBindPoint; } // clang-format on -#ifdef DILIGENT_DEVELOPMENT - void dvpVerifyCommittedResources(ID3D11Buffer* CommittedD3D11CBs[], - ID3D11ShaderResourceView* CommittedD3D11SRVs[], - ID3D11Resource* CommittedD3D11SRVResources[], - ID3D11SamplerState* CommittedD3D11Samplers[], - ID3D11UnorderedAccessView* CommittedD3D11UAVs[], - ID3D11Resource* CommittedD3D11UAVResources[], - class ShaderResourceCacheD3D11& ResourceCache) const; -#endif - - private: using MaxBindPointType = Int8; diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index 6c62dc89..90a4c73b 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -233,7 +233,7 @@ void DeviceContextD3D11Impl::TransitionShaderResources(IPipelineState* pPipeline auto* pShaderResBindingD3D11 = ValidatedCast<ShaderResourceBindingD3D11Impl>(pShaderResourceBinding); auto& ResourceCache = pShaderResBindingD3D11->GetResourceCache(); - ResourceCache.TransitionResourceStates(*this, ShaderResourceCacheD3D11::StateTransitionMode::Transition); + ResourceCache.TransitionResourceStates<ShaderResourceCacheD3D11::StateTransitionMode::Transition>(*this); } void DeviceContextD3D11Impl::CommitShaderResources(IShaderResourceBinding* pShaderResourceBinding, RESOURCE_STATE_TRANSITION_MODE StateTransitionMode) @@ -254,27 +254,179 @@ void DeviceContextD3D11Impl::CommitShaderResources(IShaderResourceBinding* pShad if (StateTransitionMode == RESOURCE_STATE_TRANSITION_MODE_TRANSITION) { - ResourceCache.TransitionResourceStates(*this, ShaderResourceCacheD3D11::StateTransitionMode::Transition); + ResourceCache.TransitionResourceStates<ShaderResourceCacheD3D11::StateTransitionMode::Transition>(*this); } #ifdef DILIGENT_DEVELOPMENT else if (StateTransitionMode == RESOURCE_STATE_TRANSITION_MODE_VERIFY) { - ResourceCache.TransitionResourceStates(*this, ShaderResourceCacheD3D11::StateTransitionMode::Verify); + ResourceCache.TransitionResourceStates<ShaderResourceCacheD3D11::StateTransitionMode::Verify>(*this); } #endif } + +void DeviceContextD3D11Impl::BindCacheResources(const ShaderResourceCacheD3D11& ResourceCache, + const TBindingsPerStage& BaseBindings, + TMinMaxSlotPerStage& MinMaxSlot, + SHADER_TYPE ActiveStages) +{ + const auto CBCount = ResourceCache.GetCBCount(); + if (CBCount != 0) + { + constexpr auto Range = D3D11_RESOURCE_RANGE_CBV; + ShaderResourceCacheD3D11::CachedCB const* CBs; + ID3D11Buffer* const* d3d11CBs; + BindPointsD3D11 const* bindPoints; + ResourceCache.GetConstCBArrays(CBs, d3d11CBs, bindPoints); + + for (Uint32 cb = 0; cb < CBCount; ++cb) + { + const auto& BindPoints = bindPoints[cb]; + Uint32 ActiveBits = BindPoints.GetActiveBits() & ActiveStages; + VERIFY(ActiveBits != 0, "resource is not initialized"); + while (ActiveBits != 0) + { + const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits); + ActiveBits &= ~(1u << ShaderInd); + VERIFY_EXPR(BindPoints.IsValid(ShaderInd)); + + auto* CommittedD3D11CBs = m_CommittedRes.D3D11CBs[ShaderInd]; + UINT& MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot; + UINT& MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot; + const UINT Slot = BaseBindings[ShaderInd][Range] + BindPoints[ShaderInd]; + const bool IsNewCB = CommittedD3D11CBs[Slot] != d3d11CBs[cb]; + MinSlot = IsNewCB ? std::min(MinSlot, Slot) : MinSlot; + MaxSlot = IsNewCB ? Slot : MaxSlot; + + VERIFY_EXPR(!IsNewCB || (Slot >= MinSlot && Slot <= MaxSlot)); + VERIFY_EXPR(d3d11CBs[cb] != nullptr); + CommittedD3D11CBs[Slot] = d3d11CBs[cb]; + } + } + } + + const auto SRVCount = ResourceCache.GetSRVCount(); + if (SRVCount != 0) + { + constexpr auto Range = D3D11_RESOURCE_RANGE_SRV; + ShaderResourceCacheD3D11::CachedResource const* SRVResources; + ID3D11ShaderResourceView* const* d3d11SRVs; + BindPointsD3D11 const* bindPoints; + ResourceCache.GetConstSRVArrays(SRVResources, d3d11SRVs, bindPoints); + + for (Uint32 srv = 0; srv < SRVCount; ++srv) + { + const auto& BindPoints = bindPoints[srv]; + Uint32 ActiveBits = BindPoints.GetActiveBits() & ActiveStages; + VERIFY(ActiveBits != 0, "resource is not initialized"); + while (ActiveBits != 0) + { + const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits); + ActiveBits &= ~(1u << ShaderInd); + VERIFY_EXPR(BindPoints.IsValid(ShaderInd)); + + auto* CommittedD3D11SRVs = m_CommittedRes.D3D11SRVs[ShaderInd]; + auto* CommittedD3D11SRVRes = m_CommittedRes.D3D11SRVResources[ShaderInd]; + UINT& MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot; + UINT& MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot; + const UINT Slot = BaseBindings[ShaderInd][Range] + BindPoints[ShaderInd]; + const bool IsNewSRV = CommittedD3D11SRVs[Slot] != d3d11SRVs[srv]; + MinSlot = IsNewSRV ? std::min(MinSlot, Slot) : MinSlot; + MaxSlot = IsNewSRV ? Slot : MaxSlot; + + VERIFY_EXPR(!IsNewSRV || (Slot >= MinSlot && Slot <= MaxSlot)); + VERIFY_EXPR(d3d11SRVs[srv] != nullptr); + CommittedD3D11SRVRes[Slot] = SRVResources[srv].pd3d11Resource; + CommittedD3D11SRVs[Slot] = d3d11SRVs[srv]; + } + } + } + + const auto SamplerCount = ResourceCache.GetSamplerCount(); + if (SamplerCount != 0) + { + constexpr auto Range = D3D11_RESOURCE_RANGE_SAMPLER; + ShaderResourceCacheD3D11::CachedSampler const* Samplers; + ID3D11SamplerState* const* d3d11Samplers; + BindPointsD3D11 const* bindPoints; + ResourceCache.GetConstSamplerArrays(Samplers, d3d11Samplers, bindPoints); + + for (Uint32 sam = 0; sam < SamplerCount; ++sam) + { + const auto& BindPoints = bindPoints[sam]; + Uint32 ActiveBits = BindPoints.GetActiveBits() & ActiveStages; + VERIFY(ActiveBits != 0, "resource is not initialized"); + while (ActiveBits != 0) + { + const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits); + ActiveBits &= ~(1u << ShaderInd); + VERIFY_EXPR(BindPoints.IsValid(ShaderInd)); + + auto* CommittedD3D11Samplers = m_CommittedRes.D3D11Samplers[ShaderInd]; + UINT& MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot; + UINT& MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot; + const UINT Slot = BaseBindings[ShaderInd][Range] + BindPoints[ShaderInd]; + const bool IsNewSam = CommittedD3D11Samplers[Slot] != d3d11Samplers[sam]; + MinSlot = IsNewSam ? std::min(MinSlot, Slot) : MinSlot; + MaxSlot = IsNewSam ? Slot : MaxSlot; + + VERIFY_EXPR(!IsNewSam || (Slot >= MinSlot && Slot <= MaxSlot)); + VERIFY_EXPR(d3d11Samplers[sam] != nullptr); + CommittedD3D11Samplers[Slot] = d3d11Samplers[sam]; + } + } + } + + const auto UAVCount = ResourceCache.GetUAVCount(); + if (UAVCount != 0) + { + constexpr auto Range = D3D11_RESOURCE_RANGE_UAV; + ShaderResourceCacheD3D11::CachedResource const* UAVResources; + ID3D11UnorderedAccessView* const* d3d11UAVs; + BindPointsD3D11 const* bindPoints; + ResourceCache.GetConstUAVArrays(UAVResources, d3d11UAVs, bindPoints); + + for (Uint32 uav = 0; uav < UAVCount; ++uav) + { + const auto& BindPoints = bindPoints[uav]; + Uint32 ActiveBits = BindPoints.GetActiveBits() & ActiveStages; + VERIFY(ActiveBits != 0, "resource is not initialized"); + while (ActiveBits != 0) + { + const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits); + ActiveBits &= ~(1u << ShaderInd); + VERIFY_EXPR(BindPoints.IsValid(ShaderInd)); + + auto* CommittedD3D11UAVs = m_CommittedRes.D3D11UAVs[ShaderInd]; + auto* CommittedD3D11UAVRes = m_CommittedRes.D3D11UAVResources[ShaderInd]; + UINT& MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot; + UINT& MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot; + const UINT Slot = BaseBindings[ShaderInd][Range] + BindPoints[ShaderInd]; + const bool IsNewUAV = CommittedD3D11UAVs[Slot] != d3d11UAVs[uav]; + MinSlot = IsNewUAV ? std::min(MinSlot, Slot) : MinSlot; + MaxSlot = IsNewUAV ? Slot : MaxSlot; + + VERIFY_EXPR(!IsNewUAV || (Slot >= MinSlot && Slot <= MaxSlot)); + VERIFY_EXPR(d3d11UAVs[uav] != nullptr); + CommittedD3D11UAVRes[Slot] = UAVResources[uav].pd3d11Resource; + CommittedD3D11UAVs[Slot] = d3d11UAVs[uav]; + } + } + } +} + void DeviceContextD3D11Impl::BindShaderResources() { if ((m_BindInfo.StaleSRBMask & m_BindInfo.ActiveSRBMask) == 0) return; + TBindingsPerStage Bindings = {}; TMinMaxSlotPerStage MinMaxSlot = {}; const auto ActiveStages = m_BindInfo.ActiveStages; if (m_pPipelineState->GetDesc().IsAnyGraphicsPipeline()) - Bindings[GetShaderTypeIndex(SHADER_TYPE_PIXEL)][DESCRIPTOR_RANGE_UAV] = static_cast<Uint8>(m_pPipelineState->GetGraphicsPipelineDesc().NumRenderTargets); + Bindings[GetShaderTypeIndex(SHADER_TYPE_PIXEL)][D3D11_RESOURCE_RANGE_UAV] = static_cast<Uint8>(m_pPipelineState->GetGraphicsPipelineDesc().NumRenderTargets); auto ActiveSRBMask = Uint32{m_BindInfo.ActiveSRBMask}; while (ActiveSRBMask != 0) @@ -293,7 +445,7 @@ void DeviceContextD3D11Impl::BindShaderResources() #ifdef DILIGENT_DEVELOPMENT m_BindInfo.BoundResOffsets[sign] = Bindings; #endif - pSRB->GetResourceCache().BindResources(*this, Bindings, MinMaxSlot, m_CommittedRes, ActiveStages); + BindCacheResources(pSRB->GetResourceCache(), Bindings, MinMaxSlot, ActiveStages); } pSRB->GetSignature()->ShiftBindings(Bindings); } @@ -308,7 +460,7 @@ void DeviceContextD3D11Impl::BindShaderResources() // CBV { - const auto Range = DESCRIPTOR_RANGE_CBV; + const auto Range = D3D11_RESOURCE_RANGE_CBV; const UINT MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot; const UINT MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot; if (MinSlot != UINT_MAX) @@ -328,7 +480,7 @@ void DeviceContextD3D11Impl::BindShaderResources() // SRV { - const auto Range = DESCRIPTOR_RANGE_SRV; + const auto Range = D3D11_RESOURCE_RANGE_SRV; const UINT MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot; const UINT MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot; if (MinSlot != UINT_MAX) @@ -348,7 +500,7 @@ void DeviceContextD3D11Impl::BindShaderResources() // Sampler { - const auto Range = DESCRIPTOR_RANGE_SAMPLER; + const auto Range = D3D11_RESOURCE_RANGE_SAMPLER; const UINT MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot; const UINT MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot; if (MinSlot != UINT_MAX) @@ -368,7 +520,7 @@ void DeviceContextD3D11Impl::BindShaderResources() // UAV { - const auto Range = DESCRIPTOR_RANGE_UAV; + const auto Range = D3D11_RESOURCE_RANGE_UAV; const UINT MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot; const UINT MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot; if (MinSlot != UINT_MAX) diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineResourceSignatureD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineResourceSignatureD3D11Impl.cpp index 4e3fdea4..8aae6a01 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineResourceSignatureD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineResourceSignatureD3D11Impl.cpp @@ -32,23 +32,23 @@ namespace Diligent { -DESCRIPTOR_RANGE ShaderResourceToDescriptorRange(SHADER_RESOURCE_TYPE Type) +D3D11_RESOURCE_RANGE ShaderResourceToDescriptorRange(SHADER_RESOURCE_TYPE Type) { static_assert(SHADER_RESOURCE_TYPE_LAST == 8, "Please update the switch below to handle the new shader resource type"); switch (Type) { // clang-format off - case SHADER_RESOURCE_TYPE_CONSTANT_BUFFER: return DESCRIPTOR_RANGE_CBV; - case SHADER_RESOURCE_TYPE_TEXTURE_SRV: return DESCRIPTOR_RANGE_SRV; - case SHADER_RESOURCE_TYPE_BUFFER_SRV: return DESCRIPTOR_RANGE_SRV; - case SHADER_RESOURCE_TYPE_TEXTURE_UAV: return DESCRIPTOR_RANGE_UAV; - case SHADER_RESOURCE_TYPE_BUFFER_UAV: return DESCRIPTOR_RANGE_UAV; - case SHADER_RESOURCE_TYPE_SAMPLER: return DESCRIPTOR_RANGE_SAMPLER; - case SHADER_RESOURCE_TYPE_INPUT_ATTACHMENT: return DESCRIPTOR_RANGE_SRV; + case SHADER_RESOURCE_TYPE_CONSTANT_BUFFER: return D3D11_RESOURCE_RANGE_CBV; + case SHADER_RESOURCE_TYPE_TEXTURE_SRV: return D3D11_RESOURCE_RANGE_SRV; + case SHADER_RESOURCE_TYPE_BUFFER_SRV: return D3D11_RESOURCE_RANGE_SRV; + case SHADER_RESOURCE_TYPE_TEXTURE_UAV: return D3D11_RESOURCE_RANGE_UAV; + case SHADER_RESOURCE_TYPE_BUFFER_UAV: return D3D11_RESOURCE_RANGE_UAV; + case SHADER_RESOURCE_TYPE_SAMPLER: return D3D11_RESOURCE_RANGE_SAMPLER; + case SHADER_RESOURCE_TYPE_INPUT_ATTACHMENT: return D3D11_RESOURCE_RANGE_SRV; // clang-format on default: UNEXPECTED("Unsupported resource type"); - return DESCRIPTOR_RANGE_UNKNOWN; + return D3D11_RESOURCE_RANGE_UNKNOWN; } } @@ -117,7 +117,7 @@ PipelineResourceSignatureD3D11Impl::PipelineResourceSignatureD3D11Impl(IReferenc void PipelineResourceSignatureD3D11Impl::CreateLayout() { - using TBindings32 = std::array<Uint32, DESCRIPTOR_RANGE_COUNT>; + using TBindings32 = std::array<Uint32, D3D11_RESOURCE_RANGE_COUNT>; using TBindingsPerStage32 = std::array<TBindings32, NumShaderTypes>; if (m_pStaticResCache) @@ -175,7 +175,7 @@ void PipelineResourceSignatureD3D11Impl::CreateLayout() TBindings32 ResourceCount = {}; TBindingsPerStage32 BindingPerStage = {}; - const auto AllocBindPoints = [&BindingPerStage](BindPointsD3D11& BindPoints, SHADER_TYPE ShaderStages, Uint32 ArraySize, DESCRIPTOR_RANGE Range) // + const auto AllocBindPoints = [&BindingPerStage](BindPointsD3D11& BindPoints, SHADER_TYPE ShaderStages, Uint32 ArraySize, D3D11_RESOURCE_RANGE Range) // { while (ShaderStages != 0) { @@ -202,7 +202,7 @@ void PipelineResourceSignatureD3D11Impl::CreateLayout() } constexpr SHADER_TYPE UAVStages = SHADER_TYPE_PIXEL | SHADER_TYPE_COMPUTE; - if (Range == DESCRIPTOR_RANGE_UAV && (ResDesc.ShaderStages & ~UAVStages) != 0) + if (Range == D3D11_RESOURCE_RANGE_UAV && (ResDesc.ShaderStages & ~UAVStages) != 0) { LOG_ERROR_AND_THROW("Description of a pipeline resource signature '", m_Desc.Name, "' is invalid: ", "Desc.Resources[", i, "].ResourceType (", GetShaderResourceTypeLiteralName(ResDesc.ResourceType), @@ -218,9 +218,9 @@ void PipelineResourceSignatureD3D11Impl::CreateLayout() if (!ImtblSampAttribs.IsAllocated()) { - ImtblSampAttribs.CacheOffset = ResourceCount[DESCRIPTOR_RANGE_SAMPLER]; - AllocBindPoints(ImtblSampAttribs.BindPoints, ImtblSamp.ShaderStages, ImtblSampAttribs.ArraySize, DESCRIPTOR_RANGE_SAMPLER); - ResourceCount[DESCRIPTOR_RANGE_SAMPLER] += ImtblSampAttribs.ArraySize; + ImtblSampAttribs.CacheOffset = ResourceCount[D3D11_RESOURCE_RANGE_SAMPLER]; + AllocBindPoints(ImtblSampAttribs.BindPoints, ImtblSamp.ShaderStages, ImtblSampAttribs.ArraySize, D3D11_RESOURCE_RANGE_SAMPLER); + ResourceCount[D3D11_RESOURCE_RANGE_SAMPLER] += ImtblSampAttribs.ArraySize; } } @@ -253,7 +253,7 @@ void PipelineResourceSignatureD3D11Impl::CreateLayout() // Add bindings for immutable samplers that is not assigned to texture or separate sampler. for (Uint32 i = 0; i < m_Desc.NumImmutableSamplers; ++i) { - const auto Range = DESCRIPTOR_RANGE_SAMPLER; + const auto Range = D3D11_RESOURCE_RANGE_SAMPLER; const auto& ImtblSamp = GetImmutableSamplerDesc(i); auto& ImtblSampAttribs = m_ImmutableSamplers[i]; @@ -329,10 +329,10 @@ void PipelineResourceSignatureD3D11Impl::CopyStaticResources(ShaderResourceCache const auto& ResAttr = GetResourceAttribs(r); VERIFY_EXPR(ResDesc.VarType == SHADER_RESOURCE_VARIABLE_TYPE_STATIC); - static_assert(DESCRIPTOR_RANGE_COUNT == 4, "Please update the switch below to handle the new descriptor range"); + static_assert(D3D11_RESOURCE_RANGE_COUNT == 4, "Please update the switch below to handle the new descriptor range"); switch (ShaderResourceToDescriptorRange(ResDesc.ResourceType)) { - case DESCRIPTOR_RANGE_CBV: + case D3D11_RESOURCE_RANGE_CBV: for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd) { const auto& SrcCachedRes = SrcResourceCache.GetCB(ResAttr.CacheOffset + ArrInd); @@ -342,7 +342,7 @@ void PipelineResourceSignatureD3D11Impl::CopyStaticResources(ShaderResourceCache DstResourceCache.SetCB(ResAttr.CacheOffset + ArrInd, ResAttr.BindPoints + ArrInd, RefCntAutoPtr<BufferD3D11Impl>{SrcCachedRes.pBuff}); } break; - case DESCRIPTOR_RANGE_SRV: + case D3D11_RESOURCE_RANGE_SRV: for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd) { const auto& SrcCachedRes = SrcResourceCache.GetSRV(ResAttr.CacheOffset + ArrInd); @@ -355,7 +355,7 @@ void PipelineResourceSignatureD3D11Impl::CopyStaticResources(ShaderResourceCache DstResourceCache.SetBufSRV(ResAttr.CacheOffset + ArrInd, ResAttr.BindPoints + ArrInd, RefCntAutoPtr<BufferViewD3D11Impl>{SrcCachedRes.pView.RawPtr<BufferViewD3D11Impl>()}); } break; - case DESCRIPTOR_RANGE_SAMPLER: + case D3D11_RESOURCE_RANGE_SAMPLER: // Immutable samplers will be copied in InitSRBResourceCache(). if (!ResAttr.IsImmutableSamplerAssigned()) { @@ -369,7 +369,7 @@ void PipelineResourceSignatureD3D11Impl::CopyStaticResources(ShaderResourceCache } } break; - case DESCRIPTOR_RANGE_UAV: + case D3D11_RESOURCE_RANGE_UAV: for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd) { const auto& SrcCachedRes = SrcResourceCache.GetUAV(ResAttr.CacheOffset + ArrInd); @@ -441,7 +441,7 @@ void PipelineResourceSignatureD3D11Impl::UpdateShaderResourceBindingMap(Resource { const auto& ImtblSam = GetImmutableSamplerDesc(samp); const auto& SampAttr = GetImmutableSamplerAttribs(samp); - const auto Range = DESCRIPTOR_RANGE_SAMPLER; + const auto Range = D3D11_RESOURCE_RANGE_SAMPLER; if ((ImtblSam.ShaderStages & ShaderStage) != 0 && SampAttr.IsAllocated()) { @@ -489,7 +489,7 @@ bool PipelineResourceSignatureD3D11Impl::DvpValidateCommittedResource(const D3DS bool BindingsOK = true; switch (ShaderResourceToDescriptorRange(ResDesc.ResourceType)) { - case DESCRIPTOR_RANGE_CBV: + case D3D11_RESOURCE_RANGE_CBV: for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd) { if (!ResourceCache.IsCBBound(ResAttr.CacheOffset + ArrInd)) @@ -501,7 +501,7 @@ bool PipelineResourceSignatureD3D11Impl::DvpValidateCommittedResource(const D3DS } break; - case DESCRIPTOR_RANGE_SAMPLER: + case D3D11_RESOURCE_RANGE_SAMPLER: for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd) { if (!ResourceCache.IsSamplerBound(ResAttr.CacheOffset + ArrInd)) @@ -513,7 +513,7 @@ bool PipelineResourceSignatureD3D11Impl::DvpValidateCommittedResource(const D3DS } break; - case DESCRIPTOR_RANGE_SRV: + case D3D11_RESOURCE_RANGE_SRV: for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd) { const bool IsTexView = (ResDesc.ResourceType == SHADER_RESOURCE_TYPE_TEXTURE_SRV || ResDesc.ResourceType == SHADER_RESOURCE_TYPE_INPUT_ATTACHMENT); @@ -533,7 +533,7 @@ bool PipelineResourceSignatureD3D11Impl::DvpValidateCommittedResource(const D3DS } break; - case DESCRIPTOR_RANGE_UAV: + case D3D11_RESOURCE_RANGE_UAV: for (Uint32 ArrInd = 0; ArrInd < ResDesc.ArraySize; ++ArrInd) { const bool IsTexView = (ResDesc.ResourceType == SHADER_RESOURCE_TYPE_TEXTURE_SRV || ResDesc.ResourceType == SHADER_RESOURCE_TYPE_TEXTURE_UAV); diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index bd74ea9b..4cb13e5a 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -202,7 +202,7 @@ void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& PipelineResourceSignatureD3D11Impl::TBindingsPerStage BindingsPerStage = {}; if (m_Desc.IsAnyGraphicsPipeline()) - BindingsPerStage[PSInd][DESCRIPTOR_RANGE_UAV] = GetGraphicsPipelineDesc().NumRenderTargets; + BindingsPerStage[PSInd][D3D11_RESOURCE_RANGE_UAV] = GetGraphicsPipelineDesc().NumRenderTargets; ResourceBinding::TMap ResourceMap; for (Uint32 sign = 0; sign < m_SignatureCount; ++sign) @@ -232,7 +232,7 @@ void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& PipelineResourceSignatureD3D11Impl::TBindingsPerStage BindingsPerStage = {}; if (m_Desc.IsAnyGraphicsPipeline()) - BindingsPerStage[PSInd][DESCRIPTOR_RANGE_UAV] = GetGraphicsPipelineDesc().NumRenderTargets; + BindingsPerStage[PSInd][D3D11_RESOURCE_RANGE_UAV] = GetGraphicsPipelineDesc().NumRenderTargets; for (Uint32 sign = 0; sign < m_SignatureCount; ++sign) { @@ -245,14 +245,14 @@ void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& { const auto& BindCount = BindingsPerStage[s]; - DEV_CHECK_ERR(BindCount[DESCRIPTOR_RANGE_CBV] <= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, - "Constant buffer count ", Uint32{BindCount[DESCRIPTOR_RANGE_CBV]}, " exceeds D3D11 limit ", D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT); - DEV_CHECK_ERR(BindCount[DESCRIPTOR_RANGE_SRV] <= D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, - "SRV count ", Uint32{BindCount[DESCRIPTOR_RANGE_SRV]}, " exceeds D3D11 limit ", D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT); - DEV_CHECK_ERR(BindCount[DESCRIPTOR_RANGE_SAMPLER] <= D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, - "Sampler count ", Uint32{BindCount[DESCRIPTOR_RANGE_SAMPLER]}, " exceeds D3D11 limit ", D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT); - DEV_CHECK_ERR(BindCount[DESCRIPTOR_RANGE_UAV] <= D3D11_PS_CS_UAV_REGISTER_COUNT, - "UAV count ", Uint32{BindCount[DESCRIPTOR_RANGE_UAV]}, " exceeds D3D11 limit ", D3D11_PS_CS_UAV_REGISTER_COUNT); + DEV_CHECK_ERR(BindCount[D3D11_RESOURCE_RANGE_CBV] <= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, + "Constant buffer count ", Uint32{BindCount[D3D11_RESOURCE_RANGE_CBV]}, " exceeds D3D11 limit ", D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT); + DEV_CHECK_ERR(BindCount[D3D11_RESOURCE_RANGE_SRV] <= D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT, + "SRV count ", Uint32{BindCount[D3D11_RESOURCE_RANGE_SRV]}, " exceeds D3D11 limit ", D3D11_COMMONSHADER_INPUT_RESOURCE_SLOT_COUNT); + DEV_CHECK_ERR(BindCount[D3D11_RESOURCE_RANGE_SAMPLER] <= D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT, + "Sampler count ", Uint32{BindCount[D3D11_RESOURCE_RANGE_SAMPLER]}, " exceeds D3D11 limit ", D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT); + DEV_CHECK_ERR(BindCount[D3D11_RESOURCE_RANGE_UAV] <= D3D11_PS_CS_UAV_REGISTER_COUNT, + "UAV count ", Uint32{BindCount[D3D11_RESOURCE_RANGE_UAV]}, " exceeds D3D11 limit ", D3D11_PS_CS_UAV_REGISTER_COUNT); } #endif } @@ -525,7 +525,7 @@ void PipelineStateD3D11Impl::DvpVerifySRBResources(class ShaderResourceBindingD3 TBindingsPerStage Bindings = {}; if (m_Desc.IsAnyGraphicsPipeline()) - Bindings[GetShaderTypeIndex(SHADER_TYPE_PIXEL)][DESCRIPTOR_RANGE_UAV] = static_cast<Uint8>(GetGraphicsPipelineDesc().NumRenderTargets); + Bindings[GetShaderTypeIndex(SHADER_TYPE_PIXEL)][D3D11_RESOURCE_RANGE_UAV] = static_cast<Uint8>(GetGraphicsPipelineDesc().NumRenderTargets); for (Uint32 sign = 0; sign < SignCount; ++sign) { diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp index a254d526..0e874a78 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp @@ -38,13 +38,14 @@ namespace Diligent
{
+
size_t ShaderResourceCacheD3D11::GetRequriedMemorySize(const TResourceCount& ResCount)
{
// clang-format off
- auto CBCount = ResCount[DESCRIPTOR_RANGE_CBV];
- auto SRVCount = ResCount[DESCRIPTOR_RANGE_SRV];
- auto SamplerCount = ResCount[DESCRIPTOR_RANGE_SAMPLER];
- auto UAVCount = ResCount[DESCRIPTOR_RANGE_UAV];
+ auto CBCount = ResCount[D3D11_RESOURCE_RANGE_CBV];
+ auto SRVCount = ResCount[D3D11_RESOURCE_RANGE_SRV];
+ auto SamplerCount = ResCount[D3D11_RESOURCE_RANGE_SAMPLER];
+ auto UAVCount = ResCount[D3D11_RESOURCE_RANGE_UAV];
size_t MemSize = 0;
MemSize = AlignUp(MemSize + (sizeof(CachedCB) + sizeof(BindPointsD3D11) + sizeof(ID3D11Buffer*)) * CBCount, MaxAlignment);
MemSize = AlignUp(MemSize + (sizeof(CachedResource) + sizeof(BindPointsD3D11) + sizeof(ID3D11ShaderResourceView*)) * SRVCount, MaxAlignment);
@@ -58,16 +59,12 @@ size_t ShaderResourceCacheD3D11::GetRequriedMemorySize(const TResourceCount& Res void ShaderResourceCacheD3D11::Initialize(const TResourceCount& ResCount, IMemoryAllocator& MemAllocator)
{
// http://diligentgraphics.com/diligent-engine/architecture/d3d11/shader-resource-cache/
- if (IsInitialized())
- {
- LOG_ERROR_MESSAGE("Resource cache is already intialized");
- return;
- }
+ VERIFY(!IsInitialized(), "Resource cache has already been intialized!");
- const Uint32 CBCount = ResCount[DESCRIPTOR_RANGE_CBV];
- const Uint32 SRVCount = ResCount[DESCRIPTOR_RANGE_SRV];
- const Uint32 SamplerCount = ResCount[DESCRIPTOR_RANGE_SAMPLER];
- const Uint32 UAVCount = ResCount[DESCRIPTOR_RANGE_UAV];
+ const Uint32 CBCount = ResCount[D3D11_RESOURCE_RANGE_CBV];
+ const Uint32 SRVCount = ResCount[D3D11_RESOURCE_RANGE_SRV];
+ const Uint32 SamplerCount = ResCount[D3D11_RESOURCE_RANGE_SAMPLER];
+ const Uint32 UAVCount = ResCount[D3D11_RESOURCE_RANGE_UAV];
// clang-format off
m_CBCount = static_cast<decltype(m_CBCount )>(CBCount);
@@ -92,9 +89,11 @@ void ShaderResourceCacheD3D11::Initialize(const TResourceCount& ResCount, IMemor if (BufferSize > 0)
{
- m_pResourceData = ALLOCATE(MemAllocator, "Shader resource cache data buffer", Uint8, BufferSize);
- memset(m_pResourceData, 0, BufferSize);
- m_pAllocator = &MemAllocator;
+ m_pResourceData = decltype(m_pResourceData){
+ ALLOCATE(MemAllocator, "Shader resource cache data buffer", Uint8, BufferSize),
+ STDDeleter<Uint8, IMemoryAllocator>(MemAllocator) //
+ };
+ memset(m_pResourceData.get(), 0, BufferSize);
}
// Explicitly construct all objects
@@ -208,17 +207,14 @@ ShaderResourceCacheD3D11::~ShaderResourceCacheD3D11() m_SamplerCount = 0;
m_UAVCount = 0;
- if (m_pResourceData != nullptr)
- m_pAllocator->Free(m_pResourceData);
-
- m_pResourceData = nullptr;
- m_pAllocator = nullptr;
+ m_pResourceData.reset();
}
}
#ifdef DILIGENT_DEVELOPMENT
namespace
{
+
void DvpVerifyResource(ShaderResourceCacheD3D11::CachedResource& Res, ID3D11View* pd3d11View, const char* ViewType)
{
if (pd3d11View != nullptr)
@@ -236,7 +232,7 @@ void DvpVerifyResource(ShaderResourceCacheD3D11::CachedResource& Res, ID3D11View VERIFY(pd3d11ActualResource == Res.pBuffer->GetD3D11Buffer(), "Inconsistent buffer ", ViewType);
if (Res.pView)
{
- RefCntAutoPtr<IBufferViewD3D11> pBufView(Res.pView, IID_BufferViewD3D11);
+ RefCntAutoPtr<IBufferViewD3D11> pBufView{Res.pView, IID_BufferViewD3D11};
VERIFY(pBufView != nullptr, "Provided resource view is not D3D11 buffer view");
if (pBufView)
VERIFY(pBufView->GetBuffer() == Res.pBuffer, "Provided resource view is not a view of the buffer");
@@ -247,7 +243,7 @@ void DvpVerifyResource(ShaderResourceCacheD3D11::CachedResource& Res, ID3D11View VERIFY(pd3d11ActualResource == Res.pTexture->GetD3D11Texture(), "Inconsistent texture ", ViewType);
if (Res.pView)
{
- RefCntAutoPtr<ITextureViewD3D11> pTexView(Res.pView, IID_TextureViewD3D11);
+ RefCntAutoPtr<ITextureViewD3D11> pTexView{Res.pView, IID_TextureViewD3D11};
VERIFY(pTexView != nullptr, "Provided resource view is not D3D11 texture view");
if (pTexView)
VERIFY(pTexView->GetTexture() == Res.pTexture, "Provided resource view is not a view of the texture");
@@ -261,6 +257,7 @@ void DvpVerifyResource(ShaderResourceCacheD3D11::CachedResource& Res, ID3D11View VERIFY(Res.pd3d11Resource == nullptr, "Unexepected D3D11 resource");
}
}
+
} // namespace
void ShaderResourceCacheD3D11::DvpVerifyCacheConsistency()
@@ -324,391 +321,153 @@ void ShaderResourceCacheD3D11::DvpVerifyCacheConsistency() }
#endif
-template <typename THandleResource>
-void ShaderResourceCacheD3D11::ProcessResources(THandleResource HandleResources)
-{
- {
- ShaderResourceCacheD3D11::CachedCB* CachedCBs;
- ID3D11Buffer** d3d11CBs;
- BindPointsD3D11* bindPoints;
- GetCBArrays(CachedCBs, d3d11CBs, bindPoints);
- HandleResources(GetCBCount(), CachedCBs, d3d11CBs);
- }
- {
- ShaderResourceCacheD3D11::CachedResource* CachedSRVResources;
- ID3D11ShaderResourceView** d3d11SRVs;
- BindPointsD3D11* bindPoints;
- GetSRVArrays(CachedSRVResources, d3d11SRVs, bindPoints);
- HandleResources(GetSRVCount(), CachedSRVResources, d3d11SRVs);
- }
- {
- ShaderResourceCacheD3D11::CachedSampler* CachedSamplers;
- ID3D11SamplerState** d3d11Samplers;
- BindPointsD3D11* bindPoints;
- GetSamplerArrays(CachedSamplers, d3d11Samplers, bindPoints);
- HandleResources(GetSamplerCount(), CachedSamplers, d3d11Samplers);
- }
- {
- ShaderResourceCacheD3D11::CachedResource* CachedUAVResources;
- ID3D11UnorderedAccessView** d3d11UAVs;
- BindPointsD3D11* bindPoints;
- GetUAVArrays(CachedUAVResources, d3d11UAVs, bindPoints);
- HandleResources(GetUAVCount(), CachedUAVResources, d3d11UAVs);
- }
-}
-
-void ShaderResourceCacheD3D11::TransitionResourceStates(DeviceContextD3D11Impl& Ctx, StateTransitionMode Mode)
+template <ShaderResourceCacheD3D11::StateTransitionMode Mode>
+void ShaderResourceCacheD3D11::TransitionResourceStates(DeviceContextD3D11Impl& Ctx)
{
VERIFY_EXPR(IsInitialized());
- switch (Mode)
- {
- case StateTransitionMode::Transition:
- {
- ProcessResources([&](Uint32 Count, auto* Resources, auto* Views) { TransitionResource(Ctx, Count, Resources, Views); });
- break;
- }
- case StateTransitionMode::Verify:
- {
-#ifdef DILIGENT_DEVELOPMENT
- ProcessResources([&](Uint32 Count, auto* Resources, auto* Views) { DvpVerifyResourceState(Ctx, Count, Resources, Views); });
-#endif
- break;
- }
- default:
- UNEXPECTED("Unexpected mode");
- }
+ TransitionResources<Mode>(Ctx, static_cast<ID3D11Buffer*>(nullptr));
+ TransitionResources<Mode>(Ctx, static_cast<ID3D11ShaderResourceView*>(nullptr));
+ TransitionResources<Mode>(Ctx, static_cast<ID3D11SamplerState*>(nullptr));
+ TransitionResources<Mode>(Ctx, static_cast<ID3D11UnorderedAccessView*>(nullptr));
}
-void ShaderResourceCacheD3D11::TransitionResource(DeviceContextD3D11Impl& Ctx, Uint32 Count, CachedCB* CBs, ID3D11Buffer** pd3d11CBs)
+template <ShaderResourceCacheD3D11::StateTransitionMode Mode>
+void ShaderResourceCacheD3D11::TransitionResources(DeviceContextD3D11Impl& Ctx, const ID3D11Buffer* /*Selector*/) const
{
- for (Uint32 i = 0; i < Count; ++i)
+ const auto CBCount = GetCBCount();
+ if (CBCount == 0)
+ return;
+
+ CachedCB* CBs;
+ ID3D11Buffer** d3d11CBs;
+ BindPointsD3D11* bindPoints;
+ GetCBArrays(CBs, d3d11CBs, bindPoints);
+
+ for (Uint32 i = 0; i < CBCount; ++i)
{
if (auto* pBuffer = CBs[i].pBuff.RawPtr<BufferD3D11Impl>())
{
if (pBuffer->IsInKnownState() && !pBuffer->CheckState(RESOURCE_STATE_CONSTANT_BUFFER))
- { /*
- if (pBuffer->CheckState(RESOURCE_STATE_UNORDERED_ACCESS))
+ {
+ if (Mode == StateTransitionMode::Transition)
+ {
+ Ctx.TransitionResource(pBuffer, RESOURCE_STATE_CONSTANT_BUFFER);
+ }
+ else
{
- // Even though we have unbound resources from UAV, we only checked shader
- // stages active in current PSO, so we still may need to unbind the resource
- // from UAV (for instance, unbind resource from CS UAV when running draw command).
- Ctx.UnbindResourceFromUAV(pBuffer, pd3d11CBs[i]);
- pBuffer->ClearState(RESOURCE_STATE_UNORDERED_ACCESS);
+ LOG_ERROR_MESSAGE("Buffer '", pBuffer->GetDesc().Name,
+ "' has not been transitioned to Constant Buffer state. Call TransitionShaderResources(), use "
+ "RESOURCE_STATE_TRANSITION_MODE_TRANSITION mode or explicitly transition the buffer to required state.");
}
- pBuffer->AddState(RESOURCE_STATE_CONSTANT_BUFFER);*/
- Ctx.TransitionResource(pBuffer, RESOURCE_STATE_CONSTANT_BUFFER);
}
}
}
}
-void ShaderResourceCacheD3D11::TransitionResource(DeviceContextD3D11Impl& Ctx, Uint32 Count, CachedResource* SRVResources, ID3D11ShaderResourceView** d3d11SRVs)
+template <ShaderResourceCacheD3D11::StateTransitionMode Mode>
+void ShaderResourceCacheD3D11::TransitionResources(DeviceContextD3D11Impl& Ctx, const ID3D11ShaderResourceView* /*Selector*/) const
{
- for (Uint32 i = 0; i < Count; ++i)
+ const auto SRVCount = GetSRVCount();
+ if (SRVCount == 0)
+ return;
+
+ CachedResource* SRVResources;
+ ID3D11ShaderResourceView** d3d11SRVs;
+ BindPointsD3D11* bindPoints;
+ GetSRVArrays(SRVResources, d3d11SRVs, bindPoints);
+
+ for (Uint32 i = 0; i < SRVCount; ++i)
{
auto& SRVRes = SRVResources[i];
if (auto* pTexture = SRVRes.pTexture)
{
if (pTexture->IsInKnownState() && !pTexture->CheckAnyState(RESOURCE_STATE_SHADER_RESOURCE | RESOURCE_STATE_INPUT_ATTACHMENT))
- { /*
- if (pTexture->CheckState(RESOURCE_STATE_UNORDERED_ACCESS))
+ {
+ if (Mode == StateTransitionMode::Transition)
{
- // Even though we have unbound resources from UAV, we only checked shader
- // stages active in current PSO, so we still may need to unbind the resource
- // from UAV (for instance, unbind resource from CS UAV when running draw command).
- Ctx.UnbindResourceFromUAV(pTexture, SRVRes.pd3d11Resource);
- pTexture->ClearState(RESOURCE_STATE_UNORDERED_ACCESS);
+ Ctx.TransitionResource(pTexture, RESOURCE_STATE_SHADER_RESOURCE);
+ }
+ else
+ {
+ LOG_ERROR_MESSAGE("Texture '", pTexture->GetDesc().Name,
+ "' has not been transitioned to Shader Resource state. Call TransitionShaderResources(), use "
+ "RESOURCE_STATE_TRANSITION_MODE_TRANSITION mode or explicitly transition the texture to required state.");
}
-
- if (pTexture->CheckState(RESOURCE_STATE_RENDER_TARGET))
- Ctx.UnbindTextureFromRenderTarget(pTexture);
-
- if (pTexture->CheckState(RESOURCE_STATE_DEPTH_WRITE))
- Ctx.UnbindTextureFromDepthStencil(pTexture);
-
- pTexture->SetState(RESOURCE_STATE_SHADER_RESOURCE);*/
- Ctx.TransitionResource(pTexture, RESOURCE_STATE_SHADER_RESOURCE);
}
}
else if (auto* pBuffer = SRVRes.pBuffer)
{
if (pBuffer->IsInKnownState() && !pBuffer->CheckState(RESOURCE_STATE_SHADER_RESOURCE))
- { /*
- if (pBuffer->CheckState(RESOURCE_STATE_UNORDERED_ACCESS))
+ {
+ if (Mode == StateTransitionMode::Transition)
{
- Ctx.UnbindResourceFromUAV(pBuffer, SRVRes.pd3d11Resource);
- pBuffer->ClearState(RESOURCE_STATE_UNORDERED_ACCESS);
+ Ctx.TransitionResource(pBuffer, RESOURCE_STATE_SHADER_RESOURCE);
+ }
+ else
+ {
+ LOG_ERROR_MESSAGE("Buffer '", pBuffer->GetDesc().Name,
+ "' has not been transitioned to Shader Resource state. Call TransitionShaderResources(), use "
+ "RESOURCE_STATE_TRANSITION_MODE_TRANSITION mode or explicitly transition the buffer to required state.");
}
- pBuffer->AddState(RESOURCE_STATE_SHADER_RESOURCE);*/
- Ctx.TransitionResource(pBuffer, RESOURCE_STATE_SHADER_RESOURCE);
- }
- }
- }
-}
-
-void ShaderResourceCacheD3D11::TransitionResource(DeviceContextD3D11Impl& Ctx, Uint32 Count, CachedSampler* Samplers, ID3D11SamplerState** pd3d11Samplers)
-{
-}
-
-void ShaderResourceCacheD3D11::TransitionResource(DeviceContextD3D11Impl& Ctx, Uint32 Count, CachedResource* UAVResources, ID3D11UnorderedAccessView** pd3d11UAVs)
-{
- for (Uint32 i = 0; i < Count; ++i)
- {
- auto& UAVRes = UAVResources[i];
- if (auto* pTexture = UAVRes.pTexture)
- {
- if (pTexture->IsInKnownState() && !pTexture->CheckState(RESOURCE_STATE_UNORDERED_ACCESS))
- { /*
- if (pTexture->CheckAnyState(RESOURCE_STATE_SHADER_RESOURCE | RESOURCE_STATE_INPUT_ATTACHMENT))
- Ctx.UnbindTextureFromInput(pTexture, UAVRes.pd3d11Resource);
- pTexture->SetState(RESOURCE_STATE_UNORDERED_ACCESS);*/
- Ctx.TransitionResource(pTexture, RESOURCE_STATE_UNORDERED_ACCESS);
- }
- }
- else if (auto* pBuffer = UAVRes.pBuffer)
- {
- if (pBuffer->IsInKnownState() && !pBuffer->CheckState(RESOURCE_STATE_UNORDERED_ACCESS))
- { /*
- if ((pBuffer->GetState() & RESOURCE_STATE_GENERIC_READ) != 0)
- Ctx.UnbindBufferFromInput(pBuffer, UAVRes.pd3d11Resource);
- pBuffer->SetState(RESOURCE_STATE_UNORDERED_ACCESS);*/
- Ctx.TransitionResource(pBuffer, RESOURCE_STATE_UNORDERED_ACCESS);
}
}
}
}
-#ifdef DILIGENT_DEVELOPMENT
-void ShaderResourceCacheD3D11::DvpVerifyResourceState(DeviceContextD3D11Impl&, Uint32 Count, const CachedCB* CBs, ID3D11Buffer**)
+template <ShaderResourceCacheD3D11::StateTransitionMode Mode>
+void ShaderResourceCacheD3D11::TransitionResources(DeviceContextD3D11Impl& Ctx, const ID3D11SamplerState* /*Selector*/) const
{
- for (Uint32 i = 0; i < Count; ++i)
- {
- if (const auto* pBuff = CBs[i].pBuff.RawPtr<BufferD3D11Impl>())
- {
- if (pBuff->IsInKnownState() && !pBuff->CheckState(RESOURCE_STATE_CONSTANT_BUFFER))
- {
- LOG_ERROR_MESSAGE("Buffer '", pBuff->GetDesc().Name, "' has not been transitioned to Constant Buffer state. Call TransitionShaderResources(), use RESOURCE_STATE_TRANSITION_MODE_TRANSITION mode or explicitly transition the buffer to required state.");
- }
- }
- }
}
-void ShaderResourceCacheD3D11::DvpVerifyResourceState(DeviceContextD3D11Impl& Ctx, Uint32 Count, const CachedResource* SRVResources, ID3D11ShaderResourceView**)
+template <ShaderResourceCacheD3D11::StateTransitionMode Mode>
+void ShaderResourceCacheD3D11::TransitionResources(DeviceContextD3D11Impl& Ctx, const ID3D11UnorderedAccessView* /*Selector*/) const
{
- for (Uint32 i = 0; i < Count; ++i)
- {
- auto& SRVRes = SRVResources[i];
- if (const auto* pTexture = SRVRes.pTexture)
- {
- if (pTexture->IsInKnownState() &&
- !(pTexture->CheckState(RESOURCE_STATE_SHADER_RESOURCE) || Ctx.HasActiveRenderPass() && pTexture->CheckState(RESOURCE_STATE_INPUT_ATTACHMENT)))
- {
- LOG_ERROR_MESSAGE("Texture '", pTexture->GetDesc().Name, "' has not been transitioned to Shader Resource state. Call TransitionShaderResources(), use RESOURCE_STATE_TRANSITION_MODE_TRANSITION mode or explicitly transition the texture to required state.");
- }
- }
- else if (const auto* pBuffer = SRVRes.pBuffer)
- {
- if (pBuffer->IsInKnownState() && !pBuffer->CheckState(RESOURCE_STATE_SHADER_RESOURCE))
- {
- LOG_ERROR_MESSAGE("Buffer '", pBuffer->GetDesc().Name, "' has not been transitioned to Shader Resource state. Call TransitionShaderResources(), use RESOURCE_STATE_TRANSITION_MODE_TRANSITION mode or explicitly transition the buffer to required state.");
- }
- }
- }
-}
+ const auto UAVCount = GetUAVCount();
+ if (UAVCount == 0)
+ return;
-void ShaderResourceCacheD3D11::DvpVerifyResourceState(DeviceContextD3D11Impl& Ctx, Uint32 Count, const CachedSampler* Samplers, ID3D11SamplerState**)
-{
-}
+ CachedResource* UAVResources;
+ ID3D11UnorderedAccessView** d3d11UAVs;
+ BindPointsD3D11* bindPoints;
+ GetUAVArrays(UAVResources, d3d11UAVs, bindPoints);
-void ShaderResourceCacheD3D11::DvpVerifyResourceState(DeviceContextD3D11Impl&, Uint32 Count, const CachedResource* UAVResources, ID3D11UnorderedAccessView**)
-{
- for (Uint32 i = 0; i < Count; ++i)
+ for (Uint32 i = 0; i < UAVCount; ++i)
{
auto& UAVRes = UAVResources[i];
- if (const auto* pTexture = UAVRes.pTexture)
+ if (auto* pTexture = UAVRes.pTexture)
{
if (pTexture->IsInKnownState() && !pTexture->CheckState(RESOURCE_STATE_UNORDERED_ACCESS))
{
- LOG_ERROR_MESSAGE("Texture '", pTexture->GetDesc().Name, "' has not been transitioned to Unordered Access state. Call TransitionShaderResources(), use RESOURCE_STATE_TRANSITION_MODE_TRANSITION mode or explicitly transition the texture to required state.");
+ if (Mode == StateTransitionMode::Transition)
+ {
+ Ctx.TransitionResource(pTexture, RESOURCE_STATE_UNORDERED_ACCESS);
+ }
+ else
+ {
+ LOG_ERROR_MESSAGE("Texture '", pTexture->GetDesc().Name,
+ "' has not been transitioned to Unordered Access state. Call TransitionShaderResources(), use "
+ "RESOURCE_STATE_TRANSITION_MODE_TRANSITION mode or explicitly transition the texture to required state.");
+ }
}
}
- else if (const auto* pBuffer = UAVRes.pBuffer)
+ else if (auto* pBuffer = UAVRes.pBuffer)
{
if (pBuffer->IsInKnownState() && !pBuffer->CheckState(RESOURCE_STATE_UNORDERED_ACCESS))
{
- LOG_ERROR_MESSAGE("Buffer '", pBuffer->GetDesc().Name, "' has not been transitioned to Unordered Access state. Call TransitionShaderResources(), use RESOURCE_STATE_TRANSITION_MODE_TRANSITION mode or explicitly transition the buffer to required state.");
- }
- }
- }
-}
-#endif // DILIGENT_DEVELOPMENT
-
-void ShaderResourceCacheD3D11::BindResources(DeviceContextD3D11Impl& Ctx, const TBindingsPerStage& BaseBindings, TMinMaxSlotPerStage& MinMaxSlot, TCommittedResources& CommittedRes, SHADER_TYPE ActiveStages) const
-{
- const auto CBCount = GetCBCount();
- if (CBCount != 0)
- {
- const auto Range = DESCRIPTOR_RANGE_CBV;
- ShaderResourceCacheD3D11::CachedCB const* CBs;
- ID3D11Buffer* const* d3d11CBs;
- BindPointsD3D11 const* bindPoints;
- GetConstCBArrays(CBs, d3d11CBs, bindPoints);
-
- for (Uint32 cb = 0; cb < CBCount; ++cb)
- {
- const auto& BindPoints = bindPoints[cb];
- Uint32 ActiveBits = BindPoints.GetActiveBits() & ActiveStages;
- VERIFY(ActiveBits != 0, "resource is not initialized");
- while (ActiveBits != 0)
- {
- const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits);
- ActiveBits &= ~(1u << ShaderInd);
- VERIFY_EXPR(BindPoints.IsValid(ShaderInd));
-
- auto* CommittedD3D11CBs = CommittedRes.D3D11CBs[ShaderInd];
- UINT& MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot;
- UINT& MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot;
- const UINT Slot = BaseBindings[ShaderInd][Range] + BindPoints[ShaderInd];
- const bool IsNewCB = CommittedD3D11CBs[Slot] != d3d11CBs[cb];
- MinSlot = IsNewCB ? std::min(MinSlot, Slot) : MinSlot;
- MaxSlot = IsNewCB ? Slot : MaxSlot;
-
- VERIFY_EXPR(!IsNewCB || (Slot >= MinSlot && Slot <= MaxSlot));
- VERIFY_EXPR(d3d11CBs[cb] != nullptr);
- CommittedD3D11CBs[Slot] = d3d11CBs[cb];
- }
- }
- }
-
- const auto SRVCount = GetSRVCount();
- if (SRVCount != 0)
- {
- const auto Range = DESCRIPTOR_RANGE_SRV;
- ShaderResourceCacheD3D11::CachedResource const* SRVResources;
- ID3D11ShaderResourceView* const* d3d11SRVs;
- BindPointsD3D11 const* bindPoints;
- GetConstSRVArrays(SRVResources, d3d11SRVs, bindPoints);
-
- for (Uint32 srv = 0; srv < SRVCount; ++srv)
- {
- const auto& BindPoints = bindPoints[srv];
- Uint32 ActiveBits = BindPoints.GetActiveBits() & ActiveStages;
- VERIFY(ActiveBits != 0, "resource is not initialized");
- while (ActiveBits != 0)
- {
- const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits);
- ActiveBits &= ~(1u << ShaderInd);
- VERIFY_EXPR(BindPoints.IsValid(ShaderInd));
-
- auto* CommittedD3D11SRVs = CommittedRes.D3D11SRVs[ShaderInd];
- auto* CommittedD3D11SRVRes = CommittedRes.D3D11SRVResources[ShaderInd];
- UINT& MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot;
- UINT& MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot;
- const UINT Slot = BaseBindings[ShaderInd][Range] + BindPoints[ShaderInd];
- const bool IsNewSRV = CommittedD3D11SRVs[Slot] != d3d11SRVs[srv];
- MinSlot = IsNewSRV ? std::min(MinSlot, Slot) : MinSlot;
- MaxSlot = IsNewSRV ? Slot : MaxSlot;
-
- VERIFY_EXPR(!IsNewSRV || (Slot >= MinSlot && Slot <= MaxSlot));
- VERIFY_EXPR(d3d11SRVs[srv] != nullptr);
- CommittedD3D11SRVRes[Slot] = SRVResources[srv].pd3d11Resource;
- CommittedD3D11SRVs[Slot] = d3d11SRVs[srv];
- }
- }
- }
-
- const auto SamplerCount = GetSamplerCount();
- if (SamplerCount != 0)
- {
- const auto Range = DESCRIPTOR_RANGE_SAMPLER;
- ShaderResourceCacheD3D11::CachedSampler const* Samplers;
- ID3D11SamplerState* const* d3d11Samplers;
- BindPointsD3D11 const* bindPoints;
- GetConstSamplerArrays(Samplers, d3d11Samplers, bindPoints);
-
- for (Uint32 sam = 0; sam < SamplerCount; ++sam)
- {
- const auto& BindPoints = bindPoints[sam];
- Uint32 ActiveBits = BindPoints.GetActiveBits() & ActiveStages;
- VERIFY(ActiveBits != 0, "resource is not initialized");
- while (ActiveBits != 0)
- {
- const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits);
- ActiveBits &= ~(1u << ShaderInd);
- VERIFY_EXPR(BindPoints.IsValid(ShaderInd));
-
- auto* CommittedD3D11Samplers = CommittedRes.D3D11Samplers[ShaderInd];
- UINT& MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot;
- UINT& MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot;
- const UINT Slot = BaseBindings[ShaderInd][Range] + BindPoints[ShaderInd];
- const bool IsNewSam = CommittedD3D11Samplers[Slot] != d3d11Samplers[sam];
- MinSlot = IsNewSam ? std::min(MinSlot, Slot) : MinSlot;
- MaxSlot = IsNewSam ? Slot : MaxSlot;
-
- VERIFY_EXPR(!IsNewSam || (Slot >= MinSlot && Slot <= MaxSlot));
- VERIFY_EXPR(d3d11Samplers[sam] != nullptr);
- CommittedD3D11Samplers[Slot] = d3d11Samplers[sam];
- }
- }
- }
-
- const auto UAVCount = GetUAVCount();
- if (UAVCount != 0)
- {
- const auto Range = DESCRIPTOR_RANGE_UAV;
- ShaderResourceCacheD3D11::CachedResource const* UAVResources;
- ID3D11UnorderedAccessView* const* d3d11UAVs;
- BindPointsD3D11 const* bindPoints;
- GetConstUAVArrays(UAVResources, d3d11UAVs, bindPoints);
-
- for (Uint32 uav = 0; uav < UAVCount; ++uav)
- {
- const auto& BindPoints = bindPoints[uav];
- Uint32 ActiveBits = BindPoints.GetActiveBits() & ActiveStages;
- VERIFY(ActiveBits != 0, "resource is not initialized");
- while (ActiveBits != 0)
- {
- const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits);
- ActiveBits &= ~(1u << ShaderInd);
- VERIFY_EXPR(BindPoints.IsValid(ShaderInd));
-
- auto* CommittedD3D11UAVs = CommittedRes.D3D11UAVs[ShaderInd];
- auto* CommittedD3D11UAVRes = CommittedRes.D3D11UAVResources[ShaderInd];
- UINT& MinSlot = MinMaxSlot[ShaderInd][Range].MinSlot;
- UINT& MaxSlot = MinMaxSlot[ShaderInd][Range].MaxSlot;
- const UINT Slot = BaseBindings[ShaderInd][Range] + BindPoints[ShaderInd];
- const bool IsNewUAV = CommittedD3D11UAVs[Slot] != d3d11UAVs[uav];
- MinSlot = IsNewUAV ? std::min(MinSlot, Slot) : MinSlot;
- MaxSlot = IsNewUAV ? Slot : MaxSlot;
-
- VERIFY_EXPR(!IsNewUAV || (Slot >= MinSlot && Slot <= MaxSlot));
- VERIFY_EXPR(d3d11UAVs[uav] != nullptr);
- CommittedD3D11UAVRes[Slot] = UAVResources[uav].pd3d11Resource;
- CommittedD3D11UAVs[Slot] = d3d11UAVs[uav];
+ if (Mode == StateTransitionMode::Transition)
+ {
+ Ctx.TransitionResource(pBuffer, RESOURCE_STATE_UNORDERED_ACCESS);
+ }
+ else
+ {
+ LOG_ERROR_MESSAGE("Buffer '", pBuffer->GetDesc().Name,
+ "' has not been transitioned to Unordered Access state. Call TransitionShaderResources(), use "
+ "RESOURCE_STATE_TRANSITION_MODE_TRANSITION mode or explicitly transition the buffer to required state.");
+ }
}
}
}
}
-void ShaderResourceCacheD3D11::TCommittedResources::Clear()
-{
- for (int ShaderType = 0; ShaderType < NumShaderTypes; ++ShaderType)
- {
- // clang-format off
- memset(D3D11CBs[ShaderType], 0, sizeof(D3D11CBs[ShaderType][0]) * NumCBs[ShaderType]);
- memset(D3D11SRVs[ShaderType], 0, sizeof(D3D11SRVs[ShaderType][0]) * NumSRVs[ShaderType]);
- memset(D3D11Samplers[ShaderType], 0, sizeof(D3D11Samplers[ShaderType][0]) * NumSamplers[ShaderType]);
- memset(D3D11UAVs[ShaderType], 0, sizeof(D3D11UAVs[ShaderType][0]) * NumUAVs[ShaderType]);
- memset(D3D11SRVResources[ShaderType], 0, sizeof(D3D11SRVResources[ShaderType][0]) * NumSRVs[ShaderType]);
- memset(D3D11UAVResources[ShaderType], 0, sizeof(D3D11UAVResources[ShaderType][0]) * NumUAVs[ShaderType]);
- // clang-format on
-
- NumCBs[ShaderType] = 0;
- NumSRVs[ShaderType] = 0;
- NumSamplers[ShaderType] = 0;
- NumUAVs[ShaderType] = 0;
- }
-}
-
} // namespace Diligent
diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp index 707a4aeb..c7fa3e6d 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourcesD3D11.cpp @@ -27,16 +27,15 @@ #include "pch.h" -#include <d3dcompiler.h> #include "ShaderResourcesD3D11.hpp" -#include "ShaderBase.hpp" -#include "ShaderResourceCacheD3D11.hpp" + +#include <d3dcompiler.h> + #include "RenderDeviceD3D11Impl.hpp" namespace Diligent { - ShaderResourcesD3D11::ShaderResourcesD3D11(RenderDeviceD3D11Impl* pDeviceD3D11Impl, ID3DBlob* pShaderBytecode, const ShaderDesc& ShdrDesc, @@ -127,332 +126,4 @@ ShaderResourcesD3D11::~ShaderResourcesD3D11() { } - -#ifdef DILIGENT_DEVELOPMENT -static String DbgMakeResourceName(const D3DShaderResourceAttribs& Attr, Uint32 BindPoint) -{ - VERIFY(BindPoint >= Uint32{Attr.BindPoint} && BindPoint < Uint32{Attr.BindPoint} + Attr.BindCount, "Bind point is out of allowed range"); - if (Attr.BindCount == 1) - return Attr.Name; - else - return String(Attr.Name) + '[' + std::to_string(BindPoint - Attr.BindPoint) + ']'; -} - -void ShaderResourcesD3D11::dvpVerifyCommittedResources(ID3D11Buffer* CommittedD3D11CBs[], - ID3D11ShaderResourceView* CommittedD3D11SRVs[], - ID3D11Resource* CommittedD3D11SRVResources[], - ID3D11SamplerState* CommittedD3D11Samplers[], - ID3D11UnorderedAccessView* CommittedD3D11UAVs[], - ID3D11Resource* CommittedD3D11UAVResources[], - ShaderResourceCacheD3D11& ResourceCache) const -{ - ShaderResourceCacheD3D11::CachedCB* CachedCBs = nullptr; - ID3D11Buffer** d3d11CBs = nullptr; - ShaderResourceCacheD3D11::CachedResource* CachedSRVResources = nullptr; - ID3D11ShaderResourceView** d3d11SRVs = nullptr; - ShaderResourceCacheD3D11::CachedSampler* CachedSamplers = nullptr; - ID3D11SamplerState** d3d11Samplers = nullptr; - ShaderResourceCacheD3D11::CachedResource* CachedUAVResources = nullptr; - ID3D11UnorderedAccessView** d3d11UAVs = nullptr; - BindPointsD3D11* BindPoints = nullptr; - // clang-format off - ResourceCache.GetCBArrays (CachedCBs, d3d11CBs, BindPoints); - ResourceCache.GetSRVArrays (CachedSRVResources, d3d11SRVs, BindPoints); - ResourceCache.GetSamplerArrays(CachedSamplers, d3d11Samplers, BindPoints); - ResourceCache.GetUAVArrays (CachedUAVResources, d3d11UAVs, BindPoints); - // clang-format on - - ProcessResources( - [&](const D3DShaderResourceAttribs& cb, Uint32) // - { - for (auto BindPoint = cb.BindPoint; BindPoint < cb.BindPoint + cb.BindCount; ++BindPoint) - { - if (BindPoint >= ResourceCache.GetCBCount()) - { - LOG_ERROR_MESSAGE("Unable to find constant buffer '", DbgMakeResourceName(cb, BindPoint), "' (slot ", - BindPoint, ") in the resource cache: the cache reserves ", ResourceCache.GetCBCount(), - " CB slots only. This should never happen and may be the result of using wrong resource cache."); - continue; - } - auto& CB = CachedCBs[BindPoint]; - if (CB.pBuff == nullptr) - { - LOG_ERROR_MESSAGE("Constant buffer '", DbgMakeResourceName(cb, BindPoint), "' (slot ", - BindPoint, ") is not initialized in the resource cache."); - continue; - } - - if (!(CB.pBuff->GetDesc().BindFlags & BIND_UNIFORM_BUFFER)) - { - LOG_ERROR_MESSAGE("Buffer '", CB.pBuff->GetDesc().Name, - "' committed in the device context as constant buffer to variable '", - DbgMakeResourceName(cb, BindPoint), "' (slot ", BindPoint, - ") in shader '", GetShaderName(), "' was not created with BIND_UNIFORM_BUFFER flag"); - continue; - } - - VERIFY_EXPR(d3d11CBs[BindPoint] == CB.pBuff->GetD3D11Buffer()); - - if (CommittedD3D11CBs[BindPoint] == nullptr) - { - LOG_ERROR_MESSAGE("No D3D11 resource committed to constant buffer '", DbgMakeResourceName(cb, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), "'"); - continue; - } - - if (CommittedD3D11CBs[BindPoint] != d3d11CBs[BindPoint]) - { - LOG_ERROR_MESSAGE("D3D11 resource committed to constant buffer '", DbgMakeResourceName(cb, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), "' does not match the resource in the resource cache"); - continue; - } - } - }, - - [&](const D3DShaderResourceAttribs& sam, Uint32) // - { - for (auto BindPoint = sam.BindPoint; BindPoint < sam.BindPoint + sam.BindCount; ++BindPoint) - { - if (BindPoint >= ResourceCache.GetSamplerCount()) - { - LOG_ERROR_MESSAGE("Unable to find sampler '", DbgMakeResourceName(sam, BindPoint), "' (slot ", BindPoint, - ") in the resource cache: the cache reserves ", ResourceCache.GetSamplerCount(), - " Sampler slots only. This should never happen and may be the result of using wrong resource cache."); - continue; - } - auto& Sam = CachedSamplers[BindPoint]; - if (Sam.pSampler == nullptr) - { - LOG_ERROR_MESSAGE("Sampler '", DbgMakeResourceName(sam, BindPoint), "' (slot ", BindPoint, - ") is not initialized in the resource cache."); - continue; - } - VERIFY_EXPR(d3d11Samplers[BindPoint] == Sam.pSampler->GetD3D11SamplerState()); - - if (CommittedD3D11Samplers[BindPoint] == nullptr) - { - LOG_ERROR_MESSAGE("No D3D11 sampler committed to variable '", DbgMakeResourceName(sam, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), "'"); - continue; - } - - if (CommittedD3D11Samplers[BindPoint] != d3d11Samplers[BindPoint]) - { - LOG_ERROR_MESSAGE("D3D11 sampler committed to variable '", DbgMakeResourceName(sam, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), "' does not match the resource in the resource cache"); - continue; - } - } - }, - - [&](const D3DShaderResourceAttribs& tex, Uint32) // - { - for (auto BindPoint = tex.BindPoint; BindPoint < tex.BindPoint + tex.BindCount; ++BindPoint) - { - if (BindPoint >= ResourceCache.GetSRVCount()) - { - LOG_ERROR_MESSAGE("Unable to find texture SRV '", DbgMakeResourceName(tex, BindPoint), "' (slot ", - BindPoint, ") in the resource cache: the cache reserves ", ResourceCache.GetSRVCount(), - " SRV slots only. This should never happen and may be the result of using wrong resource cache."); - continue; - } - auto& SRVRes = CachedSRVResources[BindPoint]; - if (SRVRes.pBuffer != nullptr) - { - LOG_ERROR_MESSAGE("Unexpected buffer bound to variable '", DbgMakeResourceName(tex, BindPoint), "' (slot ", - BindPoint, "). Texture is expected."); - continue; - } - if (SRVRes.pTexture == nullptr) - { - LOG_ERROR_MESSAGE("Texture '", DbgMakeResourceName(tex, BindPoint), "' (slot ", BindPoint, - ") is not initialized in the resource cache."); - continue; - } - - if (!(SRVRes.pTexture->GetDesc().BindFlags & BIND_SHADER_RESOURCE)) - { - LOG_ERROR_MESSAGE("Texture '", SRVRes.pTexture->GetDesc().Name, - "' committed in the device context as SRV to variable '", DbgMakeResourceName(tex, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), "' was not created with BIND_SHADER_RESOURCE flag"); - } - - if (CommittedD3D11SRVs[BindPoint] == nullptr) - { - LOG_ERROR_MESSAGE("No D3D11 resource committed to texture SRV '", DbgMakeResourceName(tex, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), "'"); - continue; - } - - if (CommittedD3D11SRVs[BindPoint] != d3d11SRVs[BindPoint]) - { - LOG_ERROR_MESSAGE("D3D11 resource committed to texture SRV '", DbgMakeResourceName(tex, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), - "' does not match the resource in the resource cache"); - continue; - } - } - - if (tex.IsCombinedWithSampler()) - { - const auto& SamAttribs = GetCombinedSampler(tex); - VERIFY_EXPR(SamAttribs.IsValidBindPoint()); - VERIFY_EXPR(SamAttribs.BindCount == 1 || SamAttribs.BindCount == tex.BindCount); - } - }, - - [&](const D3DShaderResourceAttribs& uav, Uint32) // - { - for (auto BindPoint = uav.BindPoint; BindPoint < uav.BindPoint + uav.BindCount; ++BindPoint) - { - if (BindPoint >= ResourceCache.GetUAVCount()) - { - LOG_ERROR_MESSAGE("Unable to find texture UAV '", DbgMakeResourceName(uav, BindPoint), "' (slot ", - BindPoint, ") in the resource cache: the cache reserves ", ResourceCache.GetUAVCount(), - " UAV slots only. This should never happen and may be the result of using wrong resource cache."); - continue; - } - auto& UAVRes = CachedUAVResources[BindPoint]; - if (UAVRes.pBuffer != nullptr) - { - LOG_ERROR_MESSAGE("Unexpected buffer bound to variable '", DbgMakeResourceName(uav, BindPoint), - "' (slot ", BindPoint, "). Texture is expected."); - continue; - } - if (UAVRes.pTexture == nullptr) - { - LOG_ERROR_MESSAGE("Texture '", DbgMakeResourceName(uav, BindPoint), "' (slot ", BindPoint, - ") is not initialized in the resource cache."); - continue; - } - - if (!(UAVRes.pTexture->GetDesc().BindFlags & BIND_UNORDERED_ACCESS)) - { - LOG_ERROR_MESSAGE("Texture '", UAVRes.pTexture->GetDesc().Name, - "' committed in the device context as UAV to variable '", DbgMakeResourceName(uav, BindPoint), "' (slot ", BindPoint, ") in shader '", GetShaderName(), "' was not created with BIND_UNORDERED_ACCESS flag"); - } - - if (CommittedD3D11UAVs[BindPoint] == nullptr) - { - LOG_ERROR_MESSAGE("No D3D11 resource committed to texture UAV '", DbgMakeResourceName(uav, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), "'"); - continue; - } - - if (CommittedD3D11UAVs[BindPoint] != d3d11UAVs[BindPoint]) - { - LOG_ERROR_MESSAGE("D3D11 resource committed to texture UAV '", DbgMakeResourceName(uav, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), "' does not match the resource in the resource cache"); - continue; - } - } - }, - - - [&](const D3DShaderResourceAttribs& buf, Uint32) // - { - for (auto BindPoint = buf.BindPoint; BindPoint < buf.BindPoint + buf.BindCount; ++BindPoint) - { - if (BindPoint >= ResourceCache.GetSRVCount()) - { - LOG_ERROR_MESSAGE("Unable to find buffer SRV '", DbgMakeResourceName(buf, BindPoint), "' (slot ", BindPoint, - ") in the resource cache: the cache reserves ", ResourceCache.GetSRVCount(), - " SRV slots only. This should never happen and may be the result of using wrong resource cache."); - continue; - } - auto& SRVRes = CachedSRVResources[BindPoint]; - if (SRVRes.pTexture != nullptr) - { - LOG_ERROR_MESSAGE("Unexpected texture bound to variable '", DbgMakeResourceName(buf, BindPoint), - "' (slot ", BindPoint, "). Buffer is expected."); - continue; - } - if (SRVRes.pBuffer == nullptr) - { - LOG_ERROR_MESSAGE("Buffer '", DbgMakeResourceName(buf, BindPoint), "' (slot ", BindPoint, - ") is not initialized in the resource cache."); - continue; - } - - if (!(SRVRes.pBuffer->GetDesc().BindFlags & BIND_SHADER_RESOURCE)) - { - LOG_ERROR_MESSAGE("Buffer '", SRVRes.pBuffer->GetDesc().Name, - "' committed in the device context as SRV to variable '", DbgMakeResourceName(buf, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), - "' was not created with BIND_SHADER_RESOURCE flag"); - } - - if (CommittedD3D11SRVs[BindPoint] == nullptr) - { - LOG_ERROR_MESSAGE("No D3D11 resource committed to buffer SRV '", DbgMakeResourceName(buf, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), "'"); - continue; - } - - if (CommittedD3D11SRVs[BindPoint] != d3d11SRVs[BindPoint]) - { - LOG_ERROR_MESSAGE("D3D11 resource committed to buffer SRV '", DbgMakeResourceName(buf, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), - "' does not match the resource in the resource cache"); - continue; - } - } - }, - - [&](const D3DShaderResourceAttribs& uav, Uint32) // - { - for (auto BindPoint = uav.BindPoint; BindPoint < uav.BindPoint + uav.BindCount; ++BindPoint) - { - if (BindPoint >= ResourceCache.GetUAVCount()) - { - LOG_ERROR_MESSAGE("Unable to find buffer UAV '", DbgMakeResourceName(uav, BindPoint), "' (slot ", BindPoint, - ") in the resource cache: the cache reserves ", ResourceCache.GetUAVCount(), - " UAV slots only. This should never happen and may be the result of using wrong resource cache."); - continue; - } - auto& UAVRes = CachedUAVResources[BindPoint]; - if (UAVRes.pTexture != nullptr) - { - LOG_ERROR_MESSAGE("Unexpected texture bound to variable '", DbgMakeResourceName(uav, BindPoint), - "' (slot ", BindPoint, "). Buffer is expected."); - return; - } - if (UAVRes.pBuffer == nullptr) - { - LOG_ERROR_MESSAGE("Buffer UAV '", DbgMakeResourceName(uav, BindPoint), "' (slot ", BindPoint, - ") is not initialized in the resource cache."); - return; - } - - if (!(UAVRes.pBuffer->GetDesc().BindFlags & BIND_UNORDERED_ACCESS)) - { - LOG_ERROR_MESSAGE("Buffer '", UAVRes.pBuffer->GetDesc().Name, - "' committed in the device context as UAV to variable '", DbgMakeResourceName(uav, BindPoint), "' (slot ", BindPoint, ") in shader '", GetShaderName(), "' was not created with BIND_UNORDERED_ACCESS flag"); - } - - if (CommittedD3D11UAVs[BindPoint] == nullptr) - { - LOG_ERROR_MESSAGE("No D3D11 resource committed to buffer UAV '", DbgMakeResourceName(uav, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), "'"); - return; - } - - if (CommittedD3D11UAVs[BindPoint] != d3d11UAVs[BindPoint]) - { - LOG_ERROR_MESSAGE("D3D11 resource committed to buffer UAV '", DbgMakeResourceName(uav, BindPoint), - "' (slot ", BindPoint, ") in shader '", GetShaderName(), - "' does not match the resource in the resource cache"); - return; - } - } - }, - - [&](const D3DShaderResourceAttribs&, Uint32) // - { - UNEXPECTED("Acceleration structure is not supported in DirectX 11"); - } // clang-format off - ); // clang-format on -} -#endif - } // namespace Diligent |
