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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
|
// vk-shader-object-layout.h
#pragma once
#include "vk-base.h"
#include "vk-device.h"
#include "vk-helper-functions.h"
namespace gfx
{
using namespace Slang;
namespace vk
{
enum
{
kMaxDescriptorSets = 32,
};
class ShaderObjectLayoutImpl : public ShaderObjectLayoutBase
{
public:
// A shader object comprises three main kinds of state:
//
// * Zero or more bytes of ordinary ("uniform") data
// * Zero or more *bindings* for textures, buffers, and samplers
// * Zero or more *sub-objects* representing nested parameter blocks, etc.
//
// A shader object *layout* stores information that can be used to
// organize these different kinds of state and optimize access to them.
//
// For example, both texture/buffer/sampler bindings and sub-objects
// are organized into logical *binding ranges* by the Slang reflection
// API, and a shader object layout will store information about those
// ranges in a form that is usable for the Vulkan API:
struct BindingRangeInfo
{
slang::BindingType bindingType;
Index count;
Index baseIndex;
/// An index into the sub-object array if this binding range is treated
/// as a sub-object.
Index subObjectIndex;
/// The `binding` offset to apply for this range
uint32_t bindingOffset;
/// The `set` offset to apply for this range
uint32_t setOffset;
// Note: The 99% case is that `setOffset` will be zero. For any shader object
// that was allocated from an ordinary Slang type (anything other than a root
// shader object in fact), all of the bindings will have been allocated into
// a single logical descriptor set.
//
// TODO: Ideally we could refactor so that only the root shader object layout
// stores a set offset for its binding ranges, and all other objects skip
// storing a field that never actually matters.
// Is this binding range representing a specialization point, such as
// an existential value or a ParameterBlock<IFoo>.
bool isSpecializable;
};
// Sometimes we just want to iterate over the ranges that represent
// sub-objects while skipping over the others, because sub-object
// ranges often require extra handling or more state.
//
// For that reason we also store pre-computed information about each
// sub-object range.
/// Offset information for a sub-object range
struct SubObjectRangeOffset : BindingOffset
{
SubObjectRangeOffset() {}
SubObjectRangeOffset(slang::VariableLayoutReflection* varLayout)
: BindingOffset(varLayout)
{
if (auto pendingLayout = varLayout->getPendingDataLayout())
{
pendingOrdinaryData =
(uint32_t)pendingLayout->getOffset(SLANG_PARAMETER_CATEGORY_UNIFORM);
}
}
/// The offset for "pending" ordinary data related to this range
uint32_t pendingOrdinaryData = 0;
};
/// Stride information for a sub-object range
struct SubObjectRangeStride : BindingOffset
{
SubObjectRangeStride() {}
SubObjectRangeStride(slang::TypeLayoutReflection* typeLayout)
{
if (auto pendingLayout = typeLayout->getPendingDataTypeLayout())
{
pendingOrdinaryData = (uint32_t)pendingLayout->getStride();
}
}
/// The stride for "pending" ordinary data related to this range
uint32_t pendingOrdinaryData = 0;
};
/// Information about a logical binding range as reported by Slang reflection
struct SubObjectRangeInfo
{
/// The index of the binding range that corresponds to this sub-object range
Index bindingRangeIndex;
/// The layout expected for objects bound to this range (if known)
RefPtr<ShaderObjectLayoutImpl> layout;
/// The offset to use when binding the first object in this range
SubObjectRangeOffset offset;
/// Stride between consecutive objects in this range
SubObjectRangeStride stride;
};
struct DescriptorSetInfo
{
List<VkDescriptorSetLayoutBinding> vkBindings;
Slang::Int space = -1;
VkDescriptorSetLayout descriptorSetLayout = VK_NULL_HANDLE;
};
struct Builder
{
public:
Builder(DeviceImpl* renderer, slang::ISession* session)
: m_renderer(renderer), m_session(session)
{
}
DeviceImpl* m_renderer;
slang::ISession* m_session;
slang::TypeLayoutReflection* m_elementTypeLayout;
/// The container type of this shader object. When `m_containerType` is
/// `StructuredBuffer` or `UnsizedArray`, this shader object represents a collection
/// instead of a single object.
ShaderObjectContainerType m_containerType = ShaderObjectContainerType::None;
List<BindingRangeInfo> m_bindingRanges;
List<SubObjectRangeInfo> m_subObjectRanges;
Index m_resourceViewCount = 0;
Index m_samplerCount = 0;
Index m_combinedTextureSamplerCount = 0;
Index m_subObjectCount = 0;
Index m_varyingInputCount = 0;
Index m_varyingOutputCount = 0;
List<DescriptorSetInfo> m_descriptorSetBuildInfos;
Dictionary<Index, Index> m_mapSpaceToDescriptorSetIndex;
/// The number of descriptor sets allocated by child/descendent objects
uint32_t m_childDescriptorSetCount = 0;
/// The total number of `binding`s consumed by this object and its children/descendents
uint32_t m_totalBindingCount = 0;
/// The push-constant ranges that belong to this object itself (if any)
List<VkPushConstantRange> m_ownPushConstantRanges;
/// The number of push-constant ranges owned by child/descendent objects
uint32_t m_childPushConstantRangeCount = 0;
uint32_t m_totalOrdinaryDataSize = 0;
Index findOrAddDescriptorSet(Index space);
static VkDescriptorType _mapDescriptorType(slang::BindingType slangBindingType);
/// Add any descriptor ranges implied by this object containing a leaf
/// sub-object described by `typeLayout`, at the given `offset`.
void _addDescriptorRangesAsValue(
slang::TypeLayoutReflection* typeLayout,
BindingOffset const& offset);
/// Add the descriptor ranges implied by a `ConstantBuffer<X>` where `X` is
/// described by `elementTypeLayout`.
///
/// The `containerOffset` and `elementOffset` are the binding offsets that
/// should apply to the buffer itself and the contents of the buffer, respectively.
///
void _addDescriptorRangesAsConstantBuffer(
slang::TypeLayoutReflection* elementTypeLayout,
BindingOffset const& containerOffset,
BindingOffset const& elementOffset);
/// Add the descriptor ranges implied by a `PushConstantBuffer<X>` where `X` is
/// described by `elementTypeLayout`.
///
/// The `containerOffset` and `elementOffset` are the binding offsets that
/// should apply to the buffer itself and the contents of the buffer, respectively.
///
void _addDescriptorRangesAsPushConstantBuffer(
slang::TypeLayoutReflection* elementTypeLayout,
BindingOffset const& containerOffset,
BindingOffset const& elementOffset);
/// Add binding ranges to this shader object layout, as implied by the given
/// `typeLayout`
void addBindingRanges(slang::TypeLayoutReflection* typeLayout);
Result setElementTypeLayout(slang::TypeLayoutReflection* typeLayout);
SlangResult build(ShaderObjectLayoutImpl** outLayout);
};
static Result createForElementType(
DeviceImpl* renderer,
slang::ISession* session,
slang::TypeLayoutReflection* elementType,
ShaderObjectLayoutImpl** outLayout);
~ShaderObjectLayoutImpl();
/// Get the number of descriptor sets that are allocated for this object itself
/// (if it needed to be bound as a parameter block).
///
uint32_t getOwnDescriptorSetCount() { return uint32_t(m_descriptorSetInfos.getCount()); }
/// Get information about the descriptor sets that would be allocated to
/// represent this object itself as a parameter block.
///
List<DescriptorSetInfo> const& getOwnDescriptorSets() { return m_descriptorSetInfos; }
/// Get the number of descriptor sets that would need to be allocated and bound
/// to represent the children of this object if it were bound as a parameter
/// block.
///
/// To a first approximation, this is the number of (transitive) children
/// that are declared as `ParameterBlock<X>`.
///
uint32_t getChildDescriptorSetCount() { return m_childDescriptorSetCount; }
/// Get the total number of descriptor sets that would need to be allocated and bound
/// to represent this object and its children (transitively) as a parameter block.
///
uint32_t getTotalDescriptorSetCount()
{
return getOwnDescriptorSetCount() + getChildDescriptorSetCount();
}
/// Get the total number of `binding`s required to represent this type and its
/// (transitive) children.
///
/// Note that this count does *not* include bindings that would be part of child
/// parameter blocks, nor does it include the binding for an ordinary data buffer,
/// if one is needed.
///
uint32_t getTotalBindingCount() { return m_totalBindingCount; }
/// Get the list of push constant ranges required to bind the state of this object itself.
List<VkPushConstantRange> const& getOwnPushConstantRanges() const
{
return m_ownPushConstantRanges;
}
/// Get the number of push constant ranges required to bind the state of this object itself.
uint32_t getOwnPushConstantRangeCount() { return (uint32_t)m_ownPushConstantRanges.getCount(); }
/// Get the number of push constant ranges required to bind the state of the (transitive)
/// children of this object.
uint32_t getChildPushConstantRangeCount() { return m_childPushConstantRangeCount; }
/// Get the total number of push constant ranges required to bind the state of this object
/// and its (transitive) children.
uint32_t getTotalPushConstantRangeCount()
{
return getOwnPushConstantRangeCount() + getChildPushConstantRangeCount();
}
uint32_t getTotalOrdinaryDataSize() const { return m_totalOrdinaryDataSize; }
List<BindingRangeInfo> const& getBindingRanges() { return m_bindingRanges; }
Index getBindingRangeCount() { return m_bindingRanges.getCount(); }
BindingRangeInfo const& getBindingRange(Index index) { return m_bindingRanges[index]; }
Index getResourceViewCount() { return m_resourceViewCount; }
Index getSamplerCount() { return m_samplerCount; }
Index getCombinedTextureSamplerCount() { return m_combinedTextureSamplerCount; }
Index getSubObjectCount() { return m_subObjectCount; }
SubObjectRangeInfo const& getSubObjectRange(Index index) { return m_subObjectRanges[index]; }
List<SubObjectRangeInfo> const& getSubObjectRanges() { return m_subObjectRanges; }
DeviceImpl* getDevice();
slang::TypeReflection* getType() { return m_elementTypeLayout->getType(); }
protected:
Result _init(Builder const* builder);
List<DescriptorSetInfo> m_descriptorSetInfos;
List<BindingRangeInfo> m_bindingRanges;
Index m_resourceViewCount = 0;
Index m_samplerCount = 0;
Index m_combinedTextureSamplerCount = 0;
Index m_subObjectCount = 0;
List<VkPushConstantRange> m_ownPushConstantRanges;
uint32_t m_childPushConstantRangeCount = 0;
uint32_t m_childDescriptorSetCount = 0;
uint32_t m_totalBindingCount = 0;
uint32_t m_totalOrdinaryDataSize = 0;
List<SubObjectRangeInfo> m_subObjectRanges;
};
class EntryPointLayout : public ShaderObjectLayoutImpl
{
typedef ShaderObjectLayoutImpl Super;
public:
struct Builder : Super::Builder
{
Builder(DeviceImpl* device, slang::ISession* session)
: Super::Builder(device, session)
{
}
Result build(EntryPointLayout** outLayout);
void addEntryPointParams(slang::EntryPointLayout* entryPointLayout);
slang::EntryPointLayout* m_slangEntryPointLayout = nullptr;
VkShaderStageFlags m_shaderStageFlag;
};
Result _init(Builder const* builder);
VkShaderStageFlags getShaderStageFlag() const { return m_shaderStageFlag; }
slang::EntryPointLayout* getSlangLayout() const { return m_slangEntryPointLayout; };
slang::EntryPointLayout* m_slangEntryPointLayout;
VkShaderStageFlags m_shaderStageFlag;
};
class RootShaderObjectLayout : public ShaderObjectLayoutImpl
{
typedef ShaderObjectLayoutImpl Super;
public:
~RootShaderObjectLayout();
/// Information stored for each entry point of the program
struct EntryPointInfo
{
/// Layout of the entry point
RefPtr<EntryPointLayout> layout;
/// Offset for binding the entry point, relative to the start of the program
BindingOffset offset;
};
struct Builder : Super::Builder
{
Builder(
DeviceImpl* renderer,
slang::IComponentType* program,
slang::ProgramLayout* programLayout)
: Super::Builder(renderer, program->getSession())
, m_program(program)
, m_programLayout(programLayout)
{
}
Result build(RootShaderObjectLayout** outLayout);
void addGlobalParams(slang::VariableLayoutReflection* globalsLayout);
void addEntryPoint(EntryPointLayout* entryPointLayout);
slang::IComponentType* m_program;
slang::ProgramLayout* m_programLayout;
List<EntryPointInfo> m_entryPoints;
/// Offset to apply to "pending" data from this object, sub-objects, and entry points
SimpleBindingOffset m_pendingDataOffset;
};
Index findEntryPointIndex(VkShaderStageFlags stage);
EntryPointInfo const& getEntryPoint(Index index) { return m_entryPoints[index]; }
List<EntryPointInfo> const& getEntryPoints() const { return m_entryPoints; }
static Result create(
DeviceImpl* renderer,
slang::IComponentType* program,
slang::ProgramLayout* programLayout,
RootShaderObjectLayout** outLayout);
SimpleBindingOffset const& getPendingDataOffset() const { return m_pendingDataOffset; }
slang::IComponentType* getSlangProgram() const { return m_program; }
slang::ProgramLayout* getSlangProgramLayout() const { return m_programLayout; }
/// Get all of the push constant ranges that will be bound for this object and all
/// (transitive) sub-objects
List<VkPushConstantRange> const& getAllPushConstantRanges() { return m_allPushConstantRanges; }
protected:
Result _init(Builder const* builder);
/// Add all the descriptor sets implied by this root object and sub-objects
Result addAllDescriptorSets();
/// Recurisvely add descriptor sets defined by `layout` and sub-objects
Result addAllDescriptorSetsRec(ShaderObjectLayoutImpl* layout);
/// Recurisvely add descriptor sets defined by sub-objects of `layout`
Result addChildDescriptorSetsRec(ShaderObjectLayoutImpl* layout);
/// Add all the push-constant ranges implied by this root object and sub-objects
Result addAllPushConstantRanges();
/// Recurisvely add push-constant ranges defined by `layout` and sub-objects
Result addAllPushConstantRangesRec(ShaderObjectLayoutImpl* layout);
/// Recurisvely add push-constant ranges defined by sub-objects of `layout`
Result addChildPushConstantRangesRec(ShaderObjectLayoutImpl* layout);
public:
ComPtr<slang::IComponentType> m_program;
slang::ProgramLayout* m_programLayout = nullptr;
List<EntryPointInfo> m_entryPoints;
VkPipelineLayout m_pipelineLayout = VK_NULL_HANDLE;
Array<VkDescriptorSetLayout, kMaxDescriptorSets> m_vkDescriptorSetLayouts;
List<VkPushConstantRange> m_allPushConstantRanges;
uint32_t m_totalPushConstantSize = 0;
SimpleBindingOffset m_pendingDataOffset;
DeviceImpl* m_renderer = nullptr;
};
} // namespace vk
} // namespace gfx
|