From 0420f6816d6814aa674b129a86379c1b20ba0575 Mon Sep 17 00:00:00 2001 From: assiduous Date: Wed, 17 Mar 2021 15:10:03 -0700 Subject: Some more code improvemnts in D3D11 backend --- .../include/DeviceContextD3D11Impl.hpp | 13 +-- .../include/PipelineResourceAttribsD3D11.hpp | 126 +++++++++++++++------ .../include/PipelineResourceSignatureD3D11Impl.hpp | 32 +++--- .../include/PipelineStateD3D11Impl.hpp | 3 +- .../include/ShaderResourceCacheD3D11.hpp | 66 +++++------ .../src/DeviceContextD3D11Impl.cpp | 14 +-- .../src/PipelineResourceSignatureD3D11Impl.cpp | 106 ++++++++--------- .../src/PipelineStateD3D11Impl.cpp | 14 ++- .../src/ShaderResourceCacheD3D11.cpp | 4 +- 9 files changed, 212 insertions(+), 166 deletions(-) (limited to 'Graphics/GraphicsEngineD3D11') diff --git a/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.hpp index ba1f3dea..56508075 100644 --- a/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/DeviceContextD3D11Impl.hpp @@ -338,7 +338,7 @@ private: void BindShaderResources(); - static constexpr int NumShaderTypes = BindPointsD3D11::NumShaderTypes; + static constexpr int NumShaderTypes = D3D11ResourceBindPoints::NumShaderTypes; struct TCommittedResources { // clang-format off @@ -393,11 +393,10 @@ private: Bind }; - using TBindingsPerStage = PipelineResourceSignatureD3D11Impl::TBindingsPerStage; - void BindCacheResources(const ShaderResourceCacheD3D11& ResourceCache, - const TBindingsPerStage& BaseBindings, - SHADER_TYPE ActiveStages, - PixelShaderUAVBindMode& PsUavBindMode); + void BindCacheResources(const ShaderResourceCacheD3D11& ResourceCache, + const D3D11ShaderResourceCounters& BaseBindings, + SHADER_TYPE ActiveStages, + PixelShaderUAVBindMode& PsUavBindMode); #ifdef DILIGENT_DEVELOPMENT void DvpValidateCommittedShaderResources(); @@ -424,7 +423,7 @@ private: bool CommittedResourcesValidated = false; // Binding offsets that was used at last BindProgramResources() call. - std::array BoundResOffsets = {}; + std::array BoundResOffsets = {}; #endif SRBState() diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineResourceAttribsD3D11.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineResourceAttribsD3D11.hpp index a422f4db..4f62b906 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineResourceAttribsD3D11.hpp +++ b/Graphics/GraphicsEngineD3D11/include/PipelineResourceAttribsD3D11.hpp @@ -34,6 +34,8 @@ #include "BasicTypes.h" #include "DebugUtilities.hpp" +#include "HashUtils.hpp" +#include "GraphicsAccessories.hpp" namespace Diligent { @@ -50,66 +52,126 @@ enum D3D11_RESOURCE_RANGE : Uint32 D3D11_RESOURCE_RANGE ShaderResourceToDescriptorRange(SHADER_RESOURCE_TYPE Type); -// sizeof(BindPointsD3D11) == 8, x64 -struct BindPointsD3D11 +/// Resource binding points in all shader stages. +// sizeof(D3D11ResourceBindPoints) == 8, x64 +struct D3D11ResourceBindPoints { /// Number of different shader types (Vertex, Pixel, Geometry, Domain, Hull, Compute) static constexpr Uint32 NumShaderTypes = 6; - static constexpr Uint8 InvalidBindPoint = 0xFF; + D3D11ResourceBindPoints() noexcept + { +#ifdef DILIGENT_DEBUG + for (auto BindPoint : Bindings) + VERIFY_EXPR(BindPoint == InvalidBindPoint); +#endif + } - BindPointsD3D11() noexcept {} - BindPointsD3D11(const BindPointsD3D11&) noexcept = default; + D3D11ResourceBindPoints(const D3D11ResourceBindPoints&) noexcept = default; - // clang-format off - bool IsEmpty() const { return m_ActiveBits == 0; } - Uint32 GetActiveBits() const { return m_ActiveBits; } - bool IsValid(Uint32 index) const { return m_Bindings[index] != InvalidBindPoint; } - Uint8 operator[](Uint32 index) const { return m_Bindings[index]; } - // clang-format on + SHADER_TYPE GetActiveStages() const + { + return static_cast(ActiveStages); + } + + bool IsEmpty() const + { + return GetActiveStages() == SHADER_TYPE_UNKNOWN; + } - void Set(Uint32 Index, Uint32 BindPoint) + bool IsStageActive(Uint32 ShaderInd) const { - VERIFY_EXPR(Index < NumShaderTypes); - VERIFY_EXPR(BindPoint < InvalidBindPoint); - m_ActiveBits = static_cast(m_ActiveBits | (1u << Index)); - m_Bindings[Index] = static_cast(BindPoint); + bool IsActive = (GetActiveStages() & (1u << ShaderInd)) != 0; + VERIFY_EXPR((IsActive && Bindings[ShaderInd] != InvalidBindPoint || + !IsActive && Bindings[ShaderInd] == InvalidBindPoint)); + return IsActive; + } + + Uint8 operator[](Uint32 ShaderInd) const + { + return Bindings[ShaderInd]; } size_t GetHash() const { size_t Hash = 0; - for (Uint32 i = 0; i < NumShaderTypes; ++i) - HashCombine(Hash, m_Bindings[i]); + for (auto Binding : Bindings) + HashCombine(Hash, Binding); return Hash; } - bool operator==(const BindPointsD3D11& rhs) const + bool operator==(const D3D11ResourceBindPoints& rhs) const { - return m_Bindings == rhs.m_Bindings; + return Bindings == rhs.Bindings; } - BindPointsD3D11 operator+(Uint32 value) const + D3D11ResourceBindPoints operator+(Uint32 value) const { - BindPointsD3D11 Result{*this}; - for (Uint32 Bits = Result.m_ActiveBits; Bits != 0;) + D3D11ResourceBindPoints NewBindPoints{*this}; + for (auto Stages = GetActiveStages(); Stages != 0;) { - auto Index = PlatformMisc::GetLSB(Bits); - Bits &= ~(1u << Index); + auto ShaderInd = ExtractFirstShaderStageIndex(Stages); + VERIFY_EXPR(Uint32{Bindings[ShaderInd]} + value < InvalidBindPoint); + NewBindPoints.Bindings[ShaderInd] = Bindings[ShaderInd] + static_cast(value); + } + return NewBindPoints; + } + +private: + struct SetBindPointHelper + { + SetBindPointHelper(D3D11ResourceBindPoints& _BindPoints, + const Uint32 _ShaderInd) : + BindPoints{_BindPoints}, + ShaderInd{_ShaderInd} + {} - auto NewBindPoint = Result.m_Bindings[Index] + value; - VERIFY_EXPR(NewBindPoint < InvalidBindPoint); - Result.m_Bindings[Index] = static_cast(NewBindPoint); + Uint8 operator=(Uint32 BindPoint) + { + BindPoints.Set(ShaderInd, BindPoint); + return static_cast(BindPoint); } - return Result; + + operator Uint8() const + { + return static_cast(BindPoints)[ShaderInd]; + } + + private: + D3D11ResourceBindPoints& BindPoints; + const Uint32 ShaderInd; + }; + +public: + SetBindPointHelper operator[](Uint32 ShaderInd) + { + return SetBindPointHelper{*this, ShaderInd}; } private: - Uint8 m_ActiveBits = 0; - std::array m_Bindings = {InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint}; + void Set(Uint32 ShaderInd, Uint32 BindPoint) + { + VERIFY_EXPR(ShaderInd < NumShaderTypes); + VERIFY(BindPoint < InvalidBindPoint, "Bind point (", BindPoint, ") is out of range"); + + Bindings[ShaderInd] = static_cast(BindPoint); + ActiveStages |= Uint32{1} << ShaderInd; + } + + static constexpr Uint8 InvalidBindPoint = 0xFF; + + // 0 1 2 3 4 5 + // | PS | VS | GS | HS | DS | CS | + std::array Bindings{InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint, InvalidBindPoint}; + + Uint8 ActiveStages = 0; }; +/// Resource counters for all shader stages and all resource types +using D3D11ShaderResourceCounters = std::array, D3D11_RESOURCE_RANGE_COUNT>; + + // sizeof(PipelineResourceAttribsD3D11) == 12, x64 struct PipelineResourceAttribsD3D11 { @@ -123,7 +185,7 @@ public: // clang-format off const Uint32 SamplerInd : _SamplerIndBits; // Index of the assigned sampler in m_Desc.Resources. const Uint32 ImtblSamplerAssigned : _SamplerAssignedBits; // Immutable sampler flag. - BindPointsD3D11 BindPoints; + D3D11ResourceBindPoints BindPoints; // clang-format on PipelineResourceAttribsD3D11(Uint32 _SamplerInd, diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineResourceSignatureD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineResourceSignatureD3D11Impl.hpp index bad9096c..9ae2215e 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineResourceSignatureD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/PipelineResourceSignatureD3D11Impl.hpp @@ -32,17 +32,18 @@ #include -#include "ResourceBindingMap.hpp" - #include "EngineD3D11ImplTraits.hpp" #include "PipelineResourceSignatureBase.hpp" #include "PipelineResourceAttribsD3D11.hpp" // ShaderVariableManagerD3D11, ShaderResourceCacheD3D11, and ShaderResourceBindingD3D11Impl // are required by PipelineResourceSignatureBase +#include "ShaderResourceCacheD3D11.hpp" #include "ShaderVariableManagerD3D11.hpp" #include "ShaderResourceBindingD3D11Impl.hpp" -#include "ShaderResourceCacheD3D11.hpp" +#include "SamplerD3D11Impl.hpp" + +#include "ResourceBindingMap.hpp" namespace Diligent { @@ -59,8 +60,7 @@ public: bool bIsDeviceInternal = false); ~PipelineResourceSignatureD3D11Impl(); - using ResourceAttribs = PipelineResourceAttribsD3D11; - static constexpr auto NumShaderTypes = BindPointsD3D11::NumShaderTypes; + using ResourceAttribs = PipelineResourceAttribsD3D11; const ResourceAttribs& GetResourceAttribs(Uint32 ResIndex) const { @@ -72,14 +72,13 @@ public: struct ImmutableSamplerAttribs { public: - RefCntAutoPtr pSampler; - Uint32 ArraySize = 0; - BindPointsD3D11 BindPoints; + RefCntAutoPtr pSampler; + Uint32 ArraySize = 0; + D3D11ResourceBindPoints BindPoints; ImmutableSamplerAttribs() noexcept {} - bool IsAllocated() const { return !BindPoints.IsEmpty(); } - SamplerD3D11Impl* GetSamplerD3D11() const { return ValidatedCast(pSampler.RawPtr()); } + bool IsAllocated() const { return !BindPoints.IsEmpty(); } }; const ImmutableSamplerAttribs& GetImmutableSamplerAttribs(Uint32 SampIndex) const @@ -88,10 +87,7 @@ public: return m_ImmutableSamplers[SampIndex]; } - using TResourceCount = std::array; - using TBindingsPerStage = std::array, D3D11_RESOURCE_RANGE_COUNT>; - - __forceinline void ShiftBindings(TBindingsPerStage& Bindings) const + __forceinline void ShiftBindings(D3D11ShaderResourceCounters& Bindings) const { for (Uint32 r = 0; r < Bindings.size(); ++r) { @@ -106,7 +102,7 @@ public: void InitSRBResourceCache(ShaderResourceCacheD3D11& ResourceCache); - void UpdateShaderResourceBindingMap(ResourceBinding::TMap& ResourceMap, SHADER_TYPE ShaderStage, const TBindingsPerStage& BaseBindings) const; + void UpdateShaderResourceBindingMap(ResourceBinding::TMap& ResourceMap, SHADER_TYPE ShaderStage, const D3D11ShaderResourceCounters& BaseBindings) const; // Copies static resources from the static resource cache to the destination cache void CopyStaticResources(ShaderResourceCacheD3D11& ResourceCache) const; @@ -126,9 +122,9 @@ private: void Destruct(); private: - TBindingsPerStage m_BindingCountPerStage = {}; - ResourceAttribs* m_pResourceAttribs = nullptr; // [m_Desc.NumResources] - ImmutableSamplerAttribs* m_ImmutableSamplers = nullptr; // [m_Desc.NumImmutableSamplers] + D3D11ShaderResourceCounters m_BindingCountPerStage = {}; + ResourceAttribs* m_pResourceAttribs = nullptr; // [m_Desc.NumResources] + ImmutableSamplerAttribs* m_ImmutableSamplers = nullptr; // [m_Desc.NumImmutableSamplers] }; } // namespace Diligent diff --git a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp index 68dcb479..f00ec89f 100644 --- a/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp +++ b/Graphics/GraphicsEngineD3D11/include/PipelineStateD3D11Impl.hpp @@ -92,8 +92,7 @@ public: SHADER_TYPE GetShaderStageType(Uint32 Index) const; #ifdef DILIGENT_DEVELOPMENT - using TBindingsPerStage = PipelineResourceSignatureD3D11Impl::TBindingsPerStage; - void DvpVerifySRBResources(class ShaderResourceBindingD3D11Impl* pSRBs[], const TBindingsPerStage BaseBindings[], Uint32 NumSRBs) const; + void DvpVerifySRBResources(class ShaderResourceBindingD3D11Impl* pSRBs[], const D3D11ShaderResourceCounters BaseBindings[], Uint32 NumSRBs) const; #endif private: diff --git a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp index 6e17ab0b..e290a530 100644 --- a/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp +++ b/Graphics/GraphicsEngineD3D11/include/ShaderResourceCacheD3D11.hpp @@ -156,44 +156,41 @@ public: template struct CachedResourceTraits; - static constexpr int NumShaderTypes = BindPointsD3D11::NumShaderTypes; - using TResourcesPerStage = std::array, D3D11_RESOURCE_RANGE_COUNT>; + static size_t GetRequriedMemorySize(const D3D11ShaderResourceCounters& ResCount); - static size_t GetRequriedMemorySize(const TResourcesPerStage& ResCount); + void Initialize(const D3D11ShaderResourceCounters& ResCount, IMemoryAllocator& MemAllocator); - void Initialize(const TResourcesPerStage& ResCount, IMemoryAllocator& MemAllocator); - - __forceinline void SetCB(BindPointsD3D11 BindPoints, RefCntAutoPtr pBuffD3D11Impl) + __forceinline void SetCB(const D3D11ResourceBindPoints& BindPoints, RefCntAutoPtr pBuffD3D11Impl) { auto* pd3d11Buff = pBuffD3D11Impl ? pBuffD3D11Impl->BufferD3D11Impl::GetD3D11Buffer() : nullptr; SetD3D11ResourceInternal(BindPoints, std::move(pBuffD3D11Impl), pd3d11Buff); } - __forceinline void SetTexSRV(BindPointsD3D11 BindPoints, RefCntAutoPtr pTexView) + __forceinline void SetTexSRV(const D3D11ResourceBindPoints& BindPoints, RefCntAutoPtr pTexView) { auto* pd3d11SRV = pTexView ? static_cast(pTexView->TextureViewD3D11Impl::GetD3D11View()) : nullptr; SetD3D11ResourceInternal(BindPoints, std::move(pTexView), pd3d11SRV); } - __forceinline void SetBufSRV(BindPointsD3D11 BindPoints, RefCntAutoPtr pBuffView) + __forceinline void SetBufSRV(const D3D11ResourceBindPoints& BindPoints, RefCntAutoPtr pBuffView) { auto* pd3d11SRV = pBuffView ? static_cast(pBuffView->BufferViewD3D11Impl::GetD3D11View()) : nullptr; SetD3D11ResourceInternal(BindPoints, std::move(pBuffView), pd3d11SRV); } - __forceinline void SetTexUAV(BindPointsD3D11 BindPoints, RefCntAutoPtr pTexView) + __forceinline void SetTexUAV(const D3D11ResourceBindPoints& BindPoints, RefCntAutoPtr pTexView) { auto* pd3d11UAV = pTexView ? static_cast(pTexView->TextureViewD3D11Impl::GetD3D11View()) : nullptr; SetD3D11ResourceInternal(BindPoints, std::move(pTexView), pd3d11UAV); } - __forceinline void SetBufUAV(BindPointsD3D11 BindPoints, RefCntAutoPtr pBuffView) + __forceinline void SetBufUAV(const D3D11ResourceBindPoints& BindPoints, RefCntAutoPtr pBuffView) { auto* pd3d11UAV = pBuffView ? static_cast(pBuffView->BufferViewD3D11Impl::GetD3D11View()) : nullptr; SetD3D11ResourceInternal(BindPoints, std::move(pBuffView), pd3d11UAV); } - __forceinline void SetSampler(BindPointsD3D11 BindPoints, SamplerD3D11Impl* pSampler) + __forceinline void SetSampler(const D3D11ResourceBindPoints& BindPoints, SamplerD3D11Impl* pSampler) { auto* pd3d11Sampler = pSampler ? pSampler->SamplerD3D11Impl::GetD3D11SamplerState() : nullptr; SetD3D11ResourceInternal(BindPoints, pSampler, pd3d11Sampler); @@ -201,10 +198,10 @@ public: template - __forceinline const typename CachedResourceTraits::CachedResourceType& GetResource(BindPointsD3D11 BindPoints) const + __forceinline const typename CachedResourceTraits::CachedResourceType& GetResource(const D3D11ResourceBindPoints& BindPoints) const { - VERIFY(BindPoints.GetActiveBits() != 0, "No active shader stage"); - const auto ShaderInd = PlatformMisc::GetLSB(BindPoints.GetActiveBits()); + VERIFY(BindPoints.GetActiveStages() != SHADER_TYPE_UNKNOWN, "No active shader stage"); + const auto ShaderInd = GetFirstShaderStageIndex(BindPoints.GetActiveStages()); const auto Offset = BindPoints[ShaderInd]; VERIFY(Offset < GetResourceCount(ShaderInd), "Resource slot is out of range"); const auto ResArrays = GetConstResourceArrays(ShaderInd); @@ -212,13 +209,12 @@ public: } template - bool CopyResource(const ShaderResourceCacheD3D11& SrcCache, BindPointsD3D11 BindPoints) + bool CopyResource(const ShaderResourceCacheD3D11& SrcCache, const D3D11ResourceBindPoints& BindPoints) { bool IsBound = true; - for (Uint32 ActiveBits = BindPoints.GetActiveBits(); ActiveBits != 0;) + for (auto ActiveStages = BindPoints.GetActiveStages(); ActiveStages != SHADER_TYPE_UNKNOWN;) { - const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits); - ActiveBits &= ~(1u << ShaderInd); + const auto ShaderInd = ExtractFirstShaderStageIndex(ActiveStages); auto SrcResArrays = SrcCache.GetConstResourceArrays(ShaderInd); auto DstResArrays = GetResourceArrays(ShaderInd); @@ -236,21 +232,19 @@ public: } template - __forceinline bool IsResourceBound(BindPointsD3D11 BindPoints) const + __forceinline bool IsResourceBound(const D3D11ResourceBindPoints& BindPoints) const { - Uint32 ActiveBits = BindPoints.GetActiveBits(); - if (ActiveBits == 0) + if (BindPoints.IsEmpty()) return false; - const Uint32 FirstShaderInd = PlatformMisc::GetLSB(ActiveBits); - const bool IsBound = IsResourceBound(FirstShaderInd, BindPoints[FirstShaderInd]); + auto ActiveStages = BindPoints.GetActiveStages(); + const auto FirstShaderInd = ExtractFirstShaderStageIndex(ActiveStages); + const bool IsBound = IsResourceBound(FirstShaderInd, BindPoints[FirstShaderInd]); #ifdef DILIGENT_DEBUG - ActiveBits &= ~(1u << FirstShaderInd); - while (ActiveBits != 0) + while (ActiveStages != SHADER_TYPE_UNKNOWN) { - const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits); - ActiveBits &= ~(1u << ShaderInd); + const Uint32 ShaderInd = ExtractFirstShaderStageIndex(ActiveStages); VERIFY_EXPR(IsBound == IsResourceBound(ShaderInd, BindPoints[ShaderInd])); } #endif @@ -298,13 +292,13 @@ public: template inline MinMaxSlot BindResources(Uint32 ShaderInd, typename CachedResourceTraits::D3D11ResourceType* CommittedD3D11Resources[], - const TResourcesPerStage& BaseBindings) const; + const D3D11ShaderResourceCounters& BaseBindings) const; template inline MinMaxSlot BindResourceViews(Uint32 ShaderInd, typename CachedResourceTraits::D3D11ResourceType* CommittedD3D11Views[], ID3D11Resource* CommittedD3D11Resources[], - const TResourcesPerStage& BaseBindings) const; + const D3D11ShaderResourceCounters& BaseBindings) const; enum class StateTransitionMode { @@ -345,15 +339,13 @@ private: } template - __forceinline void SetD3D11ResourceInternal(BindPointsD3D11 BindPoints, TSrcResourceType pResource, TD3D11ResourceType* pd3d11Resource) + __forceinline void SetD3D11ResourceInternal(const D3D11ResourceBindPoints& BindPoints, TSrcResourceType pResource, TD3D11ResourceType* pd3d11Resource) { VERIFY(pResource != nullptr && pd3d11Resource != nullptr || pResource == nullptr && pd3d11Resource == nullptr, "Resource and D3D11 resource must be set/unset atomically"); - for (Uint32 ActiveBits = BindPoints.GetActiveBits(); ActiveBits != 0;) + for (auto ActiveStages = BindPoints.GetActiveStages(); ActiveStages != SHADER_TYPE_UNKNOWN;) { - const Uint32 ShaderInd = PlatformMisc::GetLSB(ActiveBits); - ActiveBits &= ~(1u << ShaderInd); - + const Uint32 ShaderInd = ExtractFirstShaderStageIndex(ActiveStages); const Uint32 CacheOffset = BindPoints[ShaderInd]; VERIFY(CacheOffset < GetResourceCount(ShaderInd), "Cache offset is out of range"); @@ -390,6 +382,8 @@ private: static constexpr size_t MaxAlignment = std::max(std::max(std::max(alignof(CachedCB), alignof(CachedResource)), alignof(CachedSampler)), alignof(IUnknown*)); + static constexpr int NumShaderTypes = D3D11ResourceBindPoints::NumShaderTypes; + static constexpr Uint32 FirstCBOffsetIdx = 0; static constexpr Uint32 FirstSRVOffsetIdx = FirstCBOffsetIdx + NumShaderTypes; static constexpr Uint32 FirstSamOffsetIdx = FirstSRVOffsetIdx + NumShaderTypes; @@ -495,7 +489,7 @@ template inline ShaderResourceCacheD3D11::MinMaxSlot ShaderResourceCacheD3D11::BindResources( Uint32 ShaderInd, typename CachedResourceTraits::D3D11ResourceType* CommittedD3D11Resources[], - const TResourcesPerStage& BaseBindings) const + const D3D11ShaderResourceCounters& BaseBindings) const { const auto ResCount = GetResourceCount(ShaderInd); const auto ResArrays = GetConstResourceArrays(ShaderInd); @@ -521,7 +515,7 @@ inline ShaderResourceCacheD3D11::MinMaxSlot ShaderResourceCacheD3D11::BindResour Uint32 ShaderInd, typename CachedResourceTraits::D3D11ResourceType* CommittedD3D11Views[], ID3D11Resource* CommittedD3D11Resources[], - const TResourcesPerStage& BaseBindings) const + const D3D11ShaderResourceCounters& BaseBindings) const { const auto ResCount = GetResourceCount(ShaderInd); const auto ResArrays = GetConstResourceArrays(ShaderInd); diff --git a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp index b4df4009..7524bdd7 100755 --- a/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/DeviceContextD3D11Impl.cpp @@ -264,10 +264,10 @@ void DeviceContextD3D11Impl::CommitShaderResources(IShaderResourceBinding* pShad } -void DeviceContextD3D11Impl::BindCacheResources(const ShaderResourceCacheD3D11& ResourceCache, - const TBindingsPerStage& BaseBindings, - SHADER_TYPE ActiveStages, - PixelShaderUAVBindMode& PsUavBindMode) +void DeviceContextD3D11Impl::BindCacheResources(const ShaderResourceCacheD3D11& ResourceCache, + const D3D11ShaderResourceCounters& BaseBindings, + SHADER_TYPE ActiveStages, + PixelShaderUAVBindMode& PsUavBindMode) { while (ActiveStages != 0) { @@ -367,11 +367,11 @@ void DeviceContextD3D11Impl::BindShaderResources() if ((m_BindInfo.StaleSRBMask & m_BindInfo.ActiveSRBMask) == 0) return; - TBindingsPerStage Bindings = {}; - const auto ActiveStages = m_BindInfo.ActiveStages; + D3D11ShaderResourceCounters Bindings = {}; + const auto ActiveStages = m_BindInfo.ActiveStages; if (m_pPipelineState->GetDesc().IsAnyGraphicsPipeline()) - Bindings[D3D11_RESOURCE_RANGE_UAV][GetShaderTypeIndex(SHADER_TYPE_PIXEL)] = static_cast(m_pPipelineState->GetGraphicsPipelineDesc().NumRenderTargets); + Bindings[D3D11_RESOURCE_RANGE_UAV][PSInd] = static_cast(m_pPipelineState->GetGraphicsPipelineDesc().NumRenderTargets); PixelShaderUAVBindMode PsUavBindMode = m_CommittedRes.NumUAVs[PSInd] > 0 ? PixelShaderUAVBindMode::Clear : diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineResourceSignatureD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineResourceSignatureD3D11Impl.cpp index 33220e34..65c42f76 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineResourceSignatureD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineResourceSignatureD3D11Impl.cpp @@ -26,8 +26,10 @@ */ #include "pch.h" + #include "PipelineResourceSignatureD3D11Impl.hpp" #include "RenderDeviceD3D11Impl.hpp" +#include "ShaderVariableD3D.hpp" namespace Diligent { @@ -117,45 +119,35 @@ PipelineResourceSignatureD3D11Impl::PipelineResourceSignatureD3D11Impl(IReferenc void PipelineResourceSignatureD3D11Impl::CreateLayout() { - using TBindings32 = std::array; - using TBindingsPerStage32 = std::array, D3D11_RESOURCE_RANGE_COUNT>; - - const auto AllocBindPoints = [](TBindingsPerStage32& BindingPerStage, BindPointsD3D11& BindPoints, SHADER_TYPE ShaderStages, Uint32 ArraySize, D3D11_RESOURCE_RANGE Range) // + const auto AllocBindPoints = [](D3D11ShaderResourceCounters& BindingPerStage, D3D11ResourceBindPoints& BindPoints, SHADER_TYPE ShaderStages, Uint32 ArraySize, D3D11_RESOURCE_RANGE Range) // { while (ShaderStages != 0) { - auto Stage = ExtractLSB(ShaderStages); - Uint32 ShaderInd = GetShaderTypeIndex(Stage); + auto Stage = ExtractLSB(ShaderStages); + auto ShaderInd = GetShaderTypeIndex(Stage); + + BindPoints[ShaderInd] = BindingPerStage[Range][ShaderInd]; - BindPoints.Set(ShaderInd, BindingPerStage[Range][ShaderInd]); - BindingPerStage[Range][ShaderInd] += ArraySize; + using T = std::remove_reference::type; + VERIFY(Uint32{BindingPerStage[Range][ShaderInd]} + ArraySize < std::numeric_limits::max(), "Binding value exceeds representable range"); + BindingPerStage[Range][ShaderInd] += static_cast(ArraySize); } }; if (m_pStaticResCache) { - TBindingsPerStage32 StaticBindingsPerStage32 = {}; - const auto ResIdxRange = GetResourceIndexRange(SHADER_RESOURCE_VARIABLE_TYPE_STATIC); - for (Uint32 r = ResIdxRange.first; r < ResIdxRange.second; ++r) + D3D11ShaderResourceCounters StaticBindingsPerStage{}; + const auto StaticResIdxRange = GetResourceIndexRange(SHADER_RESOURCE_VARIABLE_TYPE_STATIC); + for (Uint32 r = StaticResIdxRange.first; r < StaticResIdxRange.second; ++r) { - const auto& ResDesc = m_Desc.Resources[r]; - const auto Range = ShaderResourceToDescriptorRange(ResDesc.ResourceType); - BindPointsD3D11 BindPoints; - AllocBindPoints(StaticBindingsPerStage32, BindPoints, ResDesc.ShaderStages, ResDesc.ArraySize, Range); - } + const auto& ResDesc = m_Desc.Resources[r]; + const auto Range = ShaderResourceToDescriptorRange(ResDesc.ResourceType); - TBindingsPerStage StaticBindingsPerStage8 = {}; - for (Uint32 r = 0; r < StaticBindingsPerStage32.size(); ++r) - { - for (Uint32 s = 0; s < StaticBindingsPerStage32[r].size(); ++s) - { - using T = std::remove_reference::type; - VERIFY_EXPR(StaticBindingsPerStage32[r][s] < std::numeric_limits::max()); - StaticBindingsPerStage8[r][s] = static_cast(StaticBindingsPerStage32[r][s]); - } + D3D11ResourceBindPoints BindPoints; + AllocBindPoints(StaticBindingsPerStage, BindPoints, ResDesc.ShaderStages, ResDesc.ArraySize, Range); } - m_pStaticResCache->Initialize(StaticBindingsPerStage8, GetRawAllocator()); + m_pStaticResCache->Initialize(StaticBindingsPerStage, GetRawAllocator()); VERIFY_EXPR(m_pStaticResCache->IsInitialized()); } @@ -191,7 +183,6 @@ void PipelineResourceSignatureD3D11Impl::CreateLayout() } } - TBindingsPerStage32 BindingPerStage = {}; for (Uint32 i = 0; i < m_Desc.NumResources; ++i) { const auto& ResDesc = m_Desc.Resources[i]; @@ -223,7 +214,7 @@ void PipelineResourceSignatureD3D11Impl::CreateLayout() if (!ImtblSampAttribs.IsAllocated()) { - AllocBindPoints(BindingPerStage, ImtblSampAttribs.BindPoints, ImtblSamp.ShaderStages, ImtblSampAttribs.ArraySize, D3D11_RESOURCE_RANGE_SAMPLER); + AllocBindPoints(m_BindingCountPerStage, ImtblSampAttribs.BindPoints, ImtblSamp.ShaderStages, ImtblSampAttribs.ArraySize, D3D11_RESOURCE_RANGE_SAMPLER); } } @@ -234,7 +225,7 @@ void PipelineResourceSignatureD3D11Impl::CreateLayout() AssignedSamplerInd, SrcImmutableSamplerInd != InvalidImmutableSamplerIndex // }; - AllocBindPoints(BindingPerStage, pAttrib->BindPoints, ResDesc.ShaderStages, ResDesc.ArraySize, Range); + AllocBindPoints(m_BindingCountPerStage, pAttrib->BindPoints, ResDesc.ShaderStages, ResDesc.ArraySize, Range); } else { @@ -261,23 +252,13 @@ void PipelineResourceSignatureD3D11Impl::CreateLayout() if (IsUsingCombinedSamplers() && !ImtblSampAttribs.IsAllocated()) continue; - GetDevice()->CreateSampler(ImtblSamp.Desc, &ImtblSampAttribs.pSampler); + GetDevice()->CreateSampler(ImtblSamp.Desc, ImtblSampAttribs.pSampler.DblPtr()); // Add as separate sampler. if (!ImtblSampAttribs.IsAllocated()) { ImtblSampAttribs.ArraySize = 1; - AllocBindPoints(BindingPerStage, ImtblSampAttribs.BindPoints, ImtblSamp.ShaderStages, ImtblSampAttribs.ArraySize, Range); - } - } - - for (Uint32 r = 0; r < BindingPerStage.size(); ++r) - { - for (Uint32 s = 0; s < BindingPerStage[r].size(); ++s) - { - using T = std::remove_reference::type; - VERIFY_EXPR(BindingPerStage[r][s] < std::numeric_limits::max()); - m_BindingCountPerStage[r][s] = static_cast(BindingPerStage[r][s]); + AllocBindPoints(m_BindingCountPerStage, ImtblSampAttribs.BindPoints, ImtblSamp.ShaderStages, ImtblSampAttribs.ArraySize, Range); } } } @@ -374,8 +355,8 @@ void PipelineResourceSignatureD3D11Impl::InitSRBResourceCache(ShaderResourceCach if (ImtblSampAttr.IsAllocated()) { - SamplerD3D11Impl* pSampler = ImtblSampAttr.GetSamplerD3D11(); - VERIFY_EXPR(pSampler != nullptr); + SamplerD3D11Impl* pSampler = ImtblSampAttr.pSampler.RawPtr(); + VERIFY_EXPR(ImtblSampAttr.pSampler != nullptr); VERIFY_EXPR(ImtblSampAttr.ArraySize > 0); for (Uint32 ArrInd = 0; ArrInd < ImtblSampAttr.ArraySize; ++ArrInd) @@ -384,7 +365,9 @@ void PipelineResourceSignatureD3D11Impl::InitSRBResourceCache(ShaderResourceCach } } -void PipelineResourceSignatureD3D11Impl::UpdateShaderResourceBindingMap(ResourceBinding::TMap& ResourceMap, SHADER_TYPE ShaderStage, const TBindingsPerStage& BaseBindings) const +void PipelineResourceSignatureD3D11Impl::UpdateShaderResourceBindingMap(ResourceBinding::TMap& ResourceMap, + SHADER_TYPE ShaderStage, + const D3D11ShaderResourceCounters& BaseBindings) const { VERIFY(ShaderStage != SHADER_TYPE_UNKNOWN && IsPowerOfTwo(ShaderStage), "Only single shader stage must be provided."); const auto ShaderInd = GetShaderTypeIndex(ShaderStage); @@ -397,7 +380,7 @@ void PipelineResourceSignatureD3D11Impl::UpdateShaderResourceBindingMap(Resource if ((ResDesc.ShaderStages & ShaderStage) != 0) { - VERIFY_EXPR(ResAttr.BindPoints.IsValid(ShaderInd)); + VERIFY_EXPR(ResAttr.BindPoints.IsStageActive(ShaderInd)); ResourceBinding::BindInfo BindInfo // { Uint32{BaseBindings[Range][ShaderInd]} + ResAttr.BindPoints[ShaderInd], @@ -420,7 +403,7 @@ void PipelineResourceSignatureD3D11Impl::UpdateShaderResourceBindingMap(Resource if ((ImtblSam.ShaderStages & ShaderStage) != 0 && SampAttr.IsAllocated()) { - VERIFY_EXPR(SampAttr.BindPoints.IsValid(ShaderInd)); + VERIFY_EXPR(SampAttr.BindPoints.IsStageActive(ShaderInd)); String SampName{ImtblSam.SamplerOrTextureName}; if (IsUsingCombinedSamplers()) @@ -498,13 +481,19 @@ bool PipelineResourceSignatureD3D11Impl::DvpValidateCommittedResource(const D3DS BindingsOK = false; continue; } - /* - const auto& SRV = ResourceCache.GetSRV(ResAttr.BindPoints + ArrInd); + + const auto& SRV = ResourceCache.GetResource(ResAttr.BindPoints + ArrInd); if (SRV.pTexture) - ValidateResourceViewDimension(ResDesc.Name, ResDesc.ArraySize, ArrInd, SRV.pView.RawPtr(), D3DAttribs.GetResourceDimension(), D3DAttribs.IsMultisample()); + { + if (!ValidateResourceViewDimension(ResDesc.Name, ResDesc.ArraySize, ArrInd, SRV.pView.RawPtr(), + D3DAttribs.GetResourceDimension(), D3DAttribs.IsMultisample())) + BindingsOK = false; + } else - ValidateResourceViewDimension(ResDesc.Name, ResDesc.ArraySize, ArrInd, SRV.pView.RawPtr(), D3DAttribs.GetResourceDimension(), D3DAttribs.IsMultisample()); - */ + { + if (!VerifyBufferViewModeD3D(SRV.pView.RawPtr(), D3DAttribs, ShaderName)) + BindingsOK = false; + } } break; @@ -518,13 +507,18 @@ bool PipelineResourceSignatureD3D11Impl::DvpValidateCommittedResource(const D3DS BindingsOK = false; continue; } - /* - const auto& UAV = ResourceCache.GetUAV(ResAttr.BindPoints + ArrInd); + const auto& UAV = ResourceCache.GetResource(ResAttr.BindPoints + ArrInd); if (UAV.pTexture) - ValidateResourceViewDimension(ResDesc.Name, ResDesc.ArraySize, ArrInd, UAV.pView.RawPtr(), D3DAttribs.GetResourceDimension(), D3DAttribs.IsMultisample()); + { + if (!ValidateResourceViewDimension(ResDesc.Name, ResDesc.ArraySize, ArrInd, UAV.pView.RawPtr(), + D3DAttribs.GetResourceDimension(), D3DAttribs.IsMultisample())) + BindingsOK = false; + } else - ValidateResourceViewDimension(ResDesc.Name, ResDesc.ArraySize, ArrInd, UAV.pView.RawPtr(), D3DAttribs.GetResourceDimension(), D3DAttribs.IsMultisample()); - */ + { + if (!VerifyBufferViewModeD3D(UAV.pView.RawPtr(), D3DAttribs, ShaderName)) + BindingsOK = false; + } } break; diff --git a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp index 2fa5b73a..7d5fcc52 100644 --- a/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp +++ b/Graphics/GraphicsEngineD3D11/src/PipelineStateD3D11Impl.cpp @@ -200,7 +200,7 @@ void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& const auto ShaderType = pShader->GetDesc().ShaderType; auto* pBytecode = Shaders[s]->GetBytecode(); - PipelineResourceSignatureD3D11Impl::TBindingsPerStage BindingsPerStage = {}; + D3D11ShaderResourceCounters BindingsPerStage = {}; if (m_Desc.IsAnyGraphicsPipeline()) BindingsPerStage[D3D11_RESOURCE_RANGE_UAV][PSInd] = GetGraphicsPipelineDesc().NumRenderTargets; @@ -229,7 +229,7 @@ void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& } #ifdef DILIGENT_DEVELOPMENT - PipelineResourceSignatureD3D11Impl::TBindingsPerStage BindingsPerStage = {}; + D3D11ShaderResourceCounters BindingsPerStage = {}; if (m_Desc.IsAnyGraphicsPipeline()) BindingsPerStage[D3D11_RESOURCE_RANGE_UAV][PSInd] = GetGraphicsPipelineDesc().NumRenderTargets; @@ -241,7 +241,7 @@ void PipelineStateD3D11Impl::InitResourceLayouts(const PipelineStateCreateInfo& pSignature->ShiftBindings(BindingsPerStage); } - for (Uint32 s = 0; s < PipelineResourceSignatureD3D11Impl::NumShaderTypes; ++s) + for (Uint32 s = 0; s < D3D11ResourceBindPoints::NumShaderTypes; ++s) { DEV_CHECK_ERR(BindingsPerStage[D3D11_RESOURCE_RANGE_CBV][s] <= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, "Constant buffer count ", Uint32{BindingsPerStage[D3D11_RESOURCE_RANGE_CBV][s]}, " exceeds D3D11 limit ", D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT); @@ -516,11 +516,13 @@ void PipelineStateD3D11Impl::ValidateShaderResources(const ShaderD3D11Impl* pSha } #ifdef DILIGENT_DEVELOPMENT -void PipelineStateD3D11Impl::DvpVerifySRBResources(class ShaderResourceBindingD3D11Impl* pSRBs[], const TBindingsPerStage BaseBindings[], Uint32 NumSRBs) const +void PipelineStateD3D11Impl::DvpVerifySRBResources(ShaderResourceBindingD3D11Impl* pSRBs[], + const D3D11ShaderResourceCounters BaseBindings[], + Uint32 NumSRBs) const { // Verify SRB compatibility with this pipeline - const auto SignCount = GetResourceSignatureCount(); - TBindingsPerStage Bindings = {}; + const auto SignCount = GetResourceSignatureCount(); + D3D11ShaderResourceCounters Bindings = {}; if (m_Desc.IsAnyGraphicsPipeline()) Bindings[D3D11_RESOURCE_RANGE_UAV][GetShaderTypeIndex(SHADER_TYPE_PIXEL)] = static_cast(GetGraphicsPipelineDesc().NumRenderTargets); diff --git a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp index b16a5c6d..0c6c6f88 100755 --- a/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp +++ b/Graphics/GraphicsEngineD3D11/src/ShaderResourceCacheD3D11.cpp @@ -39,7 +39,7 @@ namespace Diligent { -size_t ShaderResourceCacheD3D11::GetRequriedMemorySize(const TResourcesPerStage& ResCount) +size_t ShaderResourceCacheD3D11::GetRequriedMemorySize(const D3D11ShaderResourceCounters& ResCount) { size_t MemSize = 0; // clang-format off @@ -60,7 +60,7 @@ size_t ShaderResourceCacheD3D11::GetRequriedMemorySize(const TResourcesPerStage& return MemSize; } -void ShaderResourceCacheD3D11::Initialize(const TResourcesPerStage& ResCount, IMemoryAllocator& MemAllocator) +void ShaderResourceCacheD3D11::Initialize(const D3D11ShaderResourceCounters& ResCount, IMemoryAllocator& MemAllocator) { // http://diligentgraphics.com/diligent-engine/architecture/d3d11/shader-resource-cache/ VERIFY(!IsInitialized(), "Resource cache has already been intialized!"); -- cgit v1.2.3