From c1470ef074418b5b238e0a461242f4c24b660ba2 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 2 Mar 2019 22:31:12 -0800 Subject: Reworked ShaderD3DBase to comply with the updated API --- .../include/D3DShaderResourceLoader.h | 95 ++---------------- .../GraphicsEngineD3DBase/include/ShaderD3DBase.h | 3 +- .../include/ShaderResources.h | 110 ++++----------------- .../include/ShaderVariableD3DBase.h | 8 +- .../GraphicsEngineD3DBase/src/ShaderD3DBase.cpp | 70 ++++++------- .../GraphicsEngineD3DBase/src/ShaderResources.cpp | 54 ---------- 6 files changed, 66 insertions(+), 274 deletions(-) (limited to 'Graphics/GraphicsEngineD3DBase') diff --git a/Graphics/GraphicsEngineD3DBase/include/D3DShaderResourceLoader.h b/Graphics/GraphicsEngineD3DBase/include/D3DShaderResourceLoader.h index 511f436e..c2c6e402 100644 --- a/Graphics/GraphicsEngineD3DBase/include/D3DShaderResourceLoader.h +++ b/Graphics/GraphicsEngineD3DBase/include/D3DShaderResourceLoader.h @@ -57,17 +57,13 @@ namespace Diligent typename TOnNewSampler, typename TOnNewTexSRV> void LoadD3DShaderResources(ID3DBlob* pShaderByteCode, - TOnResourcesCounted OnResourcesCounted, TOnNewCB OnNewCB, TOnNewTexUAV OnNewTexUAV, TOnNewBuffUAV OnNewBuffUAV, TOnNewBuffSRV OnNewBuffSRV, TOnNewSampler OnNewSampler, - TOnNewTexSRV OnNewTexSRV, - - const ShaderDesc& ShdrDesc, - const Char* SamplerSuffix) + TOnNewTexSRV OnNewTexSRV) { CComPtr pShaderReflection; auto hr = D3DReflect( pShaderByteCode->GetBufferPointer(), pShaderByteCode->GetBufferSize(), __uuidof(pShaderReflection), reinterpret_cast(static_cast(&pShaderReflection))); @@ -80,13 +76,11 @@ namespace Diligent Resources.reserve(shaderDesc.BoundResources); std::unordered_set ResourceNamesTmpPool; - const bool UseCombinedTextureSamplers = SamplerSuffix != nullptr; - D3DShaderResourceCounters RC; size_t ResourceNamesPoolSize = 0; // Number of resources to skip (used for array resources) UINT SkipCount = 1; - for( UINT Res = 0; Res < shaderDesc.BoundResources; Res += SkipCount ) + for (UINT Res = 0; Res < shaderDesc.BoundResources; Res += SkipCount) { D3D_SHADER_INPUT_BIND_DESC BindingDesc = {}; pShaderReflection->GetResourceBindingDesc( Res, &BindingDesc ); @@ -128,7 +122,7 @@ namespace Diligent VERIFY(Name.compare(ExistingRes.Name) != 0, "Resource with the same name has already been enumerated. All array elements are expected to be enumerated one after another"); } #endif - for( UINT ArrElem = Res+1; ArrElem < shaderDesc.BoundResources; ++ArrElem) + for (UINT ArrElem = Res+1; ArrElem < shaderDesc.BoundResources; ++ArrElem) { D3D_SHADER_INPUT_BIND_DESC ArrElemBindingDesc = {}; pShaderReflection->GetResourceBindingDesc( ArrElem, &ArrElemBindingDesc ); @@ -156,32 +150,6 @@ namespace Diligent } } - - SHADER_VARIABLE_TYPE VarType = SHADER_VARIABLE_TYPE_NUM_TYPES; - bool IsStaticSampler = false; - if (BindingDesc.Type == D3D_SIT_SAMPLER) - { - for (Uint32 s = 0; s < ShdrDesc.NumStaticSamplers; ++s) - { - if (StreqSuff(Name.c_str(), ShdrDesc.StaticSamplers[s].SamplerOrTextureName, SamplerSuffix)) - { - IsStaticSampler = true; - break; - } - } - // Use texture or sampler name to derive sampler type - VarType = GetShaderVariableType(ShdrDesc.DefaultVariableType, ShdrDesc.VariableDesc, ShdrDesc.NumVariables, - [&](const char* VarName) - { - return StreqSuff(Name.c_str(), VarName, SamplerSuffix); - }); - } - else - { - VarType = GetShaderVariableType(Name, ShdrDesc.DefaultVariableType, ShdrDesc.VariableDesc, ShdrDesc.NumVariables); - } - - switch( BindingDesc.Type ) { case D3D_SIT_CBUFFER: ++RC.NumCBs; break; @@ -205,68 +173,17 @@ namespace Diligent BindingDesc.BindPoint, BindCount, BindingDesc.Type, - VarType, BindingDesc.Dimension, - D3DShaderResourceAttribs::InvalidSamplerId, - IsStaticSampler + D3DShaderResourceAttribs::InvalidSamplerId ); } - -#ifdef DEVELOPMENT - for (Uint32 v = 0; v < ShdrDesc.NumVariables; ++v) - { - bool VariableFound = false; - const auto* VarName = ShdrDesc.VariableDesc[v].Name; - - for (const auto& Res : Resources) - { - // Skip samplers if combined texture samplers are used as - // in this case they are not treated as independent variables - if (UseCombinedTextureSamplers && Res.GetInputType() == D3D_SIT_SAMPLER) - continue; - - VariableFound = (strcmp(Res.Name, VarName) == 0); - if (VariableFound) - break; - } - if(!VariableFound) - { - LOG_WARNING_MESSAGE("Variable '", VarName, "' is not found in shader '", ShdrDesc.Name, '\''); - } - } - - for (Uint32 s = 0; s < ShdrDesc.NumStaticSamplers; ++s) - { - const auto* TexOrSamName = ShdrDesc.StaticSamplers[s].SamplerOrTextureName; - - bool TextureOrSamplerFound = false; - for (const auto& Res : Resources) - { - if( UseCombinedTextureSamplers && Res.GetInputType() == D3D_SIT_TEXTURE && Res.GetSRVDimension() != D3D_SRV_DIMENSION_BUFFER || - !UseCombinedTextureSamplers && Res.GetInputType() == D3D_SIT_SAMPLER) - { - TextureOrSamplerFound = (strcmp(Res.Name, TexOrSamName) == 0); - if (TextureOrSamplerFound) - break; - } - } - if (!TextureOrSamplerFound) - { - if (UseCombinedTextureSamplers) - LOG_WARNING_MESSAGE("Static sampler specifies a texture '", TexOrSamName, "' that is not found in shader '", ShdrDesc.Name, '\''); - else - LOG_WARNING_MESSAGE("Static sampler '", TexOrSamName, "' is not found in shader '", ShdrDesc.Name, '\''); - } - } -#endif - OnResourcesCounted(RC, ResourceNamesPoolSize); std::vector > TexSRVInds( STD_ALLOCATOR_RAW_MEM(size_t, GetRawAllocator(), "Allocator for vector") ); TexSRVInds.reserve(RC.NumTexSRVs); - for(size_t ResInd = 0; ResInd < Resources.size(); ++ResInd) + for (size_t ResInd = 0; ResInd < Resources.size(); ++ResInd) { const auto& Res = Resources[ResInd]; switch (Res.GetInputType()) @@ -368,7 +285,7 @@ namespace Diligent // Process texture SRVs. We need to do this after all samplers are initialized for (auto TexSRVInd : TexSRVInds) { - OnNewTexSRV( Resources[TexSRVInd] ); + OnNewTexSRV(Resources[TexSRVInd]); } } } diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderD3DBase.h b/Graphics/GraphicsEngineD3DBase/include/ShaderD3DBase.h index e7b1d10f..7f2eda96 100644 --- a/Graphics/GraphicsEngineD3DBase/include/ShaderD3DBase.h +++ b/Graphics/GraphicsEngineD3DBase/include/ShaderD3DBase.h @@ -36,10 +36,9 @@ namespace Diligent class ShaderD3DBase { public: - ShaderD3DBase(const ShaderCreationAttribs &CreationAttribs); + ShaderD3DBase(const ShaderCreateInfo& ShaderCI); protected: - CComPtr m_pShaderByteCode; }; } diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h index 3cc19fa7..aa3867f3 100644 --- a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h +++ b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h @@ -66,23 +66,6 @@ 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; -} - - struct D3DShaderResourceAttribs { const char* const Name; @@ -91,19 +74,16 @@ struct D3DShaderResourceAttribs const Uint16 BindCount; private: - // 4 3 4 20 1 - // bit | 0 1 2 3 | 4 5 6 | 7 8 9 10 | 11 12 13 ... 30 | 31 | - // | | | | | | - // | InputType | VariableType | SRV Dim | SamplerOrTexSRVIdBits | StaticSamplerFlag | + // 4 4 24 + // bit | 0 1 2 3 | 4 5 6 7 | 8 9 10 ... 31 | + // | | | | + // | InputType | SRV Dim | SamplerOrTexSRVIdBits | static constexpr const Uint32 ShaderInputTypeBits = 4; - static constexpr const Uint32 VariableTypeBits = 3; static constexpr const Uint32 SRVDimBits = 4; - static constexpr const Uint32 SamplerOrTexSRVIdBits = 20; - static constexpr const Uint32 StaticSamplerFlagBits = 1; - static_assert(ShaderInputTypeBits + VariableTypeBits + SRVDimBits + SamplerOrTexSRVIdBits + StaticSamplerFlagBits == 32, "Attributes are better be packed into 32 bits"); + static constexpr const Uint32 SamplerOrTexSRVIdBits = 24; + static_assert(ShaderInputTypeBits + SRVDimBits + SamplerOrTexSRVIdBits == 32, "Attributes are better be packed into 32 bits"); static_assert(D3D_SIT_UAV_RWSTRUCTURED_WITH_COUNTER < (1 << ShaderInputTypeBits), "Not enough bits to represent D3D_SHADER_INPUT_TYPE"); - static_assert(SHADER_VARIABLE_TYPE_NUM_TYPES < (1 << VariableTypeBits), "Not enough bits to represent SHADER_VARIABLE_TYPE"); static_assert(D3D_SRV_DIMENSION_BUFFEREX < (1 << SRVDimBits), "Not enough bits to represent D3D_SRV_DIMENSION"); // We need to use Uint32 instead of the actual type for reliability and correctness. @@ -111,11 +91,8 @@ private: // the value of D3D_SIT_UAV_RWBYTEADDRESS (8) was interpreted as -8 (as the underlying enum type // is signed) causing errors const Uint32 InputType : ShaderInputTypeBits; // Max value: D3D_SIT_UAV_RWSTRUCTURED_WITH_COUNTER == 11 - const Uint32 VariableType : VariableTypeBits; // Max value: SHADER_VARIABLE_TYPE_DYNAMIC == 2 const Uint32 SRVDimension : SRVDimBits; // Max value: D3D_SRV_DIMENSION_BUFFEREX == 11 Uint32 SamplerOrTexSRVId : SamplerOrTexSRVIdBits; // Max value: 1048575 - const Uint32 StaticSamplerFlag : StaticSamplerFlagBits; // Needs to be Uint32, otherwise sizeof(D3DShaderResourceAttribs)==24 - // (https://stackoverflow.com/questions/308364/c-bitfield-packing-with-bools) public: static constexpr const Uint32 InvalidSamplerId = (1 << SamplerOrTexSRVIdBits) - 1; @@ -129,39 +106,26 @@ public: UINT _BindPoint, UINT _BindCount, D3D_SHADER_INPUT_TYPE _InputType, - SHADER_VARIABLE_TYPE _VariableType, D3D_SRV_DIMENSION _SRVDimension, - Uint32 _SamplerId, - bool _IsStaticSampler)noexcept : + Uint32 _SamplerId)noexcept : Name (_Name), BindPoint (static_cast (_BindPoint)), BindCount (static_cast (_BindCount)), InputType (static_cast (_InputType)), - VariableType (static_cast(_VariableType)), SRVDimension (static_cast(_SRVDimension)), - SamplerOrTexSRVId (_SamplerId), - StaticSamplerFlag (_IsStaticSampler ? 1 : 0) + SamplerOrTexSRVId (_SamplerId) { #ifdef _DEBUG VERIFY(_BindPoint <= MaxBindPoint || _BindPoint == InvalidBindPoint, "Bind Point is out of allowed range"); VERIFY(_BindCount <= MaxBindCount, "Bind Count is out of allowed range"); VERIFY(_InputType < (1 << ShaderInputTypeBits), "Shader input type is out of expected range"); - VERIFY(_VariableType < (1 << VariableTypeBits), "Variable type is out of expected range"); VERIFY(_SRVDimension < (1 << SRVDimBits), "SRV dimensions is out of expected range"); VERIFY(_SamplerId < (1 << SamplerOrTexSRVIdBits), "SamplerOrTexSRVId is out of representable range"); - if (_InputType==D3D_SIT_SAMPLER) - VERIFY_EXPR(IsStaticSampler() == _IsStaticSampler); - else - VERIFY(!_IsStaticSampler, "Only samplers can be labeled as static"); - if (_InputType == D3D_SIT_TEXTURE && _SRVDimension != D3D_SRV_DIMENSION_BUFFER) VERIFY_EXPR(GetSamplerId() == _SamplerId); else VERIFY(_SamplerId == InvalidSamplerId, "Only texture SRV can be assigned a valid texture sampler"); - - if (_IsStaticSampler) - VERIFY( _InputType == D3D_SIT_SAMPLER, "Invalid input type: D3D_SIT_SAMPLER is expected" ); #endif } @@ -172,10 +136,8 @@ public: rhs.BindPoint, rhs.BindCount, rhs.GetInputType(), - rhs.GetVariableType(), rhs.GetSRVDimension(), - SamplerId, - false + SamplerId } { VERIFY(GetInputType() == D3D_SIT_TEXTURE && GetSRVDimension() != D3D_SRV_DIMENSION_BUFFER, "Only texture SRV can be assigned a texture sampler"); @@ -188,10 +150,8 @@ public: rhs.BindPoint, rhs.BindCount, rhs.GetInputType(), - rhs.GetVariableType(), rhs.GetSRVDimension(), - rhs.SamplerOrTexSRVId, - rhs.StaticSamplerFlag !=0 ? true : false + rhs.SamplerOrTexSRVId } { } @@ -206,11 +166,6 @@ public: return static_cast(InputType); } - SHADER_VARIABLE_TYPE GetVariableType()const - { - return static_cast(VariableType); - } - D3D_SRV_DIMENSION GetSRVDimension()const { return static_cast(SRVDimension); @@ -235,12 +190,6 @@ public: return SamplerOrTexSRVId; } - bool IsStaticSampler()const - { - VERIFY(GetInputType() == D3D_SIT_SAMPLER, "Invalid input type: D3D_SIT_SAMPLER is expected" ); - return StaticSamplerFlag != 0; - } - bool ValidSamplerAssigned()const { return GetSamplerId() != InvalidSamplerId; @@ -270,20 +219,13 @@ public: return BindPoint == Attribs.BindPoint && BindCount == Attribs.BindCount && InputType == Attribs.InputType && - VariableType == Attribs.VariableType && SRVDimension == Attribs.SRVDimension && - SamplerOrTexSRVId == Attribs.SamplerOrTexSRVId && - StaticSamplerFlag == Attribs.StaticSamplerFlag; + SamplerOrTexSRVId == Attribs.SamplerOrTexSRVId; } size_t GetHash()const { - return ComputeHash(BindPoint, BindCount, InputType, VariableType, SRVDimension, SamplerOrTexSRVId, StaticSamplerFlag); - } - - bool IsAllowedType(Uint32 AllowedTypeBits)const - { - return Diligent::IsAllowedType(GetVariableType(), AllowedTypeBits); + return ComputeHash(BindPoint, BindCount, InputType, SRVDimension, SamplerOrTexSRVId); } }; static_assert(sizeof(D3DShaderResourceAttribs) == sizeof(void*) + sizeof(Uint32)*2, "Unexpected sizeof(D3DShaderResourceAttribs)"); @@ -317,9 +259,6 @@ public: const D3DShaderResourceAttribs& GetBufUAV (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumBufUAV(), m_BufUAVOffset); } const D3DShaderResourceAttribs& GetSampler(Uint32 n)const noexcept{ return GetResAttribs(n, GetNumSamplers(), m_SamplersOffset); } - D3DShaderResourceCounters CountResources(const SHADER_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes)const noexcept; - SHADER_TYPE GetShaderType()const noexcept{return m_ShaderType;} // Processes only resources listed in AllowedVarTypes @@ -329,57 +268,47 @@ public: typename THandleTexUAV, typename THandleBufSRV, typename THandleBufUAV> - void ProcessResources(const SHADER_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes, - THandleCB HandleCB, + void ProcessResources(THandleCB HandleCB, THandleSampler HandleSampler, THandleTexSRV HandleTexSRV, THandleTexUAV HandleTexUAV, THandleBufSRV HandleBufSRV, THandleBufUAV HandleBufUAV)const { - Uint32 AllowedTypeBits = GetAllowedTypeBits(AllowedVarTypes, NumAllowedTypes); - for(Uint32 n=0; n < GetNumCBs(); ++n) { const auto& CB = GetCB(n); - if( CB.IsAllowedType(AllowedTypeBits) ) - HandleCB(CB, n); + HandleCB(CB, n); } for(Uint32 n=0; n < GetNumSamplers(); ++n) { const auto& Sampler = GetSampler(n); - if( Sampler.IsAllowedType(AllowedTypeBits) ) - HandleSampler(Sampler, n); + HandleSampler(Sampler, n); } for(Uint32 n=0; n < GetNumTexSRV(); ++n) { const auto& TexSRV = GetTexSRV(n); - if( TexSRV.IsAllowedType(AllowedTypeBits) ) - HandleTexSRV(TexSRV, n); + HandleTexSRV(TexSRV, n); } for(Uint32 n=0; n < GetNumTexUAV(); ++n) { const auto& TexUAV = GetTexUAV(n); - if( TexUAV.IsAllowedType(AllowedTypeBits) ) - HandleTexUAV(TexUAV, n); + HandleTexUAV(TexUAV, n); } for(Uint32 n=0; n < GetNumBufSRV(); ++n) { const auto& BufSRV = GetBufSRV(n); - if( BufSRV.IsAllowedType(AllowedTypeBits) ) - HandleBufSRV(BufSRV, n); + HandleBufSRV(BufSRV, n); } for(Uint32 n=0; n < GetNumBufUAV(); ++n) { const auto& BufUAV = GetBufUAV(n); - if( BufUAV.IsAllowedType(AllowedTypeBits) ) - HandleBufUAV(BufUAV, n); + HandleBufUAV(BufUAV, n); } } @@ -567,3 +496,4 @@ namespace std } }; } + diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderVariableD3DBase.h b/Graphics/GraphicsEngineD3DBase/include/ShaderVariableD3DBase.h index 6c683aa2..48748a6c 100644 --- a/Graphics/GraphicsEngineD3DBase/include/ShaderVariableD3DBase.h +++ b/Graphics/GraphicsEngineD3DBase/include/ShaderVariableD3DBase.h @@ -23,7 +23,7 @@ #pragma once -#include "Shader.h" +#include "ShaderResourceVariable.h" /// \file /// Declaration of Diligent::ShaderVariableD3DBase class @@ -31,7 +31,7 @@ namespace Diligent { template - struct ShaderVariableD3DBase : public IShaderVariable + struct ShaderVariableD3DBase : public IShaderResourceVariable { ShaderVariableD3DBase(TShaderResourceLayout& ParentResLayout, const D3DShaderResourceAttribs& ResourcesAttribs) : m_ParentResLayout(ParentResLayout), @@ -54,7 +54,7 @@ namespace Diligent return m_ParentResLayout.GetOwner().Release(); } - void QueryInterface( const INTERFACE_ID &IID, IObject **ppInterface )override final + void QueryInterface( const INTERFACE_ID& IID, IObject** ppInterface )override final { if( ppInterface == nullptr ) return; @@ -67,7 +67,7 @@ namespace Diligent } } - virtual SHADER_VARIABLE_TYPE GetType()const override final + virtual SHADER_RESOURCE_VARIABLE_TYPE GetType()const override final { return Attribs.GetVariableType(); } diff --git a/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp b/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp index c1fbda20..e9f1144b 100644 --- a/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp +++ b/Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp @@ -41,13 +41,13 @@ static const Char* g_HLSLDefinitions = class D3DIncludeImpl : public ID3DInclude { public: - D3DIncludeImpl(IShaderSourceInputStreamFactory *pStreamFactory) : + D3DIncludeImpl(IShaderSourceInputStreamFactory* pStreamFactory) : m_pStreamFactory(pStreamFactory) { } - STDMETHOD( Open )(THIS_ D3D_INCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes) + STDMETHOD( Open )(THIS_ D3D_INCLUDE_TYPE IncludeType, LPCSTR pFileName, LPCVOID pParentData, LPCVOID* ppData, UINT* pBytes) { RefCntAutoPtr pSourceStream; m_pStreamFactory->CreateInputStream( pFileName, &pSourceStream ); @@ -78,13 +78,13 @@ private: std::unordered_map< LPCVOID, RefCntAutoPtr > m_DataBlobs; }; -HRESULT CompileShader( const char* Source, - LPCSTR strFunctionName, - const D3D_SHADER_MACRO* pDefines, - IShaderSourceInputStreamFactory *pIncludeStreamFactory, - LPCSTR profile, - ID3DBlob **ppBlobOut, - ID3DBlob **ppCompilerOutput) +static HRESULT CompileShader(const char* Source, + LPCSTR strFunctionName, + const D3D_SHADER_MACRO* pDefines, + IShaderSourceInputStreamFactory* pIncludeStreamFactory, + LPCSTR profile, + ID3DBlob** ppBlobOut, + ID3DBlob** ppCompilerOutput) { DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; #if defined( DEBUG ) || defined( _DEBUG ) @@ -133,15 +133,15 @@ const char* DXShaderProfileToString(SHADER_PROFILE DXProfile) } } -ShaderD3DBase::ShaderD3DBase(const ShaderCreationAttribs &CreationAttribs) +ShaderD3DBase::ShaderD3DBase(const ShaderCreateInfo& ShaderCI) { - if (CreationAttribs.Source || CreationAttribs.FilePath) + if (ShaderCI.Source || ShaderCI.FilePath) { - DEV_CHECK_ERR(CreationAttribs.ByteCode == nullptr, "'ByteCode' must be null when shader is created from the source code or a file"); - DEV_CHECK_ERR(CreationAttribs.ByteCodeSize == 0, "'ByteCodeSize' must be 0 when shader is created from the source code or a file"); + DEV_CHECK_ERR(ShaderCI.ByteCode == nullptr, "'ByteCode' must be null when shader is created from the source code or a file"); + DEV_CHECK_ERR(ShaderCI.ByteCodeSize == 0, "'ByteCodeSize' must be 0 when shader is created from the source code or a file"); std::string strShaderProfile; - switch(CreationAttribs.Desc.ShaderType) + switch (ShaderCI.Desc.ShaderType) { case SHADER_TYPE_VERTEX: strShaderProfile="vs"; break; case SHADER_TYPE_PIXEL: strShaderProfile="ps"; break; @@ -153,20 +153,20 @@ ShaderD3DBase::ShaderD3DBase(const ShaderCreationAttribs &CreationAttribs) default: UNEXPECTED( "Unknown shader type" ); } strShaderProfile += "_"; - auto *pProfileSuffix = DXShaderProfileToString(CreationAttribs.Desc.TargetProfile); + auto *pProfileSuffix = DXShaderProfileToString(ShaderCI.Desc.TargetProfile); strShaderProfile += pProfileSuffix; String ShaderSource(g_HLSLDefinitions); - if (CreationAttribs.Source) + if (ShaderCI.Source) { - DEV_CHECK_ERR(CreationAttribs.FilePath == nullptr, "'FilePath' is expected to be null when shader source code is provided"); - ShaderSource.append(CreationAttribs.Source); + DEV_CHECK_ERR(ShaderCI.FilePath == nullptr, "'FilePath' is expected to be null when shader source code is provided"); + ShaderSource.append(ShaderCI.Source); } else { - DEV_CHECK_ERR(CreationAttribs.pShaderSourceStreamFactory, "Input stream factory is null"); + DEV_CHECK_ERR(ShaderCI.pShaderSourceStreamFactory, "Input stream factory is null"); RefCntAutoPtr pSourceStream; - CreationAttribs.pShaderSourceStreamFactory->CreateInputStream(CreationAttribs.FilePath, &pSourceStream); + ShaderCI.pShaderSourceStreamFactory->CreateInputStream(ShaderCI.FilePath, &pSourceStream); RefCntAutoPtr pFileData(MakeNewRCObj()(0)); if (pSourceStream == nullptr) LOG_ERROR_AND_THROW("Failed to open shader source file"); @@ -179,9 +179,9 @@ ShaderD3DBase::ShaderD3DBase(const ShaderCreationAttribs &CreationAttribs) const D3D_SHADER_MACRO *pDefines = nullptr; std::vector D3DMacros; - if (CreationAttribs.Macros) + if (ShaderCI.Macros) { - for (auto* pCurrMacro = CreationAttribs.Macros; pCurrMacro->Name && pCurrMacro->Definition; ++pCurrMacro) + for (auto* pCurrMacro = ShaderCI.Macros; pCurrMacro->Name && pCurrMacro->Definition; ++pCurrMacro) { D3DMacros.push_back({ pCurrMacro->Name, pCurrMacro->Definition }); } @@ -189,39 +189,39 @@ ShaderD3DBase::ShaderD3DBase(const ShaderCreationAttribs &CreationAttribs) pDefines = D3DMacros.data(); } - DEV_CHECK_ERR(CreationAttribs.EntryPoint != nullptr, "Entry point must not be null"); + DEV_CHECK_ERR(ShaderCI.EntryPoint != nullptr, "Entry point must not be null"); CComPtr errors; - auto hr = CompileShader(ShaderSource.c_str(), CreationAttribs.EntryPoint, pDefines, CreationAttribs.pShaderSourceStreamFactory, strShaderProfile.c_str(), &m_pShaderByteCode, &errors); + auto hr = CompileShader(ShaderSource.c_str(), ShaderCI.EntryPoint, pDefines, ShaderCI.pShaderSourceStreamFactory, strShaderProfile.c_str(), &m_pShaderByteCode, &errors); - const char *CompilerMsg = errors ? reinterpret_cast(errors->GetBufferPointer()) : nullptr; - if(CompilerMsg != nullptr && CreationAttribs.ppCompilerOutput != nullptr) + const char* CompilerMsg = errors ? reinterpret_cast(errors->GetBufferPointer()) : nullptr; + if (CompilerMsg != nullptr && ShaderCI.ppCompilerOutput != nullptr) { auto ErrorMsgLen = strlen(CompilerMsg); auto *pOutputDataBlob = MakeNewRCObj()(ErrorMsgLen + 1 + ShaderSource.length() + 1); char* DataPtr = reinterpret_cast(pOutputDataBlob->GetDataPtr()); memcpy(DataPtr, CompilerMsg, ErrorMsgLen+1); memcpy(DataPtr + ErrorMsgLen + 1, ShaderSource.data(), ShaderSource.length() + 1); - pOutputDataBlob->QueryInterface(IID_DataBlob, reinterpret_cast(CreationAttribs.ppCompilerOutput)); + pOutputDataBlob->QueryInterface(IID_DataBlob, reinterpret_cast(ShaderCI.ppCompilerOutput)); } - if(FAILED(hr)) + if (FAILED(hr)) { ComErrorDesc ErrDesc(hr); - if(CreationAttribs.ppCompilerOutput != nullptr) + if(ShaderCI.ppCompilerOutput != nullptr) { - LOG_ERROR_AND_THROW("Failed to compile D3D shader \"", (CreationAttribs.Desc.Name != nullptr ? CreationAttribs.Desc.Name : ""), "\" (", ErrDesc.Get(), ")."); + LOG_ERROR_AND_THROW("Failed to compile D3D shader \"", (ShaderCI.Desc.Name != nullptr ? ShaderCI.Desc.Name : ""), "\" (", ErrDesc.Get(), ")."); } else { - LOG_ERROR_AND_THROW("Failed to compile D3D shader \"", (CreationAttribs.Desc.Name != nullptr ? CreationAttribs.Desc.Name : ""), "\" (", ErrDesc.Get(), "):\n", (CompilerMsg != nullptr ? CompilerMsg : "") ); + LOG_ERROR_AND_THROW("Failed to compile D3D shader \"", (ShaderCI.Desc.Name != nullptr ? ShaderCI.Desc.Name : ""), "\" (", ErrDesc.Get(), "):\n", (CompilerMsg != nullptr ? CompilerMsg : "") ); } } } - else if (CreationAttribs.ByteCode) + else if (ShaderCI.ByteCode) { - DEV_CHECK_ERR(CreationAttribs.ByteCodeSize != 0, "ByteCode size must be greater than 0"); - CHECK_D3D_RESULT_THROW(D3DCreateBlob(CreationAttribs.ByteCodeSize, &m_pShaderByteCode), "Failed to create D3D blob"); - memcpy(m_pShaderByteCode->GetBufferPointer(), CreationAttribs.ByteCode, CreationAttribs.ByteCodeSize); + DEV_CHECK_ERR(ShaderCI.ByteCodeSize != 0, "ByteCode size must be greater than 0"); + CHECK_D3D_RESULT_THROW(D3DCreateBlob(ShaderCI.ByteCodeSize, &m_pShaderByteCode), "Failed to create D3D blob"); + memcpy(m_pShaderByteCode->GetBufferPointer(), ShaderCI.ByteCode, ShaderCI.ByteCodeSize); } else { diff --git a/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp b/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp index 90b85286..847e2675 100644 --- a/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp +++ b/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp @@ -96,53 +96,6 @@ ShaderResources::ShaderResources(SHADER_TYPE ShaderType): { } -D3DShaderResourceCounters ShaderResources::CountResources(const SHADER_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes)const noexcept -{ - auto AllowedTypeBits = GetAllowedTypeBits(AllowedVarTypes, NumAllowedTypes); - - D3DShaderResourceCounters Counters; - ProcessResources( - AllowedVarTypes, NumAllowedTypes, - - [&](const D3DShaderResourceAttribs& CB, Uint32) - { - VERIFY_EXPR(CB.IsAllowedType(AllowedTypeBits)); - ++Counters.NumCBs; - }, - [&](const D3DShaderResourceAttribs& Sam, Uint32) - { - VERIFY_EXPR(Sam.IsAllowedType(AllowedTypeBits)); - // Skip static samplers - if (!Sam.IsStaticSampler()) - ++Counters.NumSamplers; - }, - [&](const D3DShaderResourceAttribs& TexSRV, Uint32) - { - VERIFY_EXPR(TexSRV.IsAllowedType(AllowedTypeBits)); - ++Counters.NumTexSRVs; - }, - [&](const D3DShaderResourceAttribs& TexUAV, Uint32) - { - VERIFY_EXPR(TexUAV.IsAllowedType(AllowedTypeBits)); - ++Counters.NumTexUAVs; - }, - [&](const D3DShaderResourceAttribs& BufSRV, Uint32) - { - VERIFY_EXPR(BufSRV.IsAllowedType(AllowedTypeBits)); - ++Counters.NumBufSRVs; - }, - [&](const D3DShaderResourceAttribs& BufUAV, Uint32) - { - VERIFY_EXPR(BufUAV.IsAllowedType(AllowedTypeBits)); - ++Counters.NumBufUAVs; - } - ); - - return Counters; -} - - Uint32 ShaderResources::FindAssignedSamplerId(const D3DShaderResourceAttribs& TexSRV, const char* SamplerSuffix)const { VERIFY_EXPR(SamplerSuffix != nullptr && *SamplerSuffix != 0); @@ -153,10 +106,6 @@ Uint32 ShaderResources::FindAssignedSamplerId(const D3DShaderResourceAttribs& Te const auto& Sampler = GetSampler(s); if ( StreqSuff(Sampler.Name, TexSRV.Name, SamplerSuffix) ) { - DEV_CHECK_ERR(Sampler.GetVariableType() == TexSRV.GetVariableType(), - "The type (", GetShaderVariableTypeLiteralName(TexSRV.GetVariableType()),") of texture SRV variable '", TexSRV.Name, - "' is not consistent with the type (", GetShaderVariableTypeLiteralName(Sampler.GetVariableType()), - ") of the sampler '", Sampler.Name, "' that is assigned to it"); DEV_CHECK_ERR(Sampler.BindCount == TexSRV.BindCount || Sampler.BindCount == 1, "Sampler '", Sampler.Name, "' assigned to texture '", TexSRV.Name, "' must be scalar or have the same array dimension (", TexSRV.BindCount, "). Actual sampler array dimension : ", Sampler.BindCount); return s; } @@ -176,8 +125,6 @@ bool ShaderResources::IsCompatibleWith(const ShaderResources &Res)const bool IsCompatible = true; ProcessResources( - nullptr, 0, - [&](const D3DShaderResourceAttribs& CB, Uint32 n) { if (!CB.IsCompatibleWith(Res.GetCB(n))) @@ -216,7 +163,6 @@ size_t ShaderResources::GetHash()const { size_t hash = ComputeHash(GetNumCBs(), GetNumTexSRV(), GetNumTexUAV(), GetNumBufSRV(), GetNumBufUAV(), GetNumSamplers()); ProcessResources( - nullptr, 0, [&](const D3DShaderResourceAttribs& CB, Uint32) { HashCombine(hash, CB); -- cgit v1.2.3 From f57fc688a659087f5e85d8533f61c4a1fd914810 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 2 Mar 2019 22:43:26 -0800 Subject: Reworked ShaderD3D11Impl to comply with the updated API --- .../include/ShaderVariableD3DBase.h | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'Graphics/GraphicsEngineD3DBase') diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderVariableD3DBase.h b/Graphics/GraphicsEngineD3DBase/include/ShaderVariableD3DBase.h index 48748a6c..ae2d2728 100644 --- a/Graphics/GraphicsEngineD3DBase/include/ShaderVariableD3DBase.h +++ b/Graphics/GraphicsEngineD3DBase/include/ShaderVariableD3DBase.h @@ -33,9 +33,12 @@ namespace Diligent template struct ShaderVariableD3DBase : public IShaderResourceVariable { - ShaderVariableD3DBase(TShaderResourceLayout& ParentResLayout, const D3DShaderResourceAttribs& ResourcesAttribs) : - m_ParentResLayout(ParentResLayout), - Attribs(ResourcesAttribs) + ShaderVariableD3DBase(TShaderResourceLayout& ParentResLayout, + const D3DShaderResourceAttribs& Attribs, + SHADER_RESOURCE_VARIABLE_TYPE VariableType) : + m_ParentResLayout (ParentResLayout), + m_Attribs (Attribs), + m_VariableType (VariableType) { } @@ -60,7 +63,7 @@ namespace Diligent return; *ppInterface = nullptr; - if( IID == IID_ShaderVariable || IID == IID_Unknown ) + if( IID == IID_ShaderResourceVariable || IID == IID_Unknown ) { *ppInterface = this; (*ppInterface)->AddRef(); @@ -69,17 +72,17 @@ namespace Diligent virtual SHADER_RESOURCE_VARIABLE_TYPE GetType()const override final { - return Attribs.GetVariableType(); + return m_VariableType; } virtual Uint32 GetArraySize()const override final { - return Attribs.BindCount; + return m_Attribs.BindCount; } virtual const Char* GetName()const override final { - return Attribs.Name; + return m_Attribs.Name; } virtual Uint32 GetIndex()const override final @@ -87,9 +90,10 @@ namespace Diligent return m_ParentResLayout.GetVariableIndex(*this); } - const D3DShaderResourceAttribs& Attribs; + const D3DShaderResourceAttribs& m_Attribs; protected: - TShaderResourceLayout& m_ParentResLayout; + TShaderResourceLayout& m_ParentResLayout; + const SHADER_RESOURCE_VARIABLE_TYPE m_VariableType; }; } -- cgit v1.2.3 From 91dbc2c143a371bf0584e6c6a3cd2feb16535f8b Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 2 Mar 2019 23:01:28 -0800 Subject: Reworked ShaderResourcesD3D11 to comply with the updated API --- Graphics/GraphicsEngineD3DBase/include/ShaderResources.h | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'Graphics/GraphicsEngineD3DBase') diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h index aa3867f3..97fe7490 100644 --- a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h +++ b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h @@ -325,7 +325,7 @@ protected: typename TNewResourceHandler> void Initialize(ID3DBlob* pShaderByteCode, TNewResourceHandler NewResHandler, - const ShaderDesc& ShdrDesc, + const Char* ShaderName, const Char* SamplerSuffix); @@ -382,8 +382,8 @@ template void ShaderResources::Initialize(ID3DBlob* pShaderByteCode, - TNewResourceHandler NewResHandler, - const ShaderDesc& ShdrDesc, + TNewResourceHandler NewResHandler, + const Char* ShaderName, const Char* CombinedSamplerSuffix) { Uint32 CurrCB = 0, CurrTexSRV = 0, CurrTexUAV = 0, CurrBufSRV = 0, CurrBufUAV = 0, CurrSampler = 0; @@ -446,10 +446,7 @@ void ShaderResources::Initialize(ID3DBlob* pShaderByteCode, } ++CurrTexSRV; NewResHandler.OnNewTexSRV(*pNewTexSRV); - }, - - ShdrDesc, - CombinedSamplerSuffix); + }); if (CombinedSamplerSuffix != nullptr) { @@ -460,7 +457,7 @@ void ShaderResources::Initialize(ID3DBlob* pShaderByteCode, { const auto& Sampler = GetSampler(n); if (!Sampler.ValidTexSRVAssigned()) - LOG_ERROR_MESSAGE("Shader '", ShdrDesc.Name, "' uses combined texture samplers, but sampler '", Sampler.Name, "' is not assigned to any texture"); + LOG_ERROR_MESSAGE("Shader '", ShaderName, "' uses combined texture samplers, but sampler '", Sampler.Name, "' is not assigned to any texture"); } #endif } -- cgit v1.2.3 From 6f479d4aa4e49b1ac4b1a6fbfc8b52dbd3bf58d6 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sat, 2 Mar 2019 23:57:57 -0800 Subject: Partially reworked ShaderResourceLayoutD3D11 --- .../include/ShaderResources.h | 11 +++ .../GraphicsEngineD3DBase/src/ShaderResources.cpp | 78 ++++++++++++++++++++++ 2 files changed, 89 insertions(+) (limited to 'Graphics/GraphicsEngineD3DBase') diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h index 97fe7490..64c8fe61 100644 --- a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h +++ b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h @@ -62,6 +62,7 @@ #include "HashUtils.h" #include "StringPool.h" #include "D3DShaderResourceLoader.h" +#include "PipelineState.h" namespace Diligent { @@ -223,6 +224,10 @@ public: SamplerOrTexSRVId == Attribs.SamplerOrTexSRVId; } + SHADER_RESOURCE_VARIABLE_TYPE FindVariableType(SHADER_TYPE ShaderType, + const PipelineResourceLayoutDesc& ResourceLayoutDesc, + const char* CombinedSamplerSuffix)const; + size_t GetHash()const { return ComputeHash(BindPoint, BindCount, InputType, SRVDimension, SamplerOrTexSRVId); @@ -318,6 +323,12 @@ public: size_t GetHash()const; + D3DShaderResourceCounters CountResources(const PipelineResourceLayoutDesc& ResourceLayout, + SHADER_TYPE ShaderStage, + const char* CombinedSamplerSuffix, + const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, + Uint32 NumAllowedTypes)const noexcept; + protected: template Date: Sun, 3 Mar 2019 18:09:24 -0800 Subject: Almost completed refactoring d3d11 backend to comply with the new API --- .../include/ShaderResources.h | 99 +++++---- .../GraphicsEngineD3DBase/src/ShaderResources.cpp | 230 +++++++++++++++------ 2 files changed, 227 insertions(+), 102 deletions(-) (limited to 'Graphics/GraphicsEngineD3DBase') diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h index 64c8fe61..4c38c4f5 100644 --- a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h +++ b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h @@ -67,33 +67,35 @@ namespace Diligent { +// sizeof(D3DShaderResourceAttribs) == 16 (x64) struct D3DShaderResourceAttribs { - const char* const Name; +/* 0 */ const char* const Name; - const Uint16 BindPoint; - const Uint16 BindCount; +/* 8 */ const Uint16 BindPoint; +/*10 */ const Uint16 BindCount; private: // 4 4 24 // bit | 0 1 2 3 | 4 5 6 7 | 8 9 10 ... 31 | // | | | | // | InputType | SRV Dim | SamplerOrTexSRVIdBits | - static constexpr const Uint32 ShaderInputTypeBits = 4; - static constexpr const Uint32 SRVDimBits = 4; - static constexpr const Uint32 SamplerOrTexSRVIdBits = 24; + static constexpr const Uint32 ShaderInputTypeBits = 4; + static constexpr const Uint32 SRVDimBits = 4; + static constexpr const Uint32 SamplerOrTexSRVIdBits = 24; static_assert(ShaderInputTypeBits + SRVDimBits + SamplerOrTexSRVIdBits == 32, "Attributes are better be packed into 32 bits"); static_assert(D3D_SIT_UAV_RWSTRUCTURED_WITH_COUNTER < (1 << ShaderInputTypeBits), "Not enough bits to represent D3D_SHADER_INPUT_TYPE"); static_assert(D3D_SRV_DIMENSION_BUFFEREX < (1 << SRVDimBits), "Not enough bits to represent D3D_SRV_DIMENSION"); - // We need to use Uint32 instead of the actual type for reliability and correctness. - // There originally was a problem when the type of InputType was D3D_SHADER_INPUT_TYPE: - // the value of D3D_SIT_UAV_RWBYTEADDRESS (8) was interpreted as -8 (as the underlying enum type - // is signed) causing errors - const Uint32 InputType : ShaderInputTypeBits; // Max value: D3D_SIT_UAV_RWSTRUCTURED_WITH_COUNTER == 11 - const Uint32 SRVDimension : SRVDimBits; // Max value: D3D_SRV_DIMENSION_BUFFEREX == 11 - Uint32 SamplerOrTexSRVId : SamplerOrTexSRVIdBits; // Max value: 1048575 + // We need to use Uint32 instead of the actual type for reliability and correctness. + // There originally was a problem when the type of InputType was D3D_SHADER_INPUT_TYPE: + // the value of D3D_SIT_UAV_RWBYTEADDRESS (8) was interpreted as -8 (as the underlying enum type + // is signed) causing errors +/*12.0*/ const Uint32 InputType : ShaderInputTypeBits; // Max value: D3D_SIT_UAV_RWSTRUCTURED_WITH_COUNTER == 11 +/*12.4*/ const Uint32 SRVDimension : SRVDimBits; // Max value: D3D_SRV_DIMENSION_BUFFEREX == 11 +/*13.0*/ Uint32 SamplerOrTexSRVId : SamplerOrTexSRVIdBits; // Max value: 2^24-1 +/*16 */ // End of structure public: static constexpr const Uint32 InvalidSamplerId = (1 << SamplerOrTexSRVIdBits) - 1; @@ -224,10 +226,6 @@ public: SamplerOrTexSRVId == Attribs.SamplerOrTexSRVId; } - SHADER_RESOURCE_VARIABLE_TYPE FindVariableType(SHADER_TYPE ShaderType, - const PipelineResourceLayoutDesc& ResourceLayoutDesc, - const char* CombinedSamplerSuffix)const; - size_t GetHash()const { return ComputeHash(BindPoint, BindCount, InputType, SRVDimension, SamplerOrTexSRVId); @@ -240,33 +238,37 @@ static_assert(sizeof(D3DShaderResourceAttribs) == sizeof(void*) + sizeof(Uint32) class ShaderResources { public: - ShaderResources(SHADER_TYPE ShaderType); + ShaderResources(SHADER_TYPE ShaderType)noexcept : + m_ShaderType(ShaderType) + { + } - ShaderResources (const ShaderResources&) = delete; - ShaderResources (ShaderResources&&) = delete; - ShaderResources& operator = (const ShaderResources&) = delete; - ShaderResources& operator = (ShaderResources&&) = delete; + ShaderResources (const ShaderResources&) = delete; + ShaderResources ( ShaderResources&&) = delete; + ShaderResources& operator = (const ShaderResources&) = delete; + ShaderResources& operator = ( ShaderResources&&) = delete; ~ShaderResources(); - Uint32 GetNumCBs() const noexcept{ return (m_TexSRVOffset - 0); } - Uint32 GetNumTexSRV() const noexcept{ return (m_TexUAVOffset - m_TexSRVOffset); } - Uint32 GetNumTexUAV() const noexcept{ return (m_BufSRVOffset - m_TexUAVOffset); } - Uint32 GetNumBufSRV() const noexcept{ return (m_BufUAVOffset - m_BufSRVOffset); } - Uint32 GetNumBufUAV() const noexcept{ return (m_SamplersOffset - m_BufUAVOffset); } - Uint32 GetNumSamplers() const noexcept{ return (m_TotalResources - m_SamplersOffset); } - Uint32 GetTotalResources()const noexcept{ return m_TotalResources; } - - const D3DShaderResourceAttribs& GetCB (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumCBs(), 0); } - const D3DShaderResourceAttribs& GetTexSRV (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumTexSRV(), m_TexSRVOffset); } - const D3DShaderResourceAttribs& GetTexUAV (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumTexUAV(), m_TexUAVOffset); } - const D3DShaderResourceAttribs& GetBufSRV (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumBufSRV(), m_BufSRVOffset); } - const D3DShaderResourceAttribs& GetBufUAV (Uint32 n)const noexcept{ return GetResAttribs(n, GetNumBufUAV(), m_BufUAVOffset); } - const D3DShaderResourceAttribs& GetSampler(Uint32 n)const noexcept{ return GetResAttribs(n, GetNumSamplers(), m_SamplersOffset); } + Uint32 GetNumCBs() const noexcept { return (m_TexSRVOffset - 0); } + Uint32 GetNumTexSRV() const noexcept { return (m_TexUAVOffset - m_TexSRVOffset); } + Uint32 GetNumTexUAV() const noexcept { return (m_BufSRVOffset - m_TexUAVOffset); } + Uint32 GetNumBufSRV() const noexcept { return (m_BufUAVOffset - m_BufSRVOffset); } + Uint32 GetNumBufUAV() const noexcept { return (m_SamplersOffset - m_BufUAVOffset); } + Uint32 GetNumSamplers() const noexcept { return (m_TotalResources - m_SamplersOffset); } + Uint32 GetTotalResources()const noexcept { return m_TotalResources; } + + const D3DShaderResourceAttribs& GetCB (Uint32 n)const noexcept { return GetResAttribs(n, GetNumCBs(), 0); } + const D3DShaderResourceAttribs& GetTexSRV (Uint32 n)const noexcept { return GetResAttribs(n, GetNumTexSRV(), m_TexSRVOffset); } + const D3DShaderResourceAttribs& GetTexUAV (Uint32 n)const noexcept { return GetResAttribs(n, GetNumTexUAV(), m_TexUAVOffset); } + const D3DShaderResourceAttribs& GetBufSRV (Uint32 n)const noexcept { return GetResAttribs(n, GetNumBufSRV(), m_BufSRVOffset); } + const D3DShaderResourceAttribs& GetBufUAV (Uint32 n)const noexcept { return GetResAttribs(n, GetNumBufUAV(), m_BufUAVOffset); } + const D3DShaderResourceAttribs& GetSampler(Uint32 n)const noexcept { return GetResAttribs(n, GetNumSamplers(), m_SamplersOffset); } SHADER_TYPE GetShaderType()const noexcept{return m_ShaderType;} - // Processes only resources listed in AllowedVarTypes + ShaderResourceDesc GetShaderResourceDesc(Uint32 Index)const; + template > m_MemoryBuffer; StringPool m_ResourceNames; - const char* m_SamplerSuffix = nullptr; // The suffix is put into the m_ResourceNames + const char* m_SamplerSuffix = nullptr; // The suffix and the shader name + const char* m_ShaderName = nullptr; // are put into the m_ResourceNames // Offsets in elements of D3DShaderResourceAttribs typedef Uint16 OffsetType; @@ -384,7 +395,7 @@ private: OffsetType m_SamplersOffset = 0; OffsetType m_TotalResources = 0; - SHADER_TYPE m_ShaderType = SHADER_TYPE_UNKNOWN; + const SHADER_TYPE m_ShaderType; }; @@ -403,6 +414,9 @@ void ShaderResources::Initialize(ID3DBlob* pShaderByteCode, [&](const D3DShaderResourceCounters& ResCounters, size_t ResourceNamesPoolSize) { + VERIFY_EXPR(ShaderName != nullptr); + ResourceNamesPoolSize += strlen(ShaderName)+1; + if (CombinedSamplerSuffix != nullptr) ResourceNamesPoolSize += strlen(CombinedSamplerSuffix)+1; @@ -459,6 +473,8 @@ void ShaderResources::Initialize(ID3DBlob* pShaderByteCode, NewResHandler.OnNewTexSRV(*pNewTexSRV); }); + m_ShaderName = m_ResourceNames.CopyString(ShaderName); + if (CombinedSamplerSuffix != nullptr) { m_SamplerSuffix = m_ResourceNames.CopyString(CombinedSamplerSuffix); @@ -504,4 +520,3 @@ namespace std } }; } - diff --git a/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp b/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp index 97107560..613cd96a 100644 --- a/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp +++ b/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp @@ -31,25 +31,6 @@ namespace Diligent { -SHADER_RESOURCE_VARIABLE_TYPE D3DShaderResourceAttribs::FindVariableType(SHADER_TYPE ShaderType, - const PipelineResourceLayoutDesc& ResourceLayoutDesc, - const char* CombinedSamplerSuffix)const -{ - if (GetInputType() == D3D_SIT_SAMPLER) - { - // Only use CombinedSamplerSuffix when looking for the sampler variable type - return GetShaderVariableType(ShaderType, ResourceLayoutDesc.DefaultVariableType, ResourceLayoutDesc.Variables, ResourceLayoutDesc.NumVariables, - [&](const char* VarName) - { - return StreqSuff(Name, VarName, CombinedSamplerSuffix); - }); - } - else - { - return GetShaderVariableType(ShaderType, Name, ResourceLayoutDesc); - } -} - ShaderResources::~ShaderResources() { for(Uint32 n=0; n < GetNumCBs(); ++n) @@ -85,7 +66,7 @@ void ShaderResources::AllocateMemory(IMemoryAllocator& Allocator, return Offset; }; - auto CBOffset = AdvanceOffset(ResCounters.NumCBs); CBOffset; // To suppress warning + auto CBOffset = AdvanceOffset(ResCounters.NumCBs); (void)CBOffset; // To suppress warning m_TexSRVOffset = AdvanceOffset(ResCounters.NumTexSRVs); m_TexUAVOffset = AdvanceOffset(ResCounters.NumTexUAVs); m_BufSRVOffset = AdvanceOffset(ResCounters.NumBufSRVs); @@ -111,15 +92,41 @@ void ShaderResources::AllocateMemory(IMemoryAllocator& Allocator, } } -ShaderResources::ShaderResources(SHADER_TYPE ShaderType): - m_ShaderType(ShaderType) +SHADER_RESOURCE_VARIABLE_TYPE ShaderResources::FindVariableType(const D3DShaderResourceAttribs& ResourceAttribs, + const PipelineResourceLayoutDesc& ResourceLayout)const { + if (ResourceAttribs.GetInputType() == D3D_SIT_SAMPLER) + { + // Only use CombinedSamplerSuffix when looking for the sampler variable type + return GetShaderVariableType(m_ShaderType, ResourceLayout.DefaultVariableType, ResourceLayout.Variables, ResourceLayout.NumVariables, + [&](const char* VarName) + { + return StreqSuff(ResourceAttribs.Name, VarName, m_SamplerSuffix); + }); + } + else + { + return GetShaderVariableType(m_ShaderType, ResourceAttribs.Name, ResourceLayout); + } +} + +Int32 ShaderResources::FindStaticSampler(const D3DShaderResourceAttribs& ResourceAttribs, + const PipelineResourceLayoutDesc& ResourceLayoutDesc)const +{ + VERIFY(ResourceAttribs.GetInputType() == D3D_SIT_SAMPLER, "Sampler is expected"); + + for (Uint32 s=0; s < ResourceLayoutDesc.NumStaticSamplers; ++s) + { + const auto& StSam = ResourceLayoutDesc.StaticSamplers[s]; + if ( ((StSam.ShaderStages & m_ShaderType) != 0) && StreqSuff(ResourceAttribs.Name, StSam.SamplerOrTextureName, m_SamplerSuffix) ) + return s; + } + + return -1; } D3DShaderResourceCounters ShaderResources::CountResources(const PipelineResourceLayoutDesc& ResourceLayout, - SHADER_TYPE ShaderStage, - const char* CombinedSamplerSuffix, const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, Uint32 NumAllowedTypes)const noexcept { @@ -129,41 +136,37 @@ D3DShaderResourceCounters ShaderResources::CountResources(const PipelineResource ProcessResources( [&](const D3DShaderResourceAttribs& CB, Uint32) { - auto VarType = CB.FindVariableType(ShaderStage, ResourceLayout, CombinedSamplerSuffix); + auto VarType = FindVariableType(CB, ResourceLayout); if (IsAllowedType(VarType, AllowedTypeBits)) ++Counters.NumCBs; }, [&](const D3DShaderResourceAttribs& Sam, Uint32) { - auto VarType = Sam.FindVariableType(ShaderStage, ResourceLayout, CombinedSamplerSuffix); + auto VarType = FindVariableType(Sam, ResourceLayout); if (IsAllowedType(VarType, AllowedTypeBits)) - { - // Skip static samplers - //if (!Sam.IsStaticSampler()) ++Counters.NumSamplers; - } }, [&](const D3DShaderResourceAttribs& TexSRV, Uint32) { - auto VarType = TexSRV.FindVariableType(ShaderStage, ResourceLayout, CombinedSamplerSuffix); + auto VarType = FindVariableType(TexSRV, ResourceLayout); if (IsAllowedType(VarType, AllowedTypeBits)) ++Counters.NumTexSRVs; }, [&](const D3DShaderResourceAttribs& TexUAV, Uint32) { - auto VarType = TexUAV.FindVariableType(ShaderStage, ResourceLayout, CombinedSamplerSuffix); + auto VarType = FindVariableType(TexUAV, ResourceLayout); if (IsAllowedType(VarType, AllowedTypeBits)) ++Counters.NumTexUAVs; }, [&](const D3DShaderResourceAttribs& BufSRV, Uint32) { - auto VarType = BufSRV.FindVariableType(ShaderStage, ResourceLayout, CombinedSamplerSuffix); + auto VarType = FindVariableType(BufSRV, ResourceLayout); if (IsAllowedType(VarType, AllowedTypeBits)) ++Counters.NumBufSRVs; }, [&](const D3DShaderResourceAttribs& BufUAV, Uint32) { - auto VarType = BufUAV.FindVariableType(ShaderStage, ResourceLayout, CombinedSamplerSuffix); + auto VarType = FindVariableType(BufUAV, ResourceLayout); if (IsAllowedType(VarType, AllowedTypeBits)) ++Counters.NumBufUAVs; } @@ -172,6 +175,82 @@ D3DShaderResourceCounters ShaderResources::CountResources(const PipelineResource return Counters; } +#ifdef DEVELOPMENT +void ShaderResources::DvpVerifyResourceLayout(const PipelineResourceLayoutDesc& ResourceLayout)const +{ + const auto UseCombinedTextureSamplers = IsUsingCombinedTextureSamplers(); + for (Uint32 v = 0; v < ResourceLayout.NumVariables; ++v) + { + const auto& VarDesc = ResourceLayout.Variables[v]; + if (VarDesc.ShaderStages == SHADER_TYPE_UNKNOWN) + { + LOG_WARNING_MESSAGE("No allowed shader stages are specified for ", GetShaderVariableTypeLiteralName(VarDesc.Type), " variable '", VarDesc.Name, "'."); + continue; + } + + if( (VarDesc.ShaderStages & m_ShaderType) == 0) + continue; + + bool VariableFound = false; + for (Uint32 n=0; n < m_TotalResources && !VariableFound; ++n) + { + const auto& Res = GetResAttribs(n, m_TotalResources, 0); + + // Skip samplers if combined texture samplers are used as + // in this case they are not treated as independent variables + if (UseCombinedTextureSamplers && Res.GetInputType() == D3D_SIT_SAMPLER) + continue; + + VariableFound = (strcmp(Res.Name, VarDesc.Name) == 0); + } + + if(!VariableFound) + { + LOG_WARNING_MESSAGE("Variable '", VarDesc.Name, "' is not found in shader '", m_ShaderName, '\''); + } + } + + for (Uint32 s = 0; s < ResourceLayout.NumStaticSamplers; ++s) + { + const auto& StSamDesc = ResourceLayout.StaticSamplers[s]; + if (StSamDesc.ShaderStages == SHADER_TYPE_UNKNOWN) + { + LOG_WARNING_MESSAGE("No allowed shader stages are specified for static sampler '", StSamDesc.SamplerOrTextureName, "'."); + continue; + } + + if ( (StSamDesc.ShaderStages & m_ShaderType) == 0) + continue; + + const auto* TexOrSamName = StSamDesc.SamplerOrTextureName; + + if (UseCombinedTextureSamplers) + { + bool TextureFound = false; + for(Uint32 n=0; n < GetNumTexSRV() && !TextureFound; ++n) + { + const auto& TexSRV = GetTexSRV(n); + TextureFound = (strcmp(TexSRV.Name, TexOrSamName) == 0); + } + if (!TextureFound) + { + LOG_WARNING_MESSAGE("Static sampler specifies a texture '", TexOrSamName, "' that is not found in shader '", m_ShaderName, '\''); + } + } + else + { + bool SamplerFound = false; + for(Uint32 n=0; n < GetNumSamplers() && !SamplerFound; ++n) + { + const auto& Sampler = GetSampler(n); + SamplerFound = (strcmp(Sampler.Name, TexOrSamName) == 0); + } + if (!SamplerFound) + LOG_WARNING_MESSAGE("Static sampler '", TexOrSamName, "' is not found in shader '", m_ShaderName, '\''); + } + } +} +#endif Uint32 ShaderResources::FindAssignedSamplerId(const D3DShaderResourceAttribs& TexSRV, const char* SamplerSuffix)const @@ -237,35 +316,66 @@ bool ShaderResources::IsCompatibleWith(const ShaderResources &Res)const return IsCompatible; } -size_t ShaderResources::GetHash()const +ShaderResourceDesc ShaderResources::GetShaderResourceDesc(Uint32 Index)const { - size_t hash = ComputeHash(GetNumCBs(), GetNumTexSRV(), GetNumTexUAV(), GetNumBufSRV(), GetNumBufUAV(), GetNumSamplers()); - ProcessResources( - [&](const D3DShaderResourceAttribs& CB, Uint32) - { - HashCombine(hash, CB); - }, - [&](const D3DShaderResourceAttribs& Sam, Uint32) - { - HashCombine(hash, Sam); - }, - [&](const D3DShaderResourceAttribs& TexSRV, Uint32) - { - HashCombine(hash, TexSRV); - }, - [&](const D3DShaderResourceAttribs& TexUAV, Uint32) - { - HashCombine(hash, TexUAV); - }, - [&](const D3DShaderResourceAttribs& BufSRV, Uint32) - { - HashCombine(hash, BufSRV); - }, - [&](const D3DShaderResourceAttribs& BufUAV, Uint32) + DEV_CHECK_ERR(Index < m_TotalResources, "Resource index (", Index, ") is out of range"); + ShaderResourceDesc ResourceDesc; + if (Index < m_TotalResources) + { + const auto& Res = GetResAttribs(Index, 0, m_TotalResources); + ResourceDesc.Name = Res.Name; + ResourceDesc.ArraySize = Res.BindCount; + switch(Res.GetInputType()) { - HashCombine(hash, BufUAV); + case D3D_SIT_CBUFFER: + ResourceDesc.Type = SHADER_RESOURCE_TYPE_CONSTANT_BUFFER; + break; + + case D3D_SIT_TBUFFER: + UNSUPPORTED( "TBuffers are not supported" ); + ResourceDesc.Type = SHADER_RESOURCE_TYPE_UNKNOWN; + break; + + case D3D_SIT_TEXTURE: + ResourceDesc.Type = (Res.GetSRVDimension() == D3D_SRV_DIMENSION_BUFFER ? SHADER_RESOURCE_TYPE_BUFFER_SRV : SHADER_RESOURCE_TYPE_TEXTURE_SRV); + break; + + case D3D_SIT_SAMPLER: + ResourceDesc.Type = SHADER_RESOURCE_TYPE_SAMPLER; + break; + + case D3D_SIT_UAV_RWTYPED: + ResourceDesc.Type = (Res.GetSRVDimension() == D3D_SRV_DIMENSION_BUFFER ? SHADER_RESOURCE_TYPE_BUFFER_UAV : SHADER_RESOURCE_TYPE_TEXTURE_UAV); + break; + + case D3D_SIT_STRUCTURED: + case D3D_SIT_BYTEADDRESS: + ResourceDesc.Type = SHADER_RESOURCE_TYPE_BUFFER_SRV; + break; + + case D3D_SIT_UAV_RWSTRUCTURED: + case D3D_SIT_UAV_RWBYTEADDRESS: + case D3D_SIT_UAV_APPEND_STRUCTURED: + case D3D_SIT_UAV_CONSUME_STRUCTURED: + case D3D_SIT_UAV_RWSTRUCTURED_WITH_COUNTER: + ResourceDesc.Type = SHADER_RESOURCE_TYPE_BUFFER_UAV; + break; + + default: + UNEXPECTED("Unknown input type"); } - ); + } + return ResourceDesc; +} + +size_t ShaderResources::GetHash()const +{ + size_t hash = ComputeHash(GetNumCBs(), GetNumTexSRV(), GetNumTexUAV(), GetNumBufSRV(), GetNumBufUAV(), GetNumSamplers()); + for (Uint32 n=0; n < m_TotalResources; ++n) + { + const auto& Res = GetResAttribs(n, m_TotalResources, 0); + HashCombine(hash, Res); + } return hash; } -- cgit v1.2.3 From c3ce1b6cc0214c0c67ac6c75b742b92b18af30d8 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Sun, 3 Mar 2019 20:50:04 -0800 Subject: Final changes to complete d3d11 backend refactor --- .../include/ShaderResources.h | 7 +- .../GraphicsEngineD3DBase/src/ShaderResources.cpp | 95 +++++++++++++--------- 2 files changed, 62 insertions(+), 40 deletions(-) (limited to 'Graphics/GraphicsEngineD3DBase') diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h index 4c38c4f5..7bfd2cfd 100644 --- a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h +++ b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h @@ -334,9 +334,12 @@ public: D3DShaderResourceCounters CountResources(const PipelineResourceLayoutDesc& ResourceLayout, const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes)const noexcept; + Uint32 NumAllowedTypes, + bool CountStaticSamplers)const noexcept; #ifdef DEVELOPMENT - void DvpVerifyResourceLayout(const PipelineResourceLayoutDesc& ResourceLayout)const; + static void DvpVerifyResourceLayout(const PipelineResourceLayoutDesc& ResourceLayout, + const ShaderResources* const pShaderResources[], + Uint32 NumShaders); #endif protected: diff --git a/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp b/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp index 613cd96a..a3efe9f3 100644 --- a/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp +++ b/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp @@ -128,7 +128,8 @@ Int32 ShaderResources::FindStaticSampler(const D3DShaderResourceAttribs& Resou D3DShaderResourceCounters ShaderResources::CountResources(const PipelineResourceLayoutDesc& ResourceLayout, const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes, - Uint32 NumAllowedTypes)const noexcept + Uint32 NumAllowedTypes, + bool CountStaticSamplers)const noexcept { auto AllowedTypeBits = GetAllowedTypeBits(AllowedVarTypes, NumAllowedTypes); @@ -144,7 +145,14 @@ D3DShaderResourceCounters ShaderResources::CountResources(const PipelineResource { auto VarType = FindVariableType(Sam, ResourceLayout); if (IsAllowedType(VarType, AllowedTypeBits)) - ++Counters.NumSamplers; + { + if (!CountStaticSamplers) + { + if (FindStaticSampler(Sam, ResourceLayout) >= 0) + return; // Skip static sampler if requested + } + ++Counters.NumSamplers; + } }, [&](const D3DShaderResourceAttribs& TexSRV, Uint32) { @@ -176,9 +184,10 @@ D3DShaderResourceCounters ShaderResources::CountResources(const PipelineResource } #ifdef DEVELOPMENT -void ShaderResources::DvpVerifyResourceLayout(const PipelineResourceLayoutDesc& ResourceLayout)const +void ShaderResources::DvpVerifyResourceLayout(const PipelineResourceLayoutDesc& ResourceLayout, + const ShaderResources* const pShaderResources[], + Uint32 NumShaders) { - const auto UseCombinedTextureSamplers = IsUsingCombinedTextureSamplers(); for (Uint32 v = 0; v < ResourceLayout.NumVariables; ++v) { const auto& VarDesc = ResourceLayout.Variables[v]; @@ -188,65 +197,75 @@ void ShaderResources::DvpVerifyResourceLayout(const PipelineResourceLayoutDesc& continue; } - if( (VarDesc.ShaderStages & m_ShaderType) == 0) - continue; - bool VariableFound = false; - for (Uint32 n=0; n < m_TotalResources && !VariableFound; ++n) + for (Uint32 s = 0; s < NumShaders && !VariableFound; ++s) { - const auto& Res = GetResAttribs(n, m_TotalResources, 0); - - // Skip samplers if combined texture samplers are used as - // in this case they are not treated as independent variables - if (UseCombinedTextureSamplers && Res.GetInputType() == D3D_SIT_SAMPLER) + const auto& Resources = *pShaderResources[s]; + if( (VarDesc.ShaderStages & Resources.GetShaderType()) == 0) continue; + + const auto UseCombinedTextureSamplers = Resources.IsUsingCombinedTextureSamplers(); + for (Uint32 n=0; n < Resources.m_TotalResources && !VariableFound; ++n) + { + const auto& Res = Resources.GetResAttribs(n, Resources.m_TotalResources, 0); - VariableFound = (strcmp(Res.Name, VarDesc.Name) == 0); + // Skip samplers if combined texture samplers are used as + // in this case they are not treated as independent variables + if (UseCombinedTextureSamplers && Res.GetInputType() == D3D_SIT_SAMPLER) + continue; + + VariableFound = (strcmp(Res.Name, VarDesc.Name) == 0); + } } - if(!VariableFound) + if (!VariableFound) { - LOG_WARNING_MESSAGE("Variable '", VarDesc.Name, "' is not found in shader '", m_ShaderName, '\''); + LOG_WARNING_MESSAGE("Variable '", VarDesc.Name, "' is not found in of the designated shader stages " + "(", GetShaderStagesString(VarDesc.ShaderStages), ")"); } } - for (Uint32 s = 0; s < ResourceLayout.NumStaticSamplers; ++s) + for (Uint32 sam = 0; sam < ResourceLayout.NumStaticSamplers; ++sam) { - const auto& StSamDesc = ResourceLayout.StaticSamplers[s]; + const auto& StSamDesc = ResourceLayout.StaticSamplers[sam]; if (StSamDesc.ShaderStages == SHADER_TYPE_UNKNOWN) { LOG_WARNING_MESSAGE("No allowed shader stages are specified for static sampler '", StSamDesc.SamplerOrTextureName, "'."); continue; } - if ( (StSamDesc.ShaderStages & m_ShaderType) == 0) - continue; - const auto* TexOrSamName = StSamDesc.SamplerOrTextureName; - - if (UseCombinedTextureSamplers) + + bool TextureOrSamplerFound = false; + for (Uint32 s = 0; s < NumShaders && !TextureOrSamplerFound; ++s) { - bool TextureFound = false; - for(Uint32 n=0; n < GetNumTexSRV() && !TextureFound; ++n) + const auto& Resources = *pShaderResources[s]; + if ( (StSamDesc.ShaderStages & Resources.GetShaderType()) == 0) + continue; + + const auto UseCombinedTextureSamplers = Resources.IsUsingCombinedTextureSamplers(); + if (UseCombinedTextureSamplers) { - const auto& TexSRV = GetTexSRV(n); - TextureFound = (strcmp(TexSRV.Name, TexOrSamName) == 0); + for(Uint32 n=0; n < Resources.GetNumTexSRV() && !TextureOrSamplerFound; ++n) + { + const auto& TexSRV = Resources.GetTexSRV(n); + TextureOrSamplerFound = (strcmp(TexSRV.Name, TexOrSamName) == 0); + } } - if (!TextureFound) + else { - LOG_WARNING_MESSAGE("Static sampler specifies a texture '", TexOrSamName, "' that is not found in shader '", m_ShaderName, '\''); + for(Uint32 n=0; n < Resources.GetNumSamplers() && !TextureOrSamplerFound; ++n) + { + const auto& Sampler = Resources.GetSampler(n); + TextureOrSamplerFound = (strcmp(Sampler.Name, TexOrSamName) == 0); + } } } - else + + if (!TextureOrSamplerFound) { - bool SamplerFound = false; - for(Uint32 n=0; n < GetNumSamplers() && !SamplerFound; ++n) - { - const auto& Sampler = GetSampler(n); - SamplerFound = (strcmp(Sampler.Name, TexOrSamName) == 0); - } - if (!SamplerFound) - LOG_WARNING_MESSAGE("Static sampler '", TexOrSamName, "' is not found in shader '", m_ShaderName, '\''); + LOG_WARNING_MESSAGE("Static sampler '", TexOrSamName, "' is not found in any of the designated shader stages " + "(", GetShaderStagesString(StSamDesc.ShaderStages), ")"); } } } -- cgit v1.2.3 From c72191a465edd01905181aa47cc57e47fcc29b9e Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Mon, 4 Mar 2019 19:17:32 -0800 Subject: Completed D3D12 back-end refactor to comply with the new API --- .../include/ShaderResources.h | 66 ++++++++++++++-------- 1 file changed, 41 insertions(+), 25 deletions(-) (limited to 'Graphics/GraphicsEngineD3DBase') diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h index 7bfd2cfd..f8f6917b 100644 --- a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h +++ b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h @@ -126,7 +126,7 @@ public: VERIFY(_SamplerId < (1 << SamplerOrTexSRVIdBits), "SamplerOrTexSRVId is out of representable range"); if (_InputType == D3D_SIT_TEXTURE && _SRVDimension != D3D_SRV_DIMENSION_BUFFER) - VERIFY_EXPR(GetSamplerId() == _SamplerId); + VERIFY_EXPR(GetCombinedSamplerId() == _SamplerId); else VERIFY(_SamplerId == InvalidSamplerId, "Only texture SRV can be assigned a valid texture sampler"); #endif @@ -174,33 +174,14 @@ public: return static_cast(SRVDimension); } - Uint32 GetSamplerId()const + bool IsCombinedWithSampler()const { - VERIFY(GetInputType() == D3D_SIT_TEXTURE && GetSRVDimension() != D3D_SRV_DIMENSION_BUFFER, "Invalid input type: D3D_SIT_TEXTURE is expected" ); - return SamplerOrTexSRVId; - } - - void SetTexSRVId(Uint32 TexSRVId) - { - VERIFY(GetInputType() == D3D_SIT_SAMPLER, "Invalid input type: D3D_SIT_SAMPLER is expected" ); - VERIFY(TexSRVId < (1 << SamplerOrTexSRVIdBits), "TexSRVId (", TexSRVId, ") is out of representable range"); - SamplerOrTexSRVId = TexSRVId; - } - - Uint32 GetTexSRVId()const - { - VERIFY(GetInputType() == D3D_SIT_SAMPLER, "Invalid input type: D3D_SIT_SAMPLER is expected" ); - return SamplerOrTexSRVId; - } - - bool ValidSamplerAssigned()const - { - return GetSamplerId() != InvalidSamplerId; + return GetCombinedSamplerId() != InvalidSamplerId; } - bool ValidTexSRVAssigned()const + bool IsCombinedWithTexSRV()const { - return GetTexSRVId() != InvalidTexSRVId; + return GetCombinedTexSRVId() != InvalidTexSRVId; } bool IsValidBindPoint()const @@ -230,6 +211,29 @@ public: { return ComputeHash(BindPoint, BindCount, InputType, SRVDimension, SamplerOrTexSRVId); } + + +private: + friend class ShaderResources; + + Uint32 GetCombinedSamplerId()const + { + VERIFY(GetInputType() == D3D_SIT_TEXTURE && GetSRVDimension() != D3D_SRV_DIMENSION_BUFFER, "Invalid input type: D3D_SIT_TEXTURE is expected" ); + return SamplerOrTexSRVId; + } + + void SetTexSRVId(Uint32 TexSRVId) + { + VERIFY(GetInputType() == D3D_SIT_SAMPLER, "Invalid input type: D3D_SIT_SAMPLER is expected" ); + VERIFY(TexSRVId < (1 << SamplerOrTexSRVIdBits), "TexSRVId (", TexSRVId, ") is out of representable range"); + SamplerOrTexSRVId = TexSRVId; + } + + Uint32 GetCombinedTexSRVId()const + { + VERIFY(GetInputType() == D3D_SIT_SAMPLER, "Invalid input type: D3D_SIT_SAMPLER is expected" ); + return SamplerOrTexSRVId; + } }; static_assert(sizeof(D3DShaderResourceAttribs) == sizeof(void*) + sizeof(Uint32)*2, "Unexpected sizeof(D3DShaderResourceAttribs)"); @@ -265,6 +269,18 @@ public: const D3DShaderResourceAttribs& GetBufUAV (Uint32 n)const noexcept { return GetResAttribs(n, GetNumBufUAV(), m_BufUAVOffset); } const D3DShaderResourceAttribs& GetSampler(Uint32 n)const noexcept { return GetResAttribs(n, GetNumSamplers(), m_SamplersOffset); } + const D3DShaderResourceAttribs& GetCombinedSampler(const D3DShaderResourceAttribs& TexSRV)const noexcept + { + VERIFY(TexSRV.IsCombinedWithSampler(), "This texture SRV is not combined with any sampler"); + return GetSampler(TexSRV.GetCombinedSamplerId()); + } + + const D3DShaderResourceAttribs& GetCombinedTextureSRV(const D3DShaderResourceAttribs& Sampler)const noexcept + { + VERIFY(Sampler.IsCombinedWithTexSRV(), "This sampler is not combined with any texture SRV"); + return GetTexSRV(Sampler.GetCombinedTexSRVId()); + } + SHADER_TYPE GetShaderType()const noexcept{return m_ShaderType;} ShaderResourceDesc GetShaderResourceDesc(Uint32 Index)const; @@ -486,7 +502,7 @@ void ShaderResources::Initialize(ID3DBlob* pShaderByteCode, for (Uint32 n=0; n < GetNumSamplers(); ++n) { const auto& Sampler = GetSampler(n); - if (!Sampler.ValidTexSRVAssigned()) + if (!Sampler.IsCombinedWithTexSRV()) LOG_ERROR_MESSAGE("Shader '", ShaderName, "' uses combined texture samplers, but sampler '", Sampler.Name, "' is not assigned to any texture"); } #endif -- cgit v1.2.3 From 772ef0e6cfbb218814ad4e7d25dd94b697a6b4f3 Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Tue, 5 Mar 2019 22:05:17 -0800 Subject: Keep fixing Metal backend + few other minor changes --- Graphics/GraphicsEngineD3DBase/include/ShaderResources.h | 3 +++ Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'Graphics/GraphicsEngineD3DBase') diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h index f8f6917b..e12049e4 100644 --- a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h +++ b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h @@ -399,6 +399,9 @@ private: // Memory buffer that holds all resources as continuous chunk of memory: // | CBs | TexSRVs | TexUAVs | BufSRVs | BufUAVs | Samplers | Resource Names | + // | + // end of names data may not be aligned + std::unique_ptr< void, STDDeleterRawMem > m_MemoryBuffer; StringPool m_ResourceNames; diff --git a/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp b/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp index a3efe9f3..c52b2cee 100644 --- a/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp +++ b/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp @@ -27,6 +27,7 @@ #include "ShaderResources.h" #include "HashUtils.h" #include "ShaderResourceVariableBase.h" +#include "Align.h" namespace Diligent { @@ -74,7 +75,8 @@ void ShaderResources::AllocateMemory(IMemoryAllocator& Allocator, m_SamplersOffset = AdvanceOffset(ResCounters.NumSamplers); m_TotalResources = AdvanceOffset(0); - auto MemorySize = m_TotalResources * sizeof(D3DShaderResourceAttribs) + ResourceNamesPoolSize * sizeof(char); + auto AlignedResourceNamesPoolSize = Align(ResourceNamesPoolSize, sizeof(void*)); + auto MemorySize = m_TotalResources * sizeof(D3DShaderResourceAttribs) + AlignedResourceNamesPoolSize * sizeof(char); VERIFY_EXPR(GetNumCBs() == ResCounters.NumCBs); VERIFY_EXPR(GetNumTexSRV() == ResCounters.NumTexSRVs); -- cgit v1.2.3