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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
|
/*
* 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 "ShaderResourceCacheVk.hpp"
#include "DeviceContextVkImpl.hpp"
#include "BufferViewVkImpl.hpp"
#include "TextureViewVkImpl.hpp"
#include "TextureVkImpl.hpp"
#include "SamplerVkImpl.hpp"
#include "TopLevelASVkImpl.hpp"
#include "VulkanTypeConversions.hpp"
namespace Diligent
{
size_t ShaderResourceCacheVk::GetRequiredMemorySize(Uint32 NumSets, const Uint32* SetSizes)
{
Uint32 TotalResources = 0;
for (Uint32 t = 0; t < NumSets; ++t)
TotalResources += SetSizes[t];
auto MemorySize = NumSets * sizeof(DescriptorSet) + TotalResources * sizeof(Resource);
return MemorySize;
}
void ShaderResourceCacheVk::InitializeSets(IMemoryAllocator& MemAllocator, Uint32 NumSets, const Uint32* SetSizes)
{
VERIFY(!m_pMemory, "Memory has already been allocated");
// Memory layout:
//
// m_pMemory
// |
// V
// || DescriptorSet[0] | .... | DescriptorSet[Ns-1] | Res[0] | ... | Res[n-1] | .... | Res[0] | ... | Res[m-1] ||
//
//
// Ns = m_NumSets
m_NumSets = static_cast<Uint16>(NumSets);
VERIFY(m_NumSets == NumSets, "NumSets (", NumSets, ") exceed maximum representable value");
m_TotalResources = 0;
for (Uint32 t = 0; t < NumSets; ++t)
{
VERIFY_EXPR(SetSizes[t] > 0);
m_TotalResources += SetSizes[t];
}
const auto MemorySize = NumSets * sizeof(DescriptorSet) + m_TotalResources * sizeof(Resource);
VERIFY_EXPR(MemorySize == GetRequiredMemorySize(NumSets, SetSizes));
#ifdef DILIGENT_DEBUG
m_DbgInitializedResources.resize(m_NumSets);
#endif
if (MemorySize > 0)
{
m_pMemory = decltype(m_pMemory){
ALLOCATE_RAW(MemAllocator, "Memory for shader resource cache data", MemorySize),
STDDeleter<void, IMemoryAllocator>(MemAllocator) //
};
auto* pSets = reinterpret_cast<DescriptorSet*>(m_pMemory.get());
auto* pCurrResPtr = reinterpret_cast<Resource*>(pSets + m_NumSets);
for (Uint32 t = 0; t < NumSets; ++t)
{
new (&GetDescriptorSet(t)) DescriptorSet(SetSizes[t], SetSizes[t] > 0 ? pCurrResPtr : nullptr);
pCurrResPtr += SetSizes[t];
#ifdef DILIGENT_DEBUG
m_DbgInitializedResources[t].resize(SetSizes[t]);
#endif
}
VERIFY_EXPR((char*)pCurrResPtr == (char*)m_pMemory.get() + MemorySize);
}
}
void ShaderResourceCacheVk::InitializeResources(Uint32 Set, Uint32 Offset, Uint32 ArraySize, DescriptorType Type, bool HasImmutableSampler)
{
auto& DescrSet = GetDescriptorSet(Set);
for (Uint32 res = 0; res < ArraySize; ++res)
{
new (&DescrSet.GetResource(Offset + res)) Resource{Type, HasImmutableSampler};
#ifdef DILIGENT_DEBUG
m_DbgInitializedResources[Set][Offset + res] = true;
#endif
}
}
#ifdef DILIGENT_DEBUG
void ShaderResourceCacheVk::DbgVerifyResourceInitialization() const
{
for (const auto& SetFlags : m_DbgInitializedResources)
{
for (auto ResInitialized : SetFlags)
VERIFY(ResInitialized, "Not all resources in the cache have been initialized. This is a bug.");
}
}
void ShaderResourceCacheVk::DbgVerifyDynamicBuffersCounter() const
{
const auto* pResources = GetFirstResourcePtr();
Uint32 NumDynamicBuffers = 0;
for (Uint32 res = 0; res < m_TotalResources; ++res)
{
const auto& Res = pResources[res];
switch (Res.Type)
{
case DescriptorType::UniformBuffer:
VERIFY(!Res.pObject || Res.pObject.RawPtr<const BufferVkImpl>()->GetDesc().Usage != USAGE_DYNAMIC,
"Dynamic uniform buffer is bound to a non-dynamic descriptor. The buffer's dynamic offset will not be properly handled.");
break;
case DescriptorType::UniformBufferDynamic:
if (Res.pObject && Res.pObject.RawPtr<const BufferVkImpl>()->GetDesc().Usage == USAGE_DYNAMIC)
++NumDynamicBuffers;
break;
case DescriptorType::StorageBuffer:
case DescriptorType::StorageBuffer_ReadOnly:
VERIFY(!Res.pObject || Res.pObject.RawPtr<const BufferViewVkImpl>()->GetBuffer<const BufferVkImpl>()->GetDesc().Usage != USAGE_DYNAMIC,
"Dynamic storage buffer is bound to a non-dynamic descriptor. The buffer's dynamic offset will not be properly handled.");
break;
case DescriptorType::StorageBufferDynamic:
case DescriptorType::StorageBufferDynamic_ReadOnly:
if (Res.pObject && Res.pObject.RawPtr<const BufferViewVkImpl>()->GetBuffer<const BufferVkImpl>()->GetDesc().Usage == USAGE_DYNAMIC)
++NumDynamicBuffers;
break;
default:
// Do nothing
break;
}
}
VERIFY(NumDynamicBuffers == m_NumDynamicBuffers, "The number of dynamic buffers (", m_NumDynamicBuffers, ") does not match the actual number (", NumDynamicBuffers, ")");
}
#endif
ShaderResourceCacheVk::~ShaderResourceCacheVk()
{
if (m_pMemory)
{
auto* pResources = GetFirstResourcePtr();
for (Uint32 res = 0; res < m_TotalResources; ++res)
pResources[res].~Resource();
for (Uint32 t = 0; t < m_NumSets; ++t)
GetDescriptorSet(t).~DescriptorSet();
}
}
const ShaderResourceCacheVk::Resource& ShaderResourceCacheVk::SetResource(const VulkanUtilities::VulkanLogicalDevice* pLogicalDevice,
Uint32 SetIndex,
Uint32 Offset,
Uint32 BindingIndex,
Uint32 ArrayIndex,
RefCntAutoPtr<IDeviceObject>&& pObject)
{
auto& DescrSet = GetDescriptorSet(SetIndex);
auto& Res = DescrSet.GetResource(Offset);
const BufferVkImpl* pOldBuffer = nullptr;
const BufferVkImpl* pNewBuffer = nullptr;
switch (Res.Type)
{
case DescriptorType::UniformBuffer:
VERIFY(!pObject || pObject.RawPtr<const BufferVkImpl>()->GetDesc().Usage != USAGE_DYNAMIC,
"Dynamic uniform buffer is being bound to a non-dynamic descriptor. The buffer's dynamic offset will not be properly handled.");
break;
case DescriptorType::UniformBufferDynamic:
pOldBuffer = Res.pObject.RawPtr<const BufferVkImpl>();
pNewBuffer = pObject.RawPtr<const BufferVkImpl>();
break;
case DescriptorType::StorageBuffer:
case DescriptorType::StorageBuffer_ReadOnly:
VERIFY(!pObject || pObject.RawPtr<const BufferViewVkImpl>()->GetBuffer<const BufferVkImpl>()->GetDesc().Usage != USAGE_DYNAMIC,
"Dynamic storage buffer is being bound to a non-dynamic descriptor. The buffer's dynamic offset will not be properly handled.");
break;
case DescriptorType::StorageBufferDynamic:
case DescriptorType::StorageBufferDynamic_ReadOnly:
pOldBuffer = Res.pObject ? Res.pObject.RawPtr<const BufferViewVkImpl>()->GetBuffer<const BufferVkImpl>() : nullptr;
pNewBuffer = pObject ? pObject.RawPtr<const BufferViewVkImpl>()->GetBuffer<const BufferVkImpl>() : nullptr;
break;
default:
// Do nothing
break;
}
if (pOldBuffer != nullptr && pOldBuffer->GetDesc().Usage == USAGE_DYNAMIC)
{
VERIFY(m_NumDynamicBuffers > 0, "Dynamic buffers counter must be greater than zero when there is at least one dynamic buffer bound in the resource cache");
--m_NumDynamicBuffers;
}
if (pNewBuffer != nullptr && pNewBuffer->GetDesc().Usage == USAGE_DYNAMIC)
{
++m_NumDynamicBuffers;
}
Res.pObject = std::move(pObject);
auto vkSet = DescrSet.GetVkDescriptorSet();
if (vkSet != VK_NULL_HANDLE && Res.pObject)
{
VERIFY(pLogicalDevice != nullptr, "Logical device must not be null to write descriptor to a non-null set");
VkWriteDescriptorSet WriteDescrSet;
WriteDescrSet.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
WriteDescrSet.pNext = nullptr;
WriteDescrSet.dstSet = vkSet;
WriteDescrSet.dstBinding = BindingIndex;
WriteDescrSet.dstArrayElement = ArrayIndex;
WriteDescrSet.descriptorCount = 1;
// descriptorType must be the same type as that specified in VkDescriptorSetLayoutBinding for dstSet at dstBinding.
// The type of the descriptor also controls which array the descriptors are taken from. (13.2.4)
WriteDescrSet.descriptorType = DescriptorTypeToVkDescriptorType(Res.Type);
WriteDescrSet.pImageInfo = nullptr;
WriteDescrSet.pBufferInfo = nullptr;
WriteDescrSet.pTexelBufferView = nullptr;
// Do not zero-initialize!
VkDescriptorImageInfo vkDescrImageInfo;
VkDescriptorBufferInfo vkDescrBufferInfo;
VkBufferView vkDescrBufferView;
VkWriteDescriptorSetAccelerationStructureKHR vkDescrAccelStructInfo;
static_assert(static_cast<Uint32>(DescriptorType::Count) == 15, "Please update the switch below to handle the new descriptor type");
switch (Res.Type)
{
case DescriptorType::Sampler:
vkDescrImageInfo = Res.GetSamplerDescriptorWriteInfo();
WriteDescrSet.pImageInfo = &vkDescrImageInfo;
break;
case DescriptorType::CombinedImageSampler:
case DescriptorType::SeparateImage:
case DescriptorType::StorageImage:
vkDescrImageInfo = Res.GetImageDescriptorWriteInfo();
WriteDescrSet.pImageInfo = &vkDescrImageInfo;
break;
case DescriptorType::UniformTexelBuffer:
case DescriptorType::StorageTexelBuffer:
case DescriptorType::StorageTexelBuffer_ReadOnly:
vkDescrBufferView = Res.GetBufferViewWriteInfo();
WriteDescrSet.pTexelBufferView = &vkDescrBufferView;
break;
case DescriptorType::UniformBuffer:
case DescriptorType::UniformBufferDynamic:
vkDescrBufferInfo = Res.GetUniformBufferDescriptorWriteInfo();
WriteDescrSet.pBufferInfo = &vkDescrBufferInfo;
break;
case DescriptorType::StorageBuffer:
case DescriptorType::StorageBuffer_ReadOnly:
case DescriptorType::StorageBufferDynamic:
case DescriptorType::StorageBufferDynamic_ReadOnly:
vkDescrBufferInfo = Res.GetStorageBufferDescriptorWriteInfo();
WriteDescrSet.pBufferInfo = &vkDescrBufferInfo;
break;
case DescriptorType::InputAttachment:
vkDescrImageInfo = Res.GetInputAttachmentDescriptorWriteInfo();
WriteDescrSet.pImageInfo = &vkDescrImageInfo;
break;
case DescriptorType::AccelerationStructure:
vkDescrAccelStructInfo = Res.GetAccelerationStructureWriteInfo();
WriteDescrSet.pNext = &vkDescrAccelStructInfo;
break;
default:
UNEXPECTED("Unexpected descriptor type");
}
pLogicalDevice->UpdateDescriptorSets(1, &WriteDescrSet, 0, nullptr);
}
return Res;
}
static RESOURCE_STATE DescriptorTypeToResourceState(DescriptorType Type)
{
static_assert(static_cast<Uint32>(DescriptorType::Count) == 15, "Please update the switch below to handle the new descriptor type");
switch (Type)
{
// clang-format off
case DescriptorType::Sampler: return RESOURCE_STATE_UNKNOWN;
case DescriptorType::CombinedImageSampler: return RESOURCE_STATE_SHADER_RESOURCE;
case DescriptorType::SeparateImage: return RESOURCE_STATE_SHADER_RESOURCE;
case DescriptorType::StorageImage: return RESOURCE_STATE_UNORDERED_ACCESS;
case DescriptorType::UniformTexelBuffer: return RESOURCE_STATE_SHADER_RESOURCE;
case DescriptorType::StorageTexelBuffer: return RESOURCE_STATE_UNORDERED_ACCESS;
case DescriptorType::StorageTexelBuffer_ReadOnly: return RESOURCE_STATE_SHADER_RESOURCE;
case DescriptorType::UniformBuffer: return RESOURCE_STATE_CONSTANT_BUFFER;
case DescriptorType::UniformBufferDynamic: return RESOURCE_STATE_CONSTANT_BUFFER;
case DescriptorType::StorageBuffer: return RESOURCE_STATE_UNORDERED_ACCESS;
case DescriptorType::StorageBuffer_ReadOnly: return RESOURCE_STATE_SHADER_RESOURCE;
case DescriptorType::StorageBufferDynamic: return RESOURCE_STATE_UNORDERED_ACCESS;
case DescriptorType::StorageBufferDynamic_ReadOnly: return RESOURCE_STATE_SHADER_RESOURCE;
case DescriptorType::InputAttachment: return RESOURCE_STATE_SHADER_RESOURCE;
case DescriptorType::AccelerationStructure: return RESOURCE_STATE_SHADER_RESOURCE;
// clang-format on
default:
UNEXPECTED("unknown descriptor type");
return RESOURCE_STATE_UNKNOWN;
}
}
template <bool VerifyOnly>
void ShaderResourceCacheVk::TransitionResources(DeviceContextVkImpl* pCtxVkImpl)
{
auto* pResources = GetFirstResourcePtr();
for (Uint32 res = 0; res < m_TotalResources; ++res)
{
auto& Res = pResources[res];
switch (Res.Type)
{
case DescriptorType::UniformBuffer:
case DescriptorType::UniformBufferDynamic:
{
auto* pBufferVk = Res.pObject.RawPtr<BufferVkImpl>();
if (pBufferVk != nullptr && pBufferVk->IsInKnownState())
{
constexpr RESOURCE_STATE RequiredState = RESOURCE_STATE_CONSTANT_BUFFER;
VERIFY_EXPR((ResourceStateFlagsToVkAccessFlags(RequiredState) & VK_ACCESS_UNIFORM_READ_BIT) == VK_ACCESS_UNIFORM_READ_BIT);
const bool IsInRequiredState = pBufferVk->CheckState(RequiredState);
if (VerifyOnly)
{
if (!IsInRequiredState)
{
LOG_ERROR_MESSAGE("State of buffer '", pBufferVk->GetDesc().Name, "' is incorrect. Required state: ",
GetResourceStateString(RequiredState), ". Actual state: ",
GetResourceStateString(pBufferVk->GetState()),
". Call IDeviceContext::TransitionShaderResources(), use RESOURCE_STATE_TRANSITION_MODE_TRANSITION "
"when calling IDeviceContext::CommitShaderResources() or explicitly transition the buffer state "
"with IDeviceContext::TransitionResourceStates().");
}
}
else
{
if (!IsInRequiredState)
{
pCtxVkImpl->TransitionBufferState(*pBufferVk, RESOURCE_STATE_UNKNOWN, RequiredState, true);
}
VERIFY_EXPR(pBufferVk->CheckAccessFlags(VK_ACCESS_UNIFORM_READ_BIT));
}
}
}
break;
case DescriptorType::StorageBuffer:
case DescriptorType::StorageBufferDynamic:
case DescriptorType::StorageBuffer_ReadOnly:
case DescriptorType::StorageBufferDynamic_ReadOnly:
case DescriptorType::UniformTexelBuffer:
case DescriptorType::StorageTexelBuffer:
case DescriptorType::StorageTexelBuffer_ReadOnly:
{
auto* pBuffViewVk = Res.pObject.RawPtr<BufferViewVkImpl>();
auto* pBufferVk = pBuffViewVk != nullptr ? ValidatedCast<BufferVkImpl>(pBuffViewVk->GetBuffer()) : nullptr;
if (pBufferVk != nullptr && pBufferVk->IsInKnownState())
{
const RESOURCE_STATE RequiredState = DescriptorTypeToResourceState(Res.Type);
#ifdef DILIGENT_DEBUG
const VkAccessFlags RequiredAccessFlags = (RequiredState == RESOURCE_STATE_SHADER_RESOURCE) ?
VK_ACCESS_SHADER_READ_BIT :
(VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT);
VERIFY_EXPR((ResourceStateFlagsToVkAccessFlags(RequiredState) & RequiredAccessFlags) == RequiredAccessFlags);
#endif
const bool IsInRequiredState = pBufferVk->CheckState(RequiredState);
if (VerifyOnly)
{
if (!IsInRequiredState)
{
LOG_ERROR_MESSAGE("State of buffer '", pBufferVk->GetDesc().Name, "' is incorrect. Required state: ",
GetResourceStateString(RequiredState), ". Actual state: ",
GetResourceStateString(pBufferVk->GetState()),
". Call IDeviceContext::TransitionShaderResources(), use RESOURCE_STATE_TRANSITION_MODE_TRANSITION "
"when calling IDeviceContext::CommitShaderResources() or explicitly transition the buffer state "
"with IDeviceContext::TransitionResourceStates().");
}
}
else
{
// When both old and new states are RESOURCE_STATE_UNORDERED_ACCESS, we need to execute UAV barrier
// to make sure that all UAV writes are complete and visible.
if (!IsInRequiredState || RequiredState == RESOURCE_STATE_UNORDERED_ACCESS)
{
pCtxVkImpl->TransitionBufferState(*pBufferVk, RESOURCE_STATE_UNKNOWN, RequiredState, true);
}
VERIFY_EXPR(pBufferVk->CheckAccessFlags(RequiredAccessFlags));
}
}
}
break;
case DescriptorType::CombinedImageSampler:
case DescriptorType::SeparateImage:
case DescriptorType::StorageImage:
{
auto* pTextureViewVk = Res.pObject.RawPtr<TextureViewVkImpl>();
auto* pTextureVk = pTextureViewVk != nullptr ? ValidatedCast<TextureVkImpl>(pTextureViewVk->GetTexture()) : nullptr;
if (pTextureVk != nullptr && pTextureVk->IsInKnownState())
{
// The image subresources for a storage image must be in the VK_IMAGE_LAYOUT_GENERAL layout in
// order to access its data in a shader (13.1.1)
// The image subresources for a sampled image or a combined image sampler must be in the
// VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL,
// or VK_IMAGE_LAYOUT_GENERAL layout in order to access its data in a shader (13.1.3, 13.1.4).
RESOURCE_STATE RequiredState;
if (Res.Type == DescriptorType::StorageImage)
{
RequiredState = RESOURCE_STATE_UNORDERED_ACCESS;
VERIFY_EXPR(ResourceStateToVkImageLayout(RequiredState) == VK_IMAGE_LAYOUT_GENERAL);
}
else
{
if (pTextureVk->GetDesc().BindFlags & BIND_DEPTH_STENCIL)
{
// VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL must only be used as a read - only depth / stencil attachment
// in a VkFramebuffer and/or as a read - only image in a shader (which can be read as a sampled image, combined
// image / sampler and /or input attachment). This layout is valid only for image subresources of images created
// with the VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT usage bit enabled. (11.4)
RequiredState = RESOURCE_STATE_DEPTH_READ;
VERIFY_EXPR(ResourceStateToVkImageLayout(RequiredState) == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL);
}
else
{
RequiredState = RESOURCE_STATE_SHADER_RESOURCE;
VERIFY_EXPR(ResourceStateToVkImageLayout(RequiredState) == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL);
}
}
const bool IsInRequiredState = pTextureVk->CheckState(RequiredState);
if (VerifyOnly)
{
if (!IsInRequiredState)
{
LOG_ERROR_MESSAGE("State of texture '", pTextureVk->GetDesc().Name, "' is incorrect. Required state: ",
GetResourceStateString(RequiredState), ". Actual state: ",
GetResourceStateString(pTextureVk->GetState()),
". Call IDeviceContext::TransitionShaderResources(), use RESOURCE_STATE_TRANSITION_MODE_TRANSITION "
"when calling IDeviceContext::CommitShaderResources() or explicitly transition the texture state "
"with IDeviceContext::TransitionResourceStates().");
}
}
else
{
// When both old and new states are RESOURCE_STATE_UNORDERED_ACCESS, we need to execute UAV barrier
// to make sure that all UAV writes are complete and visible.
if (!IsInRequiredState || RequiredState == RESOURCE_STATE_UNORDERED_ACCESS)
{
pCtxVkImpl->TransitionTextureState(*pTextureVk, RESOURCE_STATE_UNKNOWN, RequiredState, true);
}
}
}
}
break;
case DescriptorType::Sampler:
{
// Nothing to do with samplers
}
break;
case DescriptorType::InputAttachment:
{
// Nothing to do with input attachments - they are transitioned by the render pass.
// There is nothing we can validate here - a texture may be in different state at
// the beginning of the render pass before being transitioned to INPUT_ATTACHMENT state.
}
break;
case DescriptorType::AccelerationStructure:
{
auto* pTLASVk = Res.pObject.RawPtr<TopLevelASVkImpl>();
if (pTLASVk != nullptr && pTLASVk->IsInKnownState())
{
constexpr auto RequiredState = RESOURCE_STATE_RAY_TRACING;
const bool IsInRequiredState = pTLASVk->CheckState(RequiredState);
if (VerifyOnly)
{
if (!IsInRequiredState)
{
LOG_ERROR_MESSAGE("State of TLAS '", pTLASVk->GetDesc().Name, "' is incorrect. Required state: ",
GetResourceStateString(RequiredState), ". Actual state: ",
GetResourceStateString(pTLASVk->GetState()),
". Call IDeviceContext::TransitionShaderResources(), use RESOURCE_STATE_TRANSITION_MODE_TRANSITION "
"when calling IDeviceContext::CommitShaderResources() or explicitly transition the TLAS state "
"with IDeviceContext::TransitionResourceStates().");
}
}
else
{
if (!IsInRequiredState)
{
pCtxVkImpl->TransitionTLASState(*pTLASVk, RESOURCE_STATE_UNKNOWN, RequiredState, true);
}
}
#ifdef DILIGENT_DEVELOPMENT
pTLASVk->ValidateContent();
#endif
}
}
break;
default: UNEXPECTED("Unexpected resource type");
}
}
}
template void ShaderResourceCacheVk::TransitionResources<false>(DeviceContextVkImpl* pCtxVkImpl);
template void ShaderResourceCacheVk::TransitionResources<true>(DeviceContextVkImpl* pCtxVkImpl);
VkDescriptorBufferInfo ShaderResourceCacheVk::Resource::GetUniformBufferDescriptorWriteInfo() const
{
VERIFY((Type == DescriptorType::UniformBuffer ||
Type == DescriptorType::UniformBufferDynamic),
"Uniform buffer resource is expected");
DEV_CHECK_ERR(pObject != nullptr, "Unable to get uniform buffer write info: cached object is null");
const auto* pBuffVk = pObject.RawPtr<const BufferVkImpl>();
// VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER or VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC descriptor type require
// buffer to be created with VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
VERIFY_EXPR((pBuffVk->GetDesc().BindFlags & BIND_UNIFORM_BUFFER) != 0);
VERIFY(Type == DescriptorType::UniformBufferDynamic || pBuffVk->GetDesc().Usage != USAGE_DYNAMIC,
"Dynamic buffer must be used with UniformBufferDynamic descriptor");
VkDescriptorBufferInfo DescrBuffInfo;
DescrBuffInfo.buffer = pBuffVk->GetVkBuffer();
// If descriptorType is VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER or VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC, the offset member
// of each element of pBufferInfo must be a multiple of VkPhysicalDeviceLimits::minUniformBufferOffsetAlignment (13.2.4)
DescrBuffInfo.offset = 0;
DescrBuffInfo.range = pBuffVk->GetDesc().uiSizeInBytes;
return DescrBuffInfo;
}
VkDescriptorBufferInfo ShaderResourceCacheVk::Resource::GetStorageBufferDescriptorWriteInfo() const
{
VERIFY((Type == DescriptorType::StorageBuffer ||
Type == DescriptorType::StorageBufferDynamic ||
Type == DescriptorType::StorageBuffer_ReadOnly ||
Type == DescriptorType::StorageBufferDynamic_ReadOnly),
"Storage buffer resource is expected");
DEV_CHECK_ERR(pObject != nullptr, "Unable to get storage buffer write info: cached object is null");
const auto* pBuffViewVk = pObject.RawPtr<const BufferViewVkImpl>();
const auto& ViewDesc = pBuffViewVk->GetDesc();
const auto* pBuffVk = pBuffViewVk->GetBuffer<const BufferVkImpl>();
VERIFY(Type == DescriptorType::StorageBufferDynamic || Type == DescriptorType::StorageBufferDynamic_ReadOnly || pBuffVk->GetDesc().Usage != USAGE_DYNAMIC,
"Dynamic buffer must be used with StorageBufferDynamic or StorageBufferDynamic_ReadOnly descriptor");
// VK_DESCRIPTOR_TYPE_STORAGE_BUFFER or VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC descriptor type
// require buffer to be created with VK_BUFFER_USAGE_STORAGE_BUFFER_BIT (13.2.4)
if (Type == DescriptorType::StorageBuffer_ReadOnly || Type == DescriptorType::StorageBufferDynamic_ReadOnly)
{
// HLSL buffer SRVs are mapped to read-only storge buffers in SPIR-V
VERIFY(ViewDesc.ViewType == BUFFER_VIEW_SHADER_RESOURCE, "Attempting to bind buffer view '", ViewDesc.Name,
"' as read-only storage buffer. Expected view type is BUFFER_VIEW_SHADER_RESOURCE. Actual type: ",
GetBufferViewTypeLiteralName(ViewDesc.ViewType));
VERIFY((pBuffVk->GetDesc().BindFlags & BIND_SHADER_RESOURCE) != 0,
"Buffer '", pBuffVk->GetDesc().Name, "' being set as read-only storage buffer was not created with BIND_SHADER_RESOURCE flag");
}
else if (Type == DescriptorType::StorageBuffer || Type == DescriptorType::StorageBufferDynamic)
{
VERIFY(ViewDesc.ViewType == BUFFER_VIEW_UNORDERED_ACCESS, "Attempting to bind buffer view '", ViewDesc.Name,
"' as writable storage buffer. Expected view type is BUFFER_VIEW_UNORDERED_ACCESS. Actual type: ",
GetBufferViewTypeLiteralName(ViewDesc.ViewType));
VERIFY((pBuffVk->GetDesc().BindFlags & BIND_UNORDERED_ACCESS) != 0,
"Buffer '", pBuffVk->GetDesc().Name, "' being set as writable storage buffer was not created with BIND_UNORDERED_ACCESS flag");
}
else
{
UNEXPECTED("Unexpected resource type");
}
VkDescriptorBufferInfo DescrBuffInfo;
DescrBuffInfo.buffer = pBuffVk->GetVkBuffer();
// If descriptorType is VK_DESCRIPTOR_TYPE_STORAGE_BUFFER or VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC, the offset member
// of each element of pBufferInfo must be a multiple of VkPhysicalDeviceLimits::minStorageBufferOffsetAlignment (13.2.4)
DescrBuffInfo.offset = ViewDesc.ByteOffset;
DescrBuffInfo.range = ViewDesc.ByteWidth;
return DescrBuffInfo;
}
VkDescriptorImageInfo ShaderResourceCacheVk::Resource::GetImageDescriptorWriteInfo() const
{
VERIFY((Type == DescriptorType::StorageImage ||
Type == DescriptorType::SeparateImage ||
Type == DescriptorType::CombinedImageSampler),
"Storage image, separate image or sampled image resource is expected");
DEV_CHECK_ERR(pObject != nullptr, "Unable to get image descriptor write info: cached object is null");
bool IsStorageImage = (Type == DescriptorType::StorageImage);
const auto* pTexViewVk = pObject.RawPtr<const TextureViewVkImpl>();
VERIFY_EXPR(pTexViewVk->GetDesc().ViewType == (IsStorageImage ? TEXTURE_VIEW_UNORDERED_ACCESS : TEXTURE_VIEW_SHADER_RESOURCE));
VkDescriptorImageInfo DescrImgInfo;
DescrImgInfo.sampler = VK_NULL_HANDLE;
VERIFY(Type == DescriptorType::CombinedImageSampler || !HasImmutableSampler,
"Immutable sampler can't be assigned to separarate image or storage image");
if (Type == DescriptorType::CombinedImageSampler && !HasImmutableSampler)
{
// Immutable samplers are permanently bound into the set layout; later binding a sampler
// into an immutable sampler slot in a descriptor set is not allowed (13.2.1)
const auto* pSamplerVk = ValidatedCast<const SamplerVkImpl>(pTexViewVk->GetSampler());
if (pSamplerVk != nullptr)
{
// If descriptorType is VK_DESCRIPTOR_TYPE_SAMPLER or VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
// and dstSet was not allocated with a layout that included immutable samplers for dstBinding with
// descriptorType, the sampler member of each element of pImageInfo must be a valid VkSampler
// object (13.2.4)
DescrImgInfo.sampler = pSamplerVk->GetVkSampler();
}
#ifdef DILIGENT_DEVELOPMENT
else
{
LOG_ERROR_MESSAGE("No sampler is assigned to texture view '", pTexViewVk->GetDesc().Name, "'");
}
#endif
}
DescrImgInfo.imageView = pTexViewVk->GetVulkanImageView();
// If descriptorType is VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, for each descriptor that will be accessed
// via load or store operations the imageLayout member for corresponding elements of pImageInfo
// MUST be VK_IMAGE_LAYOUT_GENERAL (13.2.4)
if (IsStorageImage)
DescrImgInfo.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
else
{
if (ValidatedCast<const TextureVkImpl>(pTexViewVk->GetTexture())->GetDesc().BindFlags & BIND_DEPTH_STENCIL)
DescrImgInfo.imageLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
else
DescrImgInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
}
return DescrImgInfo;
}
VkBufferView ShaderResourceCacheVk::Resource::GetBufferViewWriteInfo() const
{
VERIFY((Type == DescriptorType::UniformTexelBuffer ||
Type == DescriptorType::StorageTexelBuffer ||
Type == DescriptorType::StorageTexelBuffer_ReadOnly),
"Uniform or storage buffer resource is expected");
DEV_CHECK_ERR(pObject != nullptr, "Unable to get buffer view write info: cached object is null");
// The following bits must have been set at buffer creation time:
// * VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER -> VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT
// * VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER -> VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT
const auto* pBuffViewVk = pObject.RawPtr<const BufferViewVkImpl>();
return pBuffViewVk->GetVkBufferView();
}
VkDescriptorImageInfo ShaderResourceCacheVk::Resource::GetSamplerDescriptorWriteInfo() const
{
VERIFY(Type == DescriptorType::Sampler, "Separate sampler resource is expected");
VERIFY(!HasImmutableSampler, "Separate immutable samplers can't be updated");
DEV_CHECK_ERR(pObject != nullptr, "Unable to get separate sampler descriptor write info: cached object is null");
const auto* pSamplerVk = pObject.RawPtr<const SamplerVkImpl>();
VkDescriptorImageInfo DescrImgInfo;
// For VK_DESCRIPTOR_TYPE_SAMPLER, only the sample member of each element of VkWriteDescriptorSet::pImageInfo is accessed (13.2.4)
DescrImgInfo.sampler = pSamplerVk->GetVkSampler();
DescrImgInfo.imageView = VK_NULL_HANDLE;
DescrImgInfo.imageLayout = VK_IMAGE_LAYOUT_UNDEFINED;
return DescrImgInfo;
}
VkDescriptorImageInfo ShaderResourceCacheVk::Resource::GetInputAttachmentDescriptorWriteInfo() const
{
VERIFY(Type == DescriptorType::InputAttachment, "Input attachment resource is expected");
DEV_CHECK_ERR(pObject != nullptr, "Unable to get input attachment write info: cached object is null");
const auto* pTexViewVk = pObject.RawPtr<const TextureViewVkImpl>();
VERIFY_EXPR(pTexViewVk->GetDesc().ViewType == TEXTURE_VIEW_SHADER_RESOURCE);
VkDescriptorImageInfo DescrImgInfo;
DescrImgInfo.sampler = VK_NULL_HANDLE;
DescrImgInfo.imageView = pTexViewVk->GetVulkanImageView();
DescrImgInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
return DescrImgInfo;
}
VkWriteDescriptorSetAccelerationStructureKHR ShaderResourceCacheVk::Resource::GetAccelerationStructureWriteInfo() const
{
VERIFY(Type == DescriptorType::AccelerationStructure, "Acceleration structure resource is expected");
DEV_CHECK_ERR(pObject != nullptr, "Unable to get acceleration structure write info: cached object is null");
const auto* pTLASVk = pObject.RawPtr<const TopLevelASVkImpl>();
VkWriteDescriptorSetAccelerationStructureKHR DescrAS;
DescrAS.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR;
DescrAS.pNext = nullptr;
DescrAS.accelerationStructureCount = 1;
DescrAS.pAccelerationStructures = pTLASVk->GetVkTLASPtr();
return DescrAS;
}
} // namespace Diligent
|