1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
/*
* Copyright 2019-2021 Diligent Graphics LLC
* Copyright 2015-2019 Egor Yusov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* In no event and under no legal theory, whether in tort (including negligence),
* contract, or otherwise, unless required by applicable law (such as deliberate
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
* liable for any damages, including any direct, indirect, special, incidental,
* or consequential damages of any character arising as a result of this License or
* out of the use or inability to use the software (including but not limited to damages
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
* all other commercial damages or losses), even if such Contributor has been advised
* of the possibility of such damages.
*/
#include "PipelineResourceSignatureBase.hpp"
#include <unordered_map>
#include "HashUtils.hpp"
#include "StringTools.hpp"
namespace Diligent
{
#define LOG_PRS_ERROR_AND_THROW(...) LOG_ERROR_AND_THROW("Description of a pipeline resource signature '", (Desc.Name ? Desc.Name : ""), "' is invalid: ", ##__VA_ARGS__)
void ValidatePipelineResourceSignatureDesc(const PipelineResourceSignatureDesc& Desc,
const DeviceFeatures& Features) noexcept(false)
{
if (Desc.BindingIndex >= MAX_RESOURCE_SIGNATURES)
LOG_PRS_ERROR_AND_THROW("Desc.BindingIndex (", Uint32{Desc.BindingIndex}, ") exceeds the maximum allowed value (", MAX_RESOURCE_SIGNATURES - 1, ").");
if (Desc.NumResources > MAX_RESOURCES_IN_SIGNATURE)
LOG_PRS_ERROR_AND_THROW("Desc.NumResources (", Uint32{Desc.NumResources}, ") exceeds the maximum allowed value (", MAX_RESOURCES_IN_SIGNATURE, ").");
if (Desc.NumResources != 0 && Desc.Resources == nullptr)
LOG_PRS_ERROR_AND_THROW("Desc.NumResources (", Uint32{Desc.NumResources}, ") is not zero, but Desc.Resources is null.");
if (Desc.NumImmutableSamplers != 0 && Desc.ImmutableSamplers == nullptr)
LOG_PRS_ERROR_AND_THROW("Desc.NumImmutableSamplers (", Uint32{Desc.NumImmutableSamplers}, ") is not zero, but Desc.ImmutableSamplers is null.");
if (Desc.UseCombinedTextureSamplers && (Desc.CombinedSamplerSuffix == nullptr || Desc.CombinedSamplerSuffix[0] == '\0'))
LOG_PRS_ERROR_AND_THROW("Desc.UseCombinedTextureSamplers is true, but Desc.CombinedSamplerSuffix is null or empty");
// Hash map of all resources by name
std::unordered_multimap<HashMapStringKey, const PipelineResourceDesc&, HashMapStringKey::Hasher> Resources;
for (Uint32 i = 0; i < Desc.NumResources; ++i)
{
const auto& Res = Desc.Resources[i];
if (Res.Name == nullptr)
LOG_PRS_ERROR_AND_THROW("Desc.Resources[", i, "].Name must not be null.");
if (Res.Name[0] == '\0')
LOG_PRS_ERROR_AND_THROW("Desc.Resources[", i, "].Name must not be empty.");
if (Res.ShaderStages == SHADER_TYPE_UNKNOWN)
LOG_PRS_ERROR_AND_THROW("Desc.Resources[", i, "].ShaderStages must not be SHADER_TYPE_UNKNOWN.");
if (Res.ArraySize == 0)
LOG_PRS_ERROR_AND_THROW("Desc.Resources[", i, "].ArraySize must not be 0.");
const auto res_range = Resources.equal_range(Res.Name);
for (auto res_it = res_range.first; res_it != res_range.second; ++res_it)
{
if ((res_it->second.ShaderStages & Res.ShaderStages) != 0)
{
LOG_PRS_ERROR_AND_THROW("Multiple resources with name '", Res.Name,
"' specify overlapping shader stages. There may be multiple resources with the same name in different shader stages, "
"but the stages must not overlap.");
}
if (Features.SeparablePrograms == DEVICE_FEATURE_STATE_DISABLED)
{
VERIFY_EXPR(res_it->second.ShaderStages != SHADER_TYPE_UNKNOWN);
LOG_PRS_ERROR_AND_THROW("This device does not support separable programs, but there are separate resources with the name '",
Res.Name, "' in shader stages ",
GetShaderStagesString(Res.ShaderStages), " and ",
GetShaderStagesString(res_it->second.ShaderStages),
". When separable programs are not supported, every resource is always shared between all stages. "
"Use distinct resource names for each stage or define a single resource for all stages.");
}
}
if ((Res.Flags & PIPELINE_RESOURCE_FLAG_RUNTIME_ARRAY) != 0 && !Features.ShaderResourceRuntimeArray)
{
LOG_PRS_ERROR_AND_THROW("Incorrect Desc.Resources[", i, "].Flags (RUNTIME_ARRAY) can only be used if ShaderResourceRuntimeArray device feature is enabled.");
}
if (Res.ResourceType == SHADER_RESOURCE_TYPE_ACCEL_STRUCT && !Features.RayTracing)
{
LOG_PRS_ERROR_AND_THROW("Incorrect Desc.Resources[", i, "].ResourceType (ACCEL_STRUCT): ray tracing is not supported by device.");
}
auto AllowedResourceFlags = GetValidPipelineResourceFlags(Res.ResourceType);
if ((Res.Flags & ~AllowedResourceFlags) != 0)
{
LOG_PRS_ERROR_AND_THROW("Incorrect Desc.Resources[", i, "].Flags (", GetPipelineResourceFlagsString(Res.Flags),
"). Only the following flags are valid for a ", GetShaderResourceTypeLiteralName(Res.ResourceType),
": ", GetPipelineResourceFlagsString(AllowedResourceFlags, false, ", "), ".");
}
Resources.emplace(Res.Name, Res);
// NB: when creating immutable sampler array, we have to define the sampler as both resource and
// immutable sampler. The sampler will not be exposed as a shader variable though.
//if (Res.ResourceType == SHADER_RESOURCE_TYPE_SAMPLER)
//{
// if (FindImmutableSampler(Desc.ImmutableSamplers, Desc.NumImmutableSamplers, Res.ShaderStages, Res.Name,
// Desc.UseCombinedTextureSamplers ? Desc.CombinedSamplerSuffix : nullptr) >= 0)
// {
// LOG_PRS_ERROR_AND_THROW("Sampler '", Res.Name, "' is defined as both shader resource and immutable sampler.");
// }
//}
}
// Hash map of all immutable samplers by name
std::unordered_multimap<HashMapStringKey, const ImmutableSamplerDesc&, HashMapStringKey::Hasher> ImtblSamplers;
for (Uint32 i = 0; i < Desc.NumImmutableSamplers; ++i)
{
const auto& SamDesc = Desc.ImmutableSamplers[i];
if (SamDesc.SamplerOrTextureName == nullptr)
LOG_PRS_ERROR_AND_THROW("Desc.ImmutableSamplers[", i, "].SamplerOrTextureName must not be null.");
if (SamDesc.SamplerOrTextureName[0] == '\0')
LOG_PRS_ERROR_AND_THROW("Desc.ImmutableSamplers[", i, "].SamplerOrTextureName must not be empty.");
if (SamDesc.ShaderStages == SHADER_TYPE_UNKNOWN)
LOG_PRS_ERROR_AND_THROW("Desc.ImmutableSamplers[", i, "].ShaderStages must not be SHADER_TYPE_UNKNOWN.");
const auto sam_range = ImtblSamplers.equal_range(SamDesc.SamplerOrTextureName);
for (auto sam_it = sam_range.first; sam_it != sam_range.second; ++sam_it)
{
if ((sam_it->second.ShaderStages & SamDesc.ShaderStages) != 0)
{
LOG_PRS_ERROR_AND_THROW("Multiple immutable samplers with name '", SamDesc.SamplerOrTextureName,
"' specify overlapping shader stages. There may be multiple immutable samplers with the same name in different shader stages, "
"but the stages must not overlap.");
}
if (Features.SeparablePrograms == DEVICE_FEATURE_STATE_DISABLED)
{
VERIFY_EXPR(sam_it->second.ShaderStages != SHADER_TYPE_UNKNOWN);
LOG_PRS_ERROR_AND_THROW("This device does not support separable programs, but there are separate immutable samplers with the name '",
SamDesc.SamplerOrTextureName, "' in shader stages ",
GetShaderStagesString(SamDesc.ShaderStages), " and ",
GetShaderStagesString(sam_it->second.ShaderStages),
". When separable programs are not supported, every resource is always shared between all stages. "
"Use distinct immutable sampler names for each stage or define a single sampler for all stages.");
}
}
ImtblSamplers.emplace(SamDesc.SamplerOrTextureName, SamDesc);
}
if (Desc.UseCombinedTextureSamplers)
{
VERIFY_EXPR(Desc.CombinedSamplerSuffix != nullptr);
// List of samplers assigned to some texture
std::unordered_multimap<HashMapStringKey, SHADER_TYPE, HashMapStringKey::Hasher> AssignedSamplers;
// List of immutable samplers assigned to some texture
std::unordered_multimap<HashMapStringKey, SHADER_TYPE, HashMapStringKey::Hasher> AssignedImtblSamplers;
for (Uint32 i = 0; i < Desc.NumResources; ++i)
{
const auto& Res = Desc.Resources[i];
if (Res.ResourceType != SHADER_RESOURCE_TYPE_TEXTURE_SRV)
{
// Only texture SRVs can be combined with samplers
continue;
}
{
const auto AssignedSamplerName = String{Res.Name} + Desc.CombinedSamplerSuffix;
const auto sam_range = Resources.equal_range(AssignedSamplerName.c_str());
for (auto sam_it = sam_range.first; sam_it != sam_range.second; ++sam_it)
{
const auto& Sam = sam_it->second;
VERIFY_EXPR(AssignedSamplerName == Sam.Name);
if ((Sam.ShaderStages & Res.ShaderStages) != 0)
{
if (Sam.ResourceType != SHADER_RESOURCE_TYPE_SAMPLER)
{
LOG_PRS_ERROR_AND_THROW("Resource '", Sam.Name, "' combined with texture '", Res.Name, "' is not a sampler.");
}
if ((Sam.ShaderStages & Res.ShaderStages) != Res.ShaderStages)
{
LOG_PRS_ERROR_AND_THROW("Texture '", Res.Name, "' is defined for the following shader stages: ", GetShaderStagesString(Res.ShaderStages),
", but sampler '", Sam.Name, "' assigned to it uses only some of these stages: ", GetShaderStagesString(Sam.ShaderStages),
". A resource that is present in multiple shader stages can't be combined with different samplers in different stages. "
"Either use separate resources for different stages, or define the sampler for all stages that the resource uses.");
}
if (Sam.VarType != Res.VarType)
{
LOG_PRS_ERROR_AND_THROW("The type (", GetShaderVariableTypeLiteralName(Res.VarType), ") of texture resource '", Res.Name,
"' does not match the type (", GetShaderVariableTypeLiteralName(Sam.VarType),
") of sampler '", Sam.Name, "' that is assigned to it.");
}
AssignedSamplers.emplace(Sam.Name, Sam.ShaderStages);
break;
}
}
}
{
const auto imtbl_sam_range = ImtblSamplers.equal_range(Res.Name);
for (auto sam_it = imtbl_sam_range.first; sam_it != imtbl_sam_range.second; ++sam_it)
{
const auto& Sam = sam_it->second;
VERIFY_EXPR(strcmp(Sam.SamplerOrTextureName, Res.Name) == 0);
if ((Sam.ShaderStages & Res.ShaderStages) != 0)
{
if ((Sam.ShaderStages & Res.ShaderStages) != Res.ShaderStages)
{
LOG_PRS_ERROR_AND_THROW("Texture '", Res.Name, "' is defined for the following shader stages: ", GetShaderStagesString(Res.ShaderStages),
", but immutable sampler that is assigned to it uses only some of these stages: ", GetShaderStagesString(Sam.ShaderStages),
". A resource that is present in multiple shader stages can't be combined with different immutable samples in different stages. "
"Either use separate resources for different stages, or define the immutable sampler for all stages that the resource uses.");
}
AssignedImtblSamplers.emplace(Sam.SamplerOrTextureName, Sam.ShaderStages);
break;
}
}
}
}
for (Uint32 i = 0; i < Desc.NumResources; ++i)
{
const auto& Res = Desc.Resources[i];
if (Res.ResourceType != SHADER_RESOURCE_TYPE_SAMPLER)
continue;
auto assigned_sam_range = AssignedSamplers.equal_range(Res.Name);
auto it = assigned_sam_range.first;
while (it != assigned_sam_range.second)
{
if (it->second == Res.ShaderStages)
break;
++it;
}
if (it == assigned_sam_range.second)
{
LOG_WARNING_MESSAGE("Sampler '", Res.Name, "' (", GetShaderStagesString(Res.ShaderStages), ")' is not assigned to any texture. All samplers should be assigned to textures when combined texture samplers are used.");
}
}
for (Uint32 i = 0; i < Desc.NumImmutableSamplers; ++i)
{
const auto& SamDesc = Desc.ImmutableSamplers[i];
auto assigned_sam_range = AssignedImtblSamplers.equal_range(SamDesc.SamplerOrTextureName);
auto it = assigned_sam_range.first;
while (it != assigned_sam_range.second)
{
if (it->second == SamDesc.ShaderStages)
break;
++it;
}
if (it == assigned_sam_range.second)
{
LOG_WARNING_MESSAGE("Immutable sampler '", SamDesc.SamplerOrTextureName, "' (", GetShaderStagesString(SamDesc.ShaderStages), ") is not assigned to any texture or sampler. All immutable samplers should be assigned to textures or samplers when combined texture samplers are used.");
}
}
}
}
#undef LOG_PRS_ERROR_AND_THROW
Uint32 FindImmutableSampler(const ImmutableSamplerDesc* ImtblSamplers,
Uint32 NumImtblSamplers,
SHADER_TYPE ShaderStages,
const char* ResourceName,
const char* SamplerSuffix)
{
for (Uint32 s = 0; s < NumImtblSamplers; ++s)
{
const auto& Sam = ImtblSamplers[s];
if (((Sam.ShaderStages & ShaderStages) != 0) && StreqSuff(ResourceName, Sam.SamplerOrTextureName, SamplerSuffix))
{
VERIFY((Sam.ShaderStages & ShaderStages) == ShaderStages,
"Immutable sampler uses only some of the stages that resource '", ResourceName,
"' is defined for. This error should've been caught by ValidatePipelineResourceSignatureDesc().");
return s;
}
}
return InvalidImmutableSamplerIndex;
}
/// Returns true if two pipeline resources are compatible
inline bool PipelineResourcesCompatible(const PipelineResourceDesc& lhs, const PipelineResourceDesc& rhs)
{
// Ignore resource names.
// clang-format off
return lhs.ShaderStages == rhs.ShaderStages &&
lhs.ArraySize == rhs.ArraySize &&
lhs.ResourceType == rhs.ResourceType &&
lhs.VarType == rhs.VarType &&
lhs.Flags == rhs.Flags;
// clang-format on
}
bool PipelineResourceSignaturesCompatible(const PipelineResourceSignatureDesc& Desc0,
const PipelineResourceSignatureDesc& Desc1) noexcept
{
if (Desc0.BindingIndex != Desc1.BindingIndex)
return false;
if (Desc0.NumResources != Desc1.NumResources)
return false;
for (Uint32 r = 0; r < Desc0.NumResources; ++r)
{
if (!PipelineResourcesCompatible(Desc0.Resources[r], Desc1.Resources[r]))
return false;
}
if (Desc0.NumImmutableSamplers != Desc1.NumImmutableSamplers)
return false;
for (Uint32 s = 0; s < Desc0.NumImmutableSamplers; ++s)
{
const auto& Samp0 = Desc0.ImmutableSamplers[s];
const auto& Samp1 = Desc1.ImmutableSamplers[s];
if (Samp0.ShaderStages != Samp1.ShaderStages ||
!(Samp0.Desc == Samp1.Desc))
return false;
}
return true;
}
size_t CalculatePipelineResourceSignatureDescHash(const PipelineResourceSignatureDesc& Desc) noexcept
{
if (Desc.NumResources == 0 && Desc.NumImmutableSamplers == 0)
return 0;
size_t Hash = ComputeHash(Desc.NumResources, Desc.NumImmutableSamplers, Desc.BindingIndex);
for (Uint32 i = 0; i < Desc.NumResources; ++i)
{
const auto& Res = Desc.Resources[i];
HashCombine(Hash, Uint32{Res.ShaderStages}, Res.ArraySize, Uint32{Res.ResourceType}, Uint32{Res.VarType}, Uint32{Res.Flags});
}
for (Uint32 i = 0; i < Desc.NumImmutableSamplers; ++i)
{
HashCombine(Hash, Uint32{Desc.ImmutableSamplers[i].ShaderStages}, Desc.ImmutableSamplers[i].Desc);
}
return Hash;
}
} // namespace Diligent
|