forked from KhronosGroup/Vulkan-LoaderAndValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer_validation.cpp
3174 lines (2913 loc) · 199 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-2017 The Khronos Group Inc.
* Copyright (c) 2015-2017 Valve Corporation
* Copyright (c) 2015-2017 LunarG, Inc.
* Copyright (C) 2015-2017 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: Mark Lobodzinski <[email protected]>
* Author: Dave Houlton <[email protected]>
*/
// Allow use of STL min and max functions in Windows
#define NOMINMAX
#include <sstream>
#include "vk_enum_string_helper.h"
#include "vk_layer_data.h"
#include "vk_layer_utils.h"
#include "vk_layer_logging.h"
#include "buffer_validation.h"
void SetLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, ImageSubresourcePair imgpair, const VkImageLayout &layout) {
if (std::find(pCB->imageSubresourceMap[imgpair.image].begin(), pCB->imageSubresourceMap[imgpair.image].end(), imgpair) !=
pCB->imageSubresourceMap[imgpair.image].end()) {
pCB->imageLayoutMap[imgpair].layout = layout;
} else {
assert(imgpair.hasSubresource);
IMAGE_CMD_BUF_LAYOUT_NODE node;
if (!FindCmdBufLayout(device_data, pCB, imgpair.image, imgpair.subresource, node)) {
node.initialLayout = layout;
}
SetLayout(device_data, pCB, imgpair, {node.initialLayout, layout});
}
}
template <class OBJECT, class LAYOUT>
void SetLayout(layer_data *device_data, OBJECT *pObject, VkImage image, VkImageSubresource range, const LAYOUT &layout) {
ImageSubresourcePair imgpair = {image, true, range};
SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
SetLayout(device_data, pObject, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
}
template <class OBJECT, class LAYOUT>
void SetLayout(layer_data *device_data, OBJECT *pObject, ImageSubresourcePair imgpair, const LAYOUT &layout,
VkImageAspectFlags aspectMask) {
if (imgpair.subresource.aspectMask & aspectMask) {
imgpair.subresource.aspectMask = aspectMask;
SetLayout(device_data, pObject, imgpair, layout);
}
}
// Set the layout in supplied map
void SetLayout(std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap, ImageSubresourcePair imgpair,
VkImageLayout layout) {
imageLayoutMap[imgpair].layout = layout;
}
bool FindLayoutVerifyNode(layer_data *device_data, GLOBAL_CB_NODE *pCB, ImageSubresourcePair imgpair,
IMAGE_CMD_BUF_LAYOUT_NODE &node, const VkImageAspectFlags aspectMask) {
const debug_report_data *report_data = core_validation::GetReportData(device_data);
if (!(imgpair.subresource.aspectMask & aspectMask)) {
return false;
}
VkImageAspectFlags oldAspectMask = imgpair.subresource.aspectMask;
imgpair.subresource.aspectMask = aspectMask;
auto imgsubIt = pCB->imageLayoutMap.find(imgpair);
if (imgsubIt == pCB->imageLayoutMap.end()) {
return false;
}
if (node.layout != VK_IMAGE_LAYOUT_MAX_ENUM && node.layout != imgsubIt->second.layout) {
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
reinterpret_cast<uint64_t &>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
"Cannot query for VkImage 0x%" PRIx64 " layout when combined aspect mask %d has multiple layout types: %s and %s",
reinterpret_cast<uint64_t &>(imgpair.image), oldAspectMask, string_VkImageLayout(node.layout),
string_VkImageLayout(imgsubIt->second.layout));
}
if (node.initialLayout != VK_IMAGE_LAYOUT_MAX_ENUM && node.initialLayout != imgsubIt->second.initialLayout) {
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
reinterpret_cast<uint64_t &>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
"Cannot query for VkImage 0x%" PRIx64
" layout when combined aspect mask %d has multiple initial layout types: %s and %s",
reinterpret_cast<uint64_t &>(imgpair.image), oldAspectMask, string_VkImageLayout(node.initialLayout),
string_VkImageLayout(imgsubIt->second.initialLayout));
}
node = imgsubIt->second;
return true;
}
bool FindLayoutVerifyLayout(layer_data *device_data, ImageSubresourcePair imgpair, VkImageLayout &layout,
const VkImageAspectFlags aspectMask) {
if (!(imgpair.subresource.aspectMask & aspectMask)) {
return false;
}
const debug_report_data *report_data = core_validation::GetReportData(device_data);
VkImageAspectFlags oldAspectMask = imgpair.subresource.aspectMask;
imgpair.subresource.aspectMask = aspectMask;
auto imgsubIt = (*core_validation::GetImageLayoutMap(device_data)).find(imgpair);
if (imgsubIt == (*core_validation::GetImageLayoutMap(device_data)).end()) {
return false;
}
if (layout != VK_IMAGE_LAYOUT_MAX_ENUM && layout != imgsubIt->second.layout) {
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
reinterpret_cast<uint64_t &>(imgpair.image), __LINE__, DRAWSTATE_INVALID_LAYOUT, "DS",
"Cannot query for VkImage 0x%" PRIx64 " layout when combined aspect mask %d has multiple layout types: %s and %s",
reinterpret_cast<uint64_t &>(imgpair.image), oldAspectMask, string_VkImageLayout(layout),
string_VkImageLayout(imgsubIt->second.layout));
}
layout = imgsubIt->second.layout;
return true;
}
// Find layout(s) on the command buffer level
bool FindCmdBufLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, VkImage image, VkImageSubresource range,
IMAGE_CMD_BUF_LAYOUT_NODE &node) {
ImageSubresourcePair imgpair = {image, true, range};
node = IMAGE_CMD_BUF_LAYOUT_NODE(VK_IMAGE_LAYOUT_MAX_ENUM, VK_IMAGE_LAYOUT_MAX_ENUM);
FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_COLOR_BIT);
FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_DEPTH_BIT);
FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_STENCIL_BIT);
FindLayoutVerifyNode(device_data, pCB, imgpair, node, VK_IMAGE_ASPECT_METADATA_BIT);
if (node.layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
imgpair = {image, false, VkImageSubresource()};
auto imgsubIt = pCB->imageLayoutMap.find(imgpair);
if (imgsubIt == pCB->imageLayoutMap.end()) return false;
// TODO: This is ostensibly a find function but it changes state here
node = imgsubIt->second;
}
return true;
}
// Find layout(s) on the global level
bool FindGlobalLayout(layer_data *device_data, ImageSubresourcePair imgpair, VkImageLayout &layout) {
layout = VK_IMAGE_LAYOUT_MAX_ENUM;
FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
FindLayoutVerifyLayout(device_data, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
if (layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
imgpair = {imgpair.image, false, VkImageSubresource()};
auto imgsubIt = (*core_validation::GetImageLayoutMap(device_data)).find(imgpair);
if (imgsubIt == (*core_validation::GetImageLayoutMap(device_data)).end()) return false;
layout = imgsubIt->second.layout;
}
return true;
}
bool FindLayouts(layer_data *device_data, VkImage image, std::vector<VkImageLayout> &layouts) {
auto sub_data = (*core_validation::GetImageSubresourceMap(device_data)).find(image);
if (sub_data == (*core_validation::GetImageSubresourceMap(device_data)).end()) return false;
auto image_state = GetImageState(device_data, image);
if (!image_state) return false;
bool ignoreGlobal = false;
// TODO: Make this robust for >1 aspect mask. Now it will just say ignore potential errors in this case.
if (sub_data->second.size() >= (image_state->createInfo.arrayLayers * image_state->createInfo.mipLevels + 1)) {
ignoreGlobal = true;
}
for (auto imgsubpair : sub_data->second) {
if (ignoreGlobal && !imgsubpair.hasSubresource) continue;
auto img_data = (*core_validation::GetImageLayoutMap(device_data)).find(imgsubpair);
if (img_data != (*core_validation::GetImageLayoutMap(device_data)).end()) {
layouts.push_back(img_data->second.layout);
}
}
return true;
}
bool FindLayout(const std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap, ImageSubresourcePair imgpair,
VkImageLayout &layout, const VkImageAspectFlags aspectMask) {
if (!(imgpair.subresource.aspectMask & aspectMask)) {
return false;
}
imgpair.subresource.aspectMask = aspectMask;
auto imgsubIt = imageLayoutMap.find(imgpair);
if (imgsubIt == imageLayoutMap.end()) {
return false;
}
layout = imgsubIt->second.layout;
return true;
}
// find layout in supplied map
bool FindLayout(const std::unordered_map<ImageSubresourcePair, IMAGE_LAYOUT_NODE> &imageLayoutMap, ImageSubresourcePair imgpair,
VkImageLayout &layout) {
layout = VK_IMAGE_LAYOUT_MAX_ENUM;
FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_COLOR_BIT);
FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_DEPTH_BIT);
FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_STENCIL_BIT);
FindLayout(imageLayoutMap, imgpair, layout, VK_IMAGE_ASPECT_METADATA_BIT);
if (layout == VK_IMAGE_LAYOUT_MAX_ENUM) {
imgpair = {imgpair.image, false, VkImageSubresource()};
auto imgsubIt = imageLayoutMap.find(imgpair);
if (imgsubIt == imageLayoutMap.end()) return false;
layout = imgsubIt->second.layout;
}
return true;
}
// Set the layout on the global level
void SetGlobalLayout(layer_data *device_data, ImageSubresourcePair imgpair, const VkImageLayout &layout) {
VkImage &image = imgpair.image;
(*core_validation::GetImageLayoutMap(device_data))[imgpair].layout = layout;
auto &image_subresources = (*core_validation::GetImageSubresourceMap(device_data))[image];
auto subresource = std::find(image_subresources.begin(), image_subresources.end(), imgpair);
if (subresource == image_subresources.end()) {
image_subresources.push_back(imgpair);
}
}
// Set the layout on the cmdbuf level
void SetLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, ImageSubresourcePair imgpair, const IMAGE_CMD_BUF_LAYOUT_NODE &node) {
pCB->imageLayoutMap[imgpair] = node;
auto subresource =
std::find(pCB->imageSubresourceMap[imgpair.image].begin(), pCB->imageSubresourceMap[imgpair.image].end(), imgpair);
if (subresource == pCB->imageSubresourceMap[imgpair.image].end()) {
pCB->imageSubresourceMap[imgpair.image].push_back(imgpair);
}
}
// Set image layout for given VkImageSubresourceRange struct
void SetImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *image_state,
VkImageSubresourceRange image_subresource_range, const VkImageLayout &layout) {
assert(image_state);
for (uint32_t level_index = 0; level_index < image_subresource_range.levelCount; ++level_index) {
uint32_t level = image_subresource_range.baseMipLevel + level_index;
for (uint32_t layer_index = 0; layer_index < image_subresource_range.layerCount; layer_index++) {
uint32_t layer = image_subresource_range.baseArrayLayer + layer_index;
VkImageSubresource sub = {image_subresource_range.aspectMask, level, layer};
// TODO: If ImageView was created with depth or stencil, transition both layouts as the aspectMask is ignored and both
// are used. Verify that the extra implicit layout is OK for descriptor set layout validation
if (image_subresource_range.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
if (FormatIsDepthAndStencil(image_state->createInfo.format)) {
sub.aspectMask |= (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
}
}
SetLayout(device_data, cb_node, image_state->image, sub, layout);
}
}
}
// Set image layout for given VkImageSubresourceLayers struct
void SetImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, const IMAGE_STATE *image_state,
VkImageSubresourceLayers image_subresource_layers, const VkImageLayout &layout) {
// Transfer VkImageSubresourceLayers into VkImageSubresourceRange struct
VkImageSubresourceRange image_subresource_range;
image_subresource_range.aspectMask = image_subresource_layers.aspectMask;
image_subresource_range.baseArrayLayer = image_subresource_layers.baseArrayLayer;
image_subresource_range.layerCount = image_subresource_layers.layerCount;
image_subresource_range.baseMipLevel = image_subresource_layers.mipLevel;
image_subresource_range.levelCount = 1;
SetImageLayout(device_data, cb_node, image_state, image_subresource_range, layout);
}
// Set image layout for all slices of an image view
void SetImageViewLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImageView imageView, const VkImageLayout &layout) {
auto view_state = GetImageViewState(device_data, imageView);
assert(view_state);
SetImageLayout(device_data, cb_node, GetImageState(device_data, view_state->create_info.image),
view_state->create_info.subresourceRange, layout);
}
bool VerifyFramebufferAndRenderPassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB,
const VkRenderPassBeginInfo *pRenderPassBegin,
const FRAMEBUFFER_STATE *framebuffer_state) {
bool skip_call = false;
auto const pRenderPassInfo = GetRenderPassState(device_data, pRenderPassBegin->renderPass)->createInfo.ptr();
auto const &framebufferInfo = framebuffer_state->createInfo;
const auto report_data = core_validation::GetReportData(device_data);
if (pRenderPassInfo->attachmentCount != framebufferInfo.attachmentCount) {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t>(pCB->commandBuffer), __LINE__, DRAWSTATE_INVALID_RENDERPASS, "DS",
"You cannot start a render pass using a framebuffer "
"with a different number of attachments.");
}
for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
const VkImageView &image_view = framebufferInfo.pAttachments[i];
auto view_state = GetImageViewState(device_data, image_view);
assert(view_state);
const VkImage &image = view_state->create_info.image;
const VkImageSubresourceRange &subRange = view_state->create_info.subresourceRange;
auto initial_layout = pRenderPassInfo->pAttachments[i].initialLayout;
// TODO: Do not iterate over every possibility - consolidate where possible
for (uint32_t j = 0; j < subRange.levelCount; j++) {
uint32_t level = subRange.baseMipLevel + j;
for (uint32_t k = 0; k < subRange.layerCount; k++) {
uint32_t layer = subRange.baseArrayLayer + k;
VkImageSubresource sub = {subRange.aspectMask, level, layer};
IMAGE_CMD_BUF_LAYOUT_NODE node;
if (!FindCmdBufLayout(device_data, pCB, image, sub, node)) {
// Missing layouts will be added during state update
continue;
}
if (initial_layout != VK_IMAGE_LAYOUT_UNDEFINED && initial_layout != node.layout) {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__,
DRAWSTATE_INVALID_RENDERPASS, "DS",
"You cannot start a render pass using attachment %u "
"where the render pass initial layout is %s and the previous "
"known 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(initial_layout), string_VkImageLayout(node.layout));
}
}
}
}
return skip_call;
}
void TransitionAttachmentRefLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, FRAMEBUFFER_STATE *pFramebuffer,
VkAttachmentReference ref) {
if (ref.attachment != VK_ATTACHMENT_UNUSED) {
auto image_view = pFramebuffer->createInfo.pAttachments[ref.attachment];
SetImageViewLayout(device_data, pCB, image_view, ref.layout);
}
}
void TransitionSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *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(device_data, pCB, framebuffer_state, subpass.pInputAttachments[j]);
}
for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, subpass.pColorAttachments[j]);
}
if (subpass.pDepthStencilAttachment) {
TransitionAttachmentRefLayout(device_data, pCB, framebuffer_state, *subpass.pDepthStencilAttachment);
}
}
}
bool ValidateImageAspectLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkImageMemoryBarrier *mem_barrier,
uint32_t level, uint32_t layer, VkImageAspectFlags aspect) {
if (!(mem_barrier->subresourceRange.aspectMask & aspect)) {
return false;
}
VkImageSubresource sub = {aspect, level, layer};
IMAGE_CMD_BUF_LAYOUT_NODE node;
if (!FindCmdBufLayout(device_data, pCB, mem_barrier->image, sub, node)) {
return false;
}
bool skip = false;
if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
// TODO: Set memory invalid which is in mem_tracker currently
} else if (node.layout != mem_barrier->oldLayout) {
skip |=
log_msg(core_validation::GetReportData(device_data), VK_DEBUG_REPORT_ERROR_BIT_EXT,
VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, reinterpret_cast<uint64_t>(pCB->commandBuffer), __LINE__,
DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
"For image 0x%" PRIxLEAST64 " you cannot transition the layout of aspect %d from %s when current layout is %s.",
reinterpret_cast<const uint64_t &>(mem_barrier->image), aspect, string_VkImageLayout(mem_barrier->oldLayout),
string_VkImageLayout(node.layout));
}
return skip;
}
// 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 TransitionBeginRenderPassLayouts(layer_data *device_data, GLOBAL_CB_NODE *cb_state, const RENDER_PASS_STATE *render_pass_state,
FRAMEBUFFER_STATE *framebuffer_state) {
// First transition into initialLayout
auto const rpci = render_pass_state->createInfo.ptr();
for (uint32_t i = 0; i < rpci->attachmentCount; ++i) {
VkImageView image_view = framebuffer_state->createInfo.pAttachments[i];
SetImageViewLayout(device_data, cb_state, image_view, rpci->pAttachments[i].initialLayout);
}
// Now transition for first subpass (index 0)
TransitionSubpassLayouts(device_data, cb_state, render_pass_state, 0, framebuffer_state);
}
void TransitionImageAspectLayout(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkImageMemoryBarrier *mem_barrier,
uint32_t level, uint32_t layer, VkImageAspectFlags aspect) {
if (!(mem_barrier->subresourceRange.aspectMask & aspect)) {
return;
}
VkImageSubresource sub = {aspect, level, layer};
IMAGE_CMD_BUF_LAYOUT_NODE node;
if (!FindCmdBufLayout(device_data, pCB, mem_barrier->image, sub, node)) {
SetLayout(device_data, pCB, mem_barrier->image, sub,
IMAGE_CMD_BUF_LAYOUT_NODE(mem_barrier->oldLayout, mem_barrier->newLayout));
return;
}
if (mem_barrier->oldLayout == VK_IMAGE_LAYOUT_UNDEFINED) {
// TODO: Set memory invalid
}
SetLayout(device_data, pCB, mem_barrier->image, sub, mem_barrier->newLayout);
}
bool VerifyAspectsPresent(VkImageAspectFlags aspect_mask, VkFormat format) {
if ((aspect_mask & VK_IMAGE_ASPECT_COLOR_BIT) != 0) {
if (!FormatIsColor(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;
}
return true;
}
// Verify an ImageMemoryBarrier's old/new ImageLayouts are compatible with the Image's ImageUsageFlags.
bool ValidateBarrierLayoutToImageUsage(layer_data *device_data, const VkImageMemoryBarrier *img_barrier, bool new_not_old,
VkImageUsageFlags usage_flags, const char *func_name) {
const auto report_data = core_validation::GetReportData(device_data);
bool skip = false;
const VkImageLayout layout = (new_not_old) ? img_barrier->newLayout : img_barrier->oldLayout;
UNIQUE_VALIDATION_ERROR_CODE msg_code = VALIDATION_ERROR_UNDEFINED; // sentinel value meaning "no error"
switch (layout) {
case VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL:
if ((usage_flags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) == 0) {
msg_code = VALIDATION_ERROR_00303;
}
break;
case VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL:
if ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0) {
msg_code = VALIDATION_ERROR_00304;
}
break;
case VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL:
if ((usage_flags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0) {
msg_code = VALIDATION_ERROR_00305;
}
break;
case VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL:
if ((usage_flags & (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) == 0) {
msg_code = VALIDATION_ERROR_00306;
}
break;
case VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:
if ((usage_flags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT) == 0) {
msg_code = VALIDATION_ERROR_00307;
}
break;
case VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL:
if ((usage_flags & VK_IMAGE_USAGE_TRANSFER_DST_BIT) == 0) {
msg_code = VALIDATION_ERROR_00308;
}
break;
default:
// Other VkImageLayout values do not have VUs defined in this context.
break;
}
if (msg_code != VALIDATION_ERROR_UNDEFINED) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
reinterpret_cast<const uint64_t &>(img_barrier->image), __LINE__, msg_code, "DS",
"%s: Image barrier 0x%p %sLayout=%s is not compatible with image 0x%" PRIx64 " usage flags 0x%" PRIx32 ". %s",
func_name, img_barrier, ((new_not_old) ? "new" : "old"), string_VkImageLayout(layout),
reinterpret_cast<const uint64_t &>(img_barrier->image), usage_flags, validation_error_map[msg_code]);
}
return skip;
}
// Verify image barriers are compatible with the images they reference.
bool ValidateBarriersToImages(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t imageMemoryBarrierCount,
const VkImageMemoryBarrier *pImageMemoryBarriers, const char *func_name) {
GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
bool skip = false;
for (uint32_t i = 0; i < imageMemoryBarrierCount; ++i) {
auto img_barrier = &pImageMemoryBarriers[i];
if (!img_barrier) continue;
VkImageCreateInfo *image_create_info = &(GetImageState(device_data, img_barrier->image)->createInfo);
uint32_t level_count = ResolveRemainingLevels(&img_barrier->subresourceRange, image_create_info->mipLevels);
uint32_t layer_count = ResolveRemainingLayers(&img_barrier->subresourceRange, image_create_info->arrayLayers);
for (uint32_t j = 0; j < level_count; j++) {
uint32_t level = img_barrier->subresourceRange.baseMipLevel + j;
for (uint32_t k = 0; k < layer_count; k++) {
uint32_t layer = img_barrier->subresourceRange.baseArrayLayer + k;
skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
skip |= ValidateImageAspectLayout(device_data, pCB, img_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
}
}
IMAGE_STATE *image_state = GetImageState(device_data, img_barrier->image);
if (image_state) {
VkImageUsageFlags usage_flags = image_state->createInfo.usage;
skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, false, usage_flags, func_name);
skip |= ValidateBarrierLayoutToImageUsage(device_data, img_barrier, true, usage_flags, func_name);
}
}
return skip;
}
void TransitionImageLayouts(layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t memBarrierCount,
const VkImageMemoryBarrier *pImgMemBarriers) {
GLOBAL_CB_NODE *pCB = GetCBNode(device_data, cmdBuffer);
for (uint32_t i = 0; i < memBarrierCount; ++i) {
auto mem_barrier = &pImgMemBarriers[i];
if (!mem_barrier) continue;
VkImageCreateInfo *image_create_info = &(GetImageState(device_data, mem_barrier->image)->createInfo);
uint32_t level_count = ResolveRemainingLevels(&mem_barrier->subresourceRange, image_create_info->mipLevels);
uint32_t layer_count = ResolveRemainingLayers(&mem_barrier->subresourceRange, image_create_info->arrayLayers);
for (uint32_t j = 0; j < level_count; j++) {
uint32_t level = mem_barrier->subresourceRange.baseMipLevel + j;
for (uint32_t k = 0; k < layer_count; k++) {
uint32_t layer = mem_barrier->subresourceRange.baseArrayLayer + k;
TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
}
}
}
}
bool VerifyImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *image_state,
VkImageSubresourceLayers subLayers, VkImageLayout explicit_layout, VkImageLayout optimal_layout,
const char *caller, UNIQUE_VALIDATION_ERROR_CODE msg_code) {
const auto report_data = core_validation::GetReportData(device_data);
const auto image = image_state->image;
bool skip_call = false;
for (uint32_t i = 0; i < subLayers.layerCount; ++i) {
uint32_t layer = i + subLayers.baseArrayLayer;
VkImageSubresource sub = {subLayers.aspectMask, subLayers.mipLevel, layer};
IMAGE_CMD_BUF_LAYOUT_NODE node;
if (FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
if (node.layout != explicit_layout) {
// TODO: Improve log message in the next pass
skip_call |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
"%s: Cannot use image 0x%" PRIxLEAST64
" with specific layout %s that doesn't match the actual current layout %s.",
caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
string_VkImageLayout(node.layout));
}
}
}
// If optimal_layout is not UNDEFINED, check that layout matches optimal for this case
if ((VK_IMAGE_LAYOUT_UNDEFINED != optimal_layout) && (explicit_layout != optimal_layout)) {
if (VK_IMAGE_LAYOUT_GENERAL == explicit_layout) {
if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
// LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
skip_call |= log_msg(
report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
"%s: For optimal performance image 0x%" PRIxLEAST64 " layout should be %s instead of GENERAL.", caller,
reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(optimal_layout));
}
} else {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t>(cb_node->commandBuffer), __LINE__, msg_code, "DS",
"%s: Layout for image 0x%" PRIxLEAST64 " is %s but can only be %s or VK_IMAGE_LAYOUT_GENERAL. %s",
caller, reinterpret_cast<const uint64_t &>(image), string_VkImageLayout(explicit_layout),
string_VkImageLayout(optimal_layout), validation_error_map[msg_code]);
}
}
return skip_call;
}
void TransitionFinalSubpassLayouts(layer_data *device_data, GLOBAL_CB_NODE *pCB, const VkRenderPassBeginInfo *pRenderPassBegin,
FRAMEBUFFER_STATE *framebuffer_state) {
auto renderPass = GetRenderPassState(device_data, pRenderPassBegin->renderPass);
if (!renderPass) return;
const VkRenderPassCreateInfo *pRenderPassInfo = renderPass->createInfo.ptr();
if (framebuffer_state) {
for (uint32_t i = 0; i < pRenderPassInfo->attachmentCount; ++i) {
auto image_view = framebuffer_state->createInfo.pAttachments[i];
SetImageViewLayout(device_data, pCB, image_view, pRenderPassInfo->pAttachments[i].finalLayout);
}
}
}
bool PreCallValidateCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo,
const VkAllocationCallbacks *pAllocator, VkImage *pImage) {
bool skip_call = false;
const debug_report_data *report_data = core_validation::GetReportData(device_data);
if (pCreateInfo->format == VK_FORMAT_UNDEFINED) {
skip_call |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
VALIDATION_ERROR_00715, "IMAGE", "vkCreateImage: VkFormat for image must not be VK_FORMAT_UNDEFINED. %s",
validation_error_map[VALIDATION_ERROR_00715]);
return skip_call;
}
const VkFormatProperties *properties = GetFormatProperties(device_data, pCreateInfo->format);
if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) && (properties->linearTilingFeatures == 0)) {
std::stringstream ss;
ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
skip_call |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
VALIDATION_ERROR_02150, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02150]);
return skip_call;
}
if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) && (properties->optimalTilingFeatures == 0)) {
std::stringstream ss;
ss << "vkCreateImage format parameter (" << string_VkFormat(pCreateInfo->format) << ") is an unsupported format";
skip_call |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
VALIDATION_ERROR_02155, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02155]);
return skip_call;
}
// Validate that format supports usage as color attachment
if (pCreateInfo->usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT) {
if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
std::stringstream ss;
ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
<< ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
skip_call |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
VALIDATION_ERROR_02158, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02158]);
}
if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
((properties->linearTilingFeatures & VK_FORMAT_FEATURE_COLOR_ATTACHMENT_BIT) == 0)) {
std::stringstream ss;
ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
<< ") does not support requested Image usage type VK_IMAGE_USAGE_COLOR_ATTACHMENT";
skip_call |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
VALIDATION_ERROR_02153, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02153]);
}
}
// Validate that format supports usage as depth/stencil attachment
if (pCreateInfo->usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT) {
if ((pCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL) &&
((properties->optimalTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
std::stringstream ss;
ss << "vkCreateImage: VkFormat for TILING_OPTIMAL image (" << string_VkFormat(pCreateInfo->format)
<< ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
skip_call |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
VALIDATION_ERROR_02159, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02159]);
}
if ((pCreateInfo->tiling == VK_IMAGE_TILING_LINEAR) &&
((properties->linearTilingFeatures & VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT) == 0)) {
std::stringstream ss;
ss << "vkCreateImage: VkFormat for TILING_LINEAR image (" << string_VkFormat(pCreateInfo->format)
<< ") does not support requested Image usage type VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT";
skip_call |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
VALIDATION_ERROR_02154, "IMAGE", "%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_02154]);
}
}
const VkImageFormatProperties *ImageFormatProperties = GetImageFormatProperties(
device_data, pCreateInfo->format, pCreateInfo->imageType, pCreateInfo->tiling, pCreateInfo->usage, pCreateInfo->flags);
VkDeviceSize imageGranularity = GetPhysicalDeviceProperties(device_data)->limits.bufferImageGranularity;
imageGranularity = imageGranularity == 1 ? 0 : imageGranularity;
// TODO : This is also covering 2918 & 2919. Break out into separate checks
if ((pCreateInfo->extent.width <= 0) || (pCreateInfo->extent.height <= 0) || (pCreateInfo->extent.depth <= 0)) {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
VALIDATION_ERROR_02917, "Image",
"CreateImage extent is 0 for at least one required dimension for image: "
"Width = %d Height = %d Depth = %d. %s",
pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
validation_error_map[VALIDATION_ERROR_02917]);
}
// TODO: VALIDATION_ERROR_02125 VALIDATION_ERROR_02126 VALIDATION_ERROR_02128 VALIDATION_ERROR_00720
// All these extent-related VUs should be checked here
if ((pCreateInfo->extent.depth > ImageFormatProperties->maxExtent.depth) ||
(pCreateInfo->extent.width > ImageFormatProperties->maxExtent.width) ||
(pCreateInfo->extent.height > ImageFormatProperties->maxExtent.height)) {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
"CreateImage extents exceed allowable limits for format: "
"Width = %d Height = %d Depth = %d: Limits for Width = %d Height = %d Depth = %d for format %s.",
pCreateInfo->extent.width, pCreateInfo->extent.height, pCreateInfo->extent.depth,
ImageFormatProperties->maxExtent.width, ImageFormatProperties->maxExtent.height,
ImageFormatProperties->maxExtent.depth, string_VkFormat(pCreateInfo->format));
}
uint64_t totalSize = ((uint64_t)pCreateInfo->extent.width * (uint64_t)pCreateInfo->extent.height *
(uint64_t)pCreateInfo->extent.depth * (uint64_t)pCreateInfo->arrayLayers *
(uint64_t)pCreateInfo->samples * (uint64_t)FormatSize(pCreateInfo->format) +
(uint64_t)imageGranularity) &
~(uint64_t)imageGranularity;
if (totalSize > ImageFormatProperties->maxResourceSize) {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
"CreateImage resource size exceeds allowable maximum "
"Image resource size = 0x%" PRIxLEAST64 ", maximum resource size = 0x%" PRIxLEAST64 " ",
totalSize, ImageFormatProperties->maxResourceSize);
}
// TODO: VALIDATION_ERROR_02132
if (pCreateInfo->mipLevels > ImageFormatProperties->maxMipLevels) {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
IMAGE_INVALID_FORMAT_LIMITS_VIOLATION, "Image",
"CreateImage mipLevels=%d exceeds allowable maximum supported by format of %d", pCreateInfo->mipLevels,
ImageFormatProperties->maxMipLevels);
}
if (pCreateInfo->arrayLayers > ImageFormatProperties->maxArrayLayers) {
skip_call |= log_msg(
report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__, VALIDATION_ERROR_02133,
"Image", "CreateImage arrayLayers=%d exceeds allowable maximum supported by format of %d. %s", pCreateInfo->arrayLayers,
ImageFormatProperties->maxArrayLayers, validation_error_map[VALIDATION_ERROR_02133]);
}
if ((pCreateInfo->samples & ImageFormatProperties->sampleCounts) == 0) {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
VALIDATION_ERROR_02138, "Image", "CreateImage samples %s is not supported by format 0x%.8X. %s",
string_VkSampleCountFlagBits(pCreateInfo->samples), ImageFormatProperties->sampleCounts,
validation_error_map[VALIDATION_ERROR_02138]);
}
if (pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_UNDEFINED && pCreateInfo->initialLayout != VK_IMAGE_LAYOUT_PREINITIALIZED) {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT, 0, __LINE__,
VALIDATION_ERROR_00731, "Image",
"vkCreateImage parameter, pCreateInfo->initialLayout, must be VK_IMAGE_LAYOUT_UNDEFINED or "
"VK_IMAGE_LAYOUT_PREINITIALIZED. %s",
validation_error_map[VALIDATION_ERROR_00731]);
}
if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_BINDING_BIT) && (!GetEnabledFeatures(device_data)->sparseBinding)) {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
VALIDATION_ERROR_02143, "DS",
"vkCreateImage(): the sparseBinding device feature is disabled: Images cannot be created with the "
"VK_IMAGE_CREATE_SPARSE_BINDING_BIT set. %s",
validation_error_map[VALIDATION_ERROR_02143]);
}
if ((pCreateInfo->flags & VK_IMAGE_CREATE_SPARSE_ALIASED_BIT) && (!GetEnabledFeatures(device_data)->sparseResidencyAliased)) {
skip_call |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0, __LINE__,
DRAWSTATE_INVALID_FEATURE, "DS",
"vkCreateImage(): the sparseResidencyAliased device feature is disabled: Images cannot be created with the "
"VK_IMAGE_CREATE_SPARSE_ALIASED_BIT set.");
}
return skip_call;
}
void PostCallRecordCreateImage(layer_data *device_data, const VkImageCreateInfo *pCreateInfo, VkImage *pImage) {
IMAGE_LAYOUT_NODE image_state;
image_state.layout = pCreateInfo->initialLayout;
image_state.format = pCreateInfo->format;
GetImageMap(device_data)->insert(std::make_pair(*pImage, std::unique_ptr<IMAGE_STATE>(new IMAGE_STATE(*pImage, pCreateInfo))));
ImageSubresourcePair subpair{*pImage, false, VkImageSubresource()};
(*core_validation::GetImageSubresourceMap(device_data))[*pImage].push_back(subpair);
(*core_validation::GetImageLayoutMap(device_data))[subpair] = image_state;
}
bool PreCallValidateDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE **image_state, VK_OBJECT *obj_struct) {
const CHECK_DISABLED *disabled = core_validation::GetDisables(device_data);
*image_state = core_validation::GetImageState(device_data, image);
*obj_struct = {reinterpret_cast<uint64_t &>(image), VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT};
if (disabled->destroy_image) return false;
bool skip = false;
if (*image_state) {
skip |= core_validation::ValidateObjectNotInUse(device_data, *image_state, *obj_struct, VALIDATION_ERROR_00743);
}
return skip;
}
void PostCallRecordDestroyImage(layer_data *device_data, VkImage image, IMAGE_STATE *image_state, VK_OBJECT obj_struct) {
core_validation::invalidateCommandBuffers(device_data, image_state->cb_bindings, obj_struct);
// Clean up memory mapping, bindings and range references for image
for (auto mem_binding : image_state->GetBoundMemory()) {
auto mem_info = core_validation::GetMemObjInfo(device_data, mem_binding);
if (mem_info) {
core_validation::RemoveImageMemoryRange(obj_struct.handle, mem_info);
}
}
core_validation::ClearMemoryObjectBindings(device_data, obj_struct.handle, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT);
// Remove image from imageMap
core_validation::GetImageMap(device_data)->erase(image);
std::unordered_map<VkImage, std::vector<ImageSubresourcePair>> *imageSubresourceMap =
core_validation::GetImageSubresourceMap(device_data);
const auto &sub_entry = imageSubresourceMap->find(image);
if (sub_entry != imageSubresourceMap->end()) {
for (const auto &pair : sub_entry->second) {
core_validation::GetImageLayoutMap(device_data)->erase(pair);
}
imageSubresourceMap->erase(sub_entry);
}
}
bool ValidateImageAttributes(layer_data *device_data, IMAGE_STATE *image_state, VkImageSubresourceRange range) {
bool skip = false;
const debug_report_data *report_data = core_validation::GetReportData(device_data);
if (range.aspectMask != VK_IMAGE_ASPECT_COLOR_BIT) {
char const str[] = "vkCmdClearColorImage aspectMasks for all subresource ranges must be set to VK_IMAGE_ASPECT_COLOR_BIT";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);
}
if (FormatIsDepthOrStencil(image_state->createInfo.format)) {
char const str[] = "vkCmdClearColorImage called with depth/stencil image.";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
validation_error_map[VALIDATION_ERROR_01088]);
} else if (FormatIsCompressed(image_state->createInfo.format)) {
char const str[] = "vkCmdClearColorImage called with compressed image.";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01088, "IMAGE", "%s. %s", str,
validation_error_map[VALIDATION_ERROR_01088]);
}
if (!(image_state->createInfo.usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
char const str[] = "vkCmdClearColorImage called with image created without VK_IMAGE_USAGE_TRANSFER_DST_BIT.";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
reinterpret_cast<uint64_t &>(image_state->image), __LINE__, VALIDATION_ERROR_01084, "IMAGE", "%s. %s", str,
validation_error_map[VALIDATION_ERROR_01084]);
}
return skip;
}
uint32_t ResolveRemainingLevels(const VkImageSubresourceRange *range, uint32_t mip_levels) {
// Return correct number of mip levels taking into account VK_REMAINING_MIP_LEVELS
uint32_t mip_level_count = range->levelCount;
if (range->levelCount == VK_REMAINING_MIP_LEVELS) {
mip_level_count = mip_levels - range->baseMipLevel;
}
return mip_level_count;
}
uint32_t ResolveRemainingLayers(const VkImageSubresourceRange *range, uint32_t layers) {
// Return correct number of layers taking into account VK_REMAINING_ARRAY_LAYERS
uint32_t array_layer_count = range->layerCount;
if (range->layerCount == VK_REMAINING_ARRAY_LAYERS) {
array_layer_count = layers - range->baseArrayLayer;
}
return array_layer_count;
}
bool VerifyClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *image_state,
VkImageSubresourceRange range, VkImageLayout dest_image_layout, const char *func_name) {
bool skip = false;
const debug_report_data *report_data = core_validation::GetReportData(device_data);
uint32_t level_count = ResolveRemainingLevels(&range, image_state->createInfo.mipLevels);
uint32_t layer_count = ResolveRemainingLayers(&range, image_state->createInfo.arrayLayers);
if (dest_image_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
if (dest_image_layout == VK_IMAGE_LAYOUT_GENERAL) {
if (image_state->createInfo.tiling != VK_IMAGE_TILING_LINEAR) {
// LAYOUT_GENERAL is allowed, but may not be performance optimal, flag as perf warning.
skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
reinterpret_cast<uint64_t &>(image_state->image), __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
"%s: Layout for cleared image should be TRANSFER_DST_OPTIMAL instead of GENERAL.", func_name);
}
} else {
UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01086;
if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
error_code = VALIDATION_ERROR_01101;
} else {
assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
}
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_IMAGE_EXT,
reinterpret_cast<uint64_t &>(image_state->image), __LINE__, error_code, "DS",
"%s: Layout for cleared image is %s but can only be "
"TRANSFER_DST_OPTIMAL or GENERAL. %s",
func_name, string_VkImageLayout(dest_image_layout), validation_error_map[error_code]);
}
}
for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
uint32_t level = level_index + range.baseMipLevel;
for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
uint32_t layer = layer_index + range.baseArrayLayer;
VkImageSubresource sub = {range.aspectMask, level, layer};
IMAGE_CMD_BUF_LAYOUT_NODE node;
if (FindCmdBufLayout(device_data, cb_node, image_state->image, sub, node)) {
if (node.layout != dest_image_layout) {
UNIQUE_VALIDATION_ERROR_CODE error_code = VALIDATION_ERROR_01085;
if (strcmp(func_name, "vkCmdClearDepthStencilImage()") == 0) {
error_code = VALIDATION_ERROR_01100;
} else {
assert(strcmp(func_name, "vkCmdClearColorImage()") == 0);
}
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
__LINE__, error_code, "DS",
"%s: Cannot clear an image whose layout is %s and "
"doesn't match the current layout %s. %s",
func_name, string_VkImageLayout(dest_image_layout), string_VkImageLayout(node.layout),
validation_error_map[error_code]);
}
}
}
}
return skip;
}
void RecordClearImageLayout(layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImage image, VkImageSubresourceRange range,
VkImageLayout dest_image_layout) {
VkImageCreateInfo *image_create_info = &(GetImageState(device_data, image)->createInfo);
uint32_t level_count = ResolveRemainingLevels(&range, image_create_info->mipLevels);
uint32_t layer_count = ResolveRemainingLayers(&range, image_create_info->arrayLayers);
for (uint32_t level_index = 0; level_index < level_count; ++level_index) {
uint32_t level = level_index + range.baseMipLevel;
for (uint32_t layer_index = 0; layer_index < layer_count; ++layer_index) {
uint32_t layer = layer_index + range.baseArrayLayer;
VkImageSubresource sub = {range.aspectMask, level, layer};
IMAGE_CMD_BUF_LAYOUT_NODE node;
if (!FindCmdBufLayout(device_data, cb_node, image, sub, node)) {
SetLayout(device_data, cb_node, image, sub, IMAGE_CMD_BUF_LAYOUT_NODE(dest_image_layout, dest_image_layout));
}
}
}
}
bool PreCallValidateCmdClearColorImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image,
VkImageLayout imageLayout, uint32_t rangeCount, const VkImageSubresourceRange *pRanges) {
bool skip = false;
// TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
auto cb_node = GetCBNode(dev_data, commandBuffer);
auto image_state = GetImageState(dev_data, image);
if (cb_node && image_state) {
skip |= ValidateMemoryIsBoundToImage(dev_data, image_state, "vkCmdClearColorImage()", VALIDATION_ERROR_02527);
skip |= ValidateCmdQueueFlags(dev_data, cb_node, "vkCmdClearColorImage()", VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT,
VALIDATION_ERROR_01095);
skip |= ValidateCmd(dev_data, cb_node, CMD_CLEARCOLORIMAGE, "vkCmdClearColorImage()");
skip |= insideRenderPass(dev_data, cb_node, "vkCmdClearColorImage()", VALIDATION_ERROR_01096);
for (uint32_t i = 0; i < rangeCount; ++i) {
skip |= ValidateImageAttributes(dev_data, image_state, pRanges[i]);
skip |= VerifyClearImageLayout(dev_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearColorImage()");
}
}
return skip;
}
// This state recording routine is shared between ClearColorImage and ClearDepthStencilImage
void PreCallRecordCmdClearImage(layer_data *dev_data, VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
uint32_t rangeCount, const VkImageSubresourceRange *pRanges, CMD_TYPE cmd_type) {
auto cb_node = GetCBNode(dev_data, commandBuffer);
auto image_state = GetImageState(dev_data, image);
if (cb_node && image_state) {
AddCommandBufferBindingImage(dev_data, cb_node, image_state);
std::function<bool()> function = [=]() {
SetImageMemoryValid(dev_data, image_state, true);
return false;
};
cb_node->validate_functions.push_back(function);
core_validation::UpdateCmdBufferLastCmd(cb_node, cmd_type);
for (uint32_t i = 0; i < rangeCount; ++i) {
RecordClearImageLayout(dev_data, cb_node, image, pRanges[i], imageLayout);
}
}
}
bool PreCallValidateCmdClearDepthStencilImage(layer_data *device_data, VkCommandBuffer commandBuffer, VkImage image,
VkImageLayout imageLayout, uint32_t rangeCount,
const VkImageSubresourceRange *pRanges) {
bool skip = false;
const debug_report_data *report_data = core_validation::GetReportData(device_data);
// TODO : Verify memory is in VK_IMAGE_STATE_CLEAR state
auto cb_node = GetCBNode(device_data, commandBuffer);
auto image_state = GetImageState(device_data, image);
if (cb_node && image_state) {
skip |= ValidateMemoryIsBoundToImage(device_data, image_state, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_02528);
skip |= ValidateCmdQueueFlags(device_data, cb_node, "vkCmdClearDepthStencilImage()", VK_QUEUE_GRAPHICS_BIT,
VALIDATION_ERROR_01110);
skip |= ValidateCmd(device_data, cb_node, CMD_CLEARDEPTHSTENCILIMAGE, "vkCmdClearDepthStencilImage()");
skip |= insideRenderPass(device_data, cb_node, "vkCmdClearDepthStencilImage()", VALIDATION_ERROR_01111);
for (uint32_t i = 0; i < rangeCount; ++i) {
skip |=
VerifyClearImageLayout(device_data, cb_node, image_state, pRanges[i], imageLayout, "vkCmdClearDepthStencilImage()");
// Image aspect must be depth or stencil or both
if (((pRanges[i].aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
((pRanges[i].aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
char const str[] =
"vkCmdClearDepthStencilImage aspectMasks for all subresource ranges must be "
"set to VK_IMAGE_ASPECT_DEPTH_BIT and/or VK_IMAGE_ASPECT_STENCIL_BIT";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
(uint64_t)commandBuffer, __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE", str);