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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
|
/*
* 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.
*/
#pragma once
/// \file
/// Implementation of the Diligent::ShaderBindingTableBase template class
#include <unordered_map>
#include <cstring>
#include "ShaderBindingTable.h"
#include "TopLevelASBase.hpp"
#include "DeviceObjectBase.hpp"
#include "RenderDeviceBase.hpp"
#include "StringPool.hpp"
#include "HashUtils.hpp"
namespace Diligent
{
/// Validates SBT description and throws an exception in case of an error.
void ValidateShaderBindingTableDesc(const ShaderBindingTableDesc& Desc, Uint32 ShaderGroupHandleSize, Uint32 MaxShaderRecordStride) noexcept(false);
/// Template class implementing base functionality of the shader binding table object.
/// \tparam EngineImplTraits - Engine implementation type traits.
template <typename EngineImplTraits>
class ShaderBindingTableBase : public DeviceObjectBase<typename EngineImplTraits::ShaderBindingTableInterface, typename EngineImplTraits::RenderDeviceImplType, ShaderBindingTableDesc>
{
public:
// Base interface this class inherits (IShaderBindingTableD3D12, IShaderBindingTableVk, etc.)
using BaseInterface = typename EngineImplTraits::ShaderBindingTableInterface;
// Render device implementation type (RenderDeviceD3D12Impl, RenderDeviceVkImpl, etc.).
using RenderDeviceImplType = typename EngineImplTraits::RenderDeviceImplType;
// Pipeline state implementation type (PipelineStateD3D12Impl, PipelineStateVkImpl, etc.).
using PipelineStateImplType = typename EngineImplTraits::PipelineStateImplType;
// Top-level AS implementation type (TopLevelASD3D12Impl, TopLevelASVkImpl, etc.).
using TopLevelASImplType = typename EngineImplTraits::TopLevelASImplType;
using TDeviceObjectBase = DeviceObjectBase<BaseInterface, RenderDeviceImplType, ShaderBindingTableDesc>;
/// \param pRefCounters - Reference counters object that controls the lifetime of this SBT.
/// \param pDevice - Pointer to the device.
/// \param Desc - SBT description.
/// \param bIsDeviceInternal - Flag indicating if the BLAS is an internal device object and
/// must not keep a strong reference to the device.
ShaderBindingTableBase(IReferenceCounters* pRefCounters,
RenderDeviceImplType* pDevice,
const ShaderBindingTableDesc& Desc,
bool bIsDeviceInternal = false) :
TDeviceObjectBase{pRefCounters, pDevice, Desc, bIsDeviceInternal}
{
const auto& DeviceProps = this->m_pDevice->GetProperties();
ValidateShaderBindingTableDesc(this->m_Desc, DeviceProps.ShaderGroupHandleSize, DeviceProps.MaxShaderRecordStride);
this->m_pPSO = ValidatedCast<PipelineStateImplType>(this->m_Desc.pPSO);
this->m_ShaderRecordSize = this->m_pPSO->GetRayTracingPipelineDesc().ShaderRecordSize;
this->m_ShaderRecordStride = this->m_ShaderRecordSize + DeviceProps.ShaderGroupHandleSize;
}
~ShaderBindingTableBase()
{
}
IMPLEMENT_QUERY_INTERFACE_IN_PLACE(IID_ShaderBindingTable, TDeviceObjectBase)
void DILIGENT_CALL_TYPE Reset(IPipelineState* pPSO) override final
{
#ifdef DILIGENT_DEVELOPMENT
this->m_DbgHitGroupBindings.clear();
#endif
this->m_RayGenShaderRecord.clear();
this->m_MissShadersRecord.clear();
this->m_CallableShadersRecord.clear();
this->m_HitGroupsRecord.clear();
this->m_Changed = true;
this->m_pPSO = nullptr;
this->m_Desc.pPSO = pPSO;
const auto& DeviceProps = this->m_pDevice->GetProperties();
try
{
ValidateShaderBindingTableDesc(this->m_Desc, DeviceProps.ShaderGroupHandleSize, DeviceProps.MaxShaderRecordStride);
}
catch (const std::runtime_error&)
{
return;
}
this->m_pPSO = ValidatedCast<PipelineStateImplType>(this->m_Desc.pPSO);
this->m_ShaderRecordSize = this->m_pPSO->GetRayTracingPipelineDesc().ShaderRecordSize;
this->m_ShaderRecordStride = this->m_ShaderRecordSize + DeviceProps.ShaderGroupHandleSize;
}
void DILIGENT_CALL_TYPE ResetHitGroups() override final
{
#ifdef DILIGENT_DEVELOPMENT
this->m_DbgHitGroupBindings.clear();
#endif
this->m_HitGroupsRecord.clear();
this->m_Changed = true;
}
void DILIGENT_CALL_TYPE BindRayGenShader(const char* pShaderGroupName, const void* pData, Uint32 DataSize) override final
{
VERIFY_EXPR((pData == nullptr) == (DataSize == 0));
VERIFY_EXPR((pData == nullptr) || (DataSize == this->m_ShaderRecordSize));
this->m_RayGenShaderRecord.resize(this->m_ShaderRecordStride, Uint8{EmptyElem});
this->m_pPSO->CopyShaderHandle(pShaderGroupName, this->m_RayGenShaderRecord.data(), this->m_ShaderRecordStride);
const Uint32 GroupSize = this->m_pDevice->GetProperties().ShaderGroupHandleSize;
std::memcpy(this->m_RayGenShaderRecord.data() + GroupSize, pData, DataSize);
this->m_Changed = true;
}
void DILIGENT_CALL_TYPE BindMissShader(const char* pShaderGroupName, Uint32 MissIndex, const void* pData, Uint32 DataSize) override final
{
VERIFY_EXPR((pData == nullptr) == (DataSize == 0));
VERIFY_EXPR((pData == nullptr) || (DataSize == this->m_ShaderRecordSize));
const Uint32 GroupSize = this->m_pDevice->GetProperties().ShaderGroupHandleSize;
const size_t Stride = this->m_ShaderRecordStride;
const size_t Offset = MissIndex * Stride;
this->m_MissShadersRecord.resize(std::max(this->m_MissShadersRecord.size(), Offset + Stride), Uint8{EmptyElem});
this->m_pPSO->CopyShaderHandle(pShaderGroupName, this->m_MissShadersRecord.data() + Offset, Stride);
std::memcpy(this->m_MissShadersRecord.data() + Offset + GroupSize, pData, DataSize);
this->m_Changed = true;
}
void DILIGENT_CALL_TYPE BindHitGroupByIndex(Uint32 BindingIndex,
const char* pShaderGroupName,
const void* pData,
Uint32 DataSize) override final
{
VERIFY_EXPR((pData == nullptr) == (DataSize == 0));
VERIFY_EXPR((pData == nullptr) || (DataSize == this->m_ShaderRecordSize));
const size_t Stride = this->m_ShaderRecordStride;
const Uint32 GroupSize = this->m_pDevice->GetProperties().ShaderGroupHandleSize;
const size_t Offset = BindingIndex * Stride;
this->m_HitGroupsRecord.resize(std::max(this->m_HitGroupsRecord.size(), Offset + Stride), Uint8{EmptyElem});
this->m_pPSO->CopyShaderHandle(pShaderGroupName, this->m_HitGroupsRecord.data() + Offset, Stride);
std::memcpy(this->m_HitGroupsRecord.data() + Offset + GroupSize, pData, DataSize);
this->m_Changed = true;
#ifdef DILIGENT_DEVELOPMENT
OnBindHitGroup(nullptr, BindingIndex);
#endif
}
void DILIGENT_CALL_TYPE BindHitGroupForGeometry(ITopLevelAS* pTLAS,
const char* pInstanceName,
const char* pGeometryName,
Uint32 RayOffsetInHitGroupIndex,
const char* pShaderGroupName,
const void* pData,
Uint32 DataSize) override final
{
VERIFY_EXPR((pData == nullptr) == (DataSize == 0));
VERIFY_EXPR((pData == nullptr) || (DataSize == this->m_ShaderRecordSize));
VERIFY_EXPR(pTLAS != nullptr);
auto* const pTLASImpl = ValidatedCast<TopLevelASImplType>(pTLAS);
const auto Info = pTLASImpl->GetBuildInfo();
const auto Desc = pTLASImpl->GetInstanceDesc(pInstanceName);
VERIFY_EXPR(Info.BindingMode == HIT_GROUP_BINDING_MODE_PER_GEOMETRY);
VERIFY_EXPR(RayOffsetInHitGroupIndex < Info.HitGroupStride);
VERIFY_EXPR(Desc.ContributionToHitGroupIndex != ~0u);
VERIFY_EXPR(Desc.pBLAS != nullptr);
const Uint32 InstanceOffset = Desc.ContributionToHitGroupIndex;
const Uint32 GeometryIndex = Desc.pBLAS->GetGeometryIndex(pGeometryName);
VERIFY_EXPR(GeometryIndex != INVALID_INDEX);
const Uint32 Index = InstanceOffset + GeometryIndex * Info.HitGroupStride + RayOffsetInHitGroupIndex;
const size_t Stride = this->m_ShaderRecordStride;
const Uint32 GroupSize = this->m_pDevice->GetProperties().ShaderGroupHandleSize;
const size_t Offset = Index * Stride;
this->m_HitGroupsRecord.resize(std::max(this->m_HitGroupsRecord.size(), Offset + Stride), Uint8{EmptyElem});
this->m_pPSO->CopyShaderHandle(pShaderGroupName, this->m_HitGroupsRecord.data() + Offset, Stride);
std::memcpy(this->m_HitGroupsRecord.data() + Offset + GroupSize, pData, DataSize);
this->m_Changed = true;
#ifdef DILIGENT_DEVELOPMENT
VERIFY_EXPR(Index >= Info.FirstContributionToHitGroupIndex && Index <= Info.LastContributionToHitGroupIndex);
OnBindHitGroup(pTLASImpl, Index);
#endif
}
void DILIGENT_CALL_TYPE BindHitGroupForInstance(ITopLevelAS* pTLAS,
const char* pInstanceName,
Uint32 RayOffsetInHitGroupIndex,
const char* pShaderGroupName,
const void* pData,
Uint32 DataSize) override final
{
VERIFY_EXPR((pData == nullptr) == (DataSize == 0));
VERIFY_EXPR((pData == nullptr) || (DataSize == this->m_ShaderRecordSize));
VERIFY_EXPR(pTLAS != nullptr);
auto* const pTLASImpl = ValidatedCast<TopLevelASImplType>(pTLAS);
const auto Info = pTLASImpl->GetBuildInfo();
const auto Desc = pTLASImpl->GetInstanceDesc(pInstanceName);
VERIFY_EXPR(Info.BindingMode == HIT_GROUP_BINDING_MODE_PER_GEOMETRY ||
Info.BindingMode == HIT_GROUP_BINDING_MODE_PER_INSTANCE);
VERIFY_EXPR(RayOffsetInHitGroupIndex < Info.HitGroupStride);
VERIFY_EXPR(Desc.ContributionToHitGroupIndex != INVALID_INDEX);
VERIFY_EXPR(Desc.pBLAS != nullptr);
const Uint32 InstanceOffset = Desc.ContributionToHitGroupIndex;
Uint32 GeometryCount = 0;
switch (Info.BindingMode)
{
// clang-format off
case HIT_GROUP_BINDING_MODE_PER_GEOMETRY: GeometryCount = Desc.pBLAS->GetActualGeometryCount(); break;
case HIT_GROUP_BINDING_MODE_PER_INSTANCE: GeometryCount = 1; break;
default: UNEXPECTED("unknown binding mode");
// clang-format on
}
const Uint32 BeginIndex = InstanceOffset;
const size_t EndIndex = InstanceOffset + GeometryCount * Info.HitGroupStride;
const Uint32 GroupSize = this->m_pDevice->GetProperties().ShaderGroupHandleSize;
const size_t Stride = this->m_ShaderRecordStride;
this->m_HitGroupsRecord.resize(std::max(this->m_HitGroupsRecord.size(), EndIndex * Stride), Uint8{EmptyElem});
this->m_Changed = true;
for (Uint32 i = 0; i < GeometryCount; ++i)
{
Uint32 Index = BeginIndex + i * Info.HitGroupStride + RayOffsetInHitGroupIndex;
size_t Offset = Index * Stride;
this->m_pPSO->CopyShaderHandle(pShaderGroupName, this->m_HitGroupsRecord.data() + Offset, Stride);
std::memcpy(this->m_HitGroupsRecord.data() + Offset + GroupSize, pData, DataSize);
#ifdef DILIGENT_DEVELOPMENT
VERIFY_EXPR(Index >= Info.FirstContributionToHitGroupIndex && Index <= Info.LastContributionToHitGroupIndex);
OnBindHitGroup(pTLASImpl, Index);
#endif
}
}
void DILIGENT_CALL_TYPE BindHitGroupForTLAS(ITopLevelAS* pTLAS,
Uint32 RayOffsetInHitGroupIndex,
const char* pShaderGroupName,
const void* pData,
Uint32 DataSize) override final
{
VERIFY_EXPR((pData == nullptr) == (DataSize == 0));
VERIFY_EXPR((pData == nullptr) || (DataSize == this->m_ShaderRecordSize));
VERIFY_EXPR(pTLAS != nullptr);
auto* pTLASImpl = ValidatedCast<TopLevelASImplType>(pTLAS);
const auto Info = pTLASImpl->GetBuildInfo();
VERIFY_EXPR(Info.BindingMode == HIT_GROUP_BINDING_MODE_PER_GEOMETRY ||
Info.BindingMode == HIT_GROUP_BINDING_MODE_PER_INSTANCE ||
Info.BindingMode == HIT_GROUP_BINDING_MODE_PER_TLAS);
VERIFY_EXPR(RayOffsetInHitGroupIndex < Info.HitGroupStride);
const Uint32 GroupSize = this->m_pDevice->GetProperties().ShaderGroupHandleSize;
const size_t Stride = this->m_ShaderRecordStride;
this->m_HitGroupsRecord.resize(std::max(this->m_HitGroupsRecord.size(), (Info.LastContributionToHitGroupIndex + 1) * Stride), Uint8{EmptyElem});
this->m_Changed = true;
for (Uint32 Index = RayOffsetInHitGroupIndex + Info.FirstContributionToHitGroupIndex;
Index <= Info.LastContributionToHitGroupIndex;
Index += Info.HitGroupStride)
{
const size_t Offset = Index * Stride;
this->m_pPSO->CopyShaderHandle(pShaderGroupName, this->m_HitGroupsRecord.data() + Offset, Stride);
std::memcpy(this->m_HitGroupsRecord.data() + Offset + GroupSize, pData, DataSize);
#ifdef DILIGENT_DEVELOPMENT
OnBindHitGroup(pTLASImpl, Index);
#endif
}
}
void DILIGENT_CALL_TYPE BindCallableShader(const char* pShaderGroupName,
Uint32 CallableIndex,
const void* pData,
Uint32 DataSize) override final
{
VERIFY_EXPR((pData == nullptr) == (DataSize == 0));
VERIFY_EXPR((pData == nullptr) || (DataSize == this->m_ShaderRecordSize));
const Uint32 GroupSize = this->m_pDevice->GetProperties().ShaderGroupHandleSize;
const size_t Offset = CallableIndex * this->m_ShaderRecordStride;
this->m_CallableShadersRecord.resize(std::max(this->m_CallableShadersRecord.size(), Offset + this->m_ShaderRecordStride), Uint8{EmptyElem});
this->m_pPSO->CopyShaderHandle(pShaderGroupName, this->m_CallableShadersRecord.data() + Offset, this->m_ShaderRecordStride);
std::memcpy(this->m_CallableShadersRecord.data() + Offset + GroupSize, pData, DataSize);
this->m_Changed = true;
}
Bool DILIGENT_CALL_TYPE Verify(VERIFY_SBT_FLAGS Flags) const override final
{
#ifdef DILIGENT_DEVELOPMENT
static_assert(EmptyElem != 0, "must not be zero");
const auto Stride = this->m_ShaderRecordStride;
const auto ShSize = this->m_pDevice->GetProperties().ShaderGroupHandleSize;
const auto FindPattern = [&](const std::vector<Uint8>& Data, const char* GroupName) -> bool //
{
for (size_t i = 0; i < Data.size(); i += Stride)
{
if (Flags & VERIFY_SBT_FLAG_SHADER_ONLY)
{
Uint32 Count = 0;
for (size_t j = 0; j < ShSize; ++j)
Count += (Data[i + j] == EmptyElem);
if (Count == ShSize)
{
LOG_INFO_MESSAGE("Shader binding table '", this->m_Desc.Name, "' is not valid: shader in '", GroupName, "'(", i / Stride, ") is not bound.");
return false;
}
}
if ((Flags & VERIFY_SBT_FLAG_SHADER_RECORD) && this->m_ShaderRecordSize > 0)
{
Uint32 Count = 0;
for (size_t j = ShSize; j < Stride; ++j)
Count += (Data[i + j] == EmptyElem);
// shader record data may not be used in the shader
if (Count == Stride - ShSize)
{
LOG_INFO_MESSAGE("Shader binding table '", this->m_Desc.Name, "' is not valid: shader record data in '", GroupName, "' (", i / Stride, ") is not initialized.");
return false;
}
}
}
return true;
};
if (m_RayGenShaderRecord.empty())
{
LOG_INFO_MESSAGE("Shader binding table '", this->m_Desc.Name, "' is not valid: ray generation shader is not bound.");
return false;
}
if (Flags & VERIFY_SBT_FLAG_TLAS)
{
for (size_t i = 0; i < m_DbgHitGroupBindings.size(); ++i)
{
auto& Binding = m_DbgHitGroupBindings[i];
auto pTLAS = Binding.pTLAS.Lock();
if (!Binding.IsBound)
{
LOG_INFO_MESSAGE("Shader binding table '", this->m_Desc.Name, "' is not valid: hit group at index (", i, ") is not bound.");
return false;
}
if (!pTLAS)
{
LOG_INFO_MESSAGE("Shader binding table '", this->m_Desc.Name, "' is not valid: TLAS that was used to bind hit group at index (", i, ") was deleted.");
return false;
}
if (pTLAS->GetVersion() != Binding.Version)
{
LOG_INFO_MESSAGE("Shader binding table '", this->m_Desc.Name, "' is not valid: TLAS that was used to bind hit group at index '(", i,
") with name '", pTLAS->GetDesc().Name, " was changed and no longer compatible with SBT.");
return false;
}
}
}
bool valid = true;
valid = valid && FindPattern(m_RayGenShaderRecord, "ray generation");
valid = valid && FindPattern(m_MissShadersRecord, "miss");
valid = valid && FindPattern(m_CallableShadersRecord, "callable");
valid = valid && FindPattern(m_HitGroupsRecord, "hit groups");
return valid;
#else
return true;
#endif // DILIGENT_DEVELOPMENT
}
struct BindingTable
{
const void* pData = nullptr;
Uint32 Size = 0;
Uint32 Offset = 0;
Uint32 Stride = 0;
};
void GetData(IBuffer*& pSBTBuffer,
BindingTable& RaygenShaderBindingTable,
BindingTable& MissShaderBindingTable,
BindingTable& HitShaderBindingTable,
BindingTable& CallableShaderBindingTable)
{
const auto ShaderGroupBaseAlignment = this->m_pDevice->GetProperties().ShaderGroupBaseAlignment;
const auto AlignToLarger = [ShaderGroupBaseAlignment](size_t offset) -> Uint32 {
return AlignUp(static_cast<Uint32>(offset), ShaderGroupBaseAlignment);
};
const Uint32 RayGenOffset = 0;
const Uint32 MissShaderOffset = AlignToLarger(m_RayGenShaderRecord.size());
const Uint32 HitGroupOffset = AlignToLarger(MissShaderOffset + m_MissShadersRecord.size());
const Uint32 CallableShadersOffset = AlignToLarger(HitGroupOffset + m_HitGroupsRecord.size());
const Uint32 BufSize = AlignToLarger(CallableShadersOffset + m_CallableShadersRecord.size());
// recreate buffer
if (this->m_pBuffer == nullptr || this->m_pBuffer->GetDesc().uiSizeInBytes < BufSize)
{
this->m_pBuffer = nullptr;
String BuffName = String{this->m_Desc.Name} + " - internal buffer";
BufferDesc BuffDesc;
BuffDesc.Name = BuffName.c_str();
BuffDesc.Usage = USAGE_DEFAULT;
BuffDesc.BindFlags = BIND_RAY_TRACING;
BuffDesc.uiSizeInBytes = BufSize;
this->m_pDevice->CreateBuffer(BuffDesc, nullptr, &this->m_pBuffer);
VERIFY_EXPR(this->m_pBuffer != nullptr);
}
if (this->m_pBuffer == nullptr)
return; // Something went wrong
pSBTBuffer = this->m_pBuffer;
if (!m_RayGenShaderRecord.empty())
{
RaygenShaderBindingTable.pData = this->m_Changed ? m_RayGenShaderRecord.data() : nullptr;
RaygenShaderBindingTable.Offset = RayGenOffset;
RaygenShaderBindingTable.Size = static_cast<Uint32>(m_RayGenShaderRecord.size());
RaygenShaderBindingTable.Stride = this->m_ShaderRecordStride;
}
if (!m_MissShadersRecord.empty())
{
MissShaderBindingTable.pData = this->m_Changed ? m_MissShadersRecord.data() : nullptr;
MissShaderBindingTable.Offset = MissShaderOffset;
MissShaderBindingTable.Size = static_cast<Uint32>(m_MissShadersRecord.size());
MissShaderBindingTable.Stride = this->m_ShaderRecordStride;
}
if (!m_HitGroupsRecord.empty())
{
HitShaderBindingTable.pData = this->m_Changed ? m_HitGroupsRecord.data() : nullptr;
HitShaderBindingTable.Offset = HitGroupOffset;
HitShaderBindingTable.Size = static_cast<Uint32>(m_HitGroupsRecord.size());
HitShaderBindingTable.Stride = this->m_ShaderRecordStride;
}
if (!m_CallableShadersRecord.empty())
{
CallableShaderBindingTable.pData = this->m_Changed ? m_CallableShadersRecord.data() : nullptr;
CallableShaderBindingTable.Offset = CallableShadersOffset;
CallableShaderBindingTable.Size = static_cast<Uint32>(m_CallableShadersRecord.size());
CallableShaderBindingTable.Stride = this->m_ShaderRecordStride;
}
if (!this->m_Changed)
return;
this->m_Changed = false;
}
protected:
std::vector<Uint8> m_RayGenShaderRecord;
std::vector<Uint8> m_MissShadersRecord;
std::vector<Uint8> m_CallableShadersRecord;
std::vector<Uint8> m_HitGroupsRecord;
RefCntAutoPtr<PipelineStateImplType> m_pPSO;
RefCntAutoPtr<IBuffer> m_pBuffer;
Uint32 m_ShaderRecordSize = 0;
Uint32 m_ShaderRecordStride = 0;
bool m_Changed = true;
#ifdef DILIGENT_DEVELOPMENT
static constexpr Uint8 EmptyElem = 0xA7;
#else
// In release mode clear uninitialized data by zeros.
// This makes shader inactive, wich hides errors but prevents crashes.
static constexpr Uint8 EmptyElem = 0;
#endif
private:
#ifdef DILIGENT_DEVELOPMENT
struct HitGroupBinding
{
RefCntWeakPtr<TopLevelASImplType> pTLAS;
Uint32 Version = ~0u;
bool IsBound = false;
};
mutable std::vector<HitGroupBinding> m_DbgHitGroupBindings;
void OnBindHitGroup(TopLevelASImplType* pTLAS, size_t Index)
{
this->m_DbgHitGroupBindings.resize(std::max(this->m_DbgHitGroupBindings.size(), Index + 1));
auto& Binding = this->m_DbgHitGroupBindings[Index];
Binding.pTLAS = pTLAS;
Binding.Version = pTLAS ? pTLAS->GetVersion() : ~0u;
Binding.IsBound = true;
}
#endif
};
} // namespace Diligent
|