forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_validation.cpp
6914 lines (6317 loc) · 433 KB
/
buffer_validation.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-2021 The Khronos Group Inc.
* Copyright (c) 2015-2021 Valve Corporation
* Copyright (c) 2015-2021 LunarG, Inc.
* Copyright (C) 2015-2021 Google Inc.
* Modifications Copyright (C) 2020-2021 Advanced Micro Devices, Inc. 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.
*
* Author: Mark Lobodzinski <[email protected]>
* Author: Dave Houlton <[email protected]>
* Shannon McPherson <[email protected]>
* Author: Tobias Hector <[email protected]>
*/
#include <cmath>
#include <set>
#include <sstream>
#include <string>
#include <iostream>
#include "vk_enum_string_helper.h"
#include "vk_format_utils.h"
#include "vk_layer_data.h"
#include "vk_layer_utils.h"
#include "vk_layer_logging.h"
#include "vk_typemap_helper.h"
#include "chassis.h"
#include "core_validation.h"
#include "core_error_location.h"
#include "shader_validation.h"
#include "descriptor_sets.h"
#include "buffer_validation.h"
#include "sync_vuid_maps.h"
// All VUID from copy_bufferimage_to_imagebuffer_common.txt
static const char *GetBufferImageCopyCommandVUID(std::string id, bool image_to_buffer, bool copy2) {
// clang-format off
static const std::map<std::string, std::array<const char *, 4>> copy_imagebuffer_vuid = {
{"00193", {
"VUID-vkCmdCopyBufferToImage-bufferOffset-00193", // !copy2 & !image_to_buffer
"VUID-vkCmdCopyImageToBuffer-bufferOffset-00193", // !copy2 & image_to_buffer
"VUID-VkCopyBufferToImageInfo2KHR-bufferOffset-00193", // copy2 & !image_to_buffer
"VUID-VkCopyImageToBufferInfo2KHR-bufferOffset-00193", // copy2 & image_to_buffer
}},
{"01558", {
"VUID-vkCmdCopyBufferToImage-bufferOffset-01558",
"VUID-vkCmdCopyImageToBuffer-bufferOffset-01558",
"VUID-VkCopyBufferToImageInfo2KHR-bufferOffset-01558",
"VUID-VkCopyImageToBufferInfo2KHR-bufferOffset-01558",
}},
{"01559", {
"VUID-vkCmdCopyBufferToImage-bufferOffset-01559",
"VUID-vkCmdCopyImageToBuffer-bufferOffset-01559",
"VUID-VkCopyBufferToImageInfo2KHR-bufferOffset-01559",
"VUID-VkCopyImageToBufferInfo2KHR-bufferOffset-01559",
}},
{"00197", {
"VUID-vkCmdCopyBufferToImage-imageOffset-00197",
"VUID-vkCmdCopyImageToBuffer-imageOffset-00197",
"VUID-VkCopyBufferToImageInfo2KHR-imageOffset-00197",
"VUID-VkCopyImageToBufferInfo2KHR-imageOffset-00197",
}},
{"00198", {
"VUID-vkCmdCopyBufferToImage-imageOffset-00198",
"VUID-vkCmdCopyImageToBuffer-imageOffset-00198",
"VUID-VkCopyBufferToImageInfo2KHR-imageOffset-00198",
"VUID-VkCopyImageToBufferInfo2KHR-imageOffset-00198",
}},
{"00199", {
"VUID-vkCmdCopyBufferToImage-srcImage-00199",
"VUID-vkCmdCopyImageToBuffer-srcImage-00199",
"VUID-VkCopyBufferToImageInfo2KHR-srcImage-00199",
"VUID-VkCopyImageToBufferInfo2KHR-srcImage-00199",
}},
{"00200", {
"VUID-vkCmdCopyBufferToImage-imageOffset-00200",
"VUID-vkCmdCopyImageToBuffer-imageOffset-00200",
"VUID-VkCopyBufferToImageInfo2KHR-imageOffset-00200",
"VUID-VkCopyImageToBufferInfo2KHR-imageOffset-00200",
}},
{"00201", {
"VUID-vkCmdCopyBufferToImage-srcImage-00201",
"VUID-vkCmdCopyImageToBuffer-srcImage-00201",
"VUID-VkCopyBufferToImageInfo2KHR-srcImage-00201",
"VUID-VkCopyImageToBufferInfo2KHR-srcImage-00201",
}},
{"00203", {
"VUID-vkCmdCopyBufferToImage-bufferRowLength-00203",
"VUID-vkCmdCopyImageToBuffer-bufferRowLength-00203",
"VUID-VkCopyBufferToImageInfo2KHR-bufferRowLength-00203",
"VUID-VkCopyImageToBufferInfo2KHR-bufferRowLength-00203",
}},
{"00204", {
"VUID-vkCmdCopyBufferToImage-bufferImageHeight-00204",
"VUID-vkCmdCopyImageToBuffer-bufferImageHeight-00204",
"VUID-VkCopyBufferToImageInfo2KHR-bufferImageHeight-00204",
"VUID-VkCopyImageToBufferInfo2KHR-bufferImageHeight-00204",
}},
{"00205", {
"VUID-vkCmdCopyBufferToImage-imageOffset-00205",
"VUID-vkCmdCopyImageToBuffer-imageOffset-00205",
"VUID-VkCopyBufferToImageInfo2KHR-imageOffset-00205",
"VUID-VkCopyImageToBufferInfo2KHR-imageOffset-00205",
}},
{"00206", {
"VUID-vkCmdCopyBufferToImage-bufferOffset-00206",
"VUID-vkCmdCopyImageToBuffer-bufferOffset-00206",
"VUID-VkCopyBufferToImageInfo2KHR-bufferOffset-00206",
"VUID-VkCopyImageToBufferInfo2KHR-bufferOffset-00206",
}},
{"00207", {
"VUID-vkCmdCopyBufferToImage-imageExtent-00207",
"VUID-vkCmdCopyImageToBuffer-imageExtent-00207",
"VUID-VkCopyBufferToImageInfo2KHR-imageExtent-00207",
"VUID-VkCopyImageToBufferInfo2KHR-imageExtent-00207",
}},
{"00208", {
"VUID-vkCmdCopyBufferToImage-imageExtent-00208",
"VUID-vkCmdCopyImageToBuffer-imageExtent-00208",
"VUID-VkCopyBufferToImageInfo2KHR-imageExtent-00208",
"VUID-VkCopyImageToBufferInfo2KHR-imageExtent-00208",
}},
{"00209", {
"VUID-vkCmdCopyBufferToImage-imageExtent-00209",
"VUID-vkCmdCopyImageToBuffer-imageExtent-00209",
"VUID-VkCopyBufferToImageInfo2KHR-imageExtent-00209",
"VUID-VkCopyImageToBufferInfo2KHR-imageExtent-00209",
}},
{"00211", {
"VUID-vkCmdCopyBufferToImage-aspectMask-00211",
"VUID-vkCmdCopyImageToBuffer-aspectMask-00211",
"VUID-VkCopyBufferToImageInfo2KHR-aspectMask-00211",
"VUID-VkCopyImageToBufferInfo2KHR-aspectMask-00211",
}},
{"01560", {
"VUID-vkCmdCopyBufferToImage-aspectMask-01560",
"VUID-vkCmdCopyImageToBuffer-aspectMask-01560",
"VUID-VkCopyBufferToImageInfo2KHR-aspectMask-01560",
"VUID-VkCopyImageToBufferInfo2KHR-aspectMask-01560",
}},
{"00213", {
"VUID-vkCmdCopyBufferToImage-baseArrayLayer-00213",
"VUID-vkCmdCopyImageToBuffer-baseArrayLayer-00213",
"VUID-VkCopyBufferToImageInfo2KHR-baseArrayLayer-00213",
"VUID-VkCopyImageToBufferInfo2KHR-baseArrayLayer-00213",
}},
{"04052", {
"VUID-vkCmdCopyBufferToImage-commandBuffer-04052",
"VUID-vkCmdCopyImageToBuffer-commandBuffer-04052",
"VUID-VkCopyBufferToImageInfo2KHR-commandBuffer-04052",
"VUID-VkCopyImageToBufferInfo2KHR-commandBuffer-04052",
}},
{"04053", {
"VUID-vkCmdCopyBufferToImage-srcImage-04053",
"VUID-vkCmdCopyImageToBuffer-srcImage-04053",
"VUID-VkCopyBufferToImageInfo2KHR-srcImage-04053",
"VUID-VkCopyImageToBufferInfo2KHR-srcImage-04053",
}}
};
// clang-format on
uint8_t index = 0;
index |= (image_to_buffer) ? 0x1 : 0;
index |= (copy2) ? 0x2 : 0;
return copy_imagebuffer_vuid.at(id).at(index);
}
// Transfer VkImageSubresourceRange into VkImageSubresourceLayers struct
static VkImageSubresourceLayers LayersFromRange(const VkImageSubresourceRange &subresource_range) {
VkImageSubresourceLayers subresource_layers;
subresource_layers.aspectMask = subresource_range.aspectMask;
subresource_layers.baseArrayLayer = subresource_range.baseArrayLayer;
subresource_layers.layerCount = subresource_range.layerCount;
subresource_layers.mipLevel = subresource_range.baseMipLevel;
return subresource_layers;
}
// Transfer VkImageSubresourceLayers into VkImageSubresourceRange struct
static VkImageSubresourceRange RangeFromLayers(const VkImageSubresourceLayers &subresource_layers) {
VkImageSubresourceRange subresource_range;
subresource_range.aspectMask = subresource_layers.aspectMask;
subresource_range.baseArrayLayer = subresource_layers.baseArrayLayer;
subresource_range.layerCount = subresource_layers.layerCount;
subresource_range.baseMipLevel = subresource_layers.mipLevel;
subresource_range.levelCount = 1;
return subresource_range;
}
static VkImageSubresourceRange MakeImageFullRange(const VkImageCreateInfo &create_info) {
const auto format = create_info.format;
VkImageSubresourceRange init_range{0, 0, VK_REMAINING_MIP_LEVELS, 0, VK_REMAINING_ARRAY_LAYERS};
#ifdef VK_USE_PLATFORM_ANDROID_KHR
const VkExternalFormatANDROID *external_format_android = LvlFindInChain<VkExternalFormatANDROID>(&create_info);
bool is_external_format_conversion = (external_format_android != nullptr && external_format_android->externalFormat != 0);
#else
bool is_external_format_conversion = false;
#endif
if (FormatIsColor(format) || FormatIsMultiplane(format) || is_external_format_conversion) {
init_range.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; // Normalization will expand this for multiplane
} else {
init_range.aspectMask =
(FormatHasDepth(format) ? VK_IMAGE_ASPECT_DEPTH_BIT : 0) | (FormatHasStencil(format) ? VK_IMAGE_ASPECT_STENCIL_BIT : 0);
}
return NormalizeSubresourceRange(create_info, init_range);
}
IMAGE_STATE::IMAGE_STATE(VkDevice dev, VkImage img, const VkImageCreateInfo *pCreateInfo)
: image(img),
safe_create_info(pCreateInfo),
createInfo(*safe_create_info.ptr()),
valid(false),
acquired(false),
shared_presentable(false),
layout_locked(false),
get_sparse_reqs_called(false),
sparse_metadata_required(false),
sparse_metadata_bound(false),
has_ahb_format(false),
is_swapchain_image(false),
ahb_format(0),
full_range{MakeImageFullRange(createInfo)},
create_from_swapchain(VK_NULL_HANDLE),
bind_swapchain(VK_NULL_HANDLE),
bind_swapchain_imageIndex(0),
range_encoder(full_range),
disjoint(false),
plane0_memory_requirements_checked(false),
plane1_memory_requirements_checked(false),
plane2_memory_requirements_checked(false),
subresource_encoder(full_range),
fragment_encoder(nullptr),
store_device_as_workaround(dev), // TODO REMOVE WHEN encoder can be const
sparse_requirements{} {
if ((createInfo.sharingMode == VK_SHARING_MODE_CONCURRENT) && (createInfo.queueFamilyIndexCount > 0)) {
uint32_t *queue_family_indices = new uint32_t[createInfo.queueFamilyIndexCount];
for (uint32_t i = 0; i < createInfo.queueFamilyIndexCount; i++) {
queue_family_indices[i] = pCreateInfo->pQueueFamilyIndices[i];
}
createInfo.pQueueFamilyIndices = queue_family_indices;
}
if (createInfo.flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) {
sparse = true;
}
auto *external_memory_info = LvlFindInChain<VkExternalMemoryImageCreateInfo>(pCreateInfo->pNext);
if (external_memory_info) {
external_memory_handle = external_memory_info->handleTypes;
}
}
bool IMAGE_STATE::IsCreateInfoEqual(const VkImageCreateInfo &other_createInfo) const {
bool is_equal = (createInfo.sType == other_createInfo.sType) && (createInfo.flags == other_createInfo.flags);
is_equal = is_equal && IsImageTypeEqual(other_createInfo) && IsFormatEqual(other_createInfo);
is_equal = is_equal && IsMipLevelsEqual(other_createInfo) && IsArrayLayersEqual(other_createInfo);
is_equal = is_equal && IsUsageEqual(other_createInfo) && IsInitialLayoutEqual(other_createInfo);
is_equal = is_equal && IsExtentEqual(other_createInfo) && IsTilingEqual(other_createInfo);
is_equal = is_equal && IsSamplesEqual(other_createInfo) && IsSharingModeEqual(other_createInfo);
return is_equal &&
((createInfo.sharingMode == VK_SHARING_MODE_CONCURRENT) ? IsQueueFamilyIndicesEqual(other_createInfo) : true);
}
// Check image compatibility rules for VK_NV_dedicated_allocation_image_aliasing
bool IMAGE_STATE::IsCreateInfoDedicatedAllocationImageAliasingCompatible(const VkImageCreateInfo &other_createInfo) const {
bool is_compatible = (createInfo.sType == other_createInfo.sType) && (createInfo.flags == other_createInfo.flags);
is_compatible = is_compatible && IsImageTypeEqual(other_createInfo) && IsFormatEqual(other_createInfo);
is_compatible = is_compatible && IsMipLevelsEqual(other_createInfo);
is_compatible = is_compatible && IsUsageEqual(other_createInfo) && IsInitialLayoutEqual(other_createInfo);
is_compatible = is_compatible && IsSamplesEqual(other_createInfo) && IsSharingModeEqual(other_createInfo);
is_compatible = is_compatible &&
((createInfo.sharingMode == VK_SHARING_MODE_CONCURRENT) ? IsQueueFamilyIndicesEqual(other_createInfo) : true);
is_compatible = is_compatible && IsTilingEqual(other_createInfo);
is_compatible = is_compatible && createInfo.extent.width <= other_createInfo.extent.width &&
createInfo.extent.height <= other_createInfo.extent.height &&
createInfo.extent.depth <= other_createInfo.extent.depth &&
createInfo.arrayLayers <= other_createInfo.arrayLayers;
return is_compatible;
}
bool IMAGE_STATE::IsCompatibleAliasing(IMAGE_STATE *other_image_state) {
if (!is_swapchain_image && !other_image_state->is_swapchain_image &&
!(createInfo.flags & other_image_state->createInfo.flags & VK_IMAGE_CREATE_ALIAS_BIT)) {
return false;
}
if ((create_from_swapchain == VK_NULL_HANDLE) && binding.mem_state &&
(binding.mem_state == other_image_state->binding.mem_state) && (binding.offset == other_image_state->binding.offset) &&
IsCreateInfoEqual(other_image_state->createInfo)) {
return true;
}
if ((bind_swapchain == other_image_state->bind_swapchain) && (bind_swapchain != VK_NULL_HANDLE)) {
return true;
}
return false;
}
IMAGE_VIEW_STATE::IMAGE_VIEW_STATE(const std::shared_ptr<IMAGE_STATE> &im, VkImageView iv, const VkImageViewCreateInfo *ci)
: image_view(iv),
create_info(*ci),
normalized_subresource_range(NormalizeSubresourceRange(*im, ci->subresourceRange)),
range_generator(im->subresource_encoder, normalized_subresource_range),
samplerConversion(VK_NULL_HANDLE),
image_state(im) {
auto *conversion_info = LvlFindInChain<VkSamplerYcbcrConversionInfo>(create_info.pNext);
if (conversion_info) samplerConversion = conversion_info->conversion;
if (image_state) {
// A light normalization of the createInfo range
auto &sub_res_range = create_info.subresourceRange;
sub_res_range.levelCount = ResolveRemainingLevels(&sub_res_range, image_state->createInfo.mipLevels);
sub_res_range.layerCount = ResolveRemainingLayers(&sub_res_range, image_state->createInfo.arrayLayers);
// Cache a full normalization (for "full image/whole image" comparisons)
// normalized_subresource_range = NormalizeSubresourceRange(*image_state, ci->subresourceRange);
samples = image_state->createInfo.samples;
if (image_state->has_ahb_format) {
// When the image has a external format the views format must be VK_FORMAT_UNDEFINED and it is required to use a sampler
// Ycbcr conversion. Thus we can't extract any meaningful information from the format parameter. As a Sampler Ycbcr
// conversion must be used the shader type is always float.
descriptor_format_bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
} else {
descriptor_format_bits = DescriptorRequirementsBitsFromFormat(create_info.format);
}
}
}
static VkImageLayout NormalizeImageLayout(VkImageLayout layout, VkImageLayout non_normal, VkImageLayout normal) {
return (layout == non_normal) ? normal : layout;
}
static VkImageLayout NormalizeDepthImageLayout(VkImageLayout layout) {
return NormalizeImageLayout(layout, VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL);
}
static VkImageLayout NormalizeStencilImageLayout(VkImageLayout layout) {
return NormalizeImageLayout(layout, VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL,
VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL);
}
static VkImageLayout NormalizeSynchronization2Layout(const VkImageAspectFlags aspect_mask, VkImageLayout layout) {
if (layout == VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR) {
if (aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT) {
layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
} else if (aspect_mask == (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
} else if (aspect_mask == VK_IMAGE_ASPECT_DEPTH_BIT) {
layout = VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL;
} else if (aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT) {
layout = VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL;
}
} else if (layout == VK_IMAGE_LAYOUT_READ_ONLY_OPTIMAL_KHR) {
if (aspect_mask == VK_IMAGE_ASPECT_COLOR_BIT) {
layout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
} else if (aspect_mask == (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL;
} else if (aspect_mask == VK_IMAGE_ASPECT_DEPTH_BIT) {
layout = VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_OPTIMAL;
} else if (aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT) {
layout = VK_IMAGE_LAYOUT_STENCIL_READ_ONLY_OPTIMAL;
}
}
return layout;
}
static bool ImageLayoutMatches(const VkImageAspectFlags aspect_mask, VkImageLayout a, VkImageLayout b) {
bool matches = (a == b);
if (!matches) {
a = NormalizeSynchronization2Layout(aspect_mask, a);
b = NormalizeSynchronization2Layout(aspect_mask, b);
matches = (a == b);
if (!matches) {
// Relaxed rules when referencing *only* the depth or stencil aspects
if (aspect_mask == VK_IMAGE_ASPECT_DEPTH_BIT) {
matches = NormalizeDepthImageLayout(a) == NormalizeDepthImageLayout(b);
} else if (aspect_mask == VK_IMAGE_ASPECT_STENCIL_BIT) {
matches = NormalizeStencilImageLayout(a) == NormalizeStencilImageLayout(b);
}
}
}
return matches;
}
// Utility type for ForRange callbacks
struct LayoutUseCheckAndMessage {
const static VkImageAspectFlags kDepthOrStencil = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
const ImageSubresourceLayoutMap *layout_map;
const VkImageAspectFlags aspect_mask;
const char *message;
VkImageLayout layout;
LayoutUseCheckAndMessage() = delete;
LayoutUseCheckAndMessage(const ImageSubresourceLayoutMap *layout_map_, const VkImageAspectFlags aspect_mask_ = 0)
: layout_map(layout_map_), aspect_mask{aspect_mask_}, message(nullptr), layout(kInvalidLayout) {}
bool Check(const VkImageSubresource &subres, VkImageLayout check, VkImageLayout current_layout, VkImageLayout initial_layout) {
message = nullptr;
layout = kInvalidLayout; // Success status
if (current_layout != kInvalidLayout && !ImageLayoutMatches(aspect_mask, check, current_layout)) {
message = "previous known";
layout = current_layout;
} else if ((initial_layout != kInvalidLayout) && !ImageLayoutMatches(aspect_mask, check, initial_layout)) {
// To check the relaxed rule matching we need to see how the initial use was used
const auto initial_layout_state = layout_map->GetSubresourceInitialLayoutState(subres);
assert(initial_layout_state); // If we have an initial layout, we better have a state for it
if (!((initial_layout_state->aspect_mask & kDepthOrStencil) &&
ImageLayoutMatches(initial_layout_state->aspect_mask, check, initial_layout))) {
message = "previously used";
layout = initial_layout;
}
}
return layout == kInvalidLayout;
}
};
bool IMAGE_VIEW_STATE::OverlapSubresource(const IMAGE_VIEW_STATE &compare_view) const {
if (image_view == compare_view.image_view) {
return true;
}
if (image_state->image != compare_view.image_state->image) {
return false;
}
if (normalized_subresource_range.aspectMask != compare_view.normalized_subresource_range.aspectMask) {
return false;
}
// compare if overlap mip level
if ((normalized_subresource_range.baseMipLevel < compare_view.normalized_subresource_range.baseMipLevel) &&
((normalized_subresource_range.baseMipLevel + normalized_subresource_range.levelCount) <=
compare_view.normalized_subresource_range.baseMipLevel)) {
return false;
}
if ((normalized_subresource_range.baseMipLevel > compare_view.normalized_subresource_range.baseMipLevel) &&
(normalized_subresource_range.baseMipLevel >=
(compare_view.normalized_subresource_range.baseMipLevel + compare_view.normalized_subresource_range.levelCount))) {
return false;
}
// compare if overlap array layer
if ((normalized_subresource_range.baseArrayLayer < compare_view.normalized_subresource_range.baseArrayLayer) &&
((normalized_subresource_range.baseArrayLayer + normalized_subresource_range.layerCount) <=
compare_view.normalized_subresource_range.baseArrayLayer)) {
return false;
}
if ((normalized_subresource_range.baseArrayLayer > compare_view.normalized_subresource_range.baseArrayLayer) &&
(normalized_subresource_range.baseArrayLayer >=
(compare_view.normalized_subresource_range.baseArrayLayer + compare_view.normalized_subresource_range.layerCount))) {
return false;
}
return true;
}
uint32_t FullMipChainLevels(uint32_t height, uint32_t width, uint32_t depth) {
// uint cast applies floor()
return 1u + static_cast<uint32_t>(log2(std::max({height, width, depth})));
}
uint32_t FullMipChainLevels(VkExtent3D extent) { return FullMipChainLevels(extent.height, extent.width, extent.depth); }
uint32_t FullMipChainLevels(VkExtent2D extent) { return FullMipChainLevels(extent.height, extent.width); }
bool CoreChecks::FindLayouts(VkImage image, std::vector<VkImageLayout> &layouts) const {
auto image_state = GetImageState(image);
if (!image_state) return false;
const auto *layout_range_map = GetLayoutRangeMap(imageLayoutMap, image);
if (!layout_range_map) return false;
// TODO: FindLayouts function should mutate into a ValidatePresentableLayout with the loop wrapping the LogError
// from the caller. You can then use decode to add the subresource of the range::begin to the error message.
// TODO: what is this test and what is it supposed to do?! -- the logic doesn't match the comment below?!
// TODO: Make this robust for >1 aspect mask. Now it will just say ignore potential errors in this case.
if (layout_range_map->size() >= (image_state->createInfo.arrayLayers * image_state->createInfo.mipLevels + 1)) {
return false;
}
for (auto entry : *layout_range_map) {
layouts.push_back(entry.second);
}
return true;
}
// Set image layout for given VkImageSubresourceRange struct
void CoreChecks::SetImageLayout(CMD_BUFFER_STATE *cb_node, const IMAGE_STATE &image_state,
const VkImageSubresourceRange &image_subresource_range, VkImageLayout layout,
VkImageLayout expected_layout) {
auto *subresource_map = GetImageSubresourceLayoutMap(cb_node, image_state);
assert(subresource_map); // the non-const getter must return a valid pointer
if (subresource_map->SetSubresourceRangeLayout(*cb_node, image_subresource_range, layout, expected_layout)) {
cb_node->image_layout_change_count++; // Change the version of this data to force revalidation
}
for (const auto &image : image_state.aliasing_images) {
auto alias_state = GetImageState(image);
// The map state of the aliases should all be in sync, so no need to check the return value
subresource_map = GetImageSubresourceLayoutMap(cb_node, *alias_state);
assert(subresource_map);
subresource_map->SetSubresourceRangeLayout(*cb_node, image_subresource_range, layout, expected_layout);
}
}
// Set the initial image layout for all slices of an image view
void CoreChecks::SetImageViewInitialLayout(CMD_BUFFER_STATE *cb_node, const IMAGE_VIEW_STATE &view_state, VkImageLayout layout) {
if (disabled[image_layout_validation]) {
return;
}
IMAGE_STATE *image_state = view_state.image_state.get();
auto *subresource_map = GetImageSubresourceLayoutMap(cb_node, *image_state);
subresource_map->SetSubresourceRangeInitialLayout(*cb_node, layout, view_state);
for (const auto &image : image_state->aliasing_images) {
image_state = GetImageState(image);
subresource_map = GetImageSubresourceLayoutMap(cb_node, *image_state);
subresource_map->SetSubresourceRangeInitialLayout(*cb_node, layout, view_state);
}
}
// Set the initial image layout for a passed non-normalized subresource range
void CoreChecks::SetImageInitialLayout(CMD_BUFFER_STATE *cb_node, const IMAGE_STATE &image_state,
const VkImageSubresourceRange &range, VkImageLayout layout) {
auto *subresource_map = GetImageSubresourceLayoutMap(cb_node, image_state);
assert(subresource_map);
subresource_map->SetSubresourceRangeInitialLayout(*cb_node, NormalizeSubresourceRange(image_state, range), layout);
for (const auto &image : image_state.aliasing_images) {
auto alias_state = GetImageState(image);
subresource_map = GetImageSubresourceLayoutMap(cb_node, *alias_state);
assert(subresource_map);
subresource_map->SetSubresourceRangeInitialLayout(*cb_node, NormalizeSubresourceRange(*alias_state, range), layout);
}
}
void CoreChecks::SetImageInitialLayout(CMD_BUFFER_STATE *cb_node, VkImage image, const VkImageSubresourceRange &range,
VkImageLayout layout) {
const IMAGE_STATE *image_state = GetImageState(image);
if (!image_state) return;
SetImageInitialLayout(cb_node, *image_state, range, layout);
};
void CoreChecks::SetImageInitialLayout(CMD_BUFFER_STATE *cb_node, const IMAGE_STATE &image_state,
const VkImageSubresourceLayers &layers, VkImageLayout layout) {
SetImageInitialLayout(cb_node, image_state, RangeFromLayers(layers), layout);
}
// Set image layout for all slices of an image view
void CoreChecks::SetImageViewLayout(CMD_BUFFER_STATE *cb_node, const IMAGE_VIEW_STATE &view_state, VkImageLayout layout,
VkImageLayout layoutStencil) {
IMAGE_STATE *image_state = view_state.image_state.get();
VkImageSubresourceRange sub_range = view_state.normalized_subresource_range;
// When changing the layout of a 3D image subresource via a 2D or 2D_ARRRAY image view, all depth slices of
// the subresource mip level(s) are transitioned, ignoring any layers restriction in the subresource info.
if ((image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) && (view_state.create_info.viewType != VK_IMAGE_VIEW_TYPE_3D)) {
sub_range.baseArrayLayer = 0;
sub_range.layerCount = image_state->createInfo.extent.depth;
}
if (sub_range.aspectMask == (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT) && layoutStencil != kInvalidLayout) {
sub_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
SetImageLayout(cb_node, *image_state, sub_range, layout);
sub_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
SetImageLayout(cb_node, *image_state, sub_range, layoutStencil);
} else {
SetImageLayout(cb_node, *image_state, sub_range, layout);
}
}
bool CoreChecks::ValidateRenderPassLayoutAgainstFramebufferImageUsage(RenderPassCreateVersion rp_version, VkImageLayout layout,
VkImage image, VkImageView image_view,
VkFramebuffer framebuffer, VkRenderPass renderpass,
uint32_t attachment_index, const char *variable_name) const {
bool skip = false;
auto image_state = GetImageState(image);
const char *vuid;
const bool use_rp2 = (rp_version == RENDER_PASS_VERSION_2);
const char *function_name = use_rp2 ? "vkCmdBeginRenderPass2()" : "vkCmdBeginRenderPass()";
if (!image_state) {
LogObjectList objlist(image);
objlist.add(renderpass);
objlist.add(framebuffer);
objlist.add(image_view);
skip |=
LogError(image, "VUID-VkRenderPassBeginInfo-framebuffer-parameter",
"%s: RenderPass %s uses %s where pAttachments[%" PRIu32 "] = %s, which refers to an invalid image",
function_name, report_data->FormatHandle(renderpass).c_str(), report_data->FormatHandle(framebuffer).c_str(),
attachment_index, report_data->FormatHandle(image_view).c_str());
return skip;
}
auto image_usage = image_state->createInfo.usage;
const auto stencil_usage_info = LvlFindInChain<VkImageStencilUsageCreateInfo>(image_state->createInfo.pNext);
if (stencil_usage_info) {
image_usage |= stencil_usage_info->stencilUsage;
}
// Check for layouts that mismatch image usages in the framebuffer
if (layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL && !(image_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) {
vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-03094" : "VUID-vkCmdBeginRenderPass-initialLayout-00895";
LogObjectList objlist(image);
objlist.add(renderpass);
objlist.add(framebuffer);
objlist.add(image_view);
skip |= LogError(objlist, vuid,
"%s: Layout/usage mismatch for attachment %u in %s"
" - the %s is %s but the image attached to %s via %s"
" was not created with VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT",
function_name, attachment_index, report_data->FormatHandle(renderpass).c_str(), variable_name,
string_VkImageLayout(layout), report_data->FormatHandle(framebuffer).c_str(),
report_data->FormatHandle(image_view).c_str());
}
if (layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL &&
!(image_usage & (VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT))) {
vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-03097" : "VUID-vkCmdBeginRenderPass-initialLayout-00897";
LogObjectList objlist(image);
objlist.add(renderpass);
objlist.add(framebuffer);
objlist.add(image_view);
skip |= LogError(objlist, vuid,
"%s: Layout/usage mismatch for attachment %u in %s"
" - the %s is %s but the image attached to %s via %s"
" was not created with VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT or VK_IMAGE_USAGE_SAMPLED_BIT",
function_name, attachment_index, report_data->FormatHandle(renderpass).c_str(), variable_name,
string_VkImageLayout(layout), report_data->FormatHandle(framebuffer).c_str(),
report_data->FormatHandle(image_view).c_str());
}
if (layout == VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL && !(image_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT)) {
vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-03098" : "VUID-vkCmdBeginRenderPass-initialLayout-00898";
LogObjectList objlist(image);
objlist.add(renderpass);
objlist.add(framebuffer);
objlist.add(image_view);
skip |= LogError(objlist, vuid,
"%s: Layout/usage mismatch for attachment %u in %s"
" - the %s is %s but the image attached to %s via %s"
" was not created with VK_IMAGE_USAGE_TRANSFER_SRC_BIT",
function_name, attachment_index, report_data->FormatHandle(renderpass).c_str(), variable_name,
string_VkImageLayout(layout), report_data->FormatHandle(framebuffer).c_str(),
report_data->FormatHandle(image_view).c_str());
}
if (layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && !(image_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-03099" : "VUID-vkCmdBeginRenderPass-initialLayout-00899";
LogObjectList objlist(image);
objlist.add(renderpass);
objlist.add(framebuffer);
objlist.add(image_view);
skip |= LogError(objlist, vuid,
"%s: Layout/usage mismatch for attachment %u in %s"
" - the %s is %s but the image attached to %s via %s"
" was not created with VK_IMAGE_USAGE_TRANSFER_DST_BIT",
function_name, attachment_index, report_data->FormatHandle(renderpass).c_str(), variable_name,
string_VkImageLayout(layout), report_data->FormatHandle(framebuffer).c_str(),
report_data->FormatHandle(image_view).c_str());
}
if (device_extensions.vk_khr_maintenance2) {
if ((layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) &&
!(image_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-03096" : "VUID-vkCmdBeginRenderPass-initialLayout-01758";
LogObjectList objlist(image);
objlist.add(renderpass);
objlist.add(framebuffer);
objlist.add(image_view);
skip |= LogError(objlist, vuid,
"%s: Layout/usage mismatch for attachment %u in %s"
" - the %s is %s but the image attached to %s via %s"
" was not created with VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT",
function_name, attachment_index, report_data->FormatHandle(renderpass).c_str(), variable_name,
string_VkImageLayout(layout), report_data->FormatHandle(framebuffer).c_str(),
report_data->FormatHandle(image_view).c_str());
}
} else {
// The create render pass 2 extension requires maintenance 2 (the previous branch), so no vuid switch needed here.
if ((layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) &&
!(image_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
LogObjectList objlist(image);
objlist.add(renderpass);
objlist.add(framebuffer);
objlist.add(image_view);
skip |= LogError(objlist, "VUID-vkCmdBeginRenderPass-initialLayout-00896",
"%s: Layout/usage mismatch for attachment %u in %s"
" - the %s is %s but the image attached to %s via %s"
" was not created with VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT",
function_name, attachment_index, report_data->FormatHandle(renderpass).c_str(), variable_name,
string_VkImageLayout(layout), report_data->FormatHandle(framebuffer).c_str(),
report_data->FormatHandle(image_view).c_str());
}
}
return skip;
}
bool CoreChecks::VerifyFramebufferAndRenderPassLayouts(RenderPassCreateVersion rp_version, const CMD_BUFFER_STATE *pCB,
const VkRenderPassBeginInfo *pRenderPassBegin,
const FRAMEBUFFER_STATE *framebuffer_state) const {
bool skip = false;
auto const render_pass_info = GetRenderPassState(pRenderPassBegin->renderPass)->createInfo.ptr();
auto const &framebuffer_info = framebuffer_state->createInfo;
const VkImageView *attachments = framebuffer_info.pAttachments;
auto render_pass = GetRenderPassState(pRenderPassBegin->renderPass)->renderPass;
auto framebuffer = framebuffer_state->framebuffer;
if (render_pass_info->attachmentCount != framebuffer_info.attachmentCount) {
skip |= LogError(pCB->commandBuffer, kVUID_Core_DrawState_InvalidRenderpass,
"You cannot start a render pass using a framebuffer with a different number of attachments.");
}
const auto *attachment_info = LvlFindInChain<VkRenderPassAttachmentBeginInfo>(pRenderPassBegin->pNext);
if (((framebuffer_info.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) != 0) && attachment_info != nullptr) {
attachments = attachment_info->pAttachments;
}
if (attachments != nullptr) {
const auto *const_p_cb = static_cast<const CMD_BUFFER_STATE *>(pCB);
for (uint32_t i = 0; i < render_pass_info->attachmentCount; ++i) {
auto image_view = attachments[i];
auto view_state = GetImageViewState(image_view);
if (!view_state) {
LogObjectList objlist(pRenderPassBegin->renderPass);
objlist.add(framebuffer_state->framebuffer);
objlist.add(image_view);
skip |= LogError(objlist, "VUID-VkRenderPassBeginInfo-framebuffer-parameter",
"vkCmdBeginRenderPass(): %s pAttachments[%" PRIu32 "] = %s is not a valid VkImageView handle",
report_data->FormatHandle(framebuffer_state->framebuffer).c_str(), i,
report_data->FormatHandle(image_view).c_str());
continue;
}
const VkImage image = view_state->create_info.image;
const IMAGE_STATE *image_state = GetImageState(image);
if (!image_state) {
LogObjectList objlist(pRenderPassBegin->renderPass);
objlist.add(framebuffer_state->framebuffer);
objlist.add(image_view);
objlist.add(image);
skip |= LogError(objlist, "VUID-VkRenderPassBeginInfo-framebuffer-parameter",
"vkCmdBeginRenderPass(): %s pAttachments[%" PRIu32 "] = %s references non-extant %s.",
report_data->FormatHandle(framebuffer_state->framebuffer).c_str(), i,
report_data->FormatHandle(image_view).c_str(), report_data->FormatHandle(image).c_str());
continue;
}
auto attachment_initial_layout = render_pass_info->pAttachments[i].initialLayout;
auto final_layout = render_pass_info->pAttachments[i].finalLayout;
// Default to expecting stencil in the same layout.
auto attachment_stencil_initial_layout = attachment_initial_layout;
// If a separate layout is specified, look for that.
const auto *attachment_description_stencil_layout =
LvlFindInChain<VkAttachmentDescriptionStencilLayout>(render_pass_info->pAttachments[i].pNext);
if (attachment_description_stencil_layout) {
attachment_stencil_initial_layout = attachment_description_stencil_layout->stencilInitialLayout;
}
// Cast pCB to const because we don't want to create entries that don't exist here (in case the key changes to something
// in common with the non-const version.)
const ImageSubresourceLayoutMap *subresource_map = (attachment_initial_layout != VK_IMAGE_LAYOUT_UNDEFINED)
? GetImageSubresourceLayoutMap(const_p_cb, image)
: nullptr;
if (subresource_map) { // If no layout information for image yet, will be checked at QueueSubmit time
LayoutUseCheckAndMessage layout_check(subresource_map);
bool subres_skip = false;
auto pos = subresource_map->Find(view_state->normalized_subresource_range);
// IncrementInterval skips over all the subresources that have the same state as we just checked, incrementing to
// the next "constant value" range
for (; !(pos.AtEnd()) && !subres_skip; pos.IncrementInterval()) {
const VkImageSubresource &subres = pos->subresource;
// Allow for differing depth and stencil layouts
VkImageLayout check_layout = attachment_initial_layout;
if (subres.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) check_layout = attachment_stencil_initial_layout;
if (!layout_check.Check(subres, check_layout, pos->current_layout, pos->initial_layout)) {
subres_skip |= LogError(
device, kVUID_Core_DrawState_InvalidRenderpass,
"You cannot start a render pass using attachment %u where the render pass initial layout is %s "
"and the %s layout of the attachment is %s. The layouts must match, or the render "
"pass initial layout for the attachment must be VK_IMAGE_LAYOUT_UNDEFINED",
i, string_VkImageLayout(check_layout), layout_check.message, string_VkImageLayout(layout_check.layout));
}
}
skip |= subres_skip;
}
ValidateRenderPassLayoutAgainstFramebufferImageUsage(rp_version, attachment_initial_layout, image, image_view,
framebuffer, render_pass, i, "initial layout");
ValidateRenderPassLayoutAgainstFramebufferImageUsage(rp_version, final_layout, image, image_view, framebuffer,
render_pass, i, "final layout");
}
for (uint32_t j = 0; j < render_pass_info->subpassCount; ++j) {
auto &subpass = render_pass_info->pSubpasses[j];
for (uint32_t k = 0; k < render_pass_info->pSubpasses[j].inputAttachmentCount; ++k) {
auto &attachment_ref = subpass.pInputAttachments[k];
if (attachment_ref.attachment != VK_ATTACHMENT_UNUSED) {
auto image_view = attachments[attachment_ref.attachment];
auto view_state = GetImageViewState(image_view);
if (view_state) {
auto image = view_state->create_info.image;
ValidateRenderPassLayoutAgainstFramebufferImageUsage(rp_version, attachment_ref.layout, image, image_view,
framebuffer, render_pass, attachment_ref.attachment,
"input attachment layout");
}
}
}
for (uint32_t k = 0; k < render_pass_info->pSubpasses[j].colorAttachmentCount; ++k) {
auto &attachment_ref = subpass.pColorAttachments[k];
if (attachment_ref.attachment != VK_ATTACHMENT_UNUSED) {
auto image_view = attachments[attachment_ref.attachment];
auto view_state = GetImageViewState(image_view);
if (view_state) {
auto image = view_state->create_info.image;
ValidateRenderPassLayoutAgainstFramebufferImageUsage(rp_version, attachment_ref.layout, image, image_view,
framebuffer, render_pass, attachment_ref.attachment,
"color attachment layout");
if (subpass.pResolveAttachments) {
ValidateRenderPassLayoutAgainstFramebufferImageUsage(
rp_version, attachment_ref.layout, image, image_view, framebuffer, render_pass,
attachment_ref.attachment, "resolve attachment layout");
}
}
}
}
if (render_pass_info->pSubpasses[j].pDepthStencilAttachment) {
auto &attachment_ref = *subpass.pDepthStencilAttachment;
if (attachment_ref.attachment != VK_ATTACHMENT_UNUSED) {
auto image_view = attachments[attachment_ref.attachment];
auto view_state = GetImageViewState(image_view);
if (view_state) {
auto image = view_state->create_info.image;
ValidateRenderPassLayoutAgainstFramebufferImageUsage(rp_version, attachment_ref.layout, image, image_view,
framebuffer, render_pass, attachment_ref.attachment,
"input attachment layout");
}
}
}
}
}
return skip;
}
void CoreChecks::TransitionAttachmentRefLayout(CMD_BUFFER_STATE *pCB, FRAMEBUFFER_STATE *pFramebuffer,
const safe_VkAttachmentReference2 &ref) {
if (ref.attachment != VK_ATTACHMENT_UNUSED) {
IMAGE_VIEW_STATE *image_view = GetActiveAttachmentImageViewState(pCB, ref.attachment);
if (image_view) {
VkImageLayout stencil_layout = kInvalidLayout;
const auto *attachment_reference_stencil_layout = LvlFindInChain<VkAttachmentReferenceStencilLayout>(ref.pNext);
if (attachment_reference_stencil_layout) {
stencil_layout = attachment_reference_stencil_layout->stencilLayout;
}
SetImageViewLayout(pCB, *image_view, ref.layout, stencil_layout);
}
}
}
void CoreChecks::TransitionSubpassLayouts(CMD_BUFFER_STATE *pCB, const RENDER_PASS_STATE *render_pass_state,
const int subpass_index, FRAMEBUFFER_STATE *framebuffer_state) {
assert(render_pass_state);
if (framebuffer_state) {
auto const &subpass = render_pass_state->createInfo.pSubpasses[subpass_index];
for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
TransitionAttachmentRefLayout(pCB, framebuffer_state, subpass.pInputAttachments[j]);
}
for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
TransitionAttachmentRefLayout(pCB, framebuffer_state, subpass.pColorAttachments[j]);
}
if (subpass.pDepthStencilAttachment) {
TransitionAttachmentRefLayout(pCB, framebuffer_state, *subpass.pDepthStencilAttachment);
}
}
}
// Transition the layout state for renderpass attachments based on the BeginRenderPass() call. This includes:
// 1. Transition into initialLayout state
// 2. Transition from initialLayout to layout used in subpass 0
void CoreChecks::TransitionBeginRenderPassLayouts(CMD_BUFFER_STATE *cb_state, const RENDER_PASS_STATE *render_pass_state,
FRAMEBUFFER_STATE *framebuffer_state) {
// First record expected initialLayout as a potential initial layout usage.
auto const rpci = render_pass_state->createInfo.ptr();
for (uint32_t i = 0; i < rpci->attachmentCount; ++i) {
auto *view_state = GetActiveAttachmentImageViewState(cb_state, i);
if (view_state) {
IMAGE_STATE *image_state = view_state->image_state.get();
const auto initial_layout = rpci->pAttachments[i].initialLayout;
const auto *attachment_description_stencil_layout =
LvlFindInChain<VkAttachmentDescriptionStencilLayout>(rpci->pAttachments[i].pNext);
if (attachment_description_stencil_layout) {
const auto stencil_initial_layout = attachment_description_stencil_layout->stencilInitialLayout;
VkImageSubresourceRange sub_range = view_state->normalized_subresource_range;
sub_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
SetImageInitialLayout(cb_state, *image_state, sub_range, initial_layout);
sub_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
SetImageInitialLayout(cb_state, *image_state, sub_range, stencil_initial_layout);
} else {
SetImageInitialLayout(cb_state, *image_state, view_state->normalized_subresource_range, initial_layout);
}
}
}
// Now transition for first subpass (index 0)
TransitionSubpassLayouts(cb_state, render_pass_state, 0, framebuffer_state);
}
bool VerifyAspectsPresent(VkImageAspectFlags aspect_mask, VkFormat format) {
if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != 0) {
if (!(FormatIsColor(format) || FormatIsMultiplane(format))) return false;
}
if ((aspect_mask & VK_IMAGE_ASPECT_DEPTH_BIT) != 0) {
if (!FormatHasDepth(format)) return false;
}
if ((aspect_mask & VK_IMAGE_ASPECT_STENCIL_BIT) != 0) {
if (!FormatHasStencil(format)) return false;
}
if (0 != (aspect_mask & (VK_IMAGE_ASPECT_PLANE_0_BIT | VK_IMAGE_ASPECT_PLANE_1_BIT | VK_IMAGE_ASPECT_PLANE_2_BIT))) {
if (FormatPlaneCount(format) == 1) return false;
}
return true;
}
// Verify an ImageMemoryBarrier's old/new ImageLayouts are compatible with the Image's ImageUsageFlags.
bool CoreChecks::ValidateBarrierLayoutToImageUsage(const CoreErrorLocation &loc, VkImage image, VkImageLayout layout,
VkImageUsageFlags usage_flags) const {
bool skip = false;
bool is_error = false;
switch (layout) {
case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
is_error = ((usage_flags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) == 0);
break;
case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
is_error = ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0);
break;
case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
is_error = ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0);
break;
case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
is_error = ((usage_flags & (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) == 0);
break;
case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
is_error = ((usage_flags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) == 0);
break;
case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
is_error = ((usage_flags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) == 0);
break;
case VK_IMAGE_LAYOUT_SHADING_RATE_OPTIMAL_NV:
is_error = ((usage_flags & VK_IMAGE_USAGE_SHADING_RATE_IMAGE_BIT_NV) == 0);
break;
case VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL:
is_error = ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0);
break;
case VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL:
is_error = ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0);
break;
default:
// Other VkImageLayout values do not have VUs defined in this context.
break;
}
if (is_error) {
const auto &vuid = sync_vuid_maps::GetBadImageLayoutVUID(loc, layout);
skip |=
LogError(image, vuid, "%s Image barrier Layout=%s is not compatible with %s usage flags 0x%" PRIx32 ".",
loc.Message().c_str(), string_VkImageLayout(layout), report_data->FormatHandle(image).c_str(), usage_flags);
}
return skip;
}
// Verify image barriers are compatible with the images they reference.
template <typename ImageBarrier>