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
|
// d3d11-shader-object-layout.cpp
#include "d3d11-shader-object-layout.h"
namespace gfx
{
using namespace Slang;
namespace d3d11
{
ShaderObjectLayoutImpl::SubObjectRangeOffset::SubObjectRangeOffset(
slang::VariableLayoutReflection* varLayout)
: BindingOffset(varLayout)
{
if (auto pendingLayout = varLayout->getPendingDataLayout())
{
pendingOrdinaryData = (uint32_t)pendingLayout->getOffset(SLANG_PARAMETER_CATEGORY_UNIFORM);
}
}
ShaderObjectLayoutImpl::SubObjectRangeStride::SubObjectRangeStride(
slang::TypeLayoutReflection* typeLayout)
: BindingOffset(typeLayout)
{
if (auto pendingLayout = typeLayout->getPendingDataTypeLayout())
{
pendingOrdinaryData = (uint32_t)typeLayout->getStride();
}
}
Result ShaderObjectLayoutImpl::Builder::setElementTypeLayout(
slang::TypeLayoutReflection* typeLayout)
{
typeLayout = _unwrapParameterGroups(typeLayout, m_containerType);
m_elementTypeLayout = typeLayout;
m_totalOrdinaryDataSize = (uint32_t)typeLayout->getSize();
// Compute the binding ranges that are used to store
// the logical contents of the object in memory.
SlangInt bindingRangeCount = typeLayout->getBindingRangeCount();
for (SlangInt r = 0; r < bindingRangeCount; ++r)
{
slang::BindingType slangBindingType = typeLayout->getBindingRangeType(r);
SlangInt count = typeLayout->getBindingRangeBindingCount(r);
slang::TypeLayoutReflection* slangLeafTypeLayout =
typeLayout->getBindingRangeLeafTypeLayout(r);
BindingRangeInfo bindingRangeInfo;
bindingRangeInfo.bindingType = slangBindingType;
bindingRangeInfo.count = count;
bindingRangeInfo.isSpecializable = typeLayout->isBindingRangeSpecializable(r);
switch (slangBindingType)
{
case slang::BindingType::ConstantBuffer:
case slang::BindingType::ParameterBlock:
case slang::BindingType::ExistentialValue:
bindingRangeInfo.baseIndex = m_subObjectCount;
bindingRangeInfo.subObjectIndex = m_subObjectCount;
m_subObjectCount += count;
break;
case slang::BindingType::RawBuffer:
case slang::BindingType::MutableRawBuffer:
if (slangLeafTypeLayout->getType()->getElementType() != nullptr)
{
// A structured buffer occupies both a resource slot and
// a sub-object slot.
bindingRangeInfo.subObjectIndex = m_subObjectCount;
m_subObjectCount += count;
}
if (slangBindingType == slang::BindingType::RawBuffer)
{
bindingRangeInfo.baseIndex = m_srvCount;
m_srvCount += count;
m_srvRanges.add(r);
}
else
{
bindingRangeInfo.baseIndex = m_uavCount;
m_uavCount += count;
m_uavRanges.add(r);
}
break;
case slang::BindingType::Sampler:
bindingRangeInfo.baseIndex = m_samplerCount;
m_samplerCount += count;
m_samplerRanges.add(r);
break;
case slang::BindingType::CombinedTextureSampler:
break;
case slang::BindingType::MutableTexture:
case slang::BindingType::MutableTypedBuffer:
bindingRangeInfo.baseIndex = m_uavCount;
m_uavCount += count;
m_uavRanges.add(r);
break;
case slang::BindingType::VaryingInput:
break;
case slang::BindingType::VaryingOutput:
break;
default:
bindingRangeInfo.baseIndex = m_srvCount;
m_srvCount += count;
m_srvRanges.add(r);
break;
}
// We'd like to extract the information on the D3D11 shader
// register that this range should bind into.
//
// A binding range represents a logical member of the shader
// object type, and it may encompass zero or more *descriptor
// ranges* that describe how it is physically bound to pipeline
// state.
//
// If the current bindign range is backed by at least one descriptor
// range then we can query the register offset of that descriptor
// range. We expect that in the common case there will be exactly
// one descriptor range, and we can extract the information easily.
//
// TODO: we might eventually need to special-case our handling
// of combined texture-sampler ranges since they will need to
// store two different offsets.
//
if (typeLayout->getBindingRangeDescriptorRangeCount(r) != 0)
{
// The Slang reflection information organizes the descriptor ranges
// into "descriptor sets" but D3D11 has no notion like that so we
// expect all ranges belong to a single set.
//
SlangInt descriptorSetIndex = typeLayout->getBindingRangeDescriptorSetIndex(r);
SLANG_ASSERT(descriptorSetIndex == 0);
SlangInt descriptorRangeIndex = typeLayout->getBindingRangeFirstDescriptorRangeIndex(r);
auto registerOffset = typeLayout->getDescriptorSetDescriptorRangeIndexOffset(
descriptorSetIndex,
descriptorRangeIndex);
bindingRangeInfo.registerOffset = (uint32_t)registerOffset;
}
m_bindingRanges.add(bindingRangeInfo);
}
SlangInt subObjectRangeCount = typeLayout->getSubObjectRangeCount();
for (SlangInt r = 0; r < subObjectRangeCount; ++r)
{
SlangInt bindingRangeIndex = typeLayout->getSubObjectRangeBindingRangeIndex(r);
auto& bindingRange = m_bindingRanges[bindingRangeIndex];
auto slangBindingType = typeLayout->getBindingRangeType(bindingRangeIndex);
slang::TypeLayoutReflection* slangLeafTypeLayout =
typeLayout->getBindingRangeLeafTypeLayout(bindingRangeIndex);
SubObjectRangeInfo subObjectRange;
subObjectRange.bindingRangeIndex = bindingRangeIndex;
// We will use Slang reflection information to extract the offset and stride
// information for each sub-object range.
//
subObjectRange.offset = SubObjectRangeOffset(typeLayout->getSubObjectRangeOffset(r));
subObjectRange.stride = SubObjectRangeStride(slangLeafTypeLayout);
// A sub-object range can either represent a sub-object of a known
// type, like a `ConstantBuffer<Foo>` or `ParameterBlock<Foo>`
// *or* it can represent a sub-object of some existential type (e.g., `IBar`).
//
RefPtr<ShaderObjectLayoutImpl> subObjectLayout;
switch (slangBindingType)
{
default:
{
// In the case of `ConstantBuffer<X>` or `ParameterBlock<X>`
// we can construct a layout from the element type directly.
//
auto elementTypeLayout = slangLeafTypeLayout->getElementTypeLayout();
createForElementType(
m_renderer,
m_session,
elementTypeLayout,
subObjectLayout.writeRef());
}
break;
case slang::BindingType::ExistentialValue:
// In the case of an interface-type sub-object range, we can only
// construct a layout if we have static specialization information
// that tells us what type we expect to find in that range.
//
// The static specialization information is expected to take the
// form of a "pending" type layotu attached to the interface type
// of the leaf type layout.
//
if (auto pendingTypeLayout = slangLeafTypeLayout->getPendingDataTypeLayout())
{
createForElementType(
m_renderer,
m_session,
pendingTypeLayout,
subObjectLayout.writeRef());
// An interface-type range that includes ordinary data can
// increase the size of the ordinary data buffer we need to
// allocate for the parent object.
//
uint32_t ordinaryDataEnd =
subObjectRange.offset.pendingOrdinaryData +
(uint32_t)bindingRange.count * subObjectRange.stride.pendingOrdinaryData;
if (ordinaryDataEnd > m_totalOrdinaryDataSize)
{
m_totalOrdinaryDataSize = ordinaryDataEnd;
}
}
}
subObjectRange.layout = subObjectLayout;
m_subObjectRanges.add(subObjectRange);
}
return SLANG_OK;
}
SlangResult ShaderObjectLayoutImpl::Builder::build(ShaderObjectLayoutImpl** outLayout)
{
auto layout = RefPtr<ShaderObjectLayoutImpl>(new ShaderObjectLayoutImpl());
SLANG_RETURN_ON_FAIL(layout->_init(this));
returnRefPtrMove(outLayout, layout);
return SLANG_OK;
}
Result ShaderObjectLayoutImpl::createForElementType(
RendererBase* renderer,
slang::ISession* session,
slang::TypeLayoutReflection* elementType,
ShaderObjectLayoutImpl** outLayout)
{
Builder builder(renderer, session);
builder.setElementTypeLayout(elementType);
return builder.build(outLayout);
}
Result ShaderObjectLayoutImpl::_init(Builder const* builder)
{
auto renderer = builder->m_renderer;
initBase(renderer, builder->m_session, builder->m_elementTypeLayout);
m_bindingRanges = builder->m_bindingRanges;
m_srvRanges = builder->m_srvRanges;
m_uavRanges = builder->m_uavRanges;
m_samplerRanges = builder->m_samplerRanges;
m_srvCount = builder->m_srvCount;
m_samplerCount = builder->m_samplerCount;
m_uavCount = builder->m_uavCount;
m_subObjectCount = builder->m_subObjectCount;
m_subObjectRanges = builder->m_subObjectRanges;
m_totalOrdinaryDataSize = builder->m_totalOrdinaryDataSize;
m_containerType = builder->m_containerType;
return SLANG_OK;
}
Result RootShaderObjectLayoutImpl::Builder::build(RootShaderObjectLayoutImpl** outLayout)
{
RefPtr<RootShaderObjectLayoutImpl> layout = new RootShaderObjectLayoutImpl();
SLANG_RETURN_ON_FAIL(layout->_init(this));
returnRefPtrMove(outLayout, layout);
return SLANG_OK;
}
void RootShaderObjectLayoutImpl::Builder::addGlobalParams(
slang::VariableLayoutReflection* globalsLayout)
{
setElementTypeLayout(globalsLayout->getTypeLayout());
m_pendingDataOffset = BindingOffset(globalsLayout).pending;
}
void RootShaderObjectLayoutImpl::Builder::addEntryPoint(
SlangStage stage,
ShaderObjectLayoutImpl* entryPointLayout,
slang::EntryPointLayout* slangEntryPoint)
{
EntryPointInfo info;
info.layout = entryPointLayout;
info.offset = BindingOffset(slangEntryPoint->getVarLayout());
m_entryPoints.add(info);
}
Result RootShaderObjectLayoutImpl::create(
RendererBase* renderer,
slang::IComponentType* program,
slang::ProgramLayout* programLayout,
RootShaderObjectLayoutImpl** outLayout)
{
RootShaderObjectLayoutImpl::Builder builder(renderer, program, programLayout);
builder.addGlobalParams(programLayout->getGlobalParamsVarLayout());
SlangInt entryPointCount = programLayout->getEntryPointCount();
for (SlangInt e = 0; e < entryPointCount; ++e)
{
auto slangEntryPoint = programLayout->getEntryPointByIndex(e);
RefPtr<ShaderObjectLayoutImpl> entryPointLayout;
SLANG_RETURN_ON_FAIL(ShaderObjectLayoutImpl::createForElementType(
renderer,
program->getSession(),
slangEntryPoint->getTypeLayout(),
entryPointLayout.writeRef()));
builder.addEntryPoint(slangEntryPoint->getStage(), entryPointLayout, slangEntryPoint);
}
SLANG_RETURN_ON_FAIL(builder.build(outLayout));
return SLANG_OK;
}
Result RootShaderObjectLayoutImpl::_init(Builder const* builder)
{
auto renderer = builder->m_renderer;
SLANG_RETURN_ON_FAIL(Super::_init(builder));
m_program = builder->m_program;
m_programLayout = builder->m_programLayout;
m_entryPoints = builder->m_entryPoints;
m_pendingDataOffset = builder->m_pendingDataOffset;
m_slangSession = m_program->getSession();
return SLANG_OK;
}
} // namespace d3d11
} // namespace gfx
|