diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2019-03-07 02:58:56 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2019-03-07 02:58:56 +0000 |
| commit | bc9fc76496034a50faf3d7fe7be868d5d62f6663 (patch) | |
| tree | e9438fe09533409c57c9c63b2f69a895e2cf8615 /Graphics/GLSLTools | |
| parent | Merge pull request #67 from StarshipVendingMachine/android-vulkan (diff) | |
| parent | Minor comment updates (diff) | |
| download | DiligentCore-bc9fc76496034a50faf3d7fe7be868d5d62f6663.tar.gz DiligentCore-bc9fc76496034a50faf3d7fe7be868d5d62f6663.zip | |
Merged var_type_refactor into master
Diffstat (limited to 'Graphics/GLSLTools')
| -rw-r--r-- | Graphics/GLSLTools/include/GLSLSourceBuilder.h | 2 | ||||
| -rw-r--r-- | Graphics/GLSLTools/include/SPIRVShaderResources.h | 187 | ||||
| -rw-r--r-- | Graphics/GLSLTools/include/SPIRVUtils.h | 2 | ||||
| -rw-r--r-- | Graphics/GLSLTools/src/GLSLSourceBuilder.cpp | 2 | ||||
| -rw-r--r-- | Graphics/GLSLTools/src/SPIRVShaderResources.cpp | 279 | ||||
| -rw-r--r-- | Graphics/GLSLTools/src/SPIRVUtils.cpp | 2 |
6 files changed, 106 insertions, 368 deletions
diff --git a/Graphics/GLSLTools/include/GLSLSourceBuilder.h b/Graphics/GLSLTools/include/GLSLSourceBuilder.h index bfd6e55c..958f0faf 100644 --- a/Graphics/GLSLTools/include/GLSLSourceBuilder.h +++ b/Graphics/GLSLTools/include/GLSLSourceBuilder.h @@ -36,7 +36,7 @@ enum TargetGLSLCompiler driver }; -String BuildGLSLSourceString(const ShaderCreationAttribs& CreationAttribs, +String BuildGLSLSourceString(const ShaderCreateInfo& CreationAttribs, const DeviceCaps& deviceCaps, TargetGLSLCompiler TargetCompiler, const char* ExtraDefinitions = nullptr); diff --git a/Graphics/GLSLTools/include/SPIRVShaderResources.h b/Graphics/GLSLTools/include/SPIRVShaderResources.h index f4c9edfe..b117c05a 100644 --- a/Graphics/GLSLTools/include/SPIRVShaderResources.h +++ b/Graphics/GLSLTools/include/SPIRVShaderResources.h @@ -28,19 +28,17 @@ // SPIRVShaderResources class uses continuous chunk of memory to store all resources, as follows: // -// m_MemoryBuffer m_TotalResources -// | | -// | Uniform Buffers | Storage Buffers | Storage Images | Sampled Images | Atomic Counters | Separate Samplers | Separate Images | Immutable Samplers | Stage Inputs | Resource Names | +// m_MemoryBuffer m_TotalResources end of names data may not be aligned +// | | | +// | Uniform Buffers | Storage Buffers | Storage Images | Sampled Images | Atomic Counters | Separate Samplers | Separate Images | Stage Inputs | Resource Names | #include <memory> #include <vector> #include <sstream> #include "Shader.h" -#include "Sampler.h" #include "RenderDevice.h" #include "STDAllocator.h" -#include "HashUtils.h" #include "RefCntAutoPtr.h" #include "StringPool.h" @@ -53,22 +51,6 @@ struct Resource; namespace Diligent { -inline bool IsAllowedType(SHADER_VARIABLE_TYPE VarType, Uint32 AllowedTypeBits)noexcept -{ - return ((1 << VarType) & AllowedTypeBits) != 0; -} - -inline Uint32 GetAllowedTypeBits(const SHADER_VARIABLE_TYPE* AllowedVarTypes, Uint32 NumAllowedTypes)noexcept -{ - if(AllowedVarTypes == nullptr) - return 0xFFFFFFFF; - - Uint32 AllowedTypeBits = 0; - for(Uint32 i=0; i < NumAllowedTypes; ++i) - AllowedTypeBits |= 1 << AllowedVarTypes[i]; - return AllowedTypeBits; -} - // sizeof(SPIRVShaderResourceAttribs) == 24, msvc x64 struct SPIRVShaderResourceAttribs { @@ -86,37 +68,27 @@ struct SPIRVShaderResourceAttribs NumResourceTypes }; - static constexpr const Uint32 ResourceTypeBits = 4; - static constexpr const Uint32 VarTypeBits = 4; - static_assert(SHADER_VARIABLE_TYPE_NUM_TYPES < (1 << VarTypeBits), "Not enough bits to represent SHADER_VARIABLE_TYPE"); - static_assert(ResourceType::NumResourceTypes < (1 << ResourceTypeBits), "Not enough bits to represent ResourceType"); + static constexpr const Uint32 InvalidSepSmplrOrImgInd = static_cast<Uint32>(-1); - static constexpr const Uint32 InvalidSepSmplrOrImgInd = static_cast<Uint32>(-1); - -/* 0 */const char* const Name; -/* 8 */const Uint16 ArraySize; -/*10.0*/const ResourceType Type : ResourceTypeBits; -/*10.4*/const SHADER_VARIABLE_TYPE VarType : VarTypeBits; +/* 0 */const char* const Name; +/* 8 */const Uint16 ArraySize; +/* 10 */const ResourceType Type; +/* 11 */ // unused private: - static constexpr const Uint8 InvalidImmutableSamplerInd = static_cast<Uint8>(-1); -/*11*/const Uint8 ImmutableSamplerInd; - - // Defines mapping between separate samplers and seperate images when HLSL-style + // Defines the mapping between separate samplers and seperate images when HLSL-style // combined texture samplers are in use (i.e. texture2D g_Tex + sampler g_Tex_sampler). -/*12*/ Uint32 SepSmplrOrImgInd = InvalidSepSmplrOrImgInd; +/* 12 */ Uint32 SepSmplrOrImgInd = InvalidSepSmplrOrImgInd; public: // Offset in SPIRV words (uint32_t) of binding & descriptor set decorations in SPIRV binary -/*16*/const uint32_t BindingDecorationOffset; -/*20*/const uint32_t DescriptorSetDecorationOffset; +/* 16 */const uint32_t BindingDecorationOffset; +/* 20 */const uint32_t DescriptorSetDecorationOffset; +/* 24 */ // End of structure - - SPIRVShaderResourceAttribs(const spirv_cross::Compiler& Compiler, - const spirv_cross::Resource& Res, - const char* _Name, - ResourceType _Type, - SHADER_VARIABLE_TYPE _VarType, - Int32 _ImmutableSamplerInd = -1, - Uint32 _SamplerOrSepImgInd = InvalidSepSmplrOrImgInd)noexcept; + SPIRVShaderResourceAttribs(const spirv_cross::Compiler& Compiler, + const spirv_cross::Resource& Res, + const char* _Name, + ResourceType _Type, + Uint32 _SamplerOrSepImgInd = InvalidSepSmplrOrImgInd) noexcept; bool IsValidSepSamplerAssigned() const { @@ -154,17 +126,6 @@ public: SepSmplrOrImgInd = SepImageInd; } - bool IsImmutableSamplerAssigned() const - { - return ImmutableSamplerInd != InvalidImmutableSamplerInd; - } - - Uint32 GetImmutableSamplerInd()const - { - VERIFY(Type == ResourceType::SampledImage || Type == ResourceType::SeparateSampler, "Only sampled images and separate samplers can be assigned immutable samplers"); - return ImmutableSamplerInd; - } - String GetPrintName(Uint32 ArrayInd)const { VERIFY_EXPR(ArrayInd < ArraySize); @@ -182,14 +143,12 @@ public: { return ArraySize == Attribs.ArraySize && Type == Attribs.Type && - VarType == Attribs.VarType && - SepSmplrOrImgInd == Attribs.SepSmplrOrImgInd && - ( (IsImmutableSamplerAssigned() && Attribs.IsImmutableSamplerAssigned()) || - (!IsImmutableSamplerAssigned() && !Attribs.IsImmutableSamplerAssigned()) ); + SepSmplrOrImgInd == Attribs.SepSmplrOrImgInd; } }; static_assert(sizeof(SPIRVShaderResourceAttribs) % sizeof(void*) == 0, "Size of SPIRVShaderResourceAttribs struct must be multiple of sizeof(void*)" ); +// sizeof(SPIRVShaderResourceAttribs) == 16, msvc x64 struct SPIRVShaderStageInputAttribs { SPIRVShaderStageInputAttribs(const char* _Semantic, uint32_t _LocationDecorationOffset) : @@ -221,8 +180,6 @@ public: ~SPIRVShaderResources(); - using SamplerPtrType = RefCntAutoPtr<ISampler>; - Uint32 GetNumUBs ()const noexcept{ return (m_StorageBufferOffset - 0); } Uint32 GetNumSBs ()const noexcept{ return (m_StorageImageOffset - m_StorageBufferOffset); } Uint32 GetNumImgs ()const noexcept{ return (m_SampledImageOffset - m_StorageImageOffset); } @@ -231,7 +188,6 @@ public: Uint32 GetNumSepSmplrs()const noexcept{ return (m_SeparateImageOffset - m_SeparateSamplerOffset);} Uint32 GetNumSepImgs ()const noexcept{ return (m_TotalResources - m_SeparateImageOffset); } Uint32 GetTotalResources() const noexcept { return m_TotalResources; } - Uint32 GetNumImmutableSamplers()const noexcept { return m_NumImmutableSamplers; } Uint32 GetNumShaderStageInputs()const noexcept { return m_NumShaderStageInputs; } const SPIRVShaderResourceAttribs& GetUB (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumUBs(), 0 ); } @@ -243,23 +199,11 @@ public: const SPIRVShaderResourceAttribs& GetSepImg (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumSepImgs(), m_SeparateImageOffset ); } const SPIRVShaderResourceAttribs& GetResource(Uint32 n)const noexcept{ return GetResAttribs(n, GetTotalResources(), 0 ); } - ISampler* GetImmutableSampler(const SPIRVShaderResourceAttribs& ResAttribs)const noexcept - { - if (!ResAttribs.IsImmutableSamplerAssigned()) - return nullptr; - - auto ImmutableSamplerInd = ResAttribs.GetImmutableSamplerInd(); - VERIFY(ImmutableSamplerInd < m_NumImmutableSamplers, "Static sampler index (", ImmutableSamplerInd, ") is out of range. Array size: ", m_NumImmutableSamplers); - auto* ResourceMemoryEnd = reinterpret_cast<SPIRVShaderResourceAttribs*>(m_MemoryBuffer.get()) + m_TotalResources; - return reinterpret_cast<SamplerPtrType*>(ResourceMemoryEnd)[ImmutableSamplerInd]; - } - const SPIRVShaderStageInputAttribs& GetShaderStageInputAttribs(Uint32 n)const noexcept { VERIFY(n < m_NumShaderStageInputs, "Shader stage input index (", n, ") is out of range. Total input count: ", m_NumShaderStageInputs); auto* ResourceMemoryEnd = reinterpret_cast<const SPIRVShaderResourceAttribs*>(m_MemoryBuffer.get()) + m_TotalResources; - auto* ImmutableSamplerMemoryEnd = reinterpret_cast<const SamplerPtrType*>(ResourceMemoryEnd) + m_NumImmutableSamplers; - return reinterpret_cast<const SPIRVShaderStageInputAttribs*>(ImmutableSamplerMemoryEnd)[n]; + return reinterpret_cast<const SPIRVShaderStageInputAttribs*>(ResourceMemoryEnd)[n]; } struct ResourceCounters @@ -272,8 +216,6 @@ public: Uint32 NumSepSmplrs = 0; Uint32 NumSepImgs = 0; }; - ResourceCounters CountResources(const SHADER_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes)const noexcept; SHADER_TYPE GetShaderType()const noexcept{return m_ShaderType;} @@ -285,80 +227,64 @@ public: typename THandleAC, typename THandleSepSmpl, typename THandleSepImg> - void ProcessResources(const SHADER_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes, - THandleUB HandleUB, - THandleSB HandleSB, - THandleImg HandleImg, - THandleSmplImg HandleSmplImg, - THandleAC HandleAC, - THandleSepSmpl HandleSepSmpl, - THandleSepImg HandleSepImg)const + void ProcessResources(THandleUB HandleUB, + THandleSB HandleSB, + THandleImg HandleImg, + THandleSmplImg HandleSmplImg, + THandleAC HandleAC, + THandleSepSmpl HandleSepSmpl, + THandleSepImg HandleSepImg)const { - Uint32 AllowedTypeBits = GetAllowedTypeBits(AllowedVarTypes, NumAllowedTypes); - for(Uint32 n=0; n < GetNumUBs(); ++n) { const auto& UB = GetUB(n); - if( IsAllowedType(UB.VarType, AllowedTypeBits) ) - HandleUB(UB, n); + HandleUB(UB, n); } for (Uint32 n = 0; n < GetNumSBs(); ++n) { const auto& SB = GetSB(n); - if (IsAllowedType(SB.VarType, AllowedTypeBits)) - HandleSB(SB, n); + HandleSB(SB, n); } for (Uint32 n = 0; n < GetNumImgs(); ++n) { const auto& Img = GetImg(n); - if (IsAllowedType(Img.VarType, AllowedTypeBits)) - HandleImg(Img, n); + HandleImg(Img, n); } for (Uint32 n = 0; n < GetNumSmpldImgs(); ++n) { const auto& SmplImg = GetSmpldImg(n); - if (IsAllowedType(SmplImg.VarType, AllowedTypeBits)) - HandleSmplImg(SmplImg, n); + HandleSmplImg(SmplImg, n); } for (Uint32 n = 0; n < GetNumACs(); ++n) { const auto& AC = GetAC(n); - if (IsAllowedType(AC.VarType, AllowedTypeBits)) - HandleAC(AC, n); + HandleAC(AC, n); } for (Uint32 n = 0; n < GetNumSepSmplrs(); ++n) { const auto& SepSmpl = GetSepSmplr(n); - if (IsAllowedType(SepSmpl.VarType, AllowedTypeBits)) - HandleSepSmpl(SepSmpl, n); + HandleSepSmpl(SepSmpl, n); } for (Uint32 n = 0; n < GetNumSepImgs(); ++n) { const auto& SepImg = GetSepImg(n); - if (IsAllowedType(SepImg.VarType, AllowedTypeBits)) - HandleSepImg(SepImg, n); + HandleSepImg(SepImg, n); } } template<typename THandler> - void ProcessResources(const SHADER_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes, - THandler Handler)const + void ProcessResources(THandler Handler)const { - Uint32 AllowedTypeBits = GetAllowedTypeBits(AllowedVarTypes, NumAllowedTypes); - for(Uint32 n=0; n < GetTotalResources(); ++n) { const auto& Res = GetResource(n); - if( IsAllowedType(Res.VarType, AllowedTypeBits) ) - Handler(Res, n); + Handler(Res, n); } } @@ -366,16 +292,14 @@ public: bool IsCompatibleWith(const SPIRVShaderResources& Resources)const; - //size_t GetHash()const; - const char* GetCombinedSamplerSuffix() const { return m_CombinedSamplerSuffix; } + const char* GetShaderName() const { return m_ShaderName; } bool IsUsingCombinedSamplers() const { return m_CombinedSamplerSuffix != nullptr; } private: void Initialize(IMemoryAllocator& Allocator, const ResourceCounters& Counters, - Uint32 NumImmutableSamplers, Uint32 NumShaderStageInputs, size_t ResourceNamesPoolSize); @@ -402,24 +326,20 @@ private: SPIRVShaderResourceAttribs& GetSepImg (Uint32 n)noexcept{ return GetResAttribs(n, GetNumSepImgs(), m_SeparateImageOffset ); } SPIRVShaderResourceAttribs& GetResource(Uint32 n)noexcept{ return GetResAttribs(n, GetTotalResources(), 0 ); } - SamplerPtrType& GetImmutableSampler(Uint32 n)noexcept - { - VERIFY(n < m_NumImmutableSamplers, "Immutable sampler index (", n, ") is out of range. Total immutable sampler count: ", m_NumImmutableSamplers); - auto* ResourceMemoryEnd = reinterpret_cast<SPIRVShaderResourceAttribs*>(m_MemoryBuffer.get()) + m_TotalResources; - return reinterpret_cast<SamplerPtrType*>(ResourceMemoryEnd)[n]; - } - SPIRVShaderStageInputAttribs& GetShaderStageInputAttribs(Uint32 n)noexcept { return const_cast<SPIRVShaderStageInputAttribs&>(const_cast<const SPIRVShaderResources*>(this)->GetShaderStageInputAttribs(n)); } // Memory buffer that holds all resources as continuous chunk of memory: - // | UBs | SBs | StrgImgs | SmplImgs | ACs | SepSamplers | SepImgs | Immutable Samplers | Stage Inputs | Resource Names | + // | UBs | SBs | StrgImgs | SmplImgs | ACs | SepSamplers | SepImgs | Stage Inputs | Resource Names | + // | + // end of names data may not be aligned std::unique_ptr< void, STDDeleterRawMem<void> > m_MemoryBuffer; StringPool m_ResourceNames; const char* m_CombinedSamplerSuffix = nullptr; + const char* m_ShaderName = nullptr; using OffsetType = Uint16; OffsetType m_StorageBufferOffset = 0; @@ -429,7 +349,6 @@ private: OffsetType m_SeparateSamplerOffset = 0; OffsetType m_SeparateImageOffset = 0; OffsetType m_TotalResources = 0; - OffsetType m_NumImmutableSamplers = 0; OffsetType m_NumShaderStageInputs = 0; SHADER_TYPE m_ShaderType = SHADER_TYPE_UNKNOWN; @@ -437,25 +356,3 @@ private: } -namespace std -{ -#if 0 - template<> - struct hash<Diligent::D3DShaderResourceAttribs> - { - size_t operator()(const Diligent::D3DShaderResourceAttribs &Attribs) const - { - return Attribs.GetHash(); - } - }; - - template<> - struct hash<Diligent::ShaderResources> - { - size_t operator()(const Diligent::ShaderResources &Res) const - { - return Res.GetHash(); - } - }; -#endif -} diff --git a/Graphics/GLSLTools/include/SPIRVUtils.h b/Graphics/GLSLTools/include/SPIRVUtils.h index 75fa8108..3859a587 100644 --- a/Graphics/GLSLTools/include/SPIRVUtils.h +++ b/Graphics/GLSLTools/include/SPIRVUtils.h @@ -33,6 +33,6 @@ namespace Diligent void InitializeGlslang(); void FinalizeGlslang(); std::vector<unsigned int> GLSLtoSPIRV(SHADER_TYPE ShaderType, const char* ShaderSource, int SourceCodeLen, IDataBlob** ppCompilerOutput); -std::vector<unsigned int> HLSLtoSPIRV(const ShaderCreationAttribs& Attribs, IDataBlob** ppCompilerOutput); +std::vector<unsigned int> HLSLtoSPIRV(const ShaderCreateInfo& Attribs, IDataBlob** ppCompilerOutput); }
\ No newline at end of file diff --git a/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp b/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp index 905ecfa1..2da42437 100644 --- a/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp +++ b/Graphics/GLSLTools/src/GLSLSourceBuilder.cpp @@ -33,7 +33,7 @@ namespace Diligent { -String BuildGLSLSourceString(const ShaderCreationAttribs& CreationAttribs, +String BuildGLSLSourceString(const ShaderCreateInfo& CreationAttribs, const DeviceCaps& deviceCaps, TargetGLSLCompiler TargetCompiler, const char* ExtraDefinitions) diff --git a/Graphics/GLSLTools/src/SPIRVShaderResources.cpp b/Graphics/GLSLTools/src/SPIRVShaderResources.cpp index 6b896c8d..82743cfd 100644 --- a/Graphics/GLSLTools/src/SPIRVShaderResources.cpp +++ b/Graphics/GLSLTools/src/SPIRVShaderResources.cpp @@ -28,6 +28,7 @@ #include "ShaderBase.h" #include "GraphicsAccessories.h" #include "StringTools.h" +#include "Align.h" namespace Diligent { @@ -50,9 +51,9 @@ Type GetResourceArraySize(const spirv_cross::Compiler& Compiler, static uint32_t GetDecorationOffset(const spirv_cross::Compiler& Compiler, const spirv_cross::Resource& Res, - spv::Decoration Decoration) + spv::Decoration Decoration) { - VERIFY(Compiler.has_decoration(Res.id, Decoration), "Res \'", Res.name, "\' has no requested decoration"); + VERIFY(Compiler.has_decoration(Res.id, Decoration), "Resource \'", Res.name, "\' has no requested decoration"); uint32_t offset = 0; auto declared = Compiler.get_binary_offset_for_decoration(Res.id, Decoration, offset); VERIFY(declared, "Requested decoration is not declared"); (void)declared; @@ -63,34 +64,19 @@ SPIRVShaderResourceAttribs::SPIRVShaderResourceAttribs(const spirv_cross::Compil const spirv_cross::Resource& Res, const char* _Name, ResourceType _Type, - SHADER_VARIABLE_TYPE _VarType, - Int32 _ImmutableSamplerInd, Uint32 _SepSmplrOrImgInd)noexcept : Name (_Name), ArraySize (GetResourceArraySize<decltype(ArraySize)>(Compiler, Res)), Type (_Type), - VarType (_VarType), - ImmutableSamplerInd (_ImmutableSamplerInd >= 0 ? static_cast<decltype(ImmutableSamplerInd)>(_ImmutableSamplerInd) : InvalidImmutableSamplerInd), SepSmplrOrImgInd (_SepSmplrOrImgInd), BindingDecorationOffset (GetDecorationOffset(Compiler, Res, spv::Decoration::DecorationBinding)), DescriptorSetDecorationOffset(GetDecorationOffset(Compiler, Res, spv::Decoration::DecorationDescriptorSet)) { - VERIFY(_ImmutableSamplerInd < 0 || _ImmutableSamplerInd <= std::numeric_limits<decltype(ImmutableSamplerInd)>::max(), "Static sampler index is out of representable range" ); - VERIFY(_SepSmplrOrImgInd == SPIRVShaderResourceAttribs::InvalidSepSmplrOrImgInd || _Type == ResourceType::SeparateSampler || - _Type == ResourceType::SeparateImage, "Only separate images or separate samplers can be assinged valid SepSmplrOrImgInd value"); + VERIFY(_SepSmplrOrImgInd == SPIRVShaderResourceAttribs::InvalidSepSmplrOrImgInd || + (_Type == ResourceType::SeparateSampler || _Type == ResourceType::SeparateImage), + "Only separate images or separate samplers can be assinged valid SepSmplrOrImgInd value"); } -static Int32 FindImmutableSampler(const ShaderDesc& shaderDesc, const std::string& SamplerName, const char* SamplerSuffix) -{ - for (Uint32 s=0; s < shaderDesc.NumStaticSamplers; ++s) - { - const auto& StSam = shaderDesc.StaticSamplers[s]; - if (StreqSuff(SamplerName.c_str(), StSam.SamplerOrTextureName, SamplerSuffix)) - return s; - } - - return -1; -} static spv::ExecutionModel ShaderTypeToExecutionModel(SHADER_TYPE ShaderType) { @@ -186,9 +172,9 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator, spirv_cross::ShaderResources resources = Compiler.get_shader_resources(); size_t ResourceNamesPoolSize = 0; - for(const auto &ub : resources.uniform_buffers) + for (const auto& ub : resources.uniform_buffers) ResourceNamesPoolSize += GetUBName(Compiler, ub, ParsedIRSource).length() + 1; - for(auto *pResType : + for (auto* pResType : { &resources.storage_buffers, &resources.storage_images, @@ -198,7 +184,7 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator, &resources.separate_samplers }) { - for(const auto &res : *pResType) + for(const auto& res : *pResType) ResourceNamesPoolSize += res.name.length() + 1; } @@ -207,6 +193,9 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator, ResourceNamesPoolSize += strlen(CombinedSamplerSuffix) + 1; } + VERIFY_EXPR(shaderDesc.Name != nullptr); + ResourceNamesPoolSize += strlen(shaderDesc.Name) + 1; + Uint32 NumShaderStageInputs = 0; if (resources.stage_inputs.empty()) @@ -256,115 +245,98 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator, ResCounters.NumACs = static_cast<Uint32>(resources.atomic_counters.size()); ResCounters.NumSepSmplrs = static_cast<Uint32>(resources.separate_samplers.size()); ResCounters.NumSepImgs = static_cast<Uint32>(resources.separate_images.size()); - Initialize(Allocator, ResCounters, shaderDesc.NumStaticSamplers, NumShaderStageInputs, ResourceNamesPoolSize); + Initialize(Allocator, ResCounters, NumShaderStageInputs, ResourceNamesPoolSize); { Uint32 CurrUB = 0; - for (const auto &UB : resources.uniform_buffers) + for (const auto& UB : resources.uniform_buffers) { const auto& name = GetUBName(Compiler, UB, ParsedIRSource); - new (&GetUB(CurrUB++)) + new (&GetUB(CurrUB++)) SPIRVShaderResourceAttribs(Compiler, UB, m_ResourceNames.CopyString(name), - SPIRVShaderResourceAttribs::ResourceType::UniformBuffer, - GetShaderVariableType(name, shaderDesc)); + SPIRVShaderResourceAttribs::ResourceType::UniformBuffer); } VERIFY_EXPR(CurrUB == GetNumUBs()); } { Uint32 CurrSB = 0; - for (const auto &SB : resources.storage_buffers) + for (const auto& SB : resources.storage_buffers) { new (&GetSB(CurrSB++)) SPIRVShaderResourceAttribs(Compiler, SB, m_ResourceNames.CopyString(SB.name), - SPIRVShaderResourceAttribs::ResourceType::StorageBuffer, - GetShaderVariableType(SB.name, shaderDesc)); + SPIRVShaderResourceAttribs::ResourceType::StorageBuffer); } VERIFY_EXPR(CurrSB == GetNumSBs()); } { Uint32 CurrSmplImg = 0; - for (const auto &SmplImg : resources.sampled_images) + for (const auto& SmplImg : resources.sampled_images) { - auto ImmutableSamplerInd = FindImmutableSampler(shaderDesc, SmplImg.name, nullptr); const auto& type = Compiler.get_type(SmplImg.type_id); auto ResType = type.image.dim == spv::DimBuffer ? SPIRVShaderResourceAttribs::ResourceType::UniformTexelBuffer : SPIRVShaderResourceAttribs::ResourceType::SampledImage; - new (&GetSmpldImg(CurrSmplImg++)) + new (&GetSmpldImg(CurrSmplImg++)) SPIRVShaderResourceAttribs(Compiler, SmplImg, m_ResourceNames.CopyString(SmplImg.name), - ResType, - GetShaderVariableType(SmplImg.name, shaderDesc), - ImmutableSamplerInd); + ResType); } VERIFY_EXPR(CurrSmplImg == GetNumSmpldImgs()); } { Uint32 CurrImg = 0; - for (const auto &Img : resources.storage_images) + for (const auto& Img : resources.storage_images) { const auto& type = Compiler.get_type(Img.type_id); auto ResType = type.image.dim == spv::DimBuffer ? SPIRVShaderResourceAttribs::ResourceType::StorageTexelBuffer : SPIRVShaderResourceAttribs::ResourceType::StorageImage; - new (&GetImg(CurrImg++)) + new (&GetImg(CurrImg++)) SPIRVShaderResourceAttribs(Compiler, Img, m_ResourceNames.CopyString(Img.name), - ResType, - GetShaderVariableType(Img.name, shaderDesc)); + ResType); } VERIFY_EXPR(CurrImg == GetNumImgs()); } { Uint32 CurrAC = 0; - for (const auto &AC : resources.atomic_counters) + for (const auto& AC : resources.atomic_counters) { new (&GetAC(CurrAC++)) SPIRVShaderResourceAttribs(Compiler, AC, m_ResourceNames.CopyString(AC.name), - SPIRVShaderResourceAttribs::ResourceType::AtomicCounter, - GetShaderVariableType(AC.name, shaderDesc)); + SPIRVShaderResourceAttribs::ResourceType::AtomicCounter); } VERIFY_EXPR(CurrAC == GetNumACs()); } { Uint32 CurrSepSmpl = 0; - for (const auto &SepSam : resources.separate_samplers) + for (const auto& SepSam : resources.separate_samplers) { - auto ImmutableSamplerInd = FindImmutableSampler(shaderDesc, SepSam.name, CombinedSamplerSuffix); - // Use texture or sampler name to derive sampler type - auto VarType = GetShaderVariableType(shaderDesc.DefaultVariableType, shaderDesc.VariableDesc, shaderDesc.NumVariables, - [&](const char* VarName) - { - return StreqSuff(SepSam.name.c_str(), VarName, CombinedSamplerSuffix); - }); - new (&GetSepSmplr(CurrSepSmpl++)) SPIRVShaderResourceAttribs(Compiler, SepSam, m_ResourceNames.CopyString(SepSam.name), - SPIRVShaderResourceAttribs::ResourceType::SeparateSampler, - VarType, - ImmutableSamplerInd); + SPIRVShaderResourceAttribs::ResourceType::SeparateSampler); } VERIFY_EXPR(CurrSepSmpl == GetNumSepSmplrs()); } { Uint32 CurrSepImg = 0; - for (const auto &SepImg : resources.separate_images) + for (const auto& SepImg : resources.separate_images) { Uint32 SamplerInd = SPIRVShaderResourceAttribs::InvalidSepSmplrOrImgInd; if (CombinedSamplerSuffix != nullptr) @@ -376,10 +348,6 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator, if (StreqSuff(SepSmplr.Name, SepImg.name.c_str(), CombinedSamplerSuffix)) { SepSmplr.AssignSeparateImage(CurrSepImg); - // Do no assign immutable samplers to separate images as immutable - // samplers are permanently bound into the set layout - if (SepSmplr.IsImmutableSamplerAssigned()) - SamplerInd = NumSepSmpls; break; } } @@ -391,8 +359,6 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator, SepImg, m_ResourceNames.CopyString(SepImg.name), SPIRVShaderResourceAttribs::ResourceType::SeparateImage, - GetShaderVariableType(SepImg.name, shaderDesc), - -1, SamplerInd); if (pNewSepImg->IsValidSepSamplerAssigned()) { @@ -413,12 +379,7 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator, m_CombinedSamplerSuffix = m_ResourceNames.CopyString(CombinedSamplerSuffix); } - for (Uint32 s = 0; s < m_NumImmutableSamplers; ++s) - { - SamplerPtrType& pStaticSampler = GetImmutableSampler(s); - new (std::addressof(pStaticSampler)) SamplerPtrType; - pRenderDevice->CreateSampler(shaderDesc.StaticSamplers[s].Desc, &pStaticSampler); - } + m_ShaderName = m_ResourceNames.CopyString(shaderDesc.Name); if (LoadShaderStageInputs) { @@ -428,7 +389,7 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator, if (Compiler.has_decoration(Input.id, spv::Decoration::DecorationHlslSemanticGOOGLE)) { const auto& Semantic = Compiler.get_decoration_string(Input.id, spv::Decoration::DecorationHlslSemanticGOOGLE); - new (&GetShaderStageInputAttribs(CurrStageInput++)) + new (&GetShaderStageInputAttribs(CurrStageInput++)) SPIRVShaderStageInputAttribs(m_ResourceNames.CopyString(Semantic), GetDecorationOffset(Compiler, Input, spv::Decoration::DecorationLocation)); } } @@ -440,68 +401,6 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator, //LOG_INFO_MESSAGE(DumpResources()); #ifdef DEVELOPMENT - if (shaderDesc.NumVariables != 0) - { - for (Uint32 v = 0; v < shaderDesc.NumVariables; ++v) - { - bool VariableFound = false; - const auto* VarName = shaderDesc.VariableDesc[v].Name; - auto VarType = shaderDesc.VariableDesc[v].Type; - - for (Uint32 res = 0; res < GetTotalResources(); ++res) - { - const auto& ResAttribs = GetResource(res); - if (strcmp(ResAttribs.Name, VarName) == 0) - { - VariableFound = true; - break; - } - } - if (!VariableFound) - { - LOG_WARNING_MESSAGE("Variable '", VarName, "' labeled as ", GetShaderVariableTypeLiteralName(VarType), " is not found in shader '", shaderDesc.Name, "'"); - } - } - } - - if (shaderDesc.NumStaticSamplers != 0) - { - for (Uint32 s = 0; s < shaderDesc.NumStaticSamplers; ++s) - { - const auto* SamName = shaderDesc.StaticSamplers[s].SamplerOrTextureName; - bool SamplerFound = false; - - // Irrespective of whether HLSL-style combined image samplers are used, - // a static sampler can be assigned to GLSL sampled image (i.e. sampler2D g_tex) - for (Uint32 i = 0; i < GetNumSmpldImgs(); ++i) - { - const auto& SmplImg = GetSmpldImg(i); - SamplerFound = (strcmp(SmplImg.Name, SamName) == 0); - if (SamplerFound) - break; - } - - if (!SamplerFound) - { - // Check if static sampler is assigned to a separate sampler or - // separate image depending on whether HLSL-style combined samplers - // are used - for (Uint32 i = 0; i < GetNumSepSmplrs(); ++i) - { - const auto& SepSmpl = GetSepSmplr(i); - SamplerFound = StreqSuff(SepSmpl.Name, SamName, CombinedSamplerSuffix); - if (SamplerFound) - break; - } - } - - if (!SamplerFound) - { - LOG_WARNING_MESSAGE("Static sampler '", SamName, "' is not found in shader '", shaderDesc.Name, "'"); - } - } - } - if (CombinedSamplerSuffix != nullptr) { for (Uint32 n=0; n < GetNumSepSmplrs(); ++n) @@ -516,7 +415,6 @@ SPIRVShaderResources::SPIRVShaderResources(IMemoryAllocator& Allocator, void SPIRVShaderResources::Initialize(IMemoryAllocator& Allocator, const ResourceCounters& Counters, - Uint32 NumImmutableSamplers, Uint32 NumShaderStageInputs, size_t ResourceNamesPoolSize) { @@ -524,7 +422,7 @@ void SPIRVShaderResources::Initialize(IMemoryAllocator& Allocator, constexpr Uint32 MaxOffset = std::numeric_limits<OffsetType>::max(); auto AdvanceOffset = [&CurrentOffset, MaxOffset](Uint32 NumResources) { - VERIFY(CurrentOffset <= MaxOffset, "Current offser (", CurrentOffset, ") exceeds max allowed value (", MaxOffset, ")"); (void)MaxOffset; + VERIFY(CurrentOffset <= MaxOffset, "Current offset (", CurrentOffset, ") exceeds max allowed value (", MaxOffset, ")"); (void)MaxOffset; auto Offset = static_cast<OffsetType>(CurrentOffset); CurrentOffset += NumResources; return Offset; @@ -539,18 +437,15 @@ void SPIRVShaderResources::Initialize(IMemoryAllocator& Allocator, m_SeparateImageOffset = AdvanceOffset(Counters.NumSepImgs); m_TotalResources = AdvanceOffset(0); - VERIFY(NumImmutableSamplers <= MaxOffset, "Max offset exceeded"); - m_NumImmutableSamplers = static_cast<OffsetType>(NumImmutableSamplers); - VERIFY(NumShaderStageInputs <= MaxOffset, "Max offset exceeded"); m_NumShaderStageInputs = static_cast<OffsetType>(NumShaderStageInputs); + auto AlignedResourceNamesPoolSize = Align(ResourceNamesPoolSize, sizeof(void*)); + static_assert(sizeof(SPIRVShaderResourceAttribs) % sizeof(void*) == 0, "Size of SPIRVShaderResourceAttribs struct must be multiple of sizeof(void*)"); - static_assert(sizeof(SamplerPtrType) % sizeof(void*) == 0, "Size of SamplerPtrType must be multiple of sizeof(void*)"); - auto MemorySize = m_TotalResources * sizeof(SPIRVShaderResourceAttribs) + - m_NumImmutableSamplers * sizeof(SamplerPtrType) + - m_NumShaderStageInputs * sizeof(SPIRVShaderStageInputAttribs) + - ResourceNamesPoolSize * sizeof(char); + auto MemorySize = m_TotalResources * sizeof(SPIRVShaderResourceAttribs) + + m_NumShaderStageInputs * sizeof(SPIRVShaderStageInputAttribs) + + AlignedResourceNamesPoolSize * sizeof(char); VERIFY_EXPR(GetNumUBs() == Counters.NumUBs); VERIFY_EXPR(GetNumSBs() == Counters.NumSBs); @@ -562,11 +457,10 @@ void SPIRVShaderResources::Initialize(IMemoryAllocator& Allocator, if (MemorySize) { - auto *pRawMem = Allocator.Allocate(MemorySize, "Memory for shader resources", __FILE__, __LINE__); + auto* pRawMem = Allocator.Allocate(MemorySize, "Memory for shader resources", __FILE__, __LINE__); m_MemoryBuffer = std::unique_ptr<void, STDDeleterRawMem<void>>(pRawMem, Allocator); char* NamesPool = reinterpret_cast<char*>(m_MemoryBuffer.get()) + m_TotalResources * sizeof(SPIRVShaderResourceAttribs) + - m_NumImmutableSamplers * sizeof(SamplerPtrType) + m_NumShaderStageInputs * sizeof(SPIRVShaderStageInputAttribs); m_ResourceNames.AssignMemory(NamesPool, ResourceNamesPoolSize); } @@ -595,75 +489,19 @@ SPIRVShaderResources::~SPIRVShaderResources() for (Uint32 n = 0; n < GetNumSepImgs(); ++n) GetSepImg(n).~SPIRVShaderResourceAttribs(); - for (Uint32 n = 0; n < GetNumImmutableSamplers(); ++n) - GetImmutableSampler(n).~SamplerPtrType(); - for (Uint32 n = 0; n < GetNumShaderStageInputs(); ++n) GetShaderStageInputAttribs(n).~SPIRVShaderStageInputAttribs(); } -SPIRVShaderResources::ResourceCounters SPIRVShaderResources::CountResources(const SHADER_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes)const noexcept -{ - Uint32 AllowedTypeBits = GetAllowedTypeBits(AllowedVarTypes, NumAllowedTypes); (void)AllowedTypeBits; - ResourceCounters Counters; - ProcessResources( - AllowedVarTypes, NumAllowedTypes, - - [&](const SPIRVShaderResourceAttribs& UB, Uint32) - { - VERIFY_EXPR(UB.Type == SPIRVShaderResourceAttribs::ResourceType::UniformBuffer); - VERIFY_EXPR(IsAllowedType(UB.VarType, AllowedTypeBits)); - ++Counters.NumUBs; - }, - [&](const SPIRVShaderResourceAttribs& SB, Uint32) - { - VERIFY_EXPR(SB.Type == SPIRVShaderResourceAttribs::ResourceType::StorageBuffer); - VERIFY_EXPR(IsAllowedType(SB.VarType, AllowedTypeBits)); - ++Counters.NumSBs; - }, - [&](const SPIRVShaderResourceAttribs& Img, Uint32) - { - VERIFY_EXPR(Img.Type == SPIRVShaderResourceAttribs::ResourceType::StorageImage || Img.Type == SPIRVShaderResourceAttribs::ResourceType::StorageTexelBuffer); - VERIFY_EXPR(IsAllowedType(Img.VarType, AllowedTypeBits)); - ++Counters.NumImgs; - }, - [&](const SPIRVShaderResourceAttribs& SmplImg, Uint32) - { - VERIFY_EXPR(SmplImg.Type == SPIRVShaderResourceAttribs::ResourceType::SampledImage || SmplImg.Type == SPIRVShaderResourceAttribs::ResourceType::UniformTexelBuffer); - VERIFY_EXPR(IsAllowedType(SmplImg.VarType, AllowedTypeBits)); - ++Counters.NumSmpldImgs; - }, - [&](const SPIRVShaderResourceAttribs& AC, Uint32) - { - VERIFY_EXPR(AC.Type == SPIRVShaderResourceAttribs::ResourceType::AtomicCounter); - VERIFY_EXPR(IsAllowedType(AC.VarType, AllowedTypeBits)); - ++Counters.NumACs; - }, - [&](const SPIRVShaderResourceAttribs& SepSmpl, Uint32) - { - VERIFY_EXPR(SepSmpl.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateSampler); - VERIFY_EXPR(IsAllowedType(SepSmpl.VarType, AllowedTypeBits)); - ++Counters.NumSepSmplrs; - }, - [&](const SPIRVShaderResourceAttribs& SepImg, Uint32) - { - VERIFY_EXPR(SepImg.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateImage); - VERIFY_EXPR(IsAllowedType(SepImg.VarType, AllowedTypeBits)); - ++Counters.NumSepImgs; - } - ); - return Counters; -} std::string SPIRVShaderResources::DumpResources() { std::stringstream ss; - ss << "Resource counters (" << GetTotalResources() << " total):" << std::endl << "UBs: " << GetNumUBs() << "; SBs: " - << GetNumSBs() << "; Imgs: " << GetNumImgs() << "; Smpl Imgs: " << GetNumSmpldImgs() << "; ACs: " << GetNumACs() - << "; Sep Imgs: " << GetNumSepImgs() << "; Sep Smpls: " << GetNumSepSmplrs() << '.' << std::endl - << "Num Static Samplers: " << GetNumImmutableSamplers() << std::endl << "Resources:"; + ss << "Shader '" << m_ShaderName << "' resource stats: total resources: " << GetTotalResources() << ":" << std::endl + << "UBs: " << GetNumUBs() << "; SBs: " << GetNumSBs() << "; Imgs: " << GetNumImgs() << "; Smpl Imgs: " << GetNumSmpldImgs() + << "; ACs: " << GetNumACs() << "; Sep Imgs: " << GetNumSepImgs() << "; Sep Smpls: " << GetNumSepSmplrs() << '.' << std::endl + << "Resources:"; Uint32 ResNum = 0; auto DumpResource = [&ss, &ResNum](const SPIRVShaderResourceAttribs& Res) @@ -674,17 +512,21 @@ std::string SPIRVShaderResources::DumpResources() FullResNameSS << '[' << Res.ArraySize << ']'; FullResNameSS << '\''; ss << std::setw(32) << FullResNameSS.str(); - ss << " (" << GetShaderVariableTypeLiteralName(Res.VarType) << ")"; - - if (Res.IsImmutableSamplerAssigned()) + + if (Res.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateImage && Res.IsValidSepSamplerAssigned()) + { + ss << " Assigned sep sampler ind: " << Res.GetAssignedSepSamplerInd(); + } + else if (Res.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateSampler && Res.IsValidSepImageAssigned()) { - ss << " Immutable sampler: " << Res.GetImmutableSamplerInd(); + ss << " Assigned sep image ind: " << Res.GetAssignedSepImageInd(); } + ++ResNum; }; - ProcessResources(nullptr, 0, - [&](const SPIRVShaderResourceAttribs &UB, Uint32) + ProcessResources( + [&](const SPIRVShaderResourceAttribs& UB, Uint32) { VERIFY(UB.Type == SPIRVShaderResourceAttribs::ResourceType::UniformBuffer, "Unexpected resource type"); ss << std::endl << std::setw(3) << ResNum << " Uniform Buffer "; @@ -696,9 +538,9 @@ std::string SPIRVShaderResources::DumpResources() ss << std::endl << std::setw(3) << ResNum << " Storage Buffer "; DumpResource(SB); }, - [&](const SPIRVShaderResourceAttribs &Img, Uint32) + [&](const SPIRVShaderResourceAttribs& Img, Uint32) { - if(Img.Type == SPIRVShaderResourceAttribs::ResourceType::StorageImage) + if (Img.Type == SPIRVShaderResourceAttribs::ResourceType::StorageImage) ss << std::endl << std::setw(3) << ResNum << " Storage Image "; else if(Img.Type == SPIRVShaderResourceAttribs::ResourceType::StorageTexelBuffer) ss << std::endl << std::setw(3) << ResNum << " Storage Txl Buff"; @@ -706,7 +548,7 @@ std::string SPIRVShaderResources::DumpResources() UNEXPECTED("Unexpected resource type"); DumpResource(Img); }, - [&](const SPIRVShaderResourceAttribs &SmplImg, Uint32) + [&](const SPIRVShaderResourceAttribs& SmplImg, Uint32) { if (SmplImg.Type == SPIRVShaderResourceAttribs::ResourceType::SampledImage) ss << std::endl << std::setw(3) << ResNum << " Sampled Image "; @@ -716,19 +558,19 @@ std::string SPIRVShaderResources::DumpResources() UNEXPECTED("Unexpected resource type"); DumpResource(SmplImg); }, - [&](const SPIRVShaderResourceAttribs &AC, Uint32) + [&](const SPIRVShaderResourceAttribs& AC, Uint32) { VERIFY(AC.Type == SPIRVShaderResourceAttribs::ResourceType::AtomicCounter, "Unexpected resource type"); ss << std::endl << std::setw(3) << ResNum << " Atomic Cntr "; DumpResource(AC); }, - [&](const SPIRVShaderResourceAttribs &SepSmpl, Uint32) + [&](const SPIRVShaderResourceAttribs& SepSmpl, Uint32) { VERIFY(SepSmpl.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateSampler, "Unexpected resource type"); ss << std::endl << std::setw(3) << ResNum << " Separate Smpl "; DumpResource(SepSmpl); }, - [&](const SPIRVShaderResourceAttribs &SepImg, Uint32) + [&](const SPIRVShaderResourceAttribs& SepImg, Uint32) { VERIFY(SepImg.Type == SPIRVShaderResourceAttribs::ResourceType::SeparateImage, "Unexpected resource type"); ss << std::endl << std::setw(3) << ResNum << " Separate Img "; @@ -750,13 +592,12 @@ bool SPIRVShaderResources::IsCompatibleWith(const SPIRVShaderResources& Resource GetNumSmpldImgs() != Resources.GetNumSmpldImgs() || GetNumACs() != Resources.GetNumACs() || GetNumSepImgs() != Resources.GetNumSepImgs() || - GetNumSepSmplrs() != Resources.GetNumSepSmplrs() || - GetNumImmutableSamplers() != Resources.GetNumImmutableSamplers()) + GetNumSepSmplrs() != Resources.GetNumSepSmplrs()) return false; VERIFY_EXPR(GetTotalResources() == Resources.GetTotalResources()); bool IsCompatible = true; - ProcessResources(nullptr, 0, + ProcessResources( [&](const SPIRVShaderResourceAttribs& Res, Uint32 n) { const auto& Res2 = Resources.GetResource(n); diff --git a/Graphics/GLSLTools/src/SPIRVUtils.cpp b/Graphics/GLSLTools/src/SPIRVUtils.cpp index 799270db..46e26470 100644 --- a/Graphics/GLSLTools/src/SPIRVUtils.cpp +++ b/Graphics/GLSLTools/src/SPIRVUtils.cpp @@ -398,7 +398,7 @@ private: std::unordered_map<IncludeResult*, RefCntAutoPtr<IDataBlob>> m_DataBlobs; }; -std::vector<unsigned int> HLSLtoSPIRV(const ShaderCreationAttribs& Attribs, IDataBlob** ppCompilerOutput) +std::vector<unsigned int> HLSLtoSPIRV(const ShaderCreateInfo& Attribs, IDataBlob** ppCompilerOutput) { EShLanguage ShLang = ShaderTypeToShLanguage(Attribs.Desc.ShaderType); glslang::TShader Shader(ShLang); |
