From 065191204c45e5d10caef5157e8bf293b5f7a099 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Thu, 3 May 2018 10:02:10 -0700 Subject: Working on Vulkan pipeline layout --- .../GraphicsEngineVulkan/include/PipelineLayout.h | 324 ++------------------- .../GraphicsEngineVulkan/src/PipelineLayout.cpp | 174 ++++------- .../src/ShaderResourceLayoutVk.cpp | 2 +- 3 files changed, 77 insertions(+), 423 deletions(-) (limited to 'Graphics/GraphicsEngineVulkan') diff --git a/Graphics/GraphicsEngineVulkan/include/PipelineLayout.h b/Graphics/GraphicsEngineVulkan/include/PipelineLayout.h index 89e9dc5f..8e31e0ac 100644 --- a/Graphics/GraphicsEngineVulkan/include/PipelineLayout.h +++ b/Graphics/GraphicsEngineVulkan/include/PipelineLayout.h @@ -24,7 +24,7 @@ #pragma once /// \file -/// Declaration of Diligent::RootSignature class +/// Declaration of Diligent::PipelineLayout class #include "ShaderBase.h" #include "ShaderResourceLayoutVk.h" #include "VulkanUtilities/VulkanObjectWrappers.h" @@ -35,264 +35,34 @@ namespace Diligent class RenderDeviceVkImpl; -//SHADER_TYPE ShaderTypeFromShaderVisibility(Vk_SHADER_VISIBILITY ShaderVisibility); -//Vk_SHADER_VISIBILITY GetShaderVisibility(SHADER_TYPE ShaderType); -//Vk_DESCRIPTOR_HEAP_TYPE dbgHeapTypeFromRangeType(Vk_DESCRIPTOR_RANGE_TYPE RangeType); -#if 0 -class DescriptorSetLayout -{ -public: - - - RootParameter(Vk_ROOT_PARAMETER_TYPE ParameterType, Uint32 RootIndex, UINT Register, UINT RegisterSpace, Vk_SHADER_VISIBILITY Visibility, SHADER_VARIABLE_TYPE VarType) : - m_RootIndex(RootIndex), - m_ShaderVarType(VarType) - { - VERIFY(ParameterType == Vk_ROOT_PARAMETER_TYPE_CBV || ParameterType == Vk_ROOT_PARAMETER_TYPE_SRV || ParameterType == Vk_ROOT_PARAMETER_TYPE_UAV, "Unexpected parameter type - verify argument list"); - m_RootParam.ParameterType = ParameterType; - m_RootParam.ShaderVisibility = Visibility; - m_RootParam.Descriptor.ShaderRegister = Register; - m_RootParam.Descriptor.RegisterSpace = RegisterSpace; - } - - RootParameter(Vk_ROOT_PARAMETER_TYPE ParameterType, Uint32 RootIndex, UINT Register, UINT RegisterSpace, UINT NumDwords, Vk_SHADER_VISIBILITY Visibility, SHADER_VARIABLE_TYPE VarType) : - m_RootIndex(RootIndex), - m_ShaderVarType(VarType) - { - VERIFY(ParameterType == Vk_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS, "Unexpected parameter type - verify argument list"); - m_RootParam.ParameterType = ParameterType; - m_RootParam.ShaderVisibility = Visibility; - m_RootParam.Constants.Num32BitValues = NumDwords; - m_RootParam.Constants.ShaderRegister = Register; - m_RootParam.Constants.RegisterSpace = RegisterSpace; - } - - RootParameter(Vk_ROOT_PARAMETER_TYPE ParameterType, Uint32 RootIndex, UINT NumRanges, Vk_DESCRIPTOR_RANGE *pRanges, Vk_SHADER_VISIBILITY Visibility, SHADER_VARIABLE_TYPE VarType) : - m_RootIndex(RootIndex), - m_ShaderVarType(VarType) - { - VERIFY(ParameterType == Vk_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, "Unexpected parameter type - verify argument list"); - VERIFY_EXPR(pRanges != nullptr); - m_RootParam.ParameterType = ParameterType; - m_RootParam.ShaderVisibility = Visibility; - m_RootParam.DescriptorTable.NumDescriptorRanges = NumRanges; - m_RootParam.DescriptorTable.pDescriptorRanges = pRanges; -#ifdef _DEBUG - for (Uint32 r = 0; r < NumRanges; ++r) - pRanges[r].RangeType = static_cast(-1); -#endif - } - - RootParameter(const RootParameter &RP) : - m_RootParam(RP.m_RootParam), - m_DescriptorTableSize(RP.m_DescriptorTableSize), - m_ShaderVarType(RP.m_ShaderVarType), - m_RootIndex(RP.m_RootIndex) - { - VERIFY(m_RootParam.ParameterType != Vk_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, "Use another constructor to copy descriptor table"); - } - - RootParameter(const RootParameter &RP, UINT NumRanges, Vk_DESCRIPTOR_RANGE *pRanges) : - m_RootParam(RP.m_RootParam), - m_DescriptorTableSize(RP.m_DescriptorTableSize), - m_ShaderVarType(RP.m_ShaderVarType), - m_RootIndex(RP.m_RootIndex) - { - VERIFY(m_RootParam.ParameterType == Vk_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, "Root parameter is expected to be a descriptor table"); - VERIFY(NumRanges >= m_RootParam.DescriptorTable.NumDescriptorRanges, "New table must be larger than source one"); - auto &DstTbl = m_RootParam.DescriptorTable; - DstTbl.NumDescriptorRanges = NumRanges; - DstTbl.pDescriptorRanges = pRanges; - const auto &SrcTbl = RP.m_RootParam.DescriptorTable; - memcpy(pRanges, SrcTbl.pDescriptorRanges, SrcTbl.NumDescriptorRanges * sizeof(Vk_DESCRIPTOR_RANGE)); -#ifdef _DEBUG - { - Uint32 dbgTableSize = 0; - for (Uint32 r = 0; r < SrcTbl.NumDescriptorRanges; ++r) - { - const auto &Range = SrcTbl.pDescriptorRanges[r]; - dbgTableSize = std::max(dbgTableSize, Range.OffsetInDescriptorsFromTableStart + Range.NumDescriptors); - } - VERIFY(dbgTableSize == m_DescriptorTableSize, "Incorrect descriptor table size"); - - for (Uint32 r = SrcTbl.NumDescriptorRanges; r < DstTbl.NumDescriptorRanges; ++r) - pRanges[r].RangeType = static_cast(-1); - } -#endif -} - - RootParameter& operator = (const RootParameter &RP) = delete; - RootParameter& operator = (RootParameter &&RP) = delete; - - void SetDescriptorRange(UINT RangeIndex, Vk_DESCRIPTOR_RANGE_TYPE Type, UINT Register, UINT Count, UINT Space = 0, UINT OffsetFromTableStart = Vk_DESCRIPTOR_RANGE_OFFSET_APPEND) - { - VERIFY(m_RootParam.ParameterType == Vk_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, "Incorrect parameter table: descriptor table is expected"); - auto &Tbl = m_RootParam.DescriptorTable; - VERIFY(RangeIndex < Tbl.NumDescriptorRanges, "Invalid descriptor range index"); - Vk_DESCRIPTOR_RANGE &range = const_cast(Tbl.pDescriptorRanges[RangeIndex]); - VERIFY(range.RangeType == static_cast(-1), "Descriptor range has already been initialized. m_DescriptorTableSize may be updated incorrectly"); - range.RangeType = Type; - range.NumDescriptors = Count; - range.BaseShaderRegister = Register; - range.RegisterSpace = Space; - range.OffsetInDescriptorsFromTableStart = OffsetFromTableStart; - m_DescriptorTableSize = std::max(m_DescriptorTableSize, OffsetFromTableStart + Count); - } - - SHADER_VARIABLE_TYPE GetShaderVariableType()const { return m_ShaderVarType; } - Uint32 GetDescriptorTableSize()const - { - VERIFY(m_RootParam.ParameterType == Vk_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE, "Incorrect parameter table: descriptor table is expected"); - return m_DescriptorTableSize; - } - Vk_SHADER_VISIBILITY GetShaderVisibility()const { return m_RootParam.ShaderVisibility; } - Vk_ROOT_PARAMETER_TYPE GetParameterType()const { return m_RootParam.ParameterType; } - - Uint32 GetRootIndex()const { return m_RootIndex; } - - operator const Vk_ROOT_PARAMETER&()const { return m_RootParam; } - - bool operator == (const RootParameter&rhs)const - { - if (m_ShaderVarType != rhs.m_ShaderVarType || - m_DescriptorTableSize != rhs.m_DescriptorTableSize || - m_RootIndex != rhs.m_RootIndex) - return false; - - if (m_RootParam.ParameterType != rhs.m_RootParam.ParameterType || - m_RootParam.ShaderVisibility != rhs.m_RootParam.ShaderVisibility) - return false; - - switch (m_RootParam.ParameterType) - { - case Vk_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE: - { - const auto &tbl0 = m_RootParam.DescriptorTable; - const auto &tbl1 = rhs.m_RootParam.DescriptorTable; - if (tbl0.NumDescriptorRanges != tbl1.NumDescriptorRanges) - return false; - for (UINT r = 0; r < tbl0.NumDescriptorRanges; ++r) - { - const auto &rng0 = tbl0.pDescriptorRanges[r]; - const auto &rng1 = tbl1.pDescriptorRanges[r]; - if (memcmp(&rng0, &rng1, sizeof(rng0)) != 0) - return false; - } - } - break; - - case Vk_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS: - { - const auto &cnst0 = m_RootParam.Constants; - const auto &cnst1 = rhs.m_RootParam.Constants; - if (memcmp(&cnst0, &cnst1, sizeof(cnst0)) != 0) - return false; - } - break; - - case Vk_ROOT_PARAMETER_TYPE_CBV: - case Vk_ROOT_PARAMETER_TYPE_SRV: - case Vk_ROOT_PARAMETER_TYPE_UAV: - { - const auto &dscr0 = m_RootParam.Descriptor; - const auto &dscr1 = rhs.m_RootParam.Descriptor; - if (memcmp(&dscr0, &dscr1, sizeof(dscr0)) != 0) - return false; - } - break; - - default: UNEXPECTED("Unexpected root parameter type"); - } - - return true; - } - - bool operator != (const RootParameter&rhs)const - { - return !(*this == rhs); - } - - size_t GetHash()const - { - size_t hash = ComputeHash(m_ShaderVarType, m_DescriptorTableSize, m_RootIndex); - HashCombine(hash, m_RootParam.ParameterType, m_RootParam.ShaderVisibility); - - switch (m_RootParam.ParameterType) - { - case Vk_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE: - { - const auto &tbl = m_RootParam.DescriptorTable; - HashCombine(hash, tbl.NumDescriptorRanges); - for (UINT r = 0; r < tbl.NumDescriptorRanges; ++r) - { - const auto &rng = tbl.pDescriptorRanges[r]; - HashCombine(hash, rng.BaseShaderRegister, rng.NumDescriptors, rng.OffsetInDescriptorsFromTableStart, rng.RangeType, rng.RegisterSpace); - } - } - break; - - case Vk_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS: - { - const auto &cnst = m_RootParam.Constants; - HashCombine(hash, cnst.Num32BitValues, cnst.RegisterSpace, cnst.ShaderRegister); - } - break; - - case Vk_ROOT_PARAMETER_TYPE_CBV: - case Vk_ROOT_PARAMETER_TYPE_SRV: - case Vk_ROOT_PARAMETER_TYPE_UAV: - { - const auto &dscr = m_RootParam.Descriptor; - HashCombine(hash, dscr.RegisterSpace, dscr.ShaderRegister); - } - break; - - default: UNEXPECTED("Unexpected root parameter type"); - } - - return hash; - } - -private: - - SHADER_VARIABLE_TYPE m_ShaderVarType = static_cast(-1); - Vk_ROOT_PARAMETER m_RootParam = {}; - Uint32 m_DescriptorTableSize = 0; - Uint32 m_RootIndex = static_cast(-1); - - -}; -#endif - - -/// Implementation of the Diligent::RootSignature class +/// Implementation of the Diligent::PipelineLayout class class PipelineLayout { public: + PipelineLayout(); #if 0 - RootSignature(); - void AllocateStaticSamplers(IShader* const *ppShaders, Uint32 NumShaders); void Finalize(IVkDevice *pVkDevice); - - IVkRootSignature* GetVkRootSignature()const{return m_pVkRootSignature;} +#endif + VkPipelineLayout GetVkPipelineLayout()const{return m_LayoutMgr.GetVkPipelineLayout();} +#if 0 void InitResourceCache(RenderDeviceVkImpl *pDeviceVkImpl, class ShaderResourceCacheVk& ResourceCache, IMemoryAllocator &CacheMemAllocator)const; void InitStaticSampler(SHADER_TYPE ShaderType, const String &TextureName, const D3DShaderResourceAttribs &ShaderResAttribs); #endif - //void AllocateResourceSlot(SHADER_TYPE ShaderType, const D3DShaderResourceAttribs &ShaderResAttribs, Vk_DESCRIPTOR_RANGE_TYPE RangeType, Uint32 &RootIndex, Uint32 &OffsetFromTableStart); + void AllocateResourceSlot(SHADER_TYPE ShaderType, VkDescriptorType DescriptorType, Uint32 DescriptorCount, Uint32 &DescriptorSet, Uint32 &OffsetFromSetStart); #if 0 // This method should be thread-safe as it does not modify any object state - void (RootSignature::*CommitDescriptorHandles)(RenderDeviceVkImpl *pRenderDeviceVk, + void (PipelineLayout::*CommitDescriptorHandles)(RenderDeviceVkImpl *pRenderDeviceVk, ShaderResourceCacheVk& ResourceCache, class CommandContext &Ctx, bool IsCompute)const = nullptr; - void (RootSignature::*TransitionAndCommitDescriptorHandles)(RenderDeviceVkImpl *pRenderDeviceVk, + void (PipelineLayout::*TransitionAndCommitDescriptorHandles)(RenderDeviceVkImpl *pRenderDeviceVk, ShaderResourceCacheVk& ResourceCache, class CommandContext &Ctx, bool IsCompute)const = nullptr; @@ -304,38 +74,29 @@ public: class CommandContext &Ctx, bool IsCompute, Uint32 ContextId)const; - - Uint32 GetTotalSrvCbvUavSlots(SHADER_VARIABLE_TYPE VarType)const - { - VERIFY_EXPR(VarType >= 0 && VarType < SHADER_VARIABLE_TYPE_NUM_TYPES); - return m_TotalSrvCbvUavSlots[VarType]; - } - Uint32 GetTotalSamplerSlots(SHADER_VARIABLE_TYPE VarType)const +#endif + Uint32 GetTotalDescriptors(SHADER_VARIABLE_TYPE VarType)const { VERIFY_EXPR(VarType >= 0 && VarType < SHADER_VARIABLE_TYPE_NUM_TYPES); - return m_TotalSamplerSlots[VarType]; + return m_TotalDescriptors[VarType]; } - bool IsSameAs(const RootSignature& RS)const + bool IsSameAs(const PipelineLayout& RS)const { - return m_RootParams == RS.m_RootParams; + return m_LayoutMgr == RS.m_LayoutMgr; } size_t GetHash()const { - return m_RootParams.GetHash(); + return m_LayoutMgr.GetHash(); } private: #ifdef _DEBUG - void dbgVerifyRootParameters()const; + //void dbgVerifyRootParameters()const; #endif - Uint32 m_TotalSrvCbvUavSlots[SHADER_VARIABLE_TYPE_NUM_TYPES]; - Uint32 m_TotalSamplerSlots[SHADER_VARIABLE_TYPE_NUM_TYPES]; + Uint32 m_TotalDescriptors[SHADER_VARIABLE_TYPE_NUM_TYPES] = {}; - CComPtr m_pVkRootSignature; -#endif - class DescriptorSetLayoutManager { public: @@ -361,6 +122,7 @@ private: bool operator == (const DescriptorSetLayout& rhs)const; bool operator != (const DescriptorSetLayout& rhs)const{return !(*this == rhs);} + size_t GetHash()const; private: void ReserveMemory(Uint32 NumBindings, IMemoryAllocator &MemAllocator); @@ -378,58 +140,18 @@ private: void Finalize(const VulkanUtilities::VulkanLogicalDevice &LogicalDevice); void Release(RenderDeviceVkImpl *pRenderDeviceVk); - //Uint32 GetNumRootTables()const{return m_NumRootTables;} - //Uint32 GetNumRootViews()const{return m_NumRootViews;} - // - //const RootParameter& GetRootTable(Uint32 TableInd)const - //{ - // VERIFY_EXPR(TableInd < m_NumRootTables); - // return m_pRootTables[TableInd]; - //} - - //RootParameter& GetRootTable(Uint32 TableInd) - //{ - // VERIFY_EXPR(TableInd < m_NumRootTables); - // return m_pRootTables[TableInd]; - //} - - //const RootParameter& GetRootView(Uint32 ViewInd)const - //{ - // VERIFY_EXPR(ViewInd < m_NumRootViews); - // return m_pRootViews[ViewInd]; - //} - - //RootParameter& GetRootView(Uint32 ViewInd) - //{ - // VERIFY_EXPR(ViewInd < m_NumRootViews); - // return m_pRootViews[ViewInd]; - //} - DescriptorSetLayout& GetDescriptorSet(SHADER_VARIABLE_TYPE VarType); DescriptorSetLayout& GetDescriptorSetByInd(size_t Ind){return m_DescriptorSetLayouts[Ind];} const DescriptorSetLayout& GetDescriptorSetByInd(size_t Ind)const { return m_DescriptorSetLayouts[Ind]; } size_t GetNumDescriptorSetLayouts()const { return m_DescriptorSetLayouts.size(); } - //void AddDescriptorSet(SHADER_VARIABLE_TYPE VarType); - //void AddRootTable(Uint32 RootIndex, Vk_SHADER_VISIBILITY Visibility, SHADER_VARIABLE_TYPE VarType, Uint32 NumRangesInNewTable = 1); - //void AddDescriptorRanges(Uint32 RootTableInd, Uint32 NumExtraRanges = 1); - - //template - //void ProcessDescriptorSets(TOperation)const; - bool operator == (const DescriptorSetLayoutManager& rhs)const; bool operator != (const DescriptorSetLayoutManager& rhs)const {return !(*this == rhs);} - //size_t GetHash()const; + size_t GetHash()const; + VkPipelineLayout GetVkPipelineLayout()const{return m_VkPipelineLayout;} private: - IMemoryAllocator &m_MemAllocator; - //std::unique_ptr> m_pMemory; - //Uint32 m_NumRootTables = 0; - //Uint32 m_NumRootViews = 0; - //Uint32 m_TotalDescriptorRanges = 0; - //RootParameter *m_pRootTables = nullptr; - //RootParameter *m_pRootViews = nullptr; VulkanUtilities::PipelineLayoutWrapper m_VkPipelineLayout; std::vector> m_DescriptorSetLayouts; std::vector> m_LayoutBindings; @@ -446,9 +168,11 @@ private: Uint8 m_SrvCbvUavRootTablesMap[SHADER_VARIABLE_TYPE_NUM_TYPES * 6]; // This array contains the same data for Sampler root table Uint8 m_SamplerRootTablesMap[SHADER_VARIABLE_TYPE_NUM_TYPES * 6]; +#endif - RootParamsManager m_RootParams; - + DescriptorSetLayoutManager m_LayoutMgr; + +#if 0 struct StaticSamplerAttribs { StaticSamplerDesc SamplerDesc; diff --git a/Graphics/GraphicsEngineVulkan/src/PipelineLayout.cpp b/Graphics/GraphicsEngineVulkan/src/PipelineLayout.cpp index 8c3ec83a..740305b2 100644 --- a/Graphics/GraphicsEngineVulkan/src/PipelineLayout.cpp +++ b/Graphics/GraphicsEngineVulkan/src/PipelineLayout.cpp @@ -37,6 +37,23 @@ namespace Diligent { +static VkShaderStageFlagBits ShaderTypeToVkShaderStageFlagBit(SHADER_TYPE ShaderType) +{ + switch(ShaderType) + { + case SHADER_TYPE_VERTEX: return VK_SHADER_STAGE_VERTEX_BIT; + case SHADER_TYPE_HULL: return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT; + case SHADER_TYPE_DOMAIN: return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT; + case SHADER_TYPE_GEOMETRY: return VK_SHADER_STAGE_GEOMETRY_BIT; + case SHADER_TYPE_PIXEL: return VK_SHADER_STAGE_FRAGMENT_BIT; + case SHADER_TYPE_COMPUTE: return VK_SHADER_STAGE_COMPUTE_BIT; + + default: + UNEXPECTED("Unknown shader type"); + return VK_SHADER_STAGE_VERTEX_BIT; + } +} + PipelineLayout::DescriptorSetLayoutManager::DescriptorSetLayoutManager(IMemoryAllocator &MemAllocator): m_MemAllocator(MemAllocator), m_DescriptorSetLayouts(STD_ALLOCATOR_RAW_MEM(DescriptorSetLayout, MemAllocator, "Allocator for Decriptor Set Layouts")), @@ -171,6 +188,18 @@ bool PipelineLayout::DescriptorSetLayoutManager::DescriptorSetLayout::operator = return true; } +size_t PipelineLayout::DescriptorSetLayoutManager::DescriptorSetLayout::GetHash()const +{ + size_t Hash = ComputeHash(static_cast(ShaderVarType), BindingCount); + for (uint32_t b = 0; b < BindingCount; ++b) + { + const auto &B = pBindings[b]; + HashCombine(Hash, B.binding, static_cast(B.descriptorType), B.descriptorCount, static_cast(B.stageFlags), B.pImmutableSamplers != nullptr); + } + + return Hash; +} + void PipelineLayout::DescriptorSetLayoutManager::Finalize(const VulkanUtilities::VulkanLogicalDevice &LogicalDevice) { size_t TotalBindings = 0; @@ -233,66 +262,15 @@ bool PipelineLayout::DescriptorSetLayoutManager::operator == (const DescriptorSe return m_VarTypeToDescrSetLayout == rhs.m_VarTypeToDescrSetLayout; } - -#if 0 -size_t PipelineLayout::DescriptorSetLayoutManager::GetRequiredMemorySize(Uint32 NumExtraRootTables, Uint32 NumExtraRootViews, Uint32 NumExtraDescriptorRanges)const -{ - return sizeof(RootParameter) * (m_NumRootTables + NumExtraRootTables + m_NumRootViews + NumExtraRootViews) + sizeof(Vk_DESCRIPTOR_RANGE) * (m_TotalDescriptorRanges + NumExtraDescriptorRanges); -} - -Vk_DESCRIPTOR_RANGE* PipelineLayout::DescriptorSetLayoutManager::Extend(Uint32 NumExtraRootTables, Uint32 NumExtraRootViews, Uint32 NumExtraDescriptorRanges, Uint32 RootTableToAddRanges) +size_t PipelineLayout::DescriptorSetLayoutManager::GetHash()const { - VERIFY(NumExtraRootTables > 0 || NumExtraRootViews > 0 || NumExtraDescriptorRanges > 0, "At least one root table, root view or descriptor range must be added" ); - auto MemorySize = GetRequiredMemorySize(NumExtraRootTables, NumExtraRootViews, NumExtraDescriptorRanges); - VERIFY_EXPR(MemorySize > 0); - auto *pNewMemory = ALLOCATE(m_MemAllocator, "Memory buffer for root tables, root views & descriptor ranges", MemorySize); - memset(pNewMemory, 0, MemorySize); - - // Note: this order is more efficient than views->tables->ranges - auto *pNewRootTables = reinterpret_cast(pNewMemory); - auto *pNewRootViews = pNewRootTables + (m_NumRootTables + NumExtraRootTables); - auto *pCurrDescriptorRangePtr = reinterpret_cast(pNewRootViews+m_NumRootViews+NumExtraRootViews); - - // Copy existing root tables to new memory - for (Uint32 rt = 0; rt < m_NumRootTables; ++rt) - { - const auto &SrcTbl = GetRootTable(rt); - auto &VkSrcTbl = static_cast(SrcTbl).DescriptorTable; - auto NumRanges = VkSrcTbl.NumDescriptorRanges; - if(rt == RootTableToAddRanges) - { - VERIFY(NumExtraRootTables == 0 || NumExtraRootTables == 1, "Up to one descriptor table can be extended at a time"); - NumRanges += NumExtraDescriptorRanges; - } - new(pNewRootTables + rt) RootParameter(SrcTbl, NumRanges, pCurrDescriptorRangePtr); - pCurrDescriptorRangePtr += NumRanges; - } - - // Copy existing root views to new memory - for (Uint32 rv = 0; rv < m_NumRootViews; ++rv) - { - const auto &SrcView = GetRootView(rv); - new(pNewRootViews + rv) RootParameter(SrcView); - } - - m_pMemory.reset(pNewMemory); - m_NumRootTables += NumExtraRootTables; - m_NumRootViews += NumExtraRootViews; - m_TotalDescriptorRanges += NumExtraDescriptorRanges; - m_pRootTables = m_NumRootTables != 0 ? pNewRootTables : nullptr; - m_pRootViews = m_NumRootViews != 0 ? pNewRootViews : nullptr; + size_t Hash = ComputeHash(m_DescriptorSetLayouts.size()); + for(const auto &SetLayout : m_DescriptorSetLayouts) + HashCombine(Hash, SetLayout.GetHash()); - return pCurrDescriptorRangePtr; + return Hash; } -void PipelineLayout::DescriptorSetLayoutManager::AddRootView(Vk_ROOT_PARAMETER_TYPE ParameterType, Uint32 RootIndex, UINT Register, Vk_SHADER_VISIBILITY Visibility, SHADER_VARIABLE_TYPE VarType) -{ - auto *pRangePtr = Extend(0, 1, 0); - VERIFY_EXPR((char*)pRangePtr == (char*)m_pMemory.get() + GetRequiredMemorySize(0, 0, 0)); - new(m_pRootViews + m_NumRootViews-1) RootParameter(ParameterType, RootIndex, Register, 0u, Visibility, VarType); -} -#endif - PipelineLayout::DescriptorSetLayoutManager::DescriptorSetLayout& PipelineLayout::DescriptorSetLayoutManager::GetDescriptorSet(SHADER_VARIABLE_TYPE VarType) { auto DescrSetLayoutInd = m_VarTypeToDescrSetLayout[VarType]; @@ -306,75 +284,21 @@ PipelineLayout::DescriptorSetLayoutManager::DescriptorSetLayout& PipelineLayout: return m_DescriptorSetLayouts[DescrSetLayoutInd]; } -#if 0 -void PipelineLayout::DescriptorSetLayoutManager::AddDescriptorSet(SHADER_VARIABLE_TYPE VarType) -{ - VERIFY(m_VkPipelineLayout == VK_NULL_HANDLE, "Pipeline layout must not be finalized to add descriptor set"); - m_DescriptorSetLayouts.emplace_back(VarType); -} - - -void PipelineLayout::DescriptorSetLayoutManager::AddDescriptorRanges(Uint32 RootTableInd, Uint32 NumExtraRanges) -{ - auto *pRangePtr = Extend(0, 0, NumExtraRanges, RootTableInd); - VERIFY_EXPR( (char*)pRangePtr == (char*)m_pMemory.get() + GetRequiredMemorySize(0, 0, 0)); -} - -bool PipelineLayout::DescriptorSetLayoutManager::operator == (const DescriptorSetLayoutManager& RootParams)const -{ - if (m_NumRootTables != RootParams.m_NumRootTables || - m_NumRootViews != RootParams.m_NumRootViews) - return false; - - for (Uint32 rv = 0; rv < m_NumRootViews; ++rv) - { - const auto &RV0 = GetRootView(rv); - const auto &RV1 = RootParams.GetRootView(rv); - if (RV0 != RV1) - return false; - } - - for (Uint32 rv = 0; rv < m_NumRootTables; ++rv) - { - const auto &RT0 = GetRootTable(rv); - const auto &RT1 = RootParams.GetRootTable(rv); - if (RT0 != RT1) - return false; - } - - return true; -} - -size_t PipelineLayout::DescriptorSetLayoutManager::GetHash()const -{ - size_t hash = ComputeHash(m_NumRootTables, m_NumRootViews); - for (Uint32 rv = 0; rv < m_NumRootViews; ++rv) - HashCombine(hash, GetRootView(rv).GetHash()); - - for (Uint32 rv = 0; rv < m_NumRootTables; ++rv) - HashCombine(hash, GetRootTable(rv).GetHash()); - - return hash; -} - PipelineLayout::PipelineLayout() : - m_RootParams(GetRawAllocator()), + m_LayoutMgr(GetRawAllocator())/*, m_MemAllocator(GetRawAllocator()), m_StaticSamplers( STD_ALLOCATOR_RAW_MEM(StaticSamplerAttribs, GetRawAllocator(), "Allocator for vector") ) + */ { - for(size_t s=0; s < SHADER_VARIABLE_TYPE_NUM_TYPES; ++s) - { - m_TotalSrvCbvUavSlots[s] = 0; - m_TotalSamplerSlots[s] = 0; - } - +#if 0 for(size_t i=0; i < _countof(m_SrvCbvUavRootTablesMap); ++i) m_SrvCbvUavRootTablesMap[i] = InvalidRootTableIndex; for(size_t i=0; i < _countof(m_SamplerRootTablesMap); ++i) m_SamplerRootTablesMap[i] = InvalidRootTableIndex; - +#endif } +#if 0 static Vk_SHADER_VISIBILITY ShaderTypeInd2ShaderVisibilityMap[] { Vk_SHADER_VISIBILITY_VERTEX, // 0 @@ -480,15 +404,17 @@ void PipelineLayout::InitStaticSampler(SHADER_TYPE ShaderType, const String &Tex LOG_ERROR("Failed to find static sampler for variable \"", TextureName, '\"'); } } +#endif + -// http://diligentgraphics.com/diligent-engine/architecture/Vk/shader-resource-layout#Initializing-Shader-Resource-Layouts-and-Root-Signature-in-a-Pipeline-State-Object void PipelineLayout::AllocateResourceSlot(SHADER_TYPE ShaderType, - const D3DShaderResourceAttribs &ShaderResAttribs, - Vk_DESCRIPTOR_RANGE_TYPE RangeType, - Uint32 &RootIndex, // Output parameter - Uint32 &OffsetFromTableStart // Output parameter + VkDescriptorType DescriptorType, + Uint32 DescriptorCount, + Uint32 &DescriptorSet, // Output parameter + Uint32 &OffsetFromSetStart // Output parameter ) { +#if 0 auto ShaderInd = GetShaderTypeIndex(ShaderType); auto ShaderVisibility = GetShaderVisibility(ShaderType); if (RangeType == Vk_DESCRIPTOR_RANGE_TYPE_CBV && ShaderResAttribs.BindCount == 1) @@ -548,9 +474,10 @@ void PipelineLayout::AllocateResourceSlot(SHADER_TYPE ShaderType, OffsetFromTableStart // Offset in descriptors from the table start ); } +#endif } - +#if 0 #ifdef _DEBUG void PipelineLayout::dbgVerifyRootParameters()const { @@ -625,9 +552,10 @@ void PipelineLayout::AllocateStaticSamplers(IShader* const*ppShaders, Uint32 Num VERIFY_EXPR(m_StaticSamplers.size() == TotalSamplers); } } - +#endif void PipelineLayout::Finalize(IVkDevice *pVkDevice) { +#if 0 for(Uint32 rt = 0; rt < m_RootParams.GetNumRootTables(); ++rt) { auto &RootTbl = m_RootParams.GetRootTable(rt); @@ -730,8 +658,10 @@ void PipelineLayout::Finalize(IVkDevice *pVkDevice) CommitDescriptorHandles = &PipelineLayout::CommitDescriptorHandlesInternal_SM; TransitionAndCommitDescriptorHandles = &PipelineLayout::CommitDescriptorHandlesInternal_SM; } +#endif } +#if 0 //http://diligentgraphics.com/diligent-engine/architecture/Vk/shader-resource-cache#Initializing-the-Cache-for-Shader-Resource-Binding-Object void PipelineLayout::InitResourceCache(RenderDeviceVkImpl *pDeviceVkImpl, ShaderResourceCacheVk& ResourceCache, IMemoryAllocator &CacheMemAllocator)const { diff --git a/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp b/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp index 4b6dd174..4fc3a695 100644 --- a/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp +++ b/Graphics/GraphicsEngineVulkan/src/ShaderResourceLayoutVk.cpp @@ -31,7 +31,7 @@ #include "TextureViewVkImpl.h" #include "SamplerVkImpl.h" #include "ShaderVkImpl.h" -#include "RootSignature.h" +#include "PipelineLayout.h" #include "PipelineStateVkImpl.h" namespace Diligent -- cgit v1.2.3