diff options
| author | azhirnov <zh1dron@gmail.com> | 2020-10-08 19:14:06 +0000 |
|---|---|---|
| committer | azhirnov <zh1dron@gmail.com> | 2020-10-08 19:36:02 +0000 |
| commit | 998165f350cfd8ae6bd9cbf6fc812874c1c3c3da (patch) | |
| tree | 39e748a9f09b7bc3b187c4061e19313eb8e42626 /Graphics/GraphicsEngineVulkan | |
| parent | Some minor fixes (diff) | |
| download | DiligentCore-998165f350cfd8ae6bd9cbf6fc812874c1c3c3da.tar.gz DiligentCore-998165f350cfd8ae6bd9cbf6fc812874c1c3c3da.zip | |
Revert some changes in ShaderResourceLayoutVk, fixed PSO comparison
Diffstat (limited to 'Graphics/GraphicsEngineVulkan')
3 files changed, 83 insertions, 108 deletions
diff --git a/Graphics/GraphicsEngineVulkan/include/ShaderResourceLayoutVk.hpp b/Graphics/GraphicsEngineVulkan/include/ShaderResourceLayoutVk.hpp index 28defd0f..4d29093d 100644 --- a/Graphics/GraphicsEngineVulkan/include/ShaderResourceLayoutVk.hpp +++ b/Graphics/GraphicsEngineVulkan/include/ShaderResourceLayoutVk.hpp @@ -177,21 +177,33 @@ public: /* 7.5 */ const Uint32 VariableType : VariableTypeBits; /* 7.7 */ const Uint32 ImmutableSamplerAssigned : ImmutableSamplerFlagBits; -/* 8 */ const SPIRVShaderResourceAttribs SpirvAttribs; -/* 16 */ const ShaderResourceLayoutVk& ParentResLayout; +/* 8 */ const SPIRVShaderResourceAttribs& SpirvAttribs; +/* 16 */ const ShaderResourceLayoutVk& ParentResLayout; + + VkResource(const ShaderResourceLayoutVk& _ParentLayout, + const SPIRVShaderResourceAttribs& _SpirvAttribs, + SHADER_RESOURCE_VARIABLE_TYPE _VariableType, + uint32_t _Binding, + uint32_t _DescriptorSet, + Uint32 _CacheOffset, + Uint32 _SamplerInd, + bool _ImmutableSamplerAssigned = false)noexcept : + Binding {static_cast<decltype(Binding)>(_Binding) }, + DescriptorSet {static_cast<decltype(DescriptorSet)>(_DescriptorSet)}, + CacheOffset {_CacheOffset }, + SamplerInd {_SamplerInd }, + VariableType {_VariableType }, + ImmutableSamplerAssigned {_ImmutableSamplerAssigned ? 1U : 0U}, + SpirvAttribs {_SpirvAttribs }, + ParentResLayout {_ParentLayout } + { + VERIFY(_CacheOffset < (1 << CacheOffsetBits), "Cache offset (", _CacheOffset, ") exceeds max representable value ", (1 << CacheOffsetBits) ); + VERIFY(_SamplerInd < (1 << SamplerIndBits), "Sampler index (", _SamplerInd, ") exceeds max representable value ", (1 << SamplerIndBits) ); + VERIFY(_Binding <= std::numeric_limits<decltype(Binding)>::max(), "Binding (", _Binding, ") exceeds max representable value ", std::numeric_limits<decltype(Binding)>::max() ); + VERIFY(_DescriptorSet <= std::numeric_limits<decltype(DescriptorSet)>::max(), "Descriptor set (", _DescriptorSet, ") exceeds max representable value ", std::numeric_limits<decltype(DescriptorSet)>::max()); + } // clang-format on - VkResource(const ShaderResourceLayoutVk& _ParentLayout, - const SPIRVShaderResourceAttribs& _SpirvAttribs, - SHADER_RESOURCE_VARIABLE_TYPE _VariableType, - uint32_t _Binding, - uint32_t _DescriptorSet, - Uint32 _CacheOffset, - Uint32 _SamplerInd, - bool _ImmutableSamplerAssigned = false) noexcept; - - ~VkResource(); - // Checks if a resource is bound in ResourceCache at the given ArrayIndex bool IsBound(Uint32 ArrayIndex, const ShaderResourceCacheVk& ResourceCache) const; @@ -289,10 +301,15 @@ public: const Char* GetShaderName() const { - return ""; // AZ TODO + return m_pResources->GetShaderName(); } - SHADER_TYPE GetShaderType() const { return m_ShaderType; } + SHADER_TYPE GetShaderType() const + { + return m_pResources->GetShaderType(); + } + + const SPIRVShaderResources& GetResources() const { return *m_pResources; } const VkResource& GetResource(SHADER_RESOURCE_VARIABLE_TYPE VarType, Uint32 r) const { @@ -301,7 +318,7 @@ public: return Resources[GetResourceOffset(VarType, r)]; } - bool IsUsingSeparateSamplers() const { return m_IsUsingSeparateSamplers; } + bool IsUsingSeparateSamplers() const { return !m_pResources->IsUsingCombinedSamplers(); } private: Uint32 GetResourceOffset(SHADER_RESOURCE_VARIABLE_TYPE VarType, Uint32 r) const @@ -348,16 +365,16 @@ private: } // clang-format off -/* 0 */ const VulkanUtilities::VulkanLogicalDevice& m_LogicalDevice; -/* 8 */ std::unique_ptr<void, STDDeleterRawMem<void> > m_ResourceBuffer; - -/*24 */ std::array<Uint16, SHADER_RESOURCE_VARIABLE_TYPE_NUM_TYPES+1> m_NumResources = {}; +/* 0 */ const VulkanUtilities::VulkanLogicalDevice& m_LogicalDevice; +/* 8 */ std::unique_ptr<void, STDDeleterRawMem<void> > m_ResourceBuffer; -/*32 */ Uint32 m_NumImmutableSamplers = 0; -/*36 */ bool m_IsUsingSeparateSamplers = false; -/*37 */ SHADER_TYPE m_ShaderType = SHADER_TYPE_UNKNOWN; + // We must use shared_ptr to reference ShaderResources instance, because + // there may be multiple objects referencing the same set of resources +/*24 */ std::shared_ptr<const SPIRVShaderResources> m_pResources; -/*40 */ // End of class +/*40 */ std::array<Uint16, SHADER_RESOURCE_VARIABLE_TYPE_NUM_TYPES+1> m_NumResources = {}; +/*48 */ Uint32 m_NumImmutableSamplers = 0; +/*56*/ // End of class // clang-format on }; diff --git a/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp b/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp index 29d16239..f40f22c8 100644 --- a/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp +++ b/Graphics/GraphicsEngineVulkan/src/PipelineStateVkImpl.cpp @@ -575,15 +575,14 @@ bool PipelineStateVkImpl::IsCompatibleWith(const IPipelineState* pPSO) const IsCompatibleShaders = false; break; } - - // AZ TODO - /*const auto* pRes0 = pShader0->GetShaderResources().get(); - const auto* pRes1 = pShader1->GetShaderResources().get(); - if (!pRes0->IsCompatibleWith(*pRes1)) + + const auto& Res0 = GetShaderResLayout(s).GetResources(); + const auto& Res1 = pPSOVk->GetShaderResLayout(s).GetResources(); + if (!Res0.IsCompatibleWith(Res1)) { IsCompatibleShaders = false; break; - }*/ + } } } diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp index 1f798eb9..a7410210 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp @@ -111,21 +111,21 @@ void ShaderResourceLayoutVk::AllocateMemory(IShader* bool AllocateImmutableSamplers) { VERIFY(!m_ResourceBuffer, "Memory has already been initialized"); - VERIFY_EXPR(pShader != nullptr); - VERIFY_EXPR(m_ShaderType == SHADER_TYPE_UNKNOWN); + VERIFY_EXPR(!m_pResources); const Uint32 AllowedTypeBits = GetAllowedTypeBits(AllowedVarTypes, NumAllowedTypes); - m_ShaderType = pShader->GetDesc().ShaderType; + const auto ShaderType = pShader->GetDesc().ShaderType; // Count the number of resources to allocate all needed memory { auto* pShaderVk = ValidatedCast<ShaderVkImpl>(pShader); auto pResources = pShaderVk->GetShaderResources(); const auto* CombinedSamplerSuffix = pResources->GetCombinedSamplerSuffix(); - VERIFY_EXPR(pResources->GetShaderType() == m_ShaderType); - pResources->ProcessResources( + VERIFY_EXPR(pResources->GetShaderType() == ShaderType); + m_pResources = pResources; + m_pResources->ProcessResources( [&](const SPIRVShaderResourceAttribs& ResAttribs, Uint32) // { - auto VarType = FindShaderVariableType(m_ShaderType, ResAttribs, ResourceLayoutDesc, CombinedSamplerSuffix); + auto VarType = FindShaderVariableType(ShaderType, ResAttribs, ResourceLayoutDesc, CombinedSamplerSuffix); if (IsAllowedType(VarType, AllowedTypeBits)) { // For immutable separate samplers we still allocate VkResource instances, but they are never exposed to the app @@ -135,7 +135,6 @@ void ShaderResourceLayoutVk::AllocateMemory(IShader* } } // ); - m_IsUsingSeparateSamplers = !pResources->IsUsingCombinedSamplers(); } Uint32 TotalResources = 0; @@ -152,7 +151,7 @@ void ShaderResourceLayoutVk::AllocateMemory(IShader* for (Uint32 s = 0; s < ResourceLayoutDesc.NumStaticSamplers; ++s) { const auto& StSamDesc = ResourceLayoutDesc.StaticSamplers[s]; - if ((StSamDesc.ShaderStages & m_ShaderType) != 0) + if ((StSamDesc.ShaderStages & ShaderType) != 0) ++m_NumImmutableSamplers; } } @@ -227,45 +226,41 @@ void ShaderResourceLayoutVk::InitializeStaticResourceLayout(IShader* Uint32 StaticResCacheSize = 0; - const Uint32 AllowedTypeBits = GetAllowedTypeBits(&AllowedVarType, 1); + const Uint32 AllowedTypeBits = GetAllowedTypeBits(&AllowedVarType, 1); + const auto* CombinedSamplerSuffix = m_pResources->GetCombinedSamplerSuffix(); + const auto ShaderType = pShader->GetDesc().ShaderType; - { - auto* pShaderVk = ValidatedCast<ShaderVkImpl>(pShader); - auto pResources = pShaderVk->GetShaderResources(); - const auto* CombinedSamplerSuffix = pResources->GetCombinedSamplerSuffix(); + m_pResources->ProcessResources( + [&](const SPIRVShaderResourceAttribs& Attribs, Uint32) // + { + auto VarType = FindShaderVariableType(ShaderType, Attribs, ResourceLayoutDesc, CombinedSamplerSuffix); + if (!IsAllowedType(VarType, AllowedTypeBits)) + return; - pResources->ProcessResources( - [&](const SPIRVShaderResourceAttribs& Attribs, Uint32) // + Int32 SrcImmutableSamplerInd = -1; + if (Attribs.Type == SPIRVShaderResourceAttribs::ResourceType::SampledImage || + Attribs.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateSampler) { - auto VarType = FindShaderVariableType(m_ShaderType, Attribs, ResourceLayoutDesc, CombinedSamplerSuffix); - if (!IsAllowedType(VarType, AllowedTypeBits)) - return; - - Int32 SrcImmutableSamplerInd = -1; - if (Attribs.Type == SPIRVShaderResourceAttribs::ResourceType::SampledImage || - Attribs.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateSampler) - { - // Only search for the immutable sampler for combined image samplers and separate samplers - SrcImmutableSamplerInd = FindImmutableSampler(m_ShaderType, ResourceLayoutDesc, Attribs, CombinedSamplerSuffix); - // For immutable separate samplers we allocate VkResource instances, but they are never exposed to the app - } + // Only search for the immutable sampler for combined image samplers and separate samplers + SrcImmutableSamplerInd = FindImmutableSampler(ShaderType, ResourceLayoutDesc, Attribs, CombinedSamplerSuffix); + // For immutable separate samplers we allocate VkResource instances, but they are never exposed to the app + } - Uint32 Binding = Attribs.Type; - Uint32 DescriptorSet = 0; - Uint32 CacheOffset = StaticResCacheSize; - StaticResCacheSize += Attribs.ArraySize; + Uint32 Binding = Attribs.Type; + Uint32 DescriptorSet = 0; + Uint32 CacheOffset = StaticResCacheSize; + StaticResCacheSize += Attribs.ArraySize; - Uint32 SamplerInd = VkResource::InvalidSamplerInd; - if (Attribs.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateImage) - { - // Separate samplers are enumerated before separate images, so the sampler - // assigned to this separate image must have already been created. - SamplerInd = FindAssignedSampler(*this, *pResources, Attribs, CurrResInd[VarType], VarType); - } - ::new (&GetResource(VarType, CurrResInd[VarType]++)) VkResource(*this, Attribs, VarType, Binding, DescriptorSet, CacheOffset, SamplerInd, SrcImmutableSamplerInd >= 0); - } // - ); - } + Uint32 SamplerInd = VkResource::InvalidSamplerInd; + if (Attribs.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateImage) + { + // Separate samplers are enumerated before separate images, so the sampler + // assigned to this separate image must have already been created. + SamplerInd = FindAssignedSampler(*this, *m_pResources, Attribs, CurrResInd[VarType], VarType); + } + ::new (&GetResource(VarType, CurrResInd[VarType]++)) VkResource(*this, Attribs, VarType, Binding, DescriptorSet, CacheOffset, SamplerInd, SrcImmutableSamplerInd >= 0); + } // + ); #ifdef DILIGENT_DEBUG for (SHADER_RESOURCE_VARIABLE_TYPE VarType = SHADER_RESOURCE_VARIABLE_TYPE_STATIC; VarType < SHADER_RESOURCE_VARIABLE_TYPE_NUM_TYPES; VarType = static_cast<SHADER_RESOURCE_VARIABLE_TYPE>(VarType + 1)) @@ -603,42 +598,6 @@ void ShaderResourceLayoutVk::Initialize(IRenderDevice* pRende #endif } - -ShaderResourceLayoutVk::VkResource::VkResource(const ShaderResourceLayoutVk& _ParentLayout, - const SPIRVShaderResourceAttribs& _SpirvAttribs, - SHADER_RESOURCE_VARIABLE_TYPE _VariableType, - uint32_t _Binding, - uint32_t _DescriptorSet, - Uint32 _CacheOffset, - Uint32 _SamplerInd, - bool _ImmutableSamplerAssigned) noexcept : - // clang-format off - Binding {static_cast<decltype(Binding)>(_Binding) }, - DescriptorSet {static_cast<decltype(DescriptorSet)>(_DescriptorSet)}, - CacheOffset {_CacheOffset }, - SamplerInd {_SamplerInd }, - VariableType {_VariableType }, - ImmutableSamplerAssigned {_ImmutableSamplerAssigned ? 1U : 0U}, - SpirvAttribs {_SpirvAttribs }, - ParentResLayout {_ParentLayout } -// clang-format on -{ - VERIFY(_CacheOffset < (1 << CacheOffsetBits), "Cache offset (", _CacheOffset, ") exceeds max representable value ", (1 << CacheOffsetBits)); - VERIFY(_SamplerInd < (1 << SamplerIndBits), "Sampler index (", _SamplerInd, ") exceeds max representable value ", (1 << SamplerIndBits)); - VERIFY(_Binding <= std::numeric_limits<decltype(Binding)>::max(), "Binding (", _Binding, ") exceeds max representable value ", std::numeric_limits<decltype(Binding)>::max()); - VERIFY(_DescriptorSet <= std::numeric_limits<decltype(DescriptorSet)>::max(), "Descriptor set (", _DescriptorSet, ") exceeds max representable value ", std::numeric_limits<decltype(DescriptorSet)>::max()); - - const size_t Size = strlen(SpirvAttribs.Name) + 1; - char* NameCopy = ALLOCATE(GetRawAllocator(), "SPIRV Attribs Name", char, Size); - std::memcpy(NameCopy, SpirvAttribs.Name, Size); - const_cast<SPIRVShaderResourceAttribs&>(SpirvAttribs).Name = NameCopy; -} - -ShaderResourceLayoutVk::VkResource::~VkResource() -{ - FREE(GetRawAllocator(), const_cast<char*>(SpirvAttribs.Name)); -} - void ShaderResourceLayoutVk::VkResource::UpdateDescriptorHandle(VkDescriptorSet vkDescrSet, uint32_t ArrayElement, const VkDescriptorImageInfo* pImageInfo, |
