Skip to content

Commit 139f0ae

Browse files
Count inline constants in PipelineResourceSignatureBase
1 parent 158868a commit 139f0ae

8 files changed

Lines changed: 58 additions & 64 deletions

File tree

Graphics/GraphicsEngine/include/PipelineResourceSignatureBase.hpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,6 +766,36 @@ class PipelineResourceSignatureBase : public DeviceObjectBase<typename EngineImp
766766
m_ShaderStages |= ResDesc.ShaderStages;
767767
if (ResDesc.VarType == SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
768768
m_StaticResShaderStages |= ResDesc.ShaderStages;
769+
770+
// Count the number of inline constant buffers and inline constants.
771+
if (ResDesc.Flags & PIPELINE_RESOURCE_FLAG_INLINE_CONSTANTS)
772+
{
773+
VERIFY(ResDesc.ResourceType == SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, "Only constant buffers can have INLINE_CONSTANTS flag");
774+
775+
static constexpr Uint32 MaxInlineConstantBuffers = std::numeric_limits<decltype(m_NumInlineConstantBuffers)>::max();
776+
VERIFY(m_NumInlineConstantBuffers < MaxInlineConstantBuffers,
777+
"Number of inline constant buffers (", Uint32{m_NumInlineConstantBuffers} + 1,
778+
") exceeds the maximum storable value (", MaxInlineConstantBuffers,
779+
"). Do you really need ", Uint32{m_NumInlineConstantBuffers} + 1, " inline constant buffers?");
780+
++m_NumInlineConstantBuffers;
781+
782+
static constexpr Uint32 MaxStorableInlineConstants = std::numeric_limits<decltype(m_TotalInlineConstants)>::max();
783+
static_assert(MaxStorableInlineConstants >= MAX_INLINE_CONSTANTS, "MaxStorableInlineConstants is less than MAX_INLINE_CONSTANTS");
784+
785+
VERIFY(Uint32{m_TotalInlineConstants} + ResDesc.ArraySize <= MAX_INLINE_CONSTANTS,
786+
"Total number of inline constants exceeds the maximum (", MAX_INLINE_CONSTANTS,
787+
"). This error should have been caught in ValidatePipelineResourceSignatureDesc().");
788+
VERIFY(Uint32{m_TotalInlineConstants} + ResDesc.ArraySize <= MaxStorableInlineConstants,
789+
"Total number of inline constants (", Uint32{m_TotalInlineConstants} + ResDesc.ArraySize,
790+
") exceeds the maximum storable value (", MaxStorableInlineConstants, ").");
791+
m_TotalInlineConstants += static_cast<decltype(m_TotalInlineConstants)>(ResDesc.ArraySize);
792+
793+
if (ResDesc.VarType == SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
794+
{
795+
++m_NumStaticInlineConstantBuffers;
796+
m_TotalStaticInlineConstants += static_cast<decltype(m_TotalStaticInlineConstants)>(ResDesc.ArraySize);
797+
}
798+
}
769799
}
770800

771801
if (m_ShaderStages != SHADER_TYPE_UNKNOWN)
@@ -1098,6 +1128,18 @@ class PipelineResourceSignatureBase : public DeviceObjectBase<typename EngineImp
10981128
// Resource offsets (e.g. index of the first resource), for each variable type.
10991129
std::array<Uint16, SHADER_RESOURCE_VARIABLE_TYPE_NUM_TYPES + 1> m_ResourceOffsets = {};
11001130

1131+
// The number of inline constant buffers (constant buffers with PIPELINE_RESOURCE_FLAG_INLINE_CONSTANTS flag)
1132+
Uint16 m_NumInlineConstantBuffers = 0;
1133+
1134+
// The number of static inline constant buffers
1135+
Uint16 m_NumStaticInlineConstantBuffers = 0;
1136+
1137+
// The total number of inline constants (32-bit values) in all inline constant buffers
1138+
Uint16 m_TotalInlineConstants = 0;
1139+
1140+
// The total number of inline constants (32-bit values) in static inline constant buffers
1141+
Uint16 m_TotalStaticInlineConstants = 0;
1142+
11011143
// Shader stages that have resources.
11021144
SHADER_TYPE m_ShaderStages = SHADER_TYPE_UNKNOWN;
11031145

Graphics/GraphicsEngine/src/PipelineResourceSignatureBase.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ void ValidatePipelineResourceSignatureDesc(const PipelineResourceSignatureDesc&
7171
LOG_PRS_ERROR_AND_THROW("Desc.UseCombinedTextureSamplers is true, but Desc.CombinedSamplerSuffix is null or empty");
7272

7373

74+
Uint32 TotalInlineConstantValues = 0;
7475
// Hash map of all resources by name
7576
std::unordered_multimap<HashMapStringKey, const PipelineResourceDesc&> Resources;
7677
for (Uint32 i = 0; i < Desc.NumResources; ++i)
@@ -132,6 +133,13 @@ void ValidatePipelineResourceSignatureDesc(const PipelineResourceSignatureDesc&
132133
LOG_PRS_ERROR_AND_THROW("Desc.Resources[", i, "].ArraySize (", Res.ArraySize,
133134
") exceeds the maximum allowed value (", MAX_INLINE_CONSTANTS, ") for inline constants.");
134135
}
136+
137+
TotalInlineConstantValues += Res.ArraySize;
138+
if (TotalInlineConstantValues > MAX_INLINE_CONSTANTS)
139+
{
140+
LOG_PRS_ERROR_AND_THROW("Total number of inline constant values in the resource signature (", TotalInlineConstantValues,
141+
") exceeds the maximum allowed value (", MAX_INLINE_CONSTANTS, ").");
142+
}
135143
}
136144

137145
if ((Res.Flags & PIPELINE_RESOURCE_FLAG_FORMATTED_BUFFER) != 0 && Features.FormattedBuffers == DEVICE_FEATURE_STATE_DISABLED)

Graphics/GraphicsEngineD3D11/include/PipelineResourceSignatureD3D11Impl.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@ class PipelineResourceSignatureD3D11Impl final : public PipelineResourceSignatur
138138
std::array<Uint16, NumShaderTypes> m_DynamicCBSlotsMask{};
139139
static_assert(sizeof(m_DynamicCBSlotsMask[0]) * 8 >= D3D11_COMMONSHADER_CONSTANT_BUFFER_API_SLOT_COUNT, "Not enough bits for all dynamic buffer slots");
140140

141-
Uint32 m_NumInlineConstantBuffers = 0;
142141
std::unique_ptr<InlineConstantBufferAttribsD3D11[]> m_InlineConstantBuffers;
143142
};
144143

Graphics/GraphicsEngineD3D11/src/PipelineResourceSignatureD3D11Impl.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,6 @@ void PipelineResourceSignatureD3D11Impl::CreateLayout(const bool IsSerialized)
153153
DstImtblSampAttribs.ArraySize = std::max(DstImtblSampAttribs.ArraySize, ResDesc.ArraySize);
154154
}
155155
}
156-
157-
if (ResDesc.Flags & PIPELINE_RESOURCE_FLAG_INLINE_CONSTANTS)
158-
{
159-
VERIFY(ResDesc.ResourceType == SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, "Only constant buffers can have INLINE_CONSTANTS flag");
160-
++m_NumInlineConstantBuffers;
161-
}
162156
}
163157

164158
if (m_NumInlineConstantBuffers > 0)

Graphics/GraphicsEngineOpenGL/include/PipelineResourceSignatureGLImpl.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,6 @@ class PipelineResourceSignatureGLImpl final : public PipelineResourceSignatureBa
179179
// Indicates which SSBO slots allow binding buffers with dynamic offsets
180180
Uint64 m_DynamicSSBOMask = 0;
181181

182-
// Number of inline constant buffers
183-
Uint16 m_NumInlineConstantBuffers = 0;
184-
185-
// The total number of inline constants (32-bit values) in all inline constant buffers
186-
Uint16 m_TotalInlineConstants = 0;
187-
188182
// Inline constant buffer attributes
189183
std::unique_ptr<InlineConstantBufferAttribsGL[]> m_InlineConstantBuffers;
190184
};

Graphics/GraphicsEngineOpenGL/src/PipelineResourceSignatureGLImpl.cpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,25 +106,13 @@ PipelineResourceSignatureGLImpl::PipelineResourceSignatureGLImpl(IReferenceCount
106106

107107
void PipelineResourceSignatureGLImpl::CreateLayout(const bool IsSerialized)
108108
{
109-
// Count inline constant buffers
110-
for (Uint32 i = 0; i < m_Desc.NumResources; ++i)
111-
{
112-
const PipelineResourceDesc& ResDesc = m_Desc.Resources[i];
113-
if (ResDesc.Flags & PIPELINE_RESOURCE_FLAG_INLINE_CONSTANTS)
114-
{
115-
VERIFY(ResDesc.ResourceType == SHADER_RESOURCE_TYPE_CONSTANT_BUFFER, "Only constant buffers can have INLINE_CONSTANTS flag");
116-
++m_NumInlineConstantBuffers;
117-
}
118-
}
119-
120109
if (m_NumInlineConstantBuffers > 0)
121110
{
122111
m_InlineConstantBuffers = std::make_unique<InlineConstantBufferAttribsGL[]>(m_NumInlineConstantBuffers);
123112
}
124113

125-
TBindings StaticResCounter = {};
126-
Uint16 NumStaticInlineConstants = 0;
127-
Uint32 InlineConstantBufferIdx = 0;
114+
TBindings StaticResCounter = {};
115+
Uint32 InlineConstantBufferIdx = 0;
128116
for (Uint32 i = 0; i < m_Desc.NumResources; ++i)
129117
{
130118
const PipelineResourceDesc& ResDesc = m_Desc.Resources[i];
@@ -224,13 +212,6 @@ void PipelineResourceSignatureGLImpl::CreateLayout(const bool IsSerialized)
224212
// However, this will increase memory consumption as each SRB will have its own copy of the inline CB.
225213
// Besides, inline constants are expected to change frequently, so skipping updates is unlikely.
226214
InlineCBAttribs.pBuffer = CreateInlineConstantBuffer(ResDesc.Name, ResDesc.ArraySize);
227-
228-
m_TotalInlineConstants += static_cast<Uint16>(ResDesc.ArraySize);
229-
230-
if (ResDesc.VarType == SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
231-
{
232-
NumStaticInlineConstants += static_cast<Uint16>(ResDesc.ArraySize);
233-
}
234215
}
235216

236217
VERIFY(CacheOffset + ArraySize <= std::numeric_limits<TBindings::value_type>::max(), "Cache offset exceeds representable range");
@@ -248,7 +229,7 @@ void PipelineResourceSignatureGLImpl::CreateLayout(const bool IsSerialized)
248229

249230
if (m_pStaticResCache)
250231
{
251-
m_pStaticResCache->Initialize(StaticResCounter, GetRawAllocator(), 0x0, 0x0, NumStaticInlineConstants);
232+
m_pStaticResCache->Initialize(StaticResCounter, GetRawAllocator(), 0x0, 0x0, m_TotalStaticInlineConstants);
252233

253234
// Initialize inline constant buffers in the static cache.
254235
// This allows static inline constants to be set on the signature and later copied to SRBs.
@@ -274,7 +255,7 @@ void PipelineResourceSignatureGLImpl::CreateLayout(const bool IsSerialized)
274255
InlineConstantOffset += InlineCBAttr.NumConstants;
275256
}
276257
}
277-
VERIFY_EXPR(InlineConstantOffset == NumStaticInlineConstants);
258+
VERIFY_EXPR(InlineConstantOffset == m_TotalStaticInlineConstants);
278259
}
279260
#ifdef DILIGENT_DEBUG
280261
m_pStaticResCache->DbgVerifyResourceInitialization();

Graphics/GraphicsEngineVulkan/include/PipelineResourceSignatureVkImpl.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,6 @@ class PipelineResourceSignatureVkImpl final : public PipelineResourceSignatureBa
232232
// accounting for array size.
233233
Uint16 m_DynamicStorageBufferCount = 0;
234234

235-
// Number of inline constant buffers
236-
Uint16 m_NumInlineConstantBuffers = 0;
237-
238-
// The total number of inline constants (32-bit values) in all inline constant buffers
239-
Uint16 m_TotalInlineConstants = 0;
240-
241235
// Inline constant buffer attributes
242236
std::unique_ptr<InlineConstantBufferAttribsVk[]> m_InlineConstantBuffers;
243237
};

Graphics/GraphicsEngineVulkan/src/PipelineResourceSignatureVkImpl.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,8 @@ void PipelineResourceSignatureVkImpl::CreateSetLayouts(const bool IsSerialized)
173173
CacheOffsetsType CacheGroupSizes = {}; // Required cache size for each cache group
174174
BindingCountType BindingCount = {}; // Binding count in each cache group
175175

176-
// Count resources and inline constants
177-
Uint32 StaticResourceCount = 0; // The total number of static resources in all stages
178-
Uint32 TotalStaticInlineConstants = 0; // The total number of static inline constants
176+
// Count resources
177+
Uint32 StaticResourceCount = 0; // The total number of static resources in all stages
179178
for (Uint32 i = 0; i < m_Desc.NumResources; ++i)
180179
{
181180
const PipelineResourceDesc& ResDesc = m_Desc.Resources[i];
@@ -195,30 +194,13 @@ void PipelineResourceSignatureVkImpl::CreateSetLayouts(const bool IsSerialized)
195194
{
196195
StaticResourceCount += DescriptorCount;
197196
}
198-
199-
// Count inline constant buffers
200-
if (ResDesc.Flags & PIPELINE_RESOURCE_FLAG_INLINE_CONSTANTS)
201-
{
202-
VERIFY(ResDesc.ResourceType == SHADER_RESOURCE_TYPE_CONSTANT_BUFFER,
203-
"Only constant buffers can have INLINE_CONSTANTS flag");
204-
++m_NumInlineConstantBuffers;
205-
206-
// ArraySize contains the number of 32-bit constants for inline constants
207-
const Uint32 NumInlineConstants = ResDesc.ArraySize;
208-
209-
m_TotalInlineConstants += static_cast<Uint16>(NumInlineConstants);
210-
if (ResDesc.VarType == SHADER_RESOURCE_VARIABLE_TYPE_STATIC)
211-
{
212-
TotalStaticInlineConstants += NumInlineConstants;
213-
}
214-
}
215197
}
216198

217199
// Initialize static resource cache (now that we know resource and inline constant counts)
218200
if (StaticResourceCount > 0)
219201
{
220202
VERIFY_EXPR(GetNumStaticResStages() > 0);
221-
m_pStaticResCache->InitializeSets(GetRawAllocator(), 1, &StaticResourceCount, TotalStaticInlineConstants);
203+
m_pStaticResCache->InitializeSets(GetRawAllocator(), 1, &StaticResourceCount, m_TotalStaticInlineConstants);
222204
}
223205

224206
// Allocate inline constant buffer attributes array
@@ -414,7 +396,7 @@ void PipelineResourceSignatureVkImpl::CreateSetLayouts(const bool IsSerialized)
414396
}
415397
}
416398
VERIFY_EXPR(InlineConstantBufferIdx == m_NumInlineConstantBuffers);
417-
VERIFY_EXPR(StaticInlineConstantOffset == TotalStaticInlineConstants);
399+
VERIFY_EXPR(StaticInlineConstantOffset == m_TotalStaticInlineConstants);
418400

419401
#ifdef DILIGENT_DEBUG
420402
if (m_pStaticResCache != nullptr)

0 commit comments

Comments
 (0)