summaryrefslogtreecommitdiffstats
path: root/Graphics/GraphicsEngineD3DBase
diff options
context:
space:
mode:
authorEgor Yusov <egor.yusov@gmail.com>2019-03-07 02:58:56 +0000
committerEgor Yusov <egor.yusov@gmail.com>2019-03-07 02:58:56 +0000
commitbc9fc76496034a50faf3d7fe7be868d5d62f6663 (patch)
treee9438fe09533409c57c9c63b2f69a895e2cf8615 /Graphics/GraphicsEngineD3DBase
parentMerge pull request #67 from StarshipVendingMachine/android-vulkan (diff)
parentMinor comment updates (diff)
downloadDiligentCore-bc9fc76496034a50faf3d7fe7be868d5d62f6663.tar.gz
DiligentCore-bc9fc76496034a50faf3d7fe7be868d5d62f6663.zip
Merged var_type_refactor into master
Diffstat (limited to 'Graphics/GraphicsEngineD3DBase')
-rw-r--r--Graphics/GraphicsEngineD3DBase/include/D3DShaderResourceLoader.h95
-rw-r--r--Graphics/GraphicsEngineD3DBase/include/ShaderD3DBase.h3
-rw-r--r--Graphics/GraphicsEngineD3DBase/include/ShaderResources.h281
-rw-r--r--Graphics/GraphicsEngineD3DBase/include/ShaderVariableD3DBase.h30
-rw-r--r--Graphics/GraphicsEngineD3DBase/src/ShaderD3DBase.cpp70
-rw-r--r--Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp263
6 files changed, 396 insertions, 346 deletions
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<TShaderReflection> pShaderReflection;
auto hr = D3DReflect( pShaderByteCode->GetBufferPointer(), pShaderByteCode->GetBufferSize(), __uuidof(pShaderReflection), reinterpret_cast<void**>(static_cast<TShaderReflection**>(&pShaderReflection)));
@@ -80,13 +76,11 @@ namespace Diligent
Resources.reserve(shaderDesc.BoundResources);
std::unordered_set<std::string> 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<size_t, STDAllocatorRawMem<size_t> > TexSRVInds( STD_ALLOCATOR_RAW_MEM(size_t, GetRawAllocator(), "Allocator for vector<size_t>") );
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<ID3DBlob> m_pShaderByteCode;
};
}
diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h
index 3cc19fa7..e12049e4 100644
--- a/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h
+++ b/Graphics/GraphicsEngineD3DBase/include/ShaderResources.h
@@ -62,60 +62,40 @@
#include "HashUtils.h"
#include "StringPool.h"
#include "D3DShaderResourceLoader.h"
+#include "PipelineState.h"
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(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 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 |
- 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");
+ // 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_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.
- // 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 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)
+ // 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;
@@ -129,39 +109,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<decltype(BindPoint)> (_BindPoint)),
BindCount (static_cast<decltype(BindCount)> (_BindCount)),
InputType (static_cast<decltype(InputType)> (_InputType)),
- VariableType (static_cast<decltype(VariableType)>(_VariableType)),
SRVDimension (static_cast<decltype(SRVDimension)>(_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);
+ VERIFY_EXPR(GetCombinedSamplerId() == _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 +139,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 +153,8 @@ public:
rhs.BindPoint,
rhs.BindCount,
rhs.GetInputType(),
- rhs.GetVariableType(),
rhs.GetSRVDimension(),
- rhs.SamplerOrTexSRVId,
- rhs.StaticSamplerFlag !=0 ? true : false
+ rhs.SamplerOrTexSRVId
}
{
}
@@ -206,49 +169,19 @@ public:
return static_cast<D3D_SHADER_INPUT_TYPE>(InputType);
}
- SHADER_VARIABLE_TYPE GetVariableType()const
- {
- return static_cast<SHADER_VARIABLE_TYPE>(VariableType);
- }
-
D3D_SRV_DIMENSION GetSRVDimension()const
{
return static_cast<D3D_SRV_DIMENSION>(SRVDimension);
}
- Uint32 GetSamplerId()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 IsStaticSampler()const
- {
- VERIFY(GetInputType() == D3D_SIT_SAMPLER, "Invalid input type: D3D_SIT_SAMPLER is expected" );
- return StaticSamplerFlag != 0;
- }
-
- bool ValidSamplerAssigned()const
+ bool IsCombinedWithSampler()const
{
- return GetSamplerId() != InvalidSamplerId;
+ return GetCombinedSamplerId() != InvalidSamplerId;
}
- bool ValidTexSRVAssigned()const
+ bool IsCombinedWithTexSRV()const
{
- return GetTexSRVId() != InvalidTexSRVId;
+ return GetCombinedTexSRVId() != InvalidTexSRVId;
}
bool IsValidBindPoint()const
@@ -270,20 +203,36 @@ 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);
+ return ComputeHash(BindPoint, BindCount, InputType, SRVDimension, SamplerOrTexSRVId);
}
- bool IsAllowedType(Uint32 AllowedTypeBits)const
+
+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)
{
- return Diligent::IsAllowedType(GetVariableType(), AllowedTypeBits);
+ 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)");
@@ -293,102 +242,122 @@ 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); }
-
- D3DShaderResourceCounters CountResources(const SHADER_VARIABLE_TYPE* AllowedVarTypes,
- Uint32 NumAllowedTypes)const noexcept;
+ 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); }
+
+ 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;}
- // Processes only resources listed in AllowedVarTypes
+ ShaderResourceDesc GetShaderResourceDesc(Uint32 Index)const;
+
template<typename THandleCB,
typename THandleSampler,
typename THandleTexSRV,
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);
}
}
bool IsCompatibleWith(const ShaderResources& Resources) const;
bool IsUsingCombinedTextureSamplers() const { return m_SamplerSuffix != nullptr; }
const char* GetCombinedSamplerSuffix() const { return m_SamplerSuffix; }
+ const Char* GetShaderName() const { return m_ShaderName; }
size_t GetHash()const;
+ SHADER_RESOURCE_VARIABLE_TYPE FindVariableType(const D3DShaderResourceAttribs& ResourceAttribs,
+ const PipelineResourceLayoutDesc& ResourceLayout)const;
+
+ Int32 FindStaticSampler(const D3DShaderResourceAttribs& ResourceAttribs,
+ const PipelineResourceLayoutDesc& ResourceLayoutDesc)const;
+
+ D3DShaderResourceCounters CountResources(const PipelineResourceLayoutDesc& ResourceLayout,
+ const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes,
+ Uint32 NumAllowedTypes,
+ bool CountStaticSamplers)const noexcept;
+#ifdef DEVELOPMENT
+ static void DvpVerifyResourceLayout(const PipelineResourceLayoutDesc& ResourceLayout,
+ const ShaderResources* const pShaderResources[],
+ Uint32 NumShaders);
+#endif
+
protected:
template<typename D3D_SHADER_DESC,
typename D3D_SHADER_INPUT_BIND_DESC,
@@ -396,7 +365,7 @@ protected:
typename TNewResourceHandler>
void Initialize(ID3DBlob* pShaderByteCode,
TNewResourceHandler NewResHandler,
- const ShaderDesc& ShdrDesc,
+ const Char* ShaderName,
const Char* SamplerSuffix);
@@ -430,10 +399,14 @@ 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<void> > 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;
@@ -444,7 +417,7 @@ private:
OffsetType m_SamplersOffset = 0;
OffsetType m_TotalResources = 0;
- SHADER_TYPE m_ShaderType = SHADER_TYPE_UNKNOWN;
+ const SHADER_TYPE m_ShaderType;
};
@@ -453,8 +426,8 @@ template<typename D3D_SHADER_DESC,
typename TShaderReflection,
typename TNewResourceHandler>
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;
@@ -463,6 +436,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;
@@ -517,10 +493,9 @@ void ShaderResources::Initialize(ID3DBlob* pShaderByteCode,
}
++CurrTexSRV;
NewResHandler.OnNewTexSRV(*pNewTexSRV);
- },
+ });
- ShdrDesc,
- CombinedSamplerSuffix);
+ m_ShaderName = m_ResourceNames.CopyString(ShaderName);
if (CombinedSamplerSuffix != nullptr)
{
@@ -530,8 +505,8 @@ void ShaderResources::Initialize(ID3DBlob* pShaderByteCode,
for (Uint32 n=0; n < GetNumSamplers(); ++n)
{
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");
+ if (!Sampler.IsCombinedWithTexSRV())
+ LOG_ERROR_MESSAGE("Shader '", ShaderName, "' uses combined texture samplers, but sampler '", Sampler.Name, "' is not assigned to any texture");
}
#endif
}
diff --git a/Graphics/GraphicsEngineD3DBase/include/ShaderVariableD3DBase.h b/Graphics/GraphicsEngineD3DBase/include/ShaderVariableD3DBase.h
index 6c683aa2..ae2d2728 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,11 +31,14 @@
namespace Diligent
{
template<typename TShaderResourceLayout>
- struct ShaderVariableD3DBase : public IShaderVariable
+ 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)
{
}
@@ -54,32 +57,32 @@ 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;
*ppInterface = nullptr;
- if( IID == IID_ShaderVariable || IID == IID_Unknown )
+ if( IID == IID_ShaderResourceVariable || IID == IID_Unknown )
{
*ppInterface = this;
(*ppInterface)->AddRef();
}
}
- virtual SHADER_VARIABLE_TYPE GetType()const override final
+ 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;
};
}
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<IFileStream> pSourceStream;
m_pStreamFactory->CreateInputStream( pFileName, &pSourceStream );
@@ -78,13 +78,13 @@ private:
std::unordered_map< LPCVOID, RefCntAutoPtr<IDataBlob> > 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<IFileStream> pSourceStream;
- CreationAttribs.pShaderSourceStreamFactory->CreateInputStream(CreationAttribs.FilePath, &pSourceStream);
+ ShaderCI.pShaderSourceStreamFactory->CreateInputStream(ShaderCI.FilePath, &pSourceStream);
RefCntAutoPtr<IDataBlob> pFileData(MakeNewRCObj<DataBlobImpl>()(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<D3D_SHADER_MACRO> 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<ID3DBlob> 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<const char*>(errors->GetBufferPointer()) : nullptr;
- if(CompilerMsg != nullptr && CreationAttribs.ppCompilerOutput != nullptr)
+ const char* CompilerMsg = errors ? reinterpret_cast<const char*>(errors->GetBufferPointer()) : nullptr;
+ if (CompilerMsg != nullptr && ShaderCI.ppCompilerOutput != nullptr)
{
auto ErrorMsgLen = strlen(CompilerMsg);
auto *pOutputDataBlob = MakeNewRCObj<DataBlobImpl>()(ErrorMsgLen + 1 + ShaderSource.length() + 1);
char* DataPtr = reinterpret_cast<char*>(pOutputDataBlob->GetDataPtr());
memcpy(DataPtr, CompilerMsg, ErrorMsgLen+1);
memcpy(DataPtr + ErrorMsgLen + 1, ShaderSource.data(), ShaderSource.length() + 1);
- pOutputDataBlob->QueryInterface(IID_DataBlob, reinterpret_cast<IObject**>(CreationAttribs.ppCompilerOutput));
+ pOutputDataBlob->QueryInterface(IID_DataBlob, reinterpret_cast<IObject**>(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 : "<no compiler log available>") );
+ LOG_ERROR_AND_THROW("Failed to compile D3D shader \"", (ShaderCI.Desc.Name != nullptr ? ShaderCI.Desc.Name : ""), "\" (", ErrDesc.Get(), "):\n", (CompilerMsg != nullptr ? CompilerMsg : "<no compiler log available>") );
}
}
}
- 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..c52b2cee 100644
--- a/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp
+++ b/Graphics/GraphicsEngineD3DBase/src/ShaderResources.cpp
@@ -26,6 +26,8 @@
#include "StringTools.h"
#include "ShaderResources.h"
#include "HashUtils.h"
+#include "ShaderResourceVariableBase.h"
+#include "Align.h"
namespace Diligent
{
@@ -65,7 +67,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);
@@ -73,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);
@@ -91,57 +94,185 @@ 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 SHADER_VARIABLE_TYPE* AllowedVarTypes,
- Uint32 NumAllowedTypes)const noexcept
+
+D3DShaderResourceCounters ShaderResources::CountResources(const PipelineResourceLayoutDesc& ResourceLayout,
+ const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes,
+ Uint32 NumAllowedTypes,
+ bool CountStaticSamplers)const noexcept
{
auto AllowedTypeBits = GetAllowedTypeBits(AllowedVarTypes, NumAllowedTypes);
D3DShaderResourceCounters Counters;
ProcessResources(
- AllowedVarTypes, NumAllowedTypes,
-
[&](const D3DShaderResourceAttribs& CB, Uint32)
{
- VERIFY_EXPR(CB.IsAllowedType(AllowedTypeBits));
- ++Counters.NumCBs;
+ auto VarType = FindVariableType(CB, ResourceLayout);
+ if (IsAllowedType(VarType, AllowedTypeBits))
+ ++Counters.NumCBs;
},
[&](const D3DShaderResourceAttribs& Sam, Uint32)
{
- VERIFY_EXPR(Sam.IsAllowedType(AllowedTypeBits));
- // Skip static samplers
- if (!Sam.IsStaticSampler())
+ auto VarType = FindVariableType(Sam, ResourceLayout);
+ if (IsAllowedType(VarType, AllowedTypeBits))
+ {
+ if (!CountStaticSamplers)
+ {
+ if (FindStaticSampler(Sam, ResourceLayout) >= 0)
+ return; // Skip static sampler if requested
+ }
++Counters.NumSamplers;
+ }
},
[&](const D3DShaderResourceAttribs& TexSRV, Uint32)
{
- VERIFY_EXPR(TexSRV.IsAllowedType(AllowedTypeBits));
- ++Counters.NumTexSRVs;
+ auto VarType = FindVariableType(TexSRV, ResourceLayout);
+ if (IsAllowedType(VarType, AllowedTypeBits))
+ ++Counters.NumTexSRVs;
},
[&](const D3DShaderResourceAttribs& TexUAV, Uint32)
{
- VERIFY_EXPR(TexUAV.IsAllowedType(AllowedTypeBits));
- ++Counters.NumTexUAVs;
+ auto VarType = FindVariableType(TexUAV, ResourceLayout);
+ if (IsAllowedType(VarType, AllowedTypeBits))
+ ++Counters.NumTexUAVs;
},
[&](const D3DShaderResourceAttribs& BufSRV, Uint32)
{
- VERIFY_EXPR(BufSRV.IsAllowedType(AllowedTypeBits));
- ++Counters.NumBufSRVs;
+ auto VarType = FindVariableType(BufSRV, ResourceLayout);
+ if (IsAllowedType(VarType, AllowedTypeBits))
+ ++Counters.NumBufSRVs;
},
[&](const D3DShaderResourceAttribs& BufUAV, Uint32)
{
- VERIFY_EXPR(BufUAV.IsAllowedType(AllowedTypeBits));
- ++Counters.NumBufUAVs;
+ auto VarType = FindVariableType(BufUAV, ResourceLayout);
+ if (IsAllowedType(VarType, AllowedTypeBits))
+ ++Counters.NumBufUAVs;
}
);
return Counters;
}
+#ifdef DEVELOPMENT
+void ShaderResources::DvpVerifyResourceLayout(const PipelineResourceLayoutDesc& ResourceLayout,
+ const ShaderResources* const pShaderResources[],
+ Uint32 NumShaders)
+{
+ 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;
+ }
+
+ bool VariableFound = false;
+ for (Uint32 s = 0; s < NumShaders && !VariableFound; ++s)
+ {
+ 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);
+
+ // 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 of the designated shader stages "
+ "(", GetShaderStagesString(VarDesc.ShaderStages), ")");
+ }
+ }
+
+ for (Uint32 sam = 0; sam < ResourceLayout.NumStaticSamplers; ++sam)
+ {
+ 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;
+ }
+
+ const auto* TexOrSamName = StSamDesc.SamplerOrTextureName;
+
+ bool TextureOrSamplerFound = false;
+ for (Uint32 s = 0; s < NumShaders && !TextureOrSamplerFound; ++s)
+ {
+ const auto& Resources = *pShaderResources[s];
+ if ( (StSamDesc.ShaderStages & Resources.GetShaderType()) == 0)
+ continue;
+
+ const auto UseCombinedTextureSamplers = Resources.IsUsingCombinedTextureSamplers();
+ if (UseCombinedTextureSamplers)
+ {
+ for(Uint32 n=0; n < Resources.GetNumTexSRV() && !TextureOrSamplerFound; ++n)
+ {
+ const auto& TexSRV = Resources.GetTexSRV(n);
+ TextureOrSamplerFound = (strcmp(TexSRV.Name, TexOrSamName) == 0);
+ }
+ }
+ else
+ {
+ for(Uint32 n=0; n < Resources.GetNumSamplers() && !TextureOrSamplerFound; ++n)
+ {
+ const auto& Sampler = Resources.GetSampler(n);
+ TextureOrSamplerFound = (strcmp(Sampler.Name, TexOrSamName) == 0);
+ }
+ }
+ }
+
+ if (!TextureOrSamplerFound)
+ {
+ LOG_WARNING_MESSAGE("Static sampler '", TexOrSamName, "' is not found in any of the designated shader stages "
+ "(", GetShaderStagesString(StSamDesc.ShaderStages), ")");
+ }
+ }
+}
+#endif
+
Uint32 ShaderResources::FindAssignedSamplerId(const D3DShaderResourceAttribs& TexSRV, const char* SamplerSuffix)const
{
@@ -153,10 +284,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 +303,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)))
@@ -212,36 +337,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(
- nullptr, 0,
- [&](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;
}