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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
|
/*
* 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 "pch.h"
#include "ShaderVariableManagerD3D12.hpp"
#include "RenderDeviceD3D12Impl.hpp"
#include "PipelineResourceSignatureD3D12Impl.hpp"
#include "BufferD3D12Impl.hpp"
#include "BufferViewD3D12Impl.hpp"
#include "SamplerD3D12Impl.hpp"
#include "TextureD3D12Impl.hpp"
#include "TextureViewD3D12Impl.hpp"
#include "TopLevelASD3D12Impl.hpp"
#include "ShaderVariableD3D.hpp"
#include "ShaderResourceCacheD3D12.hpp"
namespace Diligent
{
template <typename HandlerType>
void ProcessSignatureResources(const PipelineResourceSignatureD3D12Impl& Signature,
const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes,
Uint32 NumAllowedTypes,
SHADER_TYPE ShaderStages,
HandlerType Handler)
{
const bool UsingCombinedSamplers = Signature.IsUsingCombinedSamplers();
Signature.ProcessResources(AllowedVarTypes, NumAllowedTypes, ShaderStages,
[&](const PipelineResourceDesc& ResDesc, Uint32 Index) //
{
const auto& ResAttr = Signature.GetResourceAttribs(Index);
// Skip samplers combined with textures and immutable samplers
if (ResDesc.ResourceType == SHADER_RESOURCE_TYPE_SAMPLER &&
(UsingCombinedSamplers || ResAttr.IsImmutableSamplerAssigned()))
return;
Handler(Index);
});
}
size_t ShaderVariableManagerD3D12::GetRequiredMemorySize(const PipelineResourceSignatureD3D12Impl& Signature,
const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes,
Uint32 NumAllowedTypes,
SHADER_TYPE ShaderStages,
Uint32& NumVariables)
{
NumVariables = 0;
ProcessSignatureResources(Signature, AllowedVarTypes, NumAllowedTypes, ShaderStages,
[&NumVariables](Uint32) //
{
++NumVariables;
});
return NumVariables * sizeof(ShaderVariableD3D12Impl);
}
// Creates shader variable for every resource from Signature whose type is one AllowedVarTypes
void ShaderVariableManagerD3D12::Initialize(const PipelineResourceSignatureD3D12Impl& Signature,
IMemoryAllocator& Allocator,
const SHADER_RESOURCE_VARIABLE_TYPE* AllowedVarTypes,
Uint32 NumAllowedTypes,
SHADER_TYPE ShaderType)
{
#ifdef DILIGENT_DEBUG
m_pDbgAllocator = &Allocator;
#endif
VERIFY_EXPR(m_NumVariables == 0);
const auto MemSize = GetRequiredMemorySize(Signature, AllowedVarTypes, NumAllowedTypes, ShaderType, m_NumVariables);
if (m_NumVariables == 0)
return;
auto* pRawMem = ALLOCATE_RAW(Allocator, "Raw memory buffer for shader variables", MemSize);
m_pVariables = reinterpret_cast<ShaderVariableD3D12Impl*>(pRawMem);
Uint32 VarInd = 0;
ProcessSignatureResources(Signature, AllowedVarTypes, NumAllowedTypes, ShaderType,
[this, &VarInd](Uint32 ResIndex) //
{
::new (m_pVariables + VarInd) ShaderVariableD3D12Impl{*this, ResIndex};
++VarInd;
});
VERIFY_EXPR(VarInd == m_NumVariables);
m_pSignature = &Signature;
}
ShaderVariableManagerD3D12::~ShaderVariableManagerD3D12()
{
VERIFY(m_pVariables == nullptr, "Destroy() has not been called");
}
void ShaderVariableManagerD3D12::Destroy(IMemoryAllocator& Allocator)
{
if (m_pVariables != nullptr)
{
VERIFY(m_pDbgAllocator == &Allocator, "Incosistent alloctor");
for (Uint32 v = 0; v < m_NumVariables; ++v)
m_pVariables[v].~ShaderVariableD3D12Impl();
Allocator.Free(m_pVariables);
m_pVariables = nullptr;
}
}
const PipelineResourceDesc& ShaderVariableManagerD3D12::GetResourceDesc(Uint32 Index) const
{
VERIFY_EXPR(m_pSignature != nullptr);
return m_pSignature->GetResourceDesc(Index);
}
const ShaderVariableManagerD3D12::ResourceAttribs& ShaderVariableManagerD3D12::GetResourceAttribs(Uint32 Index) const
{
VERIFY_EXPR(m_pSignature != nullptr);
return m_pSignature->GetResourceAttribs(Index);
}
ShaderVariableD3D12Impl* ShaderVariableManagerD3D12::GetVariable(const Char* Name) const
{
for (Uint32 v = 0; v < m_NumVariables; ++v)
{
auto& Var = m_pVariables[v];
if (strcmp(Var.GetDesc().Name, Name) == 0)
return &Var;
}
return nullptr;
}
ShaderVariableD3D12Impl* ShaderVariableManagerD3D12::GetVariable(Uint32 Index) const
{
if (Index >= m_NumVariables)
{
LOG_ERROR("Index ", Index, " is out of range");
return nullptr;
}
return m_pVariables + Index;
}
Uint32 ShaderVariableManagerD3D12::GetVariableIndex(const ShaderVariableD3D12Impl& Variable)
{
if (m_pVariables == nullptr)
{
LOG_ERROR("This shader variable manager has no variables");
return ~0u;
}
const auto Offset = reinterpret_cast<const Uint8*>(&Variable) - reinterpret_cast<Uint8*>(m_pVariables);
DEV_CHECK_ERR(Offset % sizeof(ShaderVariableD3D12Impl) == 0, "Offset is not multiple of ShaderVariableD3D12Impl class size");
auto Index = static_cast<Uint32>(Offset / sizeof(ShaderVariableD3D12Impl));
if (Index < m_NumVariables)
return Index;
else
{
LOG_ERROR("Failed to get variable index. The variable ", &Variable, " does not belong to this shader variable manager");
return ~0u;
}
}
void ShaderVariableManagerD3D12::BindResources(IResourceMapping* pResourceMapping, Uint32 Flags)
{
DEV_CHECK_ERR(pResourceMapping != nullptr, "Failed to bind resources: resource mapping is null");
if ((Flags & BIND_SHADER_RESOURCES_UPDATE_ALL) == 0)
Flags |= BIND_SHADER_RESOURCES_UPDATE_ALL;
for (Uint32 v = 0; v < m_NumVariables; ++v)
{
m_pVariables[v].BindResources(pResourceMapping, Flags);
}
}
namespace
{
class BindResourceHelper
{
public:
BindResourceHelper(const PipelineResourceSignatureD3D12Impl& Signature,
ShaderResourceCacheD3D12& ResourceCache,
Uint32 ResIndex,
Uint32 ArrayIndex);
void operator()(IDeviceObject* pObj) const;
private:
void CacheCB(IDeviceObject* pBuffer) const;
void CacheSampler(IDeviceObject* pBuffer) const;
void CacheAccelStruct(IDeviceObject* pBuffer) const;
void BindCombinedSampler(TextureViewD3D12Impl* pTexView) const;
void BindCombinedSampler(BufferViewD3D12Impl* pTexView) const {}
template <typename TResourceViewType, ///< The type of the view (TextureViewD3D12Impl or BufferViewD3D12Impl)
typename TViewTypeEnum ///< The type of the expected view type enum (TEXTURE_VIEW_TYPE or BUFFER_VIEW_TYPE)
>
void CacheResourceView(IDeviceObject* pBufferView,
TViewTypeEnum dbgExpectedViewType) const;
ID3D12Device* GetD3D12Device() const { return m_Signature.GetDevice()->GetD3D12Device(); }
void SetResource(D3D12_CPU_DESCRIPTOR_HANDLE CPUDescriptorHandle, RefCntAutoPtr<IDeviceObject>&& pObject) const
{
if (m_DstTableCPUDescriptorHandle.ptr != 0)
{
VERIFY(CPUDescriptorHandle.ptr != 0, "CPU descriptor handle must not be null for resources allocated in descriptor tables");
DEV_CHECK_ERR(m_ResDesc.VarType == SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC || m_DstRes.pObject == nullptr, "Static and mutable resource descriptors should only be copied once");
const auto d3d12HeapType = m_ResDesc.ResourceType == SHADER_RESOURCE_TYPE_SAMPLER ?
D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER :
D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
GetD3D12Device()->CopyDescriptorsSimple(1, m_DstTableCPUDescriptorHandle, CPUDescriptorHandle, d3d12HeapType);
}
m_ResourceCache.SetResource(m_RootIndex, m_OffsetFromTableStart, m_ResDesc.ResourceType, CPUDescriptorHandle, std::move(pObject));
}
private:
using ResourceAttribs = PipelineResourceSignatureD3D12Impl::ResourceAttribs;
const PipelineResourceSignatureD3D12Impl& m_Signature;
ShaderResourceCacheD3D12& m_ResourceCache;
const PipelineResourceDesc& m_ResDesc;
const ResourceAttribs& m_Attribs; // Must go before m_RootIndex, m_OffsetFromTableStart
const ResourceCacheContentType m_CacheType; // Must go before m_RootIndex, m_OffsetFromTableStart
const Uint32 m_RootIndex; // Must go before m_DstRes
const Uint32 m_ArrayIndex;
const Uint32 m_OffsetFromTableStart; // Must go before m_DstRes
const ShaderResourceCacheD3D12::Resource& m_DstRes;
D3D12_CPU_DESCRIPTOR_HANDLE m_DstTableCPUDescriptorHandle{};
};
BindResourceHelper::BindResourceHelper(const PipelineResourceSignatureD3D12Impl& Signature,
ShaderResourceCacheD3D12& ResourceCache,
Uint32 ResIndex,
Uint32 ArrayIndex) :
// clang-format off
m_Signature {Signature},
m_ResourceCache {ResourceCache},
m_ResDesc {Signature.GetResourceDesc(ResIndex)},
m_Attribs {Signature.GetResourceAttribs(ResIndex)},
m_CacheType {ResourceCache.GetContentType()},
m_RootIndex {m_Attribs.RootIndex(m_CacheType)},
m_ArrayIndex {ArrayIndex},
m_OffsetFromTableStart{m_Attribs.OffsetFromTableStart(m_CacheType) + ArrayIndex},
m_DstRes {const_cast<const ShaderResourceCacheD3D12&>(ResourceCache).GetRootTable(m_RootIndex).GetResource(m_OffsetFromTableStart)}
// clang-format on
{
VERIFY(ArrayIndex < m_ResDesc.ArraySize, "Array index is out of range, but it should've been corrected by VerifyAndCorrectSetArrayArguments()");
if (m_CacheType != ResourceCacheContentType::Signature && !m_Attribs.IsRootView())
{
const auto IsSampler = (m_ResDesc.ResourceType == SHADER_RESOURCE_TYPE_SAMPLER);
const auto RootParamGroup = VariableTypeToRootParameterGroup(m_ResDesc.VarType);
// Static/mutable resources are allocated in GPU-visible descriptor heap, while dynamic resources - in CPU-only heap.
m_DstTableCPUDescriptorHandle =
ResourceCache.GetDescriptorTableHandle<D3D12_CPU_DESCRIPTOR_HANDLE>(
IsSampler ? D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER : D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV,
RootParamGroup, m_RootIndex, m_OffsetFromTableStart);
}
#ifdef DILIGENT_DEBUG
if (m_CacheType == ResourceCacheContentType::Signature)
{
VERIFY(m_DstTableCPUDescriptorHandle.ptr == 0, "Static shader resource cache should never be assigned descriptor space.");
}
else if (m_CacheType == ResourceCacheContentType::SRB)
{
if (m_Attribs.GetD3D12RootParamType() == D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE)
{
VERIFY(m_DstTableCPUDescriptorHandle.ptr != 0, "Shader resources allocated in descriptor tables must be assigned descriptor space.");
}
else
{
VERIFY_EXPR(m_Attribs.IsRootView());
VERIFY((m_ResDesc.ResourceType == SHADER_RESOURCE_TYPE_CONSTANT_BUFFER ||
m_ResDesc.ResourceType == SHADER_RESOURCE_TYPE_BUFFER_SRV ||
m_ResDesc.ResourceType == SHADER_RESOURCE_TYPE_BUFFER_UAV),
"Only constant buffers and dynamic buffer views can be allocated as root views");
VERIFY(m_DstTableCPUDescriptorHandle.ptr == 0, "Resources allocated as root views should never be assigned descriptor space.");
}
}
else
{
UNEXPECTED("Unknown content type");
}
#endif
}
void BindResourceHelper::CacheCB(IDeviceObject* pBuffer) const
{
// We cannot use ValidatedCast<> here as the resource can be of wrong type
RefCntAutoPtr<BufferD3D12Impl> pBuffD3D12{pBuffer, IID_BufferD3D12};
#ifdef DILIGENT_DEVELOPMENT
VerifyConstantBufferBinding(m_ResDesc, m_ArrayIndex, pBuffer, pBuffD3D12.RawPtr(), m_DstRes.pObject.RawPtr(),
m_Signature.GetDesc().Name);
if (m_ResDesc.ArraySize != 1 && pBuffD3D12 && pBuffD3D12->GetDesc().Usage == USAGE_DYNAMIC && pBuffD3D12->GetD3D12Resource() == nullptr)
{
LOG_ERROR_MESSAGE("Attempting to bind dynamic buffer '", pBuffD3D12->GetDesc().Name, "' that doesn't have backing d3d12 resource to array variable '", m_ResDesc.Name,
"[", m_ResDesc.ArraySize, "]', which is currently not supported in Direct3D12 backend. Either use non-array variable, or bind non-dynamic buffer.");
}
#endif
if (pBuffD3D12)
{
if (m_ResDesc.VarType != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC && m_DstRes.pObject != nullptr)
{
// Do not update resource if one is already bound unless it is dynamic. This may be
// dangerous as CopyDescriptorsSimple() may interfere with GPU reading the same descriptor.
return;
}
const auto CPUDescriptorHandle = pBuffD3D12->GetCBVHandle();
VERIFY(CPUDescriptorHandle.ptr != 0 || pBuffD3D12->GetDesc().Usage == USAGE_DYNAMIC,
"Only dynamic constant buffers may have null CPU descriptor");
SetResource(CPUDescriptorHandle, std::move(pBuffD3D12));
}
}
void BindResourceHelper::CacheSampler(IDeviceObject* pSampler) const
{
RefCntAutoPtr<ISamplerD3D12> pSamplerD3D12{pSampler, IID_SamplerD3D12};
if (pSamplerD3D12)
{
if (m_ResDesc.VarType != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC && m_DstRes.pObject != nullptr)
{
if (m_DstRes.pObject != pSampler)
{
auto VarTypeStr = GetShaderVariableTypeLiteralName(m_ResDesc.VarType);
LOG_ERROR_MESSAGE("Non-null sampler is already bound to ", VarTypeStr, " shader variable '", GetShaderResourcePrintName(m_ResDesc, m_ArrayIndex),
"'. Attempting to bind another sampler is an error and will be ignored. ",
"Use another shader resource binding instance or label the variable as dynamic.");
}
// Do not update resource if one is already bound unless it is dynamic. This may be
// dangerous as CopyDescriptorsSimple() may interfere with GPU reading the same descriptor.
return;
}
const auto CPUDescriptorHandle = pSamplerD3D12->GetCPUDescriptorHandle();
VERIFY(CPUDescriptorHandle.ptr != 0, "Samplers must always have valid CPU descriptors");
VERIFY(m_CacheType == ResourceCacheContentType::Signature || m_DstTableCPUDescriptorHandle.ptr != 0,
"Samplers in SRB cache must always be allocated in root tables and thus assigned descriptor in the table");
SetResource(CPUDescriptorHandle, std::move(pSamplerD3D12));
}
else
{
LOG_ERROR_MESSAGE("Failed to bind object '", pSampler->GetDesc().Name, "' to variable '",
GetShaderResourcePrintName(m_ResDesc, m_ArrayIndex), "'. Incorect object type: sampler is expected.");
}
}
void BindResourceHelper::CacheAccelStruct(IDeviceObject* pTLAS) const
{
RefCntAutoPtr<ITopLevelASD3D12> pTLASD3D12{pTLAS, IID_TopLevelASD3D12};
#ifdef DILIGENT_DEVELOPMENT
VerifyTLASResourceBinding(m_ResDesc, m_ArrayIndex, pTLAS, pTLASD3D12.RawPtr(), m_DstRes.pObject.RawPtr(),
m_Signature.GetDesc().Name);
#endif
if (pTLASD3D12)
{
if (m_ResDesc.VarType != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC && m_DstRes.pObject != nullptr)
{
// Do not update resource if one is already bound unless it is dynamic. This may be
// dangerous as CopyDescriptorsSimple() may interfere with GPU reading the same descriptor.
return;
}
const auto CPUDescriptorHandle = pTLASD3D12->GetCPUDescriptorHandle();
VERIFY(CPUDescriptorHandle.ptr != 0, "Acceleration structures must always have valid CPU descriptor handles");
VERIFY(m_CacheType == ResourceCacheContentType::Signature || m_DstTableCPUDescriptorHandle.ptr != 0,
"Acceleration structures in SRB cache are always allocated in root tables and thus must have a descriptor");
SetResource(CPUDescriptorHandle, std::move(pTLASD3D12));
}
}
template <typename TResourceViewType>
struct ResourceViewTraits
{};
template <>
struct ResourceViewTraits<TextureViewD3D12Impl>
{
static const INTERFACE_ID& IID;
static constexpr RESOURCE_DIMENSION ExpectedResDimension = RESOURCE_DIM_UNDEFINED;
static bool VerifyView(const TextureViewD3D12Impl* pViewD3D12, const PipelineResourceDesc& ResDesc, Uint32 ArrayIndex)
{
return true;
}
};
const INTERFACE_ID& ResourceViewTraits<TextureViewD3D12Impl>::IID = IID_TextureViewD3D12;
template <>
struct ResourceViewTraits<BufferViewD3D12Impl>
{
static const INTERFACE_ID& IID;
static constexpr RESOURCE_DIMENSION ExpectedResDimension = RESOURCE_DIM_BUFFER;
static bool VerifyView(const BufferViewD3D12Impl* pViewD3D12, const PipelineResourceDesc& ResDesc, Uint32 ArrayIndex)
{
if (pViewD3D12 != nullptr)
{
const auto* const pBuffer = pViewD3D12->GetBuffer<BufferD3D12Impl>();
if (ResDesc.ArraySize != 1 && pBuffer->GetDesc().Usage == USAGE_DYNAMIC && pBuffer->GetD3D12Resource() == nullptr)
{
LOG_ERROR_MESSAGE("Attempting to bind dynamic buffer '", pBuffer->GetDesc().Name, "' that doesn't have backing d3d12 resource to array variable '", ResDesc.Name,
"[", ResDesc.ArraySize, "]', which is currently not supported in Direct3D12 backend. Either use non-array variable, or bind non-dynamic buffer.");
return false;
}
ValidateBufferMode(ResDesc, ArrayIndex, pViewD3D12);
}
return true;
}
};
const INTERFACE_ID& ResourceViewTraits<BufferViewD3D12Impl>::IID = IID_BufferViewD3D12;
template <typename TResourceViewType,
typename TViewTypeEnum>
void BindResourceHelper::CacheResourceView(IDeviceObject* pView,
TViewTypeEnum dbgExpectedViewType) const
{
// We cannot use ValidatedCast<> here as the resource can be of wrong type
RefCntAutoPtr<TResourceViewType> pViewD3D12{pView, ResourceViewTraits<TResourceViewType>::IID};
#ifdef DILIGENT_DEVELOPMENT
VerifyResourceViewBinding(m_ResDesc, m_ArrayIndex,
pView, pViewD3D12.RawPtr(),
{dbgExpectedViewType},
ResourceViewTraits<TResourceViewType>::ExpectedResDimension,
false, // IsMultisample
m_DstRes.pObject.RawPtr(),
m_Signature.GetDesc().Name);
ResourceViewTraits<TResourceViewType>::VerifyView(pViewD3D12, m_ResDesc, m_ArrayIndex);
#endif
if (pViewD3D12)
{
if (m_ResDesc.VarType != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC && m_DstRes.pObject != nullptr)
{
// Do not update resource if one is already bound unless it is dynamic. This may be
// dangerous as CopyDescriptorsSimple() may interfere with GPU reading the same descriptor.
return;
}
const auto CPUDescriptorHandle = pViewD3D12->GetCPUDescriptorHandle();
// Note that for dynamic structured buffers we still create SRV even though we don't really use it.
VERIFY(CPUDescriptorHandle.ptr != 0, "Texture/buffer views should always have valid CPU descriptor handles");
BindCombinedSampler(pViewD3D12);
SetResource(CPUDescriptorHandle, std::move(pViewD3D12));
}
}
void BindResourceHelper::BindCombinedSampler(TextureViewD3D12Impl* pTexView) const
{
if (m_ResDesc.ResourceType != SHADER_RESOURCE_TYPE_TEXTURE_SRV)
{
VERIFY(!m_Attribs.IsCombinedWithSampler(), "Only texture SRVs can be combined with sampler");
return;
}
if (!m_Attribs.IsCombinedWithSampler())
return;
const auto& SamplerResDesc = m_Signature.GetResourceDesc(m_Attribs.SamplerInd);
const auto& SamplerAttribs = m_Signature.GetResourceAttribs(m_Attribs.SamplerInd);
VERIFY_EXPR(SamplerResDesc.ResourceType == SHADER_RESOURCE_TYPE_SAMPLER);
if (SamplerAttribs.IsImmutableSamplerAssigned())
{
// Immutable samplers should not be assigned cache space
VERIFY_EXPR(SamplerAttribs.RootIndex(ResourceCacheContentType::Signature) == ResourceAttribs::InvalidSigRootIndex);
VERIFY_EXPR(SamplerAttribs.RootIndex(ResourceCacheContentType::SRB) == ResourceAttribs::InvalidSRBRootIndex);
VERIFY_EXPR(SamplerAttribs.SigOffsetFromTableStart == ResourceAttribs::InvalidOffset);
VERIFY_EXPR(SamplerAttribs.SRBOffsetFromTableStart == ResourceAttribs::InvalidOffset);
return;
}
auto* const pSampler = pTexView->GetSampler();
if (pSampler == nullptr)
{
LOG_ERROR_MESSAGE("Failed to bind sampler to variable '", SamplerResDesc.Name, ". Sampler is not set in the texture view '", pTexView->GetDesc().Name, '\'');
return;
}
VERIFY_EXPR(m_ResDesc.ArraySize == SamplerResDesc.ArraySize || SamplerResDesc.ArraySize == 1);
const auto SamplerArrInd = SamplerResDesc.ArraySize > 1 ? m_ArrayIndex : 0;
BindResourceHelper BindSampler{m_Signature, m_ResourceCache, m_Attribs.SamplerInd, SamplerArrInd};
BindSampler(pSampler);
}
void BindResourceHelper::operator()(IDeviceObject* pObj) const
{
if (pObj)
{
static_assert(SHADER_RESOURCE_TYPE_LAST == 8, "Please update this function to handle the new resource type");
switch (m_ResDesc.ResourceType)
{
case SHADER_RESOURCE_TYPE_CONSTANT_BUFFER:
CacheCB(pObj);
break;
case SHADER_RESOURCE_TYPE_TEXTURE_SRV:
case SHADER_RESOURCE_TYPE_INPUT_ATTACHMENT:
CacheResourceView<TextureViewD3D12Impl>(pObj, TEXTURE_VIEW_SHADER_RESOURCE);
break;
case SHADER_RESOURCE_TYPE_TEXTURE_UAV:
CacheResourceView<TextureViewD3D12Impl>(pObj, TEXTURE_VIEW_UNORDERED_ACCESS);
break;
case SHADER_RESOURCE_TYPE_BUFFER_SRV:
CacheResourceView<BufferViewD3D12Impl>(pObj, BUFFER_VIEW_SHADER_RESOURCE);
break;
case SHADER_RESOURCE_TYPE_BUFFER_UAV:
CacheResourceView<BufferViewD3D12Impl>(pObj, BUFFER_VIEW_UNORDERED_ACCESS);
break;
case SHADER_RESOURCE_TYPE_SAMPLER:
CacheSampler(pObj);
break;
case SHADER_RESOURCE_TYPE_ACCEL_STRUCT:
CacheAccelStruct(pObj);
break;
default: UNEXPECTED("Unknown resource type ", static_cast<Int32>(m_ResDesc.ResourceType));
}
}
else
{
if (m_DstRes.pObject != nullptr && m_ResDesc.VarType != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC)
{
LOG_ERROR_MESSAGE("Shader variable '", m_ResDesc.Name, "' is not dynamic, but is being reset to null. This is an error and may cause unpredicted behavior. ",
"Use another shader resource binding instance or label the variable as dynamic if you need to bind another resource.");
}
m_ResourceCache.ResetResource(m_RootIndex, m_OffsetFromTableStart);
if (m_Attribs.IsCombinedWithSampler())
{
auto& SamplerResDesc = m_Signature.GetResourceDesc(m_Attribs.SamplerInd);
auto& SamplerAttribs = m_Signature.GetResourceAttribs(m_Attribs.SamplerInd);
VERIFY_EXPR(SamplerResDesc.ResourceType == SHADER_RESOURCE_TYPE_SAMPLER);
if (!SamplerAttribs.IsImmutableSamplerAssigned())
{
const auto SamplerArrInd = SamplerResDesc.ArraySize > 1 ? m_ArrayIndex : 0;
const auto SamRootIndex = SamplerAttribs.RootIndex(m_CacheType);
const auto SamOffsetFromTableStart = SamplerAttribs.OffsetFromTableStart(m_CacheType) + SamplerArrInd;
const auto& DstSam = const_cast<const ShaderResourceCacheD3D12&>(m_ResourceCache).GetRootTable(SamRootIndex).GetResource(SamOffsetFromTableStart);
if (DstSam.pObject != nullptr && SamplerResDesc.VarType != SHADER_RESOURCE_VARIABLE_TYPE_DYNAMIC)
{
LOG_ERROR_MESSAGE("Sampler variable '", SamplerResDesc.Name, "' is not dynamic, but is being reset to null. This is an error and may cause unpredicted behavior. ",
"Use another shader resource binding instance or label the variable as dynamic if you need to bind another sampler.");
}
m_ResourceCache.ResetResource(SamRootIndex, SamOffsetFromTableStart);
}
}
}
}
} // namespace
void ShaderVariableManagerD3D12::BindResource(IDeviceObject* pObj,
Uint32 ArrayIndex,
Uint32 ResIndex)
{
VERIFY(m_pSignature->IsUsingSeparateSamplers() || GetResourceDesc(ResIndex).ResourceType != SHADER_RESOURCE_TYPE_SAMPLER,
"Samplers should not be set directly when using combined texture samplers");
BindResourceHelper BindResHelper{*m_pSignature, m_ResourceCache, ResIndex, ArrayIndex};
BindResHelper(pObj);
}
bool ShaderVariableManagerD3D12::IsBound(Uint32 ArrayIndex,
Uint32 ResIndex) const
{
const auto& ResDesc = GetResourceDesc(ResIndex);
const auto& Attribs = GetResourceAttribs(ResIndex);
const auto CacheType = m_ResourceCache.GetContentType();
const auto RootIndex = Attribs.RootIndex(CacheType);
const auto OffsetFromTableStart = Attribs.OffsetFromTableStart(CacheType) + ArrayIndex;
VERIFY_EXPR(ArrayIndex < ResDesc.ArraySize);
if (RootIndex < m_ResourceCache.GetNumRootTables())
{
const auto& RootTable = const_cast<const ShaderResourceCacheD3D12&>(m_ResourceCache).GetRootTable(RootIndex);
if (OffsetFromTableStart < RootTable.GetSize())
{
const auto& CachedRes = RootTable.GetResource(OffsetFromTableStart);
return !CachedRes.IsNull();
}
}
return false;
}
} // namespace Diligent
|