This repository has been archived by the owner on Jul 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
/
Copy pathdescriptor_sets.cpp
2220 lines (2110 loc) · 122 KB
/
descriptor_sets.cpp
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
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2015-2016 The Khronos Group Inc.
* Copyright (c) 2015-2016 Valve Corporation
* Copyright (c) 2015-2016 LunarG, Inc.
* Copyright (C) 2015-2016 Google Inc.
*
* 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.
*
* Author: Tobin Ehlis <tobine@google.com>
* John Zulauf <jzulauf@lunarg.com>
*/
// Allow use of STL min and max functions in Windows
#define NOMINMAX
#include "descriptor_sets.h"
#include "hash_vk_types.h"
#include "vk_enum_string_helper.h"
#include "vk_safe_struct.h"
#include "vk_typemap_helper.h"
#include "buffer_validation.h"
#include <sstream>
#include <algorithm>
#include <memory>
// ExtendedBinding collects a VkDescriptorSetLayoutBinding and any extended
// state that comes from a different array/structure so they can stay together
// while being sorted by binding number.
struct ExtendedBinding {
ExtendedBinding(const VkDescriptorSetLayoutBinding *l, VkDescriptorBindingFlagsEXT f) : layout_binding(l), binding_flags(f) {}
const VkDescriptorSetLayoutBinding *layout_binding;
VkDescriptorBindingFlagsEXT binding_flags;
};
struct BindingNumCmp {
bool operator()(const ExtendedBinding &a, const ExtendedBinding &b) const {
return a.layout_binding->binding < b.layout_binding->binding;
}
};
using DescriptorSetLayoutDef = cvdescriptorset::DescriptorSetLayoutDef;
using DescriptorSetLayoutId = cvdescriptorset::DescriptorSetLayoutId;
// Canonical dictionary of DescriptorSetLayoutDef (without any handle/device specific information)
cvdescriptorset::DescriptorSetLayoutDict descriptor_set_layout_dict;
DescriptorSetLayoutId get_canonical_id(const VkDescriptorSetLayoutCreateInfo *p_create_info) {
return descriptor_set_layout_dict.look_up(DescriptorSetLayoutDef(p_create_info));
}
// Construct DescriptorSetLayout instance from given create info
// Proactively reserve and resize as possible, as the reallocation was visible in profiling
cvdescriptorset::DescriptorSetLayoutDef::DescriptorSetLayoutDef(const VkDescriptorSetLayoutCreateInfo *p_create_info)
: flags_(p_create_info->flags), binding_count_(0), descriptor_count_(0), dynamic_descriptor_count_(0) {
const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(p_create_info->pNext);
binding_type_stats_ = {0, 0, 0};
std::set<ExtendedBinding, BindingNumCmp> sorted_bindings;
const uint32_t input_bindings_count = p_create_info->bindingCount;
// Sort the input bindings in binding number order, eliminating duplicates
for (uint32_t i = 0; i < input_bindings_count; i++) {
VkDescriptorBindingFlagsEXT flags = 0;
if (flags_create_info && flags_create_info->bindingCount == p_create_info->bindingCount) {
flags = flags_create_info->pBindingFlags[i];
}
sorted_bindings.insert(ExtendedBinding(p_create_info->pBindings + i, flags));
}
// Store the create info in the sorted order from above
std::map<uint32_t, uint32_t> binding_to_dyn_count;
uint32_t index = 0;
binding_count_ = static_cast<uint32_t>(sorted_bindings.size());
bindings_.reserve(binding_count_);
binding_flags_.reserve(binding_count_);
binding_to_index_map_.reserve(binding_count_);
for (auto input_binding : sorted_bindings) {
// Add to binding and map, s.t. it is robust to invalid duplication of binding_num
const auto binding_num = input_binding.layout_binding->binding;
binding_to_index_map_[binding_num] = index++;
bindings_.emplace_back(input_binding.layout_binding);
auto &binding_info = bindings_.back();
binding_flags_.emplace_back(input_binding.binding_flags);
descriptor_count_ += binding_info.descriptorCount;
if (binding_info.descriptorCount > 0) {
non_empty_bindings_.insert(binding_num);
}
if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC) {
binding_to_dyn_count[binding_num] = binding_info.descriptorCount;
dynamic_descriptor_count_ += binding_info.descriptorCount;
binding_type_stats_.dynamic_buffer_count++;
} else if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||
(binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER)) {
binding_type_stats_.non_dynamic_buffer_count++;
} else {
binding_type_stats_.image_sampler_count++;
}
}
assert(bindings_.size() == binding_count_);
assert(binding_flags_.size() == binding_count_);
uint32_t global_index = 0;
binding_to_global_index_range_map_.reserve(binding_count_);
// Vector order is finalized so create maps of bindings to descriptors and descriptors to indices
for (uint32_t i = 0; i < binding_count_; ++i) {
auto binding_num = bindings_[i].binding;
auto final_index = global_index + bindings_[i].descriptorCount;
binding_to_global_index_range_map_[binding_num] = IndexRange(global_index, final_index);
if (final_index != global_index) {
global_start_to_index_map_[global_index] = i;
}
global_index = final_index;
}
// Now create dyn offset array mapping for any dynamic descriptors
uint32_t dyn_array_idx = 0;
binding_to_dynamic_array_idx_map_.reserve(binding_to_dyn_count.size());
for (const auto &bc_pair : binding_to_dyn_count) {
binding_to_dynamic_array_idx_map_[bc_pair.first] = dyn_array_idx;
dyn_array_idx += bc_pair.second;
}
}
size_t cvdescriptorset::DescriptorSetLayoutDef::hash() const {
hash_util::HashCombiner hc;
hc << flags_;
hc.Combine(bindings_);
return hc.Value();
}
//
// Return valid index or "end" i.e. binding_count_;
// The asserts in "Get" are reduced to the set where no valid answer(like null or 0) could be given
// Common code for all binding lookups.
uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromBinding(uint32_t binding) const {
const auto &bi_itr = binding_to_index_map_.find(binding);
if (bi_itr != binding_to_index_map_.cend()) return bi_itr->second;
return GetBindingCount();
}
VkDescriptorSetLayoutBinding const *cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorSetLayoutBindingPtrFromIndex(
const uint32_t index) const {
if (index >= bindings_.size()) return nullptr;
return bindings_[index].ptr();
}
// Return descriptorCount for given index, 0 if index is unavailable
uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorCountFromIndex(const uint32_t index) const {
if (index >= bindings_.size()) return 0;
return bindings_[index].descriptorCount;
}
// For the given index, return descriptorType
VkDescriptorType cvdescriptorset::DescriptorSetLayoutDef::GetTypeFromIndex(const uint32_t index) const {
assert(index < bindings_.size());
if (index < bindings_.size()) return bindings_[index].descriptorType;
return VK_DESCRIPTOR_TYPE_MAX_ENUM;
}
// For the given index, return stageFlags
VkShaderStageFlags cvdescriptorset::DescriptorSetLayoutDef::GetStageFlagsFromIndex(const uint32_t index) const {
assert(index < bindings_.size());
if (index < bindings_.size()) return bindings_[index].stageFlags;
return VkShaderStageFlags(0);
}
// Return binding flags for given index, 0 if index is unavailable
VkDescriptorBindingFlagsEXT cvdescriptorset::DescriptorSetLayoutDef::GetDescriptorBindingFlagsFromIndex(
const uint32_t index) const {
if (index >= binding_flags_.size()) return 0;
return binding_flags_[index];
}
// For the given global index, return index
uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetIndexFromGlobalIndex(const uint32_t global_index) const {
auto start_it = global_start_to_index_map_.upper_bound(global_index);
uint32_t index = binding_count_;
assert(start_it != global_start_to_index_map_.cbegin());
if (start_it != global_start_to_index_map_.cbegin()) {
--start_it;
index = start_it->second;
#ifndef NDEBUG
const auto &range = GetGlobalIndexRangeFromBinding(bindings_[index].binding);
assert(range.start <= global_index && global_index < range.end);
#endif
}
return index;
}
// For the given binding, return the global index range
// As start and end are often needed in pairs, get both with a single hash lookup.
const cvdescriptorset::IndexRange &cvdescriptorset::DescriptorSetLayoutDef::GetGlobalIndexRangeFromBinding(
const uint32_t binding) const {
assert(binding_to_global_index_range_map_.count(binding));
// In error case max uint32_t so index is out of bounds to break ASAP
const static IndexRange kInvalidRange = {0xFFFFFFFF, 0xFFFFFFFF};
const auto &range_it = binding_to_global_index_range_map_.find(binding);
if (range_it != binding_to_global_index_range_map_.end()) {
return range_it->second;
}
return kInvalidRange;
}
// For given binding, return ptr to ImmutableSampler array
VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromBinding(const uint32_t binding) const {
const auto &bi_itr = binding_to_index_map_.find(binding);
if (bi_itr != binding_to_index_map_.end()) {
return bindings_[bi_itr->second].pImmutableSamplers;
}
return nullptr;
}
// Move to next valid binding having a non-zero binding count
uint32_t cvdescriptorset::DescriptorSetLayoutDef::GetNextValidBinding(const uint32_t binding) const {
auto it = non_empty_bindings_.upper_bound(binding);
assert(it != non_empty_bindings_.cend());
if (it != non_empty_bindings_.cend()) return *it;
return GetMaxBinding() + 1;
}
// For given index, return ptr to ImmutableSampler array
VkSampler const *cvdescriptorset::DescriptorSetLayoutDef::GetImmutableSamplerPtrFromIndex(const uint32_t index) const {
if (index < bindings_.size()) {
return bindings_[index].pImmutableSamplers;
}
return nullptr;
}
// If our layout is compatible with rh_ds_layout, return true,
// else return false and fill in error_msg will description of what causes incompatibility
bool cvdescriptorset::DescriptorSetLayout::IsCompatible(DescriptorSetLayout const *const rh_ds_layout,
std::string *error_msg) const {
// Trivial case
if (layout_ == rh_ds_layout->GetDescriptorSetLayout()) return true;
if (get_layout_def() == rh_ds_layout->get_layout_def()) return true;
bool detailed_compat_check =
get_layout_def()->IsCompatible(layout_, rh_ds_layout->GetDescriptorSetLayout(), rh_ds_layout->get_layout_def(), error_msg);
// The detailed check should never tell us mismatching DSL are compatible
assert(!detailed_compat_check);
return detailed_compat_check;
}
// Do a detailed compatibility check of this def (referenced by ds_layout), vs. the rhs (layout and def)
// Should only be called if trivial accept has failed, and in that context should return false.
bool cvdescriptorset::DescriptorSetLayoutDef::IsCompatible(VkDescriptorSetLayout ds_layout, VkDescriptorSetLayout rh_ds_layout,
DescriptorSetLayoutDef const *const rh_ds_layout_def,
std::string *error_msg) const {
if (descriptor_count_ != rh_ds_layout_def->descriptor_count_) {
std::stringstream error_str;
error_str << "DescriptorSetLayout " << ds_layout << " has " << descriptor_count_ << " descriptors, but DescriptorSetLayout "
<< rh_ds_layout << ", which comes from pipelineLayout, has " << rh_ds_layout_def->descriptor_count_
<< " descriptors.";
*error_msg = error_str.str();
return false; // trivial fail case
}
// Descriptor counts match so need to go through bindings one-by-one
// and verify that type and stageFlags match
for (auto binding : bindings_) {
// TODO : Do we also need to check immutable samplers?
// VkDescriptorSetLayoutBinding *rh_binding;
if (binding.descriptorCount != rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding)) {
std::stringstream error_str;
error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has a descriptorCount of "
<< binding.descriptorCount << " but binding " << binding.binding << " for DescriptorSetLayout "
<< rh_ds_layout << ", which comes from pipelineLayout, has a descriptorCount of "
<< rh_ds_layout_def->GetDescriptorCountFromBinding(binding.binding);
*error_msg = error_str.str();
return false;
} else if (binding.descriptorType != rh_ds_layout_def->GetTypeFromBinding(binding.binding)) {
std::stringstream error_str;
error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " is type '"
<< string_VkDescriptorType(binding.descriptorType) << "' but binding " << binding.binding
<< " for DescriptorSetLayout " << rh_ds_layout << ", which comes from pipelineLayout, is type '"
<< string_VkDescriptorType(rh_ds_layout_def->GetTypeFromBinding(binding.binding)) << "'";
*error_msg = error_str.str();
return false;
} else if (binding.stageFlags != rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding)) {
std::stringstream error_str;
error_str << "Binding " << binding.binding << " for DescriptorSetLayout " << ds_layout << " has stageFlags "
<< binding.stageFlags << " but binding " << binding.binding << " for DescriptorSetLayout " << rh_ds_layout
<< ", which comes from pipelineLayout, has stageFlags "
<< rh_ds_layout_def->GetStageFlagsFromBinding(binding.binding);
*error_msg = error_str.str();
return false;
}
}
return true;
}
bool cvdescriptorset::DescriptorSetLayoutDef::IsNextBindingConsistent(const uint32_t binding) const {
if (!binding_to_index_map_.count(binding + 1)) return false;
auto const &bi_itr = binding_to_index_map_.find(binding);
if (bi_itr != binding_to_index_map_.end()) {
const auto &next_bi_itr = binding_to_index_map_.find(binding + 1);
if (next_bi_itr != binding_to_index_map_.end()) {
auto type = bindings_[bi_itr->second].descriptorType;
auto stage_flags = bindings_[bi_itr->second].stageFlags;
auto immut_samp = bindings_[bi_itr->second].pImmutableSamplers ? true : false;
auto flags = binding_flags_[bi_itr->second];
if ((type != bindings_[next_bi_itr->second].descriptorType) ||
(stage_flags != bindings_[next_bi_itr->second].stageFlags) ||
(immut_samp != (bindings_[next_bi_itr->second].pImmutableSamplers ? true : false)) ||
(flags != binding_flags_[next_bi_itr->second])) {
return false;
}
return true;
}
}
return false;
}
// Starting at offset descriptor of given binding, parse over update_count
// descriptor updates and verify that for any binding boundaries that are crossed, the next binding(s) are all consistent
// Consistency means that their type, stage flags, and whether or not they use immutable samplers matches
// If so, return true. If not, fill in error_msg and return false
bool cvdescriptorset::DescriptorSetLayoutDef::VerifyUpdateConsistency(uint32_t current_binding, uint32_t offset,
uint32_t update_count, const char *type,
const VkDescriptorSet set, std::string *error_msg) const {
// Verify consecutive bindings match (if needed)
auto orig_binding = current_binding;
// Track count of descriptors in the current_bindings that are remaining to be updated
auto binding_remaining = GetDescriptorCountFromBinding(current_binding);
// First, it's legal to offset beyond your own binding so handle that case
// Really this is just searching for the binding in which the update begins and adjusting offset accordingly
while (offset >= binding_remaining) {
// Advance to next binding, decrement offset by binding size
offset -= binding_remaining;
binding_remaining = GetDescriptorCountFromBinding(++current_binding);
}
binding_remaining -= offset;
while (update_count > binding_remaining) { // While our updates overstep current binding
// Verify next consecutive binding matches type, stage flags & immutable sampler use
if (!IsNextBindingConsistent(current_binding++)) {
std::stringstream error_str;
error_str << "Attempting " << type << " descriptor set " << set << " binding #" << orig_binding << " with #"
<< update_count
<< " descriptors being updated but this update oversteps the bounds of this binding and the next binding is "
"not consistent with current binding so this update is invalid.";
*error_msg = error_str.str();
return false;
}
// For sake of this check consider the bindings updated and grab count for next binding
update_count -= binding_remaining;
binding_remaining = GetDescriptorCountFromBinding(current_binding);
}
return true;
}
// The DescriptorSetLayout stores the per handle data for a descriptor set layout, and references the common defintion for the
// handle invariant portion
cvdescriptorset::DescriptorSetLayout::DescriptorSetLayout(const VkDescriptorSetLayoutCreateInfo *p_create_info,
const VkDescriptorSetLayout layout)
: layout_(layout), layout_destroyed_(false), layout_id_(get_canonical_id(p_create_info)) {}
// Validate descriptor set layout create info
bool cvdescriptorset::DescriptorSetLayout::ValidateCreateInfo(
const debug_report_data *report_data, const VkDescriptorSetLayoutCreateInfo *create_info, const bool push_descriptor_ext,
const uint32_t max_push_descriptors, const bool descriptor_indexing_ext,
const VkPhysicalDeviceDescriptorIndexingFeaturesEXT *descriptor_indexing_features) {
bool skip = false;
std::unordered_set<uint32_t> bindings;
uint64_t total_descriptors = 0;
const auto *flags_create_info = lvl_find_in_chain<VkDescriptorSetLayoutBindingFlagsCreateInfoEXT>(create_info->pNext);
const bool push_descriptor_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR);
if (push_descriptor_set && !push_descriptor_ext) {
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
DRAWSTATE_EXTENSION_NOT_ENABLED,
"Attempted to use %s in %s but its required extension %s has not been enabled.\n",
"VK_DESCRIPTOR_SET_LAYOUT_CREATE_PUSH_DESCRIPTOR_BIT_KHR", "VkDescriptorSetLayoutCreateInfo::flags",
VK_KHR_PUSH_DESCRIPTOR_EXTENSION_NAME);
}
const bool update_after_bind_set = !!(create_info->flags & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT);
if (update_after_bind_set && !descriptor_indexing_ext) {
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
DRAWSTATE_EXTENSION_NOT_ENABLED,
"Attemped to use %s in %s but its required extension %s has not been enabled.\n",
"VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT", "VkDescriptorSetLayoutCreateInfo::flags",
VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME);
}
auto valid_type = [push_descriptor_set](const VkDescriptorType type) {
return !push_descriptor_set ||
((type != VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) && (type != VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC));
};
uint32_t max_binding = 0;
for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
const auto &binding_info = create_info->pBindings[i];
max_binding = std::max(max_binding, binding_info.binding);
if (!bindings.insert(binding_info.binding).second) {
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_0500022e, "duplicated binding number in VkDescriptorSetLayoutBinding.");
}
if (!valid_type(binding_info.descriptorType)) {
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_05000230,
"invalid type %s ,for push descriptors in VkDescriptorSetLayoutBinding entry %" PRIu32 ".",
string_VkDescriptorType(binding_info.descriptorType), i);
}
total_descriptors += binding_info.descriptorCount;
}
if (flags_create_info) {
if (flags_create_info->bindingCount != 0 && flags_create_info->bindingCount != create_info->bindingCount) {
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a01774,
"VkDescriptorSetLayoutCreateInfo::bindingCount (%d) != "
"VkDescriptorSetLayoutBindingFlagsCreateInfoEXT::bindingCount (%d)",
create_info->bindingCount, flags_create_info->bindingCount);
}
if (flags_create_info->bindingCount == create_info->bindingCount) {
for (uint32_t i = 0; i < create_info->bindingCount; ++i) {
const auto &binding_info = create_info->pBindings[i];
if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT) {
if (!update_after_bind_set) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_05001770, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER &&
!descriptor_indexing_features->descriptorBindingUniformBufferUpdateAfterBind) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a0177a, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER ||
binding_info.descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER ||
binding_info.descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) &&
!descriptor_indexing_features->descriptorBindingSampledImageUpdateAfterBind) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a0177c, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE &&
!descriptor_indexing_features->descriptorBindingStorageImageUpdateAfterBind) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a0177e, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER &&
!descriptor_indexing_features->descriptorBindingStorageBufferUpdateAfterBind) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a01780, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER &&
!descriptor_indexing_features->descriptorBindingUniformTexelBufferUpdateAfterBind) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a01782, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
if (binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER &&
!descriptor_indexing_features->descriptorBindingStorageTexelBufferUpdateAfterBind) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a01784, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT ||
binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a01786, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
}
if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT) {
if (!descriptor_indexing_features->descriptorBindingUpdateUnusedWhilePending) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a01788, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
}
if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT) {
if (!descriptor_indexing_features->descriptorBindingPartiallyBound) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a0178a, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
}
if (flags_create_info->pBindingFlags[i] & VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT) {
if (binding_info.binding != max_binding) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a01778, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
if (!descriptor_indexing_features->descriptorBindingVariableDescriptorCount) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a0178c, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
if ((binding_info.descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC ||
binding_info.descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a0178e, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
}
if (push_descriptor_set &&
(flags_create_info->pBindingFlags[i] &
(VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT |
VK_DESCRIPTOR_BINDING_VARIABLE_DESCRIPTOR_COUNT_BIT_EXT))) {
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
VALIDATION_ERROR_46a01776, "Invalid flags for VkDescriptorSetLayoutBinding entry %" PRIu32, i);
}
}
}
}
if ((push_descriptor_set) && (total_descriptors > max_push_descriptors)) {
const char *undefined = push_descriptor_ext ? "" : " -- undefined";
skip |= log_msg(
report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, VALIDATION_ERROR_05000232,
"for push descriptor, total descriptor count in layout (%" PRIu64
") must not be greater than VkPhysicalDevicePushDescriptorPropertiesKHR::maxPushDescriptors (%" PRIu32 "%s).",
total_descriptors, max_push_descriptors, undefined);
}
return skip;
}
cvdescriptorset::AllocateDescriptorSetsData::AllocateDescriptorSetsData(uint32_t count)
: required_descriptors_by_type{}, layout_nodes(count, nullptr) {}
cvdescriptorset::DescriptorSet::DescriptorSet(const VkDescriptorSet set, const VkDescriptorPool pool,
const std::shared_ptr<DescriptorSetLayout const> &layout, uint32_t variable_count,
layer_data *dev_data)
: some_update_(false),
set_(set),
pool_state_(nullptr),
p_layout_(layout),
device_data_(dev_data),
limits_(GetPhysDevProperties(dev_data)->properties.limits),
variable_count_(variable_count) {
pool_state_ = GetDescriptorPoolState(dev_data, pool);
// Foreach binding, create default descriptors of given type
descriptors_.reserve(p_layout_->GetTotalDescriptorCount());
for (uint32_t i = 0; i < p_layout_->GetBindingCount(); ++i) {
auto type = p_layout_->GetTypeFromIndex(i);
switch (type) {
case VK_DESCRIPTOR_TYPE_SAMPLER: {
auto immut_sampler = p_layout_->GetImmutableSamplerPtrFromIndex(i);
for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
if (immut_sampler) {
descriptors_.emplace_back(new SamplerDescriptor(immut_sampler + di));
some_update_ = true; // Immutable samplers are updated at creation
} else
descriptors_.emplace_back(new SamplerDescriptor(nullptr));
}
break;
}
case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER: {
auto immut = p_layout_->GetImmutableSamplerPtrFromIndex(i);
for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di) {
if (immut) {
descriptors_.emplace_back(new ImageSamplerDescriptor(immut + di));
some_update_ = true; // Immutable samplers are updated at creation
} else
descriptors_.emplace_back(new ImageSamplerDescriptor(nullptr));
}
break;
}
// ImageDescriptors
case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
descriptors_.emplace_back(new ImageDescriptor(type));
break;
case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
descriptors_.emplace_back(new TexelDescriptor(type));
break;
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
for (uint32_t di = 0; di < p_layout_->GetDescriptorCountFromIndex(i); ++di)
descriptors_.emplace_back(new BufferDescriptor(type));
break;
default:
assert(0); // Bad descriptor type specified
break;
}
}
}
cvdescriptorset::DescriptorSet::~DescriptorSet() { InvalidateBoundCmdBuffers(); }
static std::string string_descriptor_req_view_type(descriptor_req req) {
std::string result("");
for (unsigned i = 0; i <= VK_IMAGE_VIEW_TYPE_END_RANGE; i++) {
if (req & (1 << i)) {
if (result.size()) result += ", ";
result += string_VkImageViewType(VkImageViewType(i));
}
}
if (!result.size()) result = "(none)";
return result;
}
// Is this sets underlying layout compatible with passed in layout according to "Pipeline Layout Compatibility" in spec?
bool cvdescriptorset::DescriptorSet::IsCompatible(DescriptorSetLayout const *const layout, std::string *error) const {
return layout->IsCompatible(p_layout_.get(), error);
}
// Validate that the state of this set is appropriate for the given bindings and dynamic_offsets at Draw time
// This includes validating that all descriptors in the given bindings are updated,
// that any update buffers are valid, and that any dynamic offsets are within the bounds of their buffers.
// Return true if state is acceptable, or false and write an error message into error string
bool cvdescriptorset::DescriptorSet::ValidateDrawState(const std::map<uint32_t, descriptor_req> &bindings,
const std::vector<uint32_t> &dynamic_offsets, GLOBAL_CB_NODE *cb_node,
const char *caller, std::string *error) const {
for (auto binding_pair : bindings) {
auto binding = binding_pair.first;
if (!p_layout_->HasBinding(binding)) {
std::stringstream error_str;
error_str << "Attempting to validate DrawState for binding #" << binding
<< " which is an invalid binding for this descriptor set.";
*error = error_str.str();
return false;
}
IndexRange index_range = p_layout_->GetGlobalIndexRangeFromBinding(binding);
auto array_idx = 0; // Track array idx if we're dealing with array descriptors
if (IsVariableDescriptorCount(binding)) {
// Only validate the first N descriptors if it uses variable_count
index_range.end = index_range.start + GetVariableDescriptorCount();
}
for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
if (p_layout_->GetDescriptorBindingFlagsFromBinding(binding) & VK_DESCRIPTOR_BINDING_PARTIALLY_BOUND_BIT_EXT) {
// Can't validate the descriptor because it may not have been updated,
// or the view could have been destroyed
continue;
} else if (!descriptors_[i]->updated) {
std::stringstream error_str;
error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
<< " is being used in draw but has not been updated.";
*error = error_str.str();
return false;
} else {
auto descriptor_class = descriptors_[i]->GetClass();
if (descriptor_class == GeneralBuffer) {
// Verify that buffers are valid
auto buffer = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetBuffer();
auto buffer_node = GetBufferState(device_data_, buffer);
if (!buffer_node) {
std::stringstream error_str;
error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
<< " references invalid buffer " << buffer << ".";
*error = error_str.str();
return false;
} else if (!buffer_node->sparse) {
for (auto mem_binding : buffer_node->GetBoundMemory()) {
if (!GetMemObjInfo(device_data_, mem_binding)) {
std::stringstream error_str;
error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
<< " uses buffer " << buffer << " that references invalid memory " << mem_binding << ".";
*error = error_str.str();
return false;
}
}
}
if (descriptors_[i]->IsDynamic()) {
// Validate that dynamic offsets are within the buffer
auto buffer_size = buffer_node->createInfo.size;
auto range = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetRange();
auto desc_offset = static_cast<BufferDescriptor *>(descriptors_[i].get())->GetOffset();
auto dyn_offset = dynamic_offsets[GetDynamicOffsetIndexFromBinding(binding) + array_idx];
if (VK_WHOLE_SIZE == range) {
if ((dyn_offset + desc_offset) > buffer_size) {
std::stringstream error_str;
error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
<< " uses buffer " << buffer << " with update range of VK_WHOLE_SIZE has dynamic offset "
<< dyn_offset << " combined with offset " << desc_offset
<< " that oversteps the buffer size of " << buffer_size << ".";
*error = error_str.str();
return false;
}
} else {
if ((dyn_offset + desc_offset + range) > buffer_size) {
std::stringstream error_str;
error_str << "Dynamic descriptor in binding #" << binding << " at global descriptor index " << i
<< " uses buffer " << buffer << " with dynamic offset " << dyn_offset
<< " combined with offset " << desc_offset << " and range " << range
<< " that oversteps the buffer size of " << buffer_size << ".";
*error = error_str.str();
return false;
}
}
}
} else if (descriptor_class == ImageSampler || descriptor_class == Image) {
VkImageView image_view;
VkImageLayout image_layout;
if (descriptor_class == ImageSampler) {
image_view = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageView();
image_layout = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetImageLayout();
} else {
image_view = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageView();
image_layout = static_cast<ImageDescriptor *>(descriptors_[i].get())->GetImageLayout();
}
auto reqs = binding_pair.second;
auto image_view_state = GetImageViewState(device_data_, image_view);
if (nullptr == image_view_state) {
// Image view must have been destroyed since initial update. Could potentially flag the descriptor
// as "invalid" (updated = false) at DestroyImageView() time and detect this error at bind time
std::stringstream error_str;
error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
<< " is using imageView " << image_view << " that has been destroyed.";
*error = error_str.str();
return false;
}
auto image_view_ci = image_view_state->create_info;
if ((reqs & DESCRIPTOR_REQ_ALL_VIEW_TYPE_BITS) && (~reqs & (1 << image_view_ci.viewType))) {
// bad view type
std::stringstream error_str;
error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
<< " requires an image view of type " << string_descriptor_req_view_type(reqs) << " but got "
<< string_VkImageViewType(image_view_ci.viewType) << ".";
*error = error_str.str();
return false;
}
auto image_node = GetImageState(device_data_, image_view_ci.image);
assert(image_node);
// Verify Image Layout
// Copy first mip level into sub_layers and loop over each mip level to verify layout
VkImageSubresourceLayers sub_layers;
sub_layers.aspectMask = image_view_ci.subresourceRange.aspectMask;
sub_layers.baseArrayLayer = image_view_ci.subresourceRange.baseArrayLayer;
sub_layers.layerCount = image_view_ci.subresourceRange.layerCount;
bool hit_error = false;
for (auto cur_level = image_view_ci.subresourceRange.baseMipLevel;
cur_level < image_view_ci.subresourceRange.levelCount; ++cur_level) {
sub_layers.mipLevel = cur_level;
VerifyImageLayout(device_data_, cb_node, image_node, sub_layers, image_layout, VK_IMAGE_LAYOUT_UNDEFINED,
caller, VALIDATION_ERROR_046002b0, &hit_error);
if (hit_error) {
*error =
"Image layout specified at vkUpdateDescriptorSets() time doesn't match actual image layout at time "
"descriptor is used. See previous error callback for specific details.";
return false;
}
}
// Verify Sample counts
if ((reqs & DESCRIPTOR_REQ_SINGLE_SAMPLE) && image_node->createInfo.samples != VK_SAMPLE_COUNT_1_BIT) {
std::stringstream error_str;
error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
<< " requires bound image to have VK_SAMPLE_COUNT_1_BIT but got "
<< string_VkSampleCountFlagBits(image_node->createInfo.samples) << ".";
*error = error_str.str();
return false;
}
if ((reqs & DESCRIPTOR_REQ_MULTI_SAMPLE) && image_node->createInfo.samples == VK_SAMPLE_COUNT_1_BIT) {
std::stringstream error_str;
error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
<< " requires bound image to have multiple samples, but got VK_SAMPLE_COUNT_1_BIT.";
*error = error_str.str();
return false;
}
}
if (descriptor_class == ImageSampler || descriptor_class == PlainSampler) {
// Verify Sampler still valid
VkSampler sampler;
if (descriptor_class == ImageSampler) {
sampler = static_cast<ImageSamplerDescriptor *>(descriptors_[i].get())->GetSampler();
} else {
sampler = static_cast<SamplerDescriptor *>(descriptors_[i].get())->GetSampler();
}
if (!ValidateSampler(sampler, device_data_)) {
std::stringstream error_str;
error_str << "Descriptor in binding #" << binding << " at global descriptor index " << i
<< " is using sampler " << sampler << " that has been destroyed.";
*error = error_str.str();
return false;
}
}
}
}
}
return true;
}
// For given bindings, place any update buffers or images into the passed-in unordered_sets
uint32_t cvdescriptorset::DescriptorSet::GetStorageUpdates(const std::map<uint32_t, descriptor_req> &bindings,
std::unordered_set<VkBuffer> *buffer_set,
std::unordered_set<VkImageView> *image_set) const {
auto num_updates = 0;
for (auto binding_pair : bindings) {
auto binding = binding_pair.first;
// If a binding doesn't exist, skip it
if (!p_layout_->HasBinding(binding)) {
continue;
}
uint32_t start_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding).start;
if (descriptors_[start_idx]->IsStorage()) {
if (Image == descriptors_[start_idx]->descriptor_class) {
for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
if (descriptors_[start_idx + i]->updated) {
image_set->insert(static_cast<ImageDescriptor *>(descriptors_[start_idx + i].get())->GetImageView());
num_updates++;
}
}
} else if (TexelBuffer == descriptors_[start_idx]->descriptor_class) {
for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
if (descriptors_[start_idx + i]->updated) {
auto bufferview = static_cast<TexelDescriptor *>(descriptors_[start_idx + i].get())->GetBufferView();
auto bv_state = GetBufferViewState(device_data_, bufferview);
if (bv_state) {
buffer_set->insert(bv_state->create_info.buffer);
num_updates++;
}
}
}
} else if (GeneralBuffer == descriptors_[start_idx]->descriptor_class) {
for (uint32_t i = 0; i < p_layout_->GetDescriptorCountFromBinding(binding); ++i) {
if (descriptors_[start_idx + i]->updated) {
buffer_set->insert(static_cast<BufferDescriptor *>(descriptors_[start_idx + i].get())->GetBuffer());
num_updates++;
}
}
}
}
}
return num_updates;
}
// Set is being deleted or updates so invalidate all bound cmd buffers
void cvdescriptorset::DescriptorSet::InvalidateBoundCmdBuffers() {
core_validation::invalidateCommandBuffers(device_data_, cb_bindings, {HandleToUint64(set_), kVulkanObjectTypeDescriptorSet});
}
// Perform write update in given update struct
void cvdescriptorset::DescriptorSet::PerformWriteUpdate(const VkWriteDescriptorSet *update) {
// Perform update on a per-binding basis as consecutive updates roll over to next binding
auto descriptors_remaining = update->descriptorCount;
auto binding_being_updated = update->dstBinding;
auto offset = update->dstArrayElement;
uint32_t update_index = 0;
while (descriptors_remaining) {
uint32_t update_count = std::min(descriptors_remaining, GetDescriptorCountFromBinding(binding_being_updated));
auto global_idx = p_layout_->GetGlobalIndexRangeFromBinding(binding_being_updated).start + offset;
// Loop over the updates for a single binding at a time
for (uint32_t di = 0; di < update_count; ++di, ++update_index) {
descriptors_[global_idx + di]->WriteUpdate(update, update_index);
}
// Roll over to next binding in case of consecutive update
descriptors_remaining -= update_count;
offset = 0;
binding_being_updated++;
}
if (update->descriptorCount) some_update_ = true;
if (!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
(VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
InvalidateBoundCmdBuffers();
}
}
// Validate Copy update
bool cvdescriptorset::DescriptorSet::ValidateCopyUpdate(const debug_report_data *report_data, const VkCopyDescriptorSet *update,
const DescriptorSet *src_set, UNIQUE_VALIDATION_ERROR_CODE *error_code,
std::string *error_msg) {
// Verify dst layout still valid
if (p_layout_->IsDestroyed()) {
*error_code = VALIDATION_ERROR_03207601;
string_sprintf(error_msg,
"Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set dstSet 0x%" PRIxLEAST64
" created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
HandleToUint64(set_), HandleToUint64(p_layout_->GetDescriptorSetLayout()));
return false;
}
// Verify src layout still valid
if (src_set->p_layout_->IsDestroyed()) {
*error_code = VALIDATION_ERROR_0322d201;
string_sprintf(
error_msg,
"Cannot call vkUpdateDescriptorSets() to perform copy update of dstSet 0x%" PRIxLEAST64
" from descriptor set srcSet 0x%" PRIxLEAST64 " created with destroyed VkDescriptorSetLayout 0x%" PRIxLEAST64,
HandleToUint64(set_), HandleToUint64(src_set->set_), HandleToUint64(src_set->p_layout_->GetDescriptorSetLayout()));
return false;
}
if (!p_layout_->HasBinding(update->dstBinding)) {
*error_code = VALIDATION_ERROR_032002b6;
std::stringstream error_str;
error_str << "DescriptorSet " << set_ << " does not have copy update dest binding of " << update->dstBinding;
*error_msg = error_str.str();
return false;
}
if (!src_set->HasBinding(update->srcBinding)) {
*error_code = VALIDATION_ERROR_032002b2;
std::stringstream error_str;
error_str << "DescriptorSet " << set_ << " does not have copy update src binding of " << update->srcBinding;
*error_msg = error_str.str();
return false;
}
// Verify idle ds
if (in_use.load() &&
!(p_layout_->GetDescriptorBindingFlagsFromBinding(update->dstBinding) &
(VK_DESCRIPTOR_BINDING_UPDATE_UNUSED_WHILE_PENDING_BIT_EXT | VK_DESCRIPTOR_BINDING_UPDATE_AFTER_BIND_BIT_EXT))) {
// TODO : Re-using Free Idle error code, need copy update idle error code
*error_code = VALIDATION_ERROR_2860026a;
std::stringstream error_str;
error_str << "Cannot call vkUpdateDescriptorSets() to perform copy update on descriptor set " << set_
<< " that is in use by a command buffer";
*error_msg = error_str.str();
return false;
}
// src & dst set bindings are valid
// Check bounds of src & dst
auto src_start_idx = src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start + update->srcArrayElement;
if ((src_start_idx + update->descriptorCount) > src_set->GetTotalDescriptorCount()) {
// SRC update out of bounds
*error_code = VALIDATION_ERROR_032002b4;
std::stringstream error_str;
error_str << "Attempting copy update from descriptorSet " << update->srcSet << " binding#" << update->srcBinding
<< " with offset index of " << src_set->GetGlobalIndexRangeFromBinding(update->srcBinding).start
<< " plus update array offset of " << update->srcArrayElement << " and update of " << update->descriptorCount
<< " descriptors oversteps total number of descriptors in set: " << src_set->GetTotalDescriptorCount();
*error_msg = error_str.str();
return false;
}
auto dst_start_idx = p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start + update->dstArrayElement;
if ((dst_start_idx + update->descriptorCount) > p_layout_->GetTotalDescriptorCount()) {
// DST update out of bounds
*error_code = VALIDATION_ERROR_032002b8;
std::stringstream error_str;
error_str << "Attempting copy update to descriptorSet " << set_ << " binding#" << update->dstBinding
<< " with offset index of " << p_layout_->GetGlobalIndexRangeFromBinding(update->dstBinding).start
<< " plus update array offset of " << update->dstArrayElement << " and update of " << update->descriptorCount
<< " descriptors oversteps total number of descriptors in set: " << p_layout_->GetTotalDescriptorCount();
*error_msg = error_str.str();
return false;
}
// Check that types match
// TODO : Base default error case going from here is VALIDATION_ERROR_0002b8012ba which covers all consistency issues, need more
// fine-grained error codes
*error_code = VALIDATION_ERROR_032002ba;
auto src_type = src_set->GetTypeFromBinding(update->srcBinding);
auto dst_type = p_layout_->GetTypeFromBinding(update->dstBinding);
if (src_type != dst_type) {
std::stringstream error_str;
error_str << "Attempting copy update to descriptorSet " << set_ << " binding #" << update->dstBinding << " with type "
<< string_VkDescriptorType(dst_type) << " from descriptorSet " << src_set->GetSet() << " binding #"
<< update->srcBinding << " with type " << string_VkDescriptorType(src_type) << ". Types do not match";
*error_msg = error_str.str();
return false;
}
// Verify consistency of src & dst bindings if update crosses binding boundaries
if ((!src_set->GetLayout()->VerifyUpdateConsistency(update->srcBinding, update->srcArrayElement, update->descriptorCount,
"copy update from", src_set->GetSet(), error_msg)) ||
(!p_layout_->VerifyUpdateConsistency(update->dstBinding, update->dstArrayElement, update->descriptorCount, "copy update to",
set_, error_msg))) {
return false;
}
if ((src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
!(GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
*error_code = VALIDATION_ERROR_03200efc;
std::stringstream error_str;
error_str << "If pname:srcSet's (" << update->srcSet
<< ") layout was created with the "
"ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
"set, then pname:dstSet's ("
<< update->dstSet
<< ") layout must: also have been created with the "
"ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag set";
*error_msg = error_str.str();
return false;
}
if (!(src_set->GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT) &&
(GetLayout()->GetCreateFlags() & VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT)) {
*error_code = VALIDATION_ERROR_03200efe;
std::stringstream error_str;
error_str << "If pname:srcSet's (" << update->srcSet
<< ") layout was created without the "
"ename:VK_DESCRIPTOR_SET_LAYOUT_CREATE_UPDATE_AFTER_BIND_POOL_BIT_EXT flag "
"set, then pname:dstSet's ("
<< update->dstSet