-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathdescriptors.cpp
More file actions
689 lines (554 loc) · 24 KB
/
descriptors.cpp
File metadata and controls
689 lines (554 loc) · 24 KB
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
/*
* Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
*
* 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.
*
* SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#include "descriptors.hpp"
#include <nvvk/check_error.hpp>
namespace nvvk {
constexpr uint32_t NO_BINDING_INDEX = ~0u;
void DescriptorBindings::addBinding(uint32_t binding,
VkDescriptorType descriptorType,
uint32_t descriptorCount,
VkShaderStageFlags stageFlags,
const VkSampler* pImmutableSamplers,
VkDescriptorBindingFlags bindingFlags)
{
addBinding(VkDescriptorSetLayoutBinding{.binding = binding,
.descriptorType = descriptorType,
.descriptorCount = descriptorCount,
.stageFlags = stageFlags,
.pImmutableSamplers = pImmutableSamplers},
bindingFlags);
}
void DescriptorBindings::addBinding(const VkDescriptorSetLayoutBinding& layoutBinding, VkDescriptorBindingFlags bindingFlags)
{
// Update m_bindingToIndex.
if(m_bindingToIndex.size() <= layoutBinding.binding)
{
m_bindingToIndex.resize(layoutBinding.binding + 1, NO_BINDING_INDEX);
}
m_bindingToIndex[layoutBinding.binding] = static_cast<uint32_t>(m_bindings.size());
m_bindings.push_back(layoutBinding);
m_bindingFlags.push_back(bindingFlags);
}
void DescriptorBindings::addBindings(std::span<const VkDescriptorSetLayoutBinding> layoutBindings, VkDescriptorBindingFlags bindingFlags)
{
for(auto& b : layoutBindings)
{
addBinding(b, bindingFlags);
}
}
void DescriptorBindings::addBindings(std::initializer_list<const VkDescriptorSetLayoutBinding> layoutBindings,
VkDescriptorBindingFlags bindingFlags)
{
for(auto& b : layoutBindings)
{
addBinding(b, bindingFlags);
}
}
VkWriteDescriptorSet DescriptorBindings::getWriteSet(uint32_t binding, VkDescriptorSet dstSet, uint32_t dstArrayElement, uint32_t descriptorCount) const
{
VkWriteDescriptorSet writeSet = {VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET};
writeSet.descriptorType = VK_DESCRIPTOR_TYPE_MAX_ENUM;
if(binding >= m_bindingToIndex.size())
{
assert(!"`binding` was out of range!");
return writeSet;
}
const uint32_t i = m_bindingToIndex[binding];
if(i == NO_BINDING_INDEX)
{
assert(!"`binding` was never added!");
return writeSet;
}
const VkDescriptorSetLayoutBinding& b = m_bindings[i];
writeSet.descriptorCount = dstArrayElement == ~0 ? b.descriptorCount : descriptorCount;
writeSet.descriptorType = b.descriptorType;
writeSet.dstBinding = binding;
writeSet.dstSet = dstSet;
writeSet.dstArrayElement = dstArrayElement == ~0 ? 0 : dstArrayElement;
assert(writeSet.dstArrayElement + writeSet.descriptorCount <= b.descriptorCount);
return writeSet;
}
VkResult DescriptorBindings::createDescriptorSetLayout(VkDevice device,
VkDescriptorSetLayoutCreateFlags flags,
VkDescriptorSetLayout* pDescriptorSetLayout) const
{
VkResult result;
VkDescriptorSetLayoutBindingFlagsCreateInfo bindingsInfo = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO};
bindingsInfo.bindingCount = uint32_t(m_bindingFlags.size());
bindingsInfo.pBindingFlags = m_bindingFlags.data();
VkDescriptorSetLayoutCreateInfo createInfo = {VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
createInfo.bindingCount = uint32_t(m_bindings.size());
createInfo.pBindings = m_bindings.data();
createInfo.flags = flags;
createInfo.pNext = &bindingsInfo;
result = vkCreateDescriptorSetLayout(device, &createInfo, nullptr, pDescriptorSetLayout);
return result;
}
void DescriptorBindings::appendPoolSizes(std::vector<VkDescriptorPoolSize>& poolSizes, uint32_t numSets, uint32_t totalVariableCount) const
{
for(size_t i = 0; i < m_bindings.size(); i++)
{
const VkDescriptorSetLayoutBinding& it = m_bindings[i];
const VkDescriptorBindingFlags bindingFlags = m_bindingFlags[i];
// Bindings can have a zero descriptor count, used for the layout, but don't reserve storage for them.
if(it.descriptorCount == 0)
{
continue;
}
uint32_t count = it.descriptorCount * numSets;
if(totalVariableCount && bindingFlags & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT)
{
count = totalVariableCount;
}
bool found = false;
for(VkDescriptorPoolSize& itpool : poolSizes)
{
if(itpool.type == it.descriptorType)
{
itpool.descriptorCount += count;
found = true;
break;
}
}
if(!found)
{
VkDescriptorPoolSize poolSize{};
poolSize.type = it.descriptorType;
poolSize.descriptorCount = count;
poolSizes.push_back(poolSize);
}
}
}
std::vector<VkDescriptorPoolSize> DescriptorBindings::calculatePoolSizes(uint32_t numSets, uint32_t totalVariableCount) const
{
std::vector<VkDescriptorPoolSize> poolSizes;
appendPoolSizes(poolSizes, numSets, totalVariableCount);
return poolSizes;
}
//////////////////////////////////////////////////////////////////////////
VkResult DescriptorPack::init(const DescriptorBindings& bindings,
VkDevice device,
uint32_t numSets,
VkDescriptorSetLayoutCreateFlags layoutFlags,
VkDescriptorPoolCreateFlags poolFlags,
uint32_t totalVariableCount,
const uint32_t* descriptorVariableCounts)
{
assert(nullptr == m_device && "initFromBindings must not be called twice in a row!");
m_device = device;
m_bindings = bindings;
NVVK_FAIL_RETURN(bindings.createDescriptorSetLayout(device, layoutFlags, &m_layout));
if(numSets > 0)
{
// Pool
const std::vector<VkDescriptorPoolSize> poolSizes = bindings.calculatePoolSizes(numSets, totalVariableCount);
const VkDescriptorPoolCreateInfo poolCreateInfo{.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.flags = poolFlags,
.maxSets = numSets,
.poolSizeCount = static_cast<uint32_t>(poolSizes.size()),
.pPoolSizes = poolSizes.data()};
NVVK_FAIL_RETURN(vkCreateDescriptorPool(device, &poolCreateInfo, nullptr, &m_pool));
// Sets
m_sets.resize(numSets);
std::vector<VkDescriptorSetLayout> allocInfoLayouts(numSets, m_layout);
VkDescriptorSetAllocateInfo allocInfo{.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.descriptorPool = m_pool,
.descriptorSetCount = numSets,
.pSetLayouts = allocInfoLayouts.data()};
// Optional variable descriptor counts
VkDescriptorSetVariableDescriptorCountAllocateInfo varInfo{
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO,
.descriptorSetCount = numSets,
.pDescriptorCounts = descriptorVariableCounts,
};
if(totalVariableCount > 0 && descriptorVariableCounts)
{
allocInfo.pNext = &varInfo;
}
NVVK_FAIL_RETURN(vkAllocateDescriptorSets(device, &allocInfo, m_sets.data()));
}
return VK_SUCCESS;
}
void DescriptorPack::deinit()
{
m_bindings.clear();
m_sets.clear();
if(m_device) // Only run if ever initialized
{
vkDestroyDescriptorSetLayout(m_device, m_layout, nullptr);
m_layout = VK_NULL_HANDLE;
vkDestroyDescriptorPool(m_device, m_pool, nullptr);
m_pool = VK_NULL_HANDLE;
m_device = nullptr;
}
}
DescriptorPack::DescriptorPack(DescriptorPack&& other) noexcept
{
this->operator=(std::move(other));
}
DescriptorPack& DescriptorPack::operator=(DescriptorPack&& other) noexcept
{
assert(!m_device && "can't move into non-empty object");
m_sets = std::move(other.m_sets);
m_pool = other.m_pool;
m_layout = other.m_layout;
m_device = other.m_device;
other.m_pool = VK_NULL_HANDLE;
other.m_layout = VK_NULL_HANDLE;
other.m_device = VK_NULL_HANDLE;
return *this;
}
DescriptorPack::~DescriptorPack()
{
assert(!m_device && "deinit() missing");
}
//////////////////////////////////////////////////////////////////////////
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, VkBuffer buffer, VkDeviceSize offset, VkDeviceSize range)
{
assert(writeSet.pImageInfo == nullptr);
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.descriptorCount == 1);
m_writeSets.emplace_back(writeSet).pBufferInfo = (const VkDescriptorBufferInfo*)1;
BufferOrImageData basics;
basics.buffer.buffer = buffer;
basics.buffer.offset = offset;
basics.buffer.range = range;
m_bufferOrImageDatas.emplace_back(basics);
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const VkDescriptorBufferInfo& bufferInfo)
{
assert(writeSet.pImageInfo == nullptr);
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.descriptorCount == 1);
m_writeSets.emplace_back(writeSet).pBufferInfo = (const VkDescriptorBufferInfo*)1;
BufferOrImageData basics;
basics.buffer = bufferInfo;
m_bufferOrImageDatas.emplace_back(basics);
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const nvvk::Buffer& buffer, VkDeviceSize offset, VkDeviceSize range)
{
assert(writeSet.pImageInfo == nullptr);
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.descriptorCount == 1);
m_writeSets.emplace_back(writeSet).pBufferInfo = (const VkDescriptorBufferInfo*)1;
BufferOrImageData basics;
basics.buffer.buffer = buffer.buffer;
basics.buffer.offset = offset;
basics.buffer.range = range;
m_bufferOrImageDatas.emplace_back(basics);
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const nvvk::Buffer* buffers)
{
assert(writeSet.pImageInfo == nullptr);
assert(writeSet.pTexelBufferView == nullptr);
m_writeSets.emplace_back(writeSet).pBufferInfo = (const VkDescriptorBufferInfo*)1;
for(uint32_t i = 0; i < writeSet.descriptorCount; i++)
{
BufferOrImageData basics;
basics.buffer.buffer = buffers[i].buffer;
basics.buffer.offset = 0;
basics.buffer.range = VK_WHOLE_SIZE;
m_bufferOrImageDatas.emplace_back(basics);
}
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const VkDescriptorBufferInfo* bufferInfos)
{
assert(writeSet.pImageInfo == nullptr);
assert(writeSet.pTexelBufferView == nullptr);
m_writeSets.emplace_back(writeSet).pBufferInfo = (const VkDescriptorBufferInfo*)1;
for(uint32_t i = 0; i < writeSet.descriptorCount; i++)
{
BufferOrImageData basics;
basics.buffer = bufferInfos[i];
m_bufferOrImageDatas.emplace_back(basics);
}
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, VkBufferView bufferView)
{
assert(writeSet.pImageInfo == nullptr);
assert(writeSet.pBufferInfo == nullptr);
assert(writeSet.descriptorCount == 1);
m_writeSets.emplace_back(writeSet).pTexelBufferView = (const VkBufferView*)1;
AccelOrViewData basics;
basics.texelBufferView = bufferView;
m_accelOrViewDatas.emplace_back(basics);
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const VkBufferView* bufferViews)
{
assert(writeSet.pImageInfo == nullptr);
assert(writeSet.pBufferInfo == nullptr);
m_writeSets.emplace_back(writeSet).pTexelBufferView = (const VkBufferView*)1;
for(uint32_t i = 0; i < writeSet.descriptorCount; i++)
{
AccelOrViewData basics;
basics.texelBufferView = bufferViews[i];
m_accelOrViewDatas.emplace_back(basics);
}
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const VkDescriptorImageInfo& imageInfo)
{
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.pBufferInfo == nullptr);
assert(writeSet.descriptorCount == 1);
m_writeSets.emplace_back(writeSet).pImageInfo = (const VkDescriptorImageInfo*)1;
BufferOrImageData basics;
basics.image = imageInfo;
m_bufferOrImageDatas.emplace_back(basics);
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const nvvk::Image& image)
{
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.pBufferInfo == nullptr);
assert(writeSet.descriptorCount == 1);
assert(image.descriptor.imageView);
m_writeSets.emplace_back(writeSet).pImageInfo = (const VkDescriptorImageInfo*)1;
BufferOrImageData basics;
basics.image = image.descriptor;
m_bufferOrImageDatas.emplace_back(basics);
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, VkImageView imageView, VkImageLayout imageLayout, VkSampler sampler)
{
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.pBufferInfo == nullptr);
assert(writeSet.descriptorCount == 1);
m_writeSets.emplace_back(writeSet).pImageInfo = (const VkDescriptorImageInfo*)1;
BufferOrImageData basics;
basics.image.imageView = imageView;
basics.image.imageLayout = imageLayout;
basics.image.sampler = sampler;
m_bufferOrImageDatas.emplace_back(basics);
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const nvvk::Image* images)
{
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.pBufferInfo == nullptr);
m_writeSets.emplace_back(writeSet).pImageInfo = (const VkDescriptorImageInfo*)1;
for(uint32_t i = 0; i < writeSet.descriptorCount; i++)
{
assert(images[i].descriptor.imageView);
BufferOrImageData basics;
basics.image = images[i].descriptor;
m_bufferOrImageDatas.emplace_back(basics);
}
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const VkDescriptorImageInfo* imageInfos)
{
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.pBufferInfo == nullptr);
m_writeSets.emplace_back(writeSet).pImageInfo = (const VkDescriptorImageInfo*)1;
for(uint32_t i = 0; i < writeSet.descriptorCount; i++)
{
BufferOrImageData basics;
basics.image = imageInfos[i];
m_bufferOrImageDatas.emplace_back(basics);
}
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, VkAccelerationStructureKHR accel)
{
assert(writeSet.pImageInfo == nullptr);
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.pBufferInfo == nullptr);
assert(writeSet.descriptorCount == 1);
m_writeSets.emplace_back(writeSet);
AccelOrViewData basics;
basics.accel = accel;
m_accelOrViewDatas.emplace_back(basics);
m_writeAccels.push_back(
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR, .accelerationStructureCount = 1});
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const nvvk::AccelerationStructure& accel)
{
assert(writeSet.pImageInfo == nullptr);
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.pBufferInfo == nullptr);
assert(writeSet.descriptorCount == 1);
m_writeSets.emplace_back(writeSet);
AccelOrViewData basics;
basics.accel = accel.accel;
m_accelOrViewDatas.emplace_back(basics);
m_writeAccels.push_back(
{.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR, .accelerationStructureCount = 1});
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const nvvk::AccelerationStructure* accels)
{
assert(writeSet.pImageInfo == nullptr);
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.pBufferInfo == nullptr);
m_writeSets.emplace_back(writeSet);
for(uint32_t i = 0; i < writeSet.descriptorCount; i++)
{
AccelOrViewData basics;
basics.accel = accels[i].accel;
m_accelOrViewDatas.emplace_back(basics);
}
m_writeAccels.push_back({.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
.accelerationStructureCount = writeSet.descriptorCount});
m_needPointerUpdate = true;
}
void WriteSetContainer::append(const VkWriteDescriptorSet& writeSet, const VkAccelerationStructureKHR* accels)
{
assert(writeSet.pImageInfo == nullptr);
assert(writeSet.pTexelBufferView == nullptr);
assert(writeSet.pBufferInfo == nullptr);
m_writeSets.emplace_back(writeSet);
for(uint32_t i = 0; i < writeSet.descriptorCount; i++)
{
AccelOrViewData basics;
basics.accel = accels[i];
m_accelOrViewDatas.emplace_back(basics);
}
m_writeAccels.push_back({.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET_ACCELERATION_STRUCTURE_KHR,
.accelerationStructureCount = writeSet.descriptorCount});
m_needPointerUpdate = true;
}
void WriteSetContainer::clear()
{
m_writeSets.clear();
m_writeAccels.clear();
m_bufferOrImageDatas.clear();
m_accelOrViewDatas.clear();
m_needPointerUpdate = true;
}
const VkWriteDescriptorSet* WriteSetContainer::data()
{
if(m_needPointerUpdate)
{
size_t accelWriteIndex = 0;
size_t bufferOrImageIndex = 0;
size_t accelOrViewIndex = 0;
for(size_t i = 0; i < m_writeSets.size(); i++)
{
if(m_writeSets[i].pBufferInfo)
{
m_writeSets[i].pBufferInfo = &m_bufferOrImageDatas[bufferOrImageIndex].buffer;
bufferOrImageIndex += m_writeSets[i].descriptorCount;
}
else if(m_writeSets[i].pImageInfo)
{
m_writeSets[i].pImageInfo = &m_bufferOrImageDatas[bufferOrImageIndex].image;
bufferOrImageIndex += m_writeSets[i].descriptorCount;
}
else if(m_writeSets[i].pTexelBufferView)
{
m_writeSets[i].pTexelBufferView = &m_accelOrViewDatas[accelOrViewIndex].texelBufferView;
accelOrViewIndex += m_writeSets[i].descriptorCount;
}
else if(m_writeSets[i].descriptorType == VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
{
m_writeAccels[accelWriteIndex].pAccelerationStructures = &m_accelOrViewDatas[accelOrViewIndex].accel;
accelOrViewIndex += m_writeSets[i].descriptorCount;
m_writeSets[i].pNext = &m_writeAccels[accelWriteIndex];
accelWriteIndex++;
}
}
}
return m_writeSets.empty() ? nullptr : m_writeSets.data();
}
} // namespace nvvk
//--------------------------------------------------------------------------------------------------
// Usage example
//--------------------------------------------------------------------------------------------------
[[maybe_unused]] static void usage_DescriptorBindings()
{
VkDevice device = nullptr;
nvvk::Buffer myBufferA;
nvvk::Buffer myBufferB;
uint32_t SHADERIO_BINDING = 0;
struct PushConstants
{
float iResolution[2];
};
constexpr uint32_t NUM_SETS = 2;
// Manually create layout and pool
{
// Create bindings.
nvvk::DescriptorBindings bindings;
// Binding `SHADERIO_BINDING` is 1 uniform buffer accessible to all stages,
// that can be updated after binding when not in use.
bindings.addBinding(SHADERIO_BINDING, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr,
VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT | VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT);
// To manually create a layout and a pool and to allocate NUM_SETS sets:
{
VkDescriptorSetLayout dlayout = VK_NULL_HANDLE;
bindings.createDescriptorSetLayout(device, 0, &dlayout);
std::vector<VkDescriptorPoolSize> poolSizes = bindings.calculatePoolSizes(NUM_SETS);
// Or if you have multiple descriptor layouts you'd like to allocate from a
// single pool, you can use bindings.appendPoolSizes().
VkDescriptorPool dpool = VK_NULL_HANDLE;
const VkDescriptorPoolCreateInfo dpoolInfo{.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
.maxSets = NUM_SETS,
.poolSizeCount = uint32_t(poolSizes.size()),
.pPoolSizes = poolSizes.data()};
NVVK_CHECK(vkCreateDescriptorPool(device, &dpoolInfo, nullptr, &dpool));
std::vector<VkDescriptorSet> sets(NUM_SETS);
std::vector<VkDescriptorSetLayout> allocInfoLayouts(NUM_SETS, dlayout);
const VkDescriptorSetAllocateInfo allocInfo{.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
.descriptorPool = dpool,
.descriptorSetCount = NUM_SETS,
.pSetLayouts = allocInfoLayouts.data()};
NVVK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, sets.data()));
// Cleanup
vkDestroyDescriptorPool(device, dpool, nullptr);
}
}
// Or have DescriptorPack simplify the above:
nvvk::DescriptorPack dpack;
{
nvvk::DescriptorBindings bindings;
bindings.addBinding(SHADERIO_BINDING, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_ALL, nullptr,
VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT | VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT);
// The second argument, NUM_SETS here, can be left 0 when the intend is to use push descriptors, it defaults to `1`
NVVK_CHECK(dpack.init(bindings, device, NUM_SETS));
}
// To update DescriptorSets:
{
// std::vectors inside for VkWriteDescriptorSets as well as the corresponding payloads.
nvvk::WriteSetContainer writeContainer;
// makeDescriptorWrite returns a VkWriteDescriptorSet without actual binding information,
// the append function takes care of that.
// when preparing push descriptors, `dpack.sets[0]` would be omitted / nullptr
writeContainer.append(dpack.makeWrite(SHADERIO_BINDING, 0), myBufferA);
// shortcut to provide `dpack.sets[1]` (also works when dpack.sets.empty() for push descriptors)
writeContainer.append(dpack.makeWrite(SHADERIO_BINDING, 1), myBufferB);
vkUpdateDescriptorSets(device, writeContainer.size(), writeContainer.data(), 0, nullptr);
// the writeContainer can also be used for push descriptors, when the VkDescriptorSet provided were nullptr
// vkCmdPushDescriptorSet(cmd, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, writeContainer.size(), writeContainer.data());
}
// To create a pipeline layout with an additional push constant range:
VkPushConstantRange pushRange{.stageFlags = VK_SHADER_STAGE_ALL, .offset = 0, .size = sizeof(PushConstants)};
VkPipelineLayout pipelineLayout = VK_NULL_HANDLE;
NVVK_CHECK(nvvk::createPipelineLayout(device, &pipelineLayout, {dpack.getLayout()}, {pushRange}));
// Cleanup
vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
dpack.deinit();
}