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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
|
/* 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 <mark@lunarg.com>
*/
// 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(core_validation::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(core_validation::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(core_validation::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);
}
}
bool FindLayoutVerifyNode(core_validation::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(core_validation::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(core_validation::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(core_validation::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(core_validation::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;
}
// Set the layout on the global level
void SetGlobalLayout(core_validation::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(core_validation::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);
}
}
void SetImageViewLayout(core_validation::layer_data *device_data, GLOBAL_CB_NODE *pCB, VkImageView imageView,
const VkImageLayout &layout) {
auto view_state = getImageViewState(device_data, imageView);
assert(view_state);
auto image = view_state->create_info.image;
const VkImageSubresourceRange &subRange = view_state->create_info.subresourceRange;
// 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};
// 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 (subRange.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) {
if (vk_format_is_depth_and_stencil(view_state->create_info.format)) {
sub.aspectMask |= (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
}
}
SetLayout(device_data, pCB, image, sub, layout);
}
}
}
bool VerifyFramebufferAndRenderPassLayouts(core_validation::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, (VkDebugReportObjectTypeEXT)0, 0, __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;
IMAGE_CMD_BUF_LAYOUT_NODE newNode = {pRenderPassInfo->pAttachments[i].initialLayout,
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)) {
SetLayout(device_data, pCB, image, sub, newNode);
continue;
}
if (newNode.layout != VK_IMAGE_LAYOUT_UNDEFINED && newNode.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(newNode.layout), string_VkImageLayout(node.layout));
}
}
}
}
return skip_call;
}
void TransitionAttachmentRefLayout(core_validation::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(core_validation::layer_data *device_data, GLOBAL_CB_NODE *pCB,
const VkRenderPassBeginInfo *pRenderPassBegin, const int subpass_index,
FRAMEBUFFER_STATE *framebuffer_state) {
auto renderPass = getRenderPassState(device_data, pRenderPassBegin->renderPass);
if (!renderPass) return;
if (framebuffer_state) {
auto const &subpass = renderPass->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 TransitionImageAspectLayout(core_validation::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)) {
SetLayout(device_data, pCB, mem_barrier->image, sub,
IMAGE_CMD_BUF_LAYOUT_NODE(mem_barrier->oldLayout, mem_barrier->newLayout));
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, (VkDebugReportObjectTypeEXT)0,
0, __LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
"You cannot transition the layout of aspect %d from %s when current layout is %s.", aspect,
string_VkImageLayout(mem_barrier->oldLayout), string_VkImageLayout(node.layout));
}
SetLayout(device_data, pCB, mem_barrier->image, sub, mem_barrier->newLayout);
return skip;
}
// TODO: Separate validation and layout state updates
bool TransitionImageLayouts(core_validation::layer_data *device_data, VkCommandBuffer cmdBuffer, uint32_t memBarrierCount,
const VkImageMemoryBarrier *pImgMemBarriers) {
GLOBAL_CB_NODE *pCB = getCBNode(device_data, cmdBuffer);
bool skip = false;
uint32_t levelCount = 0;
uint32_t layerCount = 0;
for (uint32_t i = 0; i < memBarrierCount; ++i) {
auto mem_barrier = &pImgMemBarriers[i];
if (!mem_barrier) continue;
// TODO: Do not iterate over every possibility - consolidate where possible
ResolveRemainingLevelsLayers(device_data, &levelCount, &layerCount, mem_barrier->subresourceRange,
getImageState(device_data, mem_barrier->image));
for (uint32_t j = 0; j < levelCount; j++) {
uint32_t level = mem_barrier->subresourceRange.baseMipLevel + j;
for (uint32_t k = 0; k < layerCount; k++) {
uint32_t layer = mem_barrier->subresourceRange.baseArrayLayer + k;
skip |= TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_COLOR_BIT);
skip |= TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_DEPTH_BIT);
skip |= TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_STENCIL_BIT);
skip |= TransitionImageAspectLayout(device_data, pCB, mem_barrier, level, layer, VK_IMAGE_ASPECT_METADATA_BIT);
}
}
}
return skip;
}
bool VerifySourceImageLayout(core_validation::layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImage srcImage,
VkImageSubresourceLayers subLayers, VkImageLayout srcImageLayout,
UNIQUE_VALIDATION_ERROR_CODE msgCode) {
const auto report_data = core_validation::GetReportData(device_data);
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, srcImage, sub, node)) {
SetLayout(device_data, cb_node, srcImage, sub, IMAGE_CMD_BUF_LAYOUT_NODE(srcImageLayout, srcImageLayout));
continue;
}
if (node.layout != srcImageLayout) {
// 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, 0,
__LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
"Cannot copy from an image whose source layout is %s "
"and doesn't match the current layout %s.",
string_VkImageLayout(srcImageLayout), string_VkImageLayout(node.layout));
}
}
if (srcImageLayout != VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL) {
if (srcImageLayout == VK_IMAGE_LAYOUT_GENERAL) {
// TODO : Can we deal with image node from the top of call tree and avoid map look-up here?
auto image_state = getImageState(device_data, srcImage);
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, (VkDebugReportObjectTypeEXT)0, 0,
__LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
"Layout for input image should be TRANSFER_SRC_OPTIMAL instead of GENERAL.");
}
} else {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, msgCode,
"DS", "Layout for input image is %s but can only be TRANSFER_SRC_OPTIMAL or GENERAL. %s",
string_VkImageLayout(srcImageLayout), validation_error_map[msgCode]);
}
}
return skip_call;
}
bool VerifyDestImageLayout(core_validation::layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImage destImage,
VkImageSubresourceLayers subLayers, VkImageLayout destImageLayout,
UNIQUE_VALIDATION_ERROR_CODE msgCode) {
const auto report_data = core_validation::GetReportData(device_data);
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, destImage, sub, node)) {
SetLayout(device_data, cb_node, destImage, sub, IMAGE_CMD_BUF_LAYOUT_NODE(destImageLayout, destImageLayout));
continue;
}
if (node.layout != destImageLayout) {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, 0,
__LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
"Cannot copy from an image whose dest layout is %s and "
"doesn't match the current layout %s.",
string_VkImageLayout(destImageLayout), string_VkImageLayout(node.layout));
}
}
if (destImageLayout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) {
if (destImageLayout == VK_IMAGE_LAYOUT_GENERAL) {
auto image_state = getImageState(device_data, destImage);
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, (VkDebugReportObjectTypeEXT)0, 0,
__LINE__, DRAWSTATE_INVALID_IMAGE_LAYOUT, "DS",
"Layout for output image should be TRANSFER_DST_OPTIMAL instead of GENERAL.");
}
} else {
skip_call |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, (VkDebugReportObjectTypeEXT)0, 0, __LINE__, msgCode,
"DS", "Layout for output image is %s but can only be TRANSFER_DST_OPTIMAL or GENERAL. %s",
string_VkImageLayout(destImageLayout), validation_error_map[msgCode]);
}
}
return skip_call;
}
void TransitionFinalSubpassLayouts(core_validation::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(core_validation::layer_data *device_data, const VkImageCreateInfo *pCreateInfo,
const VkAllocationCallbacks *pAllocator, VkImage *pImage) {
bool skip_call = false;
VkImageFormatProperties ImageFormatProperties;
const VkPhysicalDevice physical_device = core_validation::GetPhysicalDevice(device_data);
const debug_report_data *report_data = core_validation::GetReportData(device_data);
if (pCreateInfo->format != VK_FORMAT_UNDEFINED) {
VkFormatProperties properties;
core_validation::GetFormatPropertiesPointer(device_data)(physical_device, pCreateInfo->format, &properties);
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]);
}
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]);
}
// 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]);
}
}
} else {
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]);
}
// Internal call to get format info. Still goes through layers, could potentially go directly to ICD.
core_validation::GetImageFormatPropertiesPointer(device_data)(physical_device, pCreateInfo->format, pCreateInfo->imageType,
pCreateInfo->tiling, pCreateInfo->usage, pCreateInfo->flags,
&ImageFormatProperties);
VkDeviceSize imageGranularity = core_validation::GetPhysicalDeviceProperties(device_data)->limits.bufferImageGranularity;
imageGranularity = imageGranularity == 1 ? 0 : imageGranularity;
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_00716, "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_00716]);
}
// 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)vk_format_get_size(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]);
}
return skip_call;
}
void PostCallRecordCreateImage(core_validation::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(core_validation::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(core_validation::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(core_validation::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 (vk_format_is_depth_or_stencil(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 (vk_format_is_compressed(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;
}
void ResolveRemainingLevelsLayers(core_validation::layer_data *dev_data, VkImageSubresourceRange *range, IMAGE_STATE *image_state) {
// If the caller used the special values VK_REMAINING_MIP_LEVELS and VK_REMAINING_ARRAY_LAYERS, resolve them now in our
// internal state to the actual values.
if (range->levelCount == VK_REMAINING_MIP_LEVELS) {
range->levelCount = image_state->createInfo.mipLevels - range->baseMipLevel;
}
if (range->layerCount == VK_REMAINING_ARRAY_LAYERS) {
range->layerCount = image_state->createInfo.arrayLayers - range->baseArrayLayer;
}
}
// Return the correct layer/level counts if the caller used the special values VK_REMAINING_MIP_LEVELS or VK_REMAINING_ARRAY_LAYERS.
void ResolveRemainingLevelsLayers(core_validation::layer_data *dev_data, uint32_t *levels, uint32_t *layers,
VkImageSubresourceRange range, IMAGE_STATE *image_state) {
*levels = range.levelCount;
*layers = range.layerCount;
if (range.levelCount == VK_REMAINING_MIP_LEVELS) {
*levels = image_state->createInfo.mipLevels - range.baseMipLevel;
}
if (range.layerCount == VK_REMAINING_ARRAY_LAYERS) {
*layers = image_state->createInfo.arrayLayers - range.baseArrayLayer;
}
}
bool VerifyClearImageLayout(core_validation::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);
VkImageSubresourceRange resolved_range = range;
ResolveRemainingLevelsLayers(device_data, &resolved_range, image_state);
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, (VkDebugReportObjectTypeEXT)0, 0,
__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, (VkDebugReportObjectTypeEXT)0, 0, __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 < resolved_range.levelCount; ++level_index) {
uint32_t level = level_index + resolved_range.baseMipLevel;
for (uint32_t layer_index = 0; layer_index < resolved_range.layerCount; ++layer_index) {
uint32_t layer = layer_index + resolved_range.baseArrayLayer;
VkImageSubresource sub = {resolved_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(core_validation::layer_data *device_data, GLOBAL_CB_NODE *cb_node, VkImage image,
VkImageSubresourceRange range, VkImageLayout dest_image_layout) {
VkImageSubresourceRange resolved_range = range;
ResolveRemainingLevelsLayers(device_data, &resolved_range, getImageState(device_data, image));
for (uint32_t level_index = 0; level_index < resolved_range.levelCount; ++level_index) {
uint32_t level = level_index + resolved_range.baseMipLevel;
for (uint32_t layer_index = 0; layer_index < resolved_range.layerCount; ++layer_index) {
uint32_t layer = layer_index + resolved_range.baseArrayLayer;
VkImageSubresource sub = {resolved_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(core_validation::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 |= 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(core_validation::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);
UpdateCmdBufferLastCmd(dev_data, cb_node, cmd_type);
for (uint32_t i = 0; i < rangeCount; ++i) {
RecordClearImageLayout(dev_data, cb_node, image, pRanges[i], imageLayout);
}
}
}
bool PreCallValidateCmdClearDepthStencilImage(core_validation::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 |= 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);
}
}
if (image_state && !vk_format_is_depth_or_stencil(image_state->createInfo.format)) {
char const str[] = "vkCmdClearDepthStencilImage called without a 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), __LINE__, VALIDATION_ERROR_01103, "IMAGE", "%s. %s", str,
validation_error_map[VALIDATION_ERROR_01103]);
}
}
return skip;
}
// Returns true if [x, xoffset] and [y, yoffset] overlap
static bool RangesIntersect(int32_t start, uint32_t start_offset, int32_t end, uint32_t end_offset) {
bool result = false;
uint32_t intersection_min = std::max(static_cast<uint32_t>(start), static_cast<uint32_t>(end));
uint32_t intersection_max = std::min(static_cast<uint32_t>(start) + start_offset, static_cast<uint32_t>(end) + end_offset);
if (intersection_max > intersection_min) {
result = true;
}
return result;
}
// Returns true if two VkImageCopy structures overlap
static bool RegionIntersects(const VkImageCopy *src, const VkImageCopy *dst, VkImageType type) {
bool result = false;
if ((src->srcSubresource.mipLevel == dst->dstSubresource.mipLevel) &&
(RangesIntersect(src->srcSubresource.baseArrayLayer, src->srcSubresource.layerCount, dst->dstSubresource.baseArrayLayer,
dst->dstSubresource.layerCount))) {
result = true;
switch (type) {
case VK_IMAGE_TYPE_3D:
result &= RangesIntersect(src->srcOffset.z, src->extent.depth, dst->dstOffset.z, dst->extent.depth);
// Intentionally fall through to 2D case
case VK_IMAGE_TYPE_2D:
result &= RangesIntersect(src->srcOffset.y, src->extent.height, dst->dstOffset.y, dst->extent.height);
// Intentionally fall through to 1D case
case VK_IMAGE_TYPE_1D:
result &= RangesIntersect(src->srcOffset.x, src->extent.width, dst->dstOffset.x, dst->extent.width);
break;
default:
// Unrecognized or new IMAGE_TYPE enums will be caught in parameter_validation
assert(false);
}
}
return result;
}
// Returns true if offset and extent exceed image extents
static bool ExceedsBounds(const VkOffset3D *offset, const VkExtent3D *extent, const IMAGE_STATE *image_state) {
bool result = false;
// Extents/depths cannot be negative but checks left in for clarity
switch (image_state->createInfo.imageType) {
case VK_IMAGE_TYPE_3D: // Validate z and depth
if ((offset->z + extent->depth > image_state->createInfo.extent.depth) || (offset->z < 0) ||
((offset->z + static_cast<int32_t>(extent->depth)) < 0)) {
result = true;
}
// Intentionally fall through to 2D case to check height
case VK_IMAGE_TYPE_2D: // Validate y and height
if ((offset->y + extent->height > image_state->createInfo.extent.height) || (offset->y < 0) ||
((offset->y + static_cast<int32_t>(extent->height)) < 0)) {
result = true;
}
// Intentionally fall through to 1D case to check width
case VK_IMAGE_TYPE_1D: // Validate x and width
if ((offset->x + extent->width > image_state->createInfo.extent.width) || (offset->x < 0) ||
((offset->x + static_cast<int32_t>(extent->width)) < 0)) {
result = true;
}
break;
default:
assert(false);
}
return result;
}
bool PreCallValidateCmdCopyImage(core_validation::layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
IMAGE_STATE *dst_image_state, uint32_t region_count, const VkImageCopy *regions) {
bool skip = false;
const debug_report_data *report_data = core_validation::GetReportData(device_data);
VkCommandBuffer command_buffer = cb_node->commandBuffer;
// TODO: This does not cover swapchain-created images. This should fall out when this layer is moved into the core_validation
// layer
if (src_image_state && dst_image_state) {
for (uint32_t i = 0; i < region_count; i++) {
if (regions[i].srcSubresource.layerCount == 0) {
std::stringstream ss;
ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] srcSubresource is zero";
skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE",
"%s", ss.str().c_str());
}
if (regions[i].dstSubresource.layerCount == 0) {
std::stringstream ss;
ss << "vkCmdCopyImage: number of layers in pRegions[" << i << "] dstSubresource is zero";
skip |= log_msg(report_data, VK_DEBUG_REPORT_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_INVALID_IMAGE_ASPECT, "IMAGE",
"%s", ss.str().c_str());
}
// For each region the layerCount member of srcSubresource and dstSubresource must match
if (regions[i].srcSubresource.layerCount != regions[i].dstSubresource.layerCount) {
std::stringstream ss;
ss << "vkCmdCopyImage: number of layers in source and destination subresources for pRegions[" << i
<< "] do not match";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01198, "IMAGE", "%s. %s",
ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01198]);
}
// For each region, the aspectMask member of srcSubresource and dstSubresource must match
if (regions[i].srcSubresource.aspectMask != regions[i].dstSubresource.aspectMask) {
char const str[] = "vkCmdCopyImage: Src and dest aspectMasks for each region must match";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01197, "IMAGE", "%s. %s",
str, validation_error_map[VALIDATION_ERROR_01197]);
}
// AspectMask must not contain VK_IMAGE_ASPECT_METADATA_BIT
if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) ||
(regions[i].dstSubresource.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT)) {
std::stringstream ss;
ss << "vkCmdCopyImage: pRegions[" << i << "] may not specify aspectMask containing VK_IMAGE_ASPECT_METADATA_BIT";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01222, "IMAGE", "%s. %s",
ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01222]);
}
// For each region, if aspectMask contains VK_IMAGE_ASPECT_COLOR_BIT, it must not contain either of
// VK_IMAGE_ASPECT_DEPTH_BIT or VK_IMAGE_ASPECT_STENCIL_BIT
if ((regions[i].srcSubresource.aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) &&
(regions[i].srcSubresource.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))) {
char const str[] = "vkCmdCopyImage aspectMask cannot specify both COLOR and DEPTH/STENCIL aspects";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01221, "IMAGE", "%s. %s",
str, validation_error_map[VALIDATION_ERROR_01221]);
}
// If either of the calling command's src_image or dst_image parameters are of VkImageType VK_IMAGE_TYPE_3D,
// the baseArrayLayer and layerCount members of both srcSubresource and dstSubresource must be 0 and 1, respectively
if (((src_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D) ||
(dst_image_state->createInfo.imageType == VK_IMAGE_TYPE_3D)) &&
((regions[i].srcSubresource.baseArrayLayer != 0) || (regions[i].srcSubresource.layerCount != 1) ||
(regions[i].dstSubresource.baseArrayLayer != 0) || (regions[i].dstSubresource.layerCount != 1))) {
std::stringstream ss;
ss << "vkCmdCopyImage: src or dstImage type was IMAGE_TYPE_3D, but in subRegion[" << i
<< "] baseArrayLayer was not zero or layerCount was not 1.";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01199, "IMAGE", "%s. %s",
ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01199]);
}
// MipLevel must be less than the mipLevels specified in VkImageCreateInfo when the image was created
if (regions[i].srcSubresource.mipLevel >= src_image_state->createInfo.mipLevels) {
std::stringstream ss;
ss << "vkCmdCopyImage: pRegions[" << i
<< "] specifies a src mipLevel greater than the number specified when the srcImage was created.";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
}
if (regions[i].dstSubresource.mipLevel >= dst_image_state->createInfo.mipLevels) {
std::stringstream ss;
ss << "vkCmdCopyImage: pRegions[" << i
<< "] specifies a dst mipLevel greater than the number specified when the dstImage was created.";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01223, "IMAGE", "%s. %s",
ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01223]);
}
// (baseArrayLayer + layerCount) must be less than or equal to the arrayLayers specified in VkImageCreateInfo when the
// image was created
if ((regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount) >
src_image_state->createInfo.arrayLayers) {
std::stringstream ss;
ss << "vkCmdCopyImage: srcImage arrayLayers was " << src_image_state->createInfo.arrayLayers << " but subRegion["
<< i << "] baseArrayLayer + layerCount is "
<< (regions[i].srcSubresource.baseArrayLayer + regions[i].srcSubresource.layerCount);
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
}
if ((regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount) >
dst_image_state->createInfo.arrayLayers) {
std::stringstream ss;
ss << "vkCmdCopyImage: dstImage arrayLayers was " << dst_image_state->createInfo.arrayLayers << " but subRegion["
<< i << "] baseArrayLayer + layerCount is "
<< (regions[i].dstSubresource.baseArrayLayer + regions[i].dstSubresource.layerCount);
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01224, "IMAGE", "%s. %s",
ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01224]);
}
// The source region specified by a given element of regions must be a region that is contained within srcImage
if (ExceedsBounds(®ions[i].srcOffset, ®ions[i].extent, src_image_state)) {
std::stringstream ss;
ss << "vkCmdCopyImage: srcSubResource in pRegions[" << i << "] exceeds extents srcImage was created with";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01175, "IMAGE", "%s. %s",
ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01175]);
}
// The destination region specified by a given element of regions must be a region that is contained within dst_image
if (ExceedsBounds(®ions[i].dstOffset, ®ions[i].extent, dst_image_state)) {
std::stringstream ss;
ss << "vkCmdCopyImage: dstSubResource in pRegions[" << i << "] exceeds extents dstImage was created with";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01176, "IMAGE", "%s. %s",
ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01176]);
}
// The union of all source regions, and the union of all destination regions, specified by the elements of regions,
// must not overlap in memory
if (src_image_state->image == dst_image_state->image) {
for (uint32_t j = 0; j < region_count; j++) {
if (RegionIntersects(®ions[i], ®ions[j], src_image_state->createInfo.imageType)) {
std::stringstream ss;
ss << "vkCmdCopyImage: pRegions[" << i << "] src overlaps with pRegions[" << j << "].";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01177, "IMAGE",
"%s. %s", ss.str().c_str(), validation_error_map[VALIDATION_ERROR_01177]);
}
}
}
}
// The formats of src_image and dst_image must be compatible. Formats are considered compatible if their texel size in bytes
// is the same between both formats. For example, VK_FORMAT_R8G8B8A8_UNORM is compatible with VK_FORMAT_R32_UINT because
// because both texels are 4 bytes in size. Depth/stencil formats must match exactly.
if (vk_format_is_depth_or_stencil(src_image_state->createInfo.format) ||
vk_format_is_depth_or_stencil(dst_image_state->createInfo.format)) {
if (src_image_state->createInfo.format != dst_image_state->createInfo.format) {
char const str[] = "vkCmdCopyImage called with unmatched source and dest image depth/stencil formats.";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, DRAWSTATE_MISMATCHED_IMAGE_FORMAT, "IMAGE",
str);
}
} else {
size_t srcSize = vk_format_get_size(src_image_state->createInfo.format);
size_t destSize = vk_format_get_size(dst_image_state->createInfo.format);
if (srcSize != destSize) {
char const str[] = "vkCmdCopyImage called with unmatched source and dest image format sizes.";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(command_buffer), __LINE__, VALIDATION_ERROR_01184, "IMAGE", "%s. %s",
str, validation_error_map[VALIDATION_ERROR_01184]);
}
}
}
return skip;
}
// TODO : Should be tracking lastBound per commandBuffer and when draws occur, report based on that cmd buffer lastBound
// Then need to synchronize the accesses based on cmd buffer so that if I'm reading state on one cmd buffer, updates
// to that same cmd buffer by separate thread are not changing state from underneath us
// Track the last cmd buffer touched by this thread
static bool hasDrawCmd(GLOBAL_CB_NODE *pCB) {
for (uint32_t i = 0; i < NUM_DRAW_TYPES; i++) {
if (pCB->drawCount[i]) return true;
}
return false;
}
// Returns true if sub_rect is entirely contained within rect
static inline bool ContainsRect(VkRect2D rect, VkRect2D sub_rect) {
if ((sub_rect.offset.x < rect.offset.x) || (sub_rect.offset.x + sub_rect.extent.width > rect.offset.x + rect.extent.width) ||
(sub_rect.offset.y < rect.offset.y) || (sub_rect.offset.y + sub_rect.extent.height > rect.offset.y + rect.extent.height))
return false;
return true;
}
bool PreCallValidateCmdClearAttachments(core_validation::layer_data *device_data, VkCommandBuffer commandBuffer,
uint32_t attachmentCount, const VkClearAttachment *pAttachments, uint32_t rectCount,
const VkClearRect *pRects) {
GLOBAL_CB_NODE *cb_node = getCBNode(device_data, commandBuffer);
const debug_report_data *report_data = core_validation::GetReportData(device_data);
bool skip = false;
if (cb_node) {
skip |= ValidateCmd(device_data, cb_node, CMD_CLEARATTACHMENTS, "vkCmdClearAttachments()");
UpdateCmdBufferLastCmd(device_data, cb_node, CMD_CLEARATTACHMENTS);
// Warn if this is issued prior to Draw Cmd and clearing the entire attachment
if (!hasDrawCmd(cb_node) && (cb_node->activeRenderPassBeginInfo.renderArea.extent.width == pRects[0].rect.extent.width) &&
(cb_node->activeRenderPassBeginInfo.renderArea.extent.height == pRects[0].rect.extent.height)) {
// There are times where app needs to use ClearAttachments (generally when reusing a buffer inside of a render pass)
// Can we make this warning more specific? I'd like to avoid triggering this test if we can tell it's a use that must
// call CmdClearAttachments. Otherwise this seems more like a performance warning.
skip |=
log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
reinterpret_cast<uint64_t &>(commandBuffer), 0, DRAWSTATE_CLEAR_CMD_BEFORE_DRAW, "DS",
"vkCmdClearAttachments() issued on command buffer object 0x%p prior to any Draw Cmds."
" It is recommended you use RenderPass LOAD_OP_CLEAR on Attachments prior to any Draw.",
commandBuffer);
}
skip |= outsideRenderPass(device_data, cb_node, "vkCmdClearAttachments()", VALIDATION_ERROR_01122);
}
// Validate that attachment is in reference list of active subpass
if (cb_node->activeRenderPass) {
const VkRenderPassCreateInfo *renderpass_create_info = cb_node->activeRenderPass->createInfo.ptr();
const VkSubpassDescription *subpass_desc = &renderpass_create_info->pSubpasses[cb_node->activeSubpass];
auto framebuffer = getFramebufferState(device_data, cb_node->activeFramebuffer);
for (uint32_t i = 0; i < attachmentCount; i++) {
auto clear_desc = &pAttachments[i];
VkImageView image_view = VK_NULL_HANDLE;
if (0 == clear_desc->aspectMask) {
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
(uint64_t)commandBuffer, __LINE__, VALIDATION_ERROR_01128, "IMAGE", "%s",
validation_error_map[VALIDATION_ERROR_01128]);
} else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_METADATA_BIT) {
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
(uint64_t)commandBuffer, __LINE__, VALIDATION_ERROR_01126, "IMAGE", "%s",
validation_error_map[VALIDATION_ERROR_01126]);
} else if (clear_desc->aspectMask & VK_IMAGE_ASPECT_COLOR_BIT) {
if (clear_desc->colorAttachment >= subpass_desc->colorAttachmentCount) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
(uint64_t)commandBuffer, __LINE__, VALIDATION_ERROR_01114, "DS",
"vkCmdClearAttachments() color attachment index %d out of range for active subpass %d. %s",
clear_desc->colorAttachment, cb_node->activeSubpass, validation_error_map[VALIDATION_ERROR_01114]);
} else if (subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment == VK_ATTACHMENT_UNUSED) {
skip |= log_msg(report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT,
VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT, (uint64_t)commandBuffer, __LINE__,
DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
"vkCmdClearAttachments() color attachment index %d is VK_ATTACHMENT_UNUSED; ignored.",
clear_desc->colorAttachment);
} else {
image_view = framebuffer->createInfo
.pAttachments[subpass_desc->pColorAttachments[clear_desc->colorAttachment].attachment];
}
if ((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) ||
(clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT)) {
char const str[] =
"vkCmdClearAttachments aspectMask [%d] must set only VK_IMAGE_ASPECT_COLOR_BIT of a color attachment. %s";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
(uint64_t)commandBuffer, __LINE__, VALIDATION_ERROR_01125, "IMAGE", str, i,
validation_error_map[VALIDATION_ERROR_01125]);
}
} else { // Must be depth and/or stencil
if (((clear_desc->aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT) != VK_IMAGE_ASPECT_DEPTH_BIT) &&
((clear_desc->aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT) != VK_IMAGE_ASPECT_STENCIL_BIT)) {
char const str[] = "vkCmdClearAttachments aspectMask [%d] is not a valid combination of bits. %s";
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
(uint64_t)commandBuffer, __LINE__, VALIDATION_ERROR_01127, "IMAGE", str, i,
validation_error_map[VALIDATION_ERROR_01127]);
}
if (!subpass_desc->pDepthStencilAttachment ||
(subpass_desc->pDepthStencilAttachment->attachment == VK_ATTACHMENT_UNUSED)) {
skip |= log_msg(
report_data, VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_COMMAND_BUFFER_EXT,
(uint64_t)commandBuffer, __LINE__, DRAWSTATE_MISSING_ATTACHMENT_REFERENCE, "DS",
"vkCmdClearAttachments() depth/stencil clear with no depth/stencil attachment in subpass; ignored");
} else {
image_view = framebuffer->createInfo.pAttachments[subpass_desc->pDepthStencilAttachment->attachment];
}
}
if (image_view) {
auto image_view_state = getImageViewState(device_data, image_view);
for (uint32_t j = 0; j < rectCount; j++) {
// The rectangular region specified by a given element of pRects must be contained within the render area of
// the current render pass instance
if (false == ContainsRect(cb_node->activeRenderPassBeginInfo.renderArea, pRects[j].rect)) {
skip |= log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
__LINE__, VALIDATION_ERROR_01115, "DS",
"vkCmdClearAttachments(): The area defined by pRects[%d] is not contained in the area of "
"the current render pass instance. %s",
j, validation_error_map[VALIDATION_ERROR_01115]);
}
// The layers specified by a given element of pRects must be contained within every attachment that
// pAttachments refers to
auto attachment_base_array_layer = image_view_state->create_info.subresourceRange.baseArrayLayer;
auto attachment_layer_count = image_view_state->create_info.subresourceRange.layerCount;
if ((pRects[j].baseArrayLayer < attachment_base_array_layer) || pRects[j].layerCount > attachment_layer_count) {
skip |=
log_msg(report_data, VK_DEBUG_REPORT_ERROR_BIT_EXT, VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, 0,
__LINE__, VALIDATION_ERROR_01116, "DS",
"vkCmdClearAttachments(): The layers defined in pRects[%d] are not contained in the layers of "
"pAttachment[%d]. %s",
j, i, validation_error_map[VALIDATION_ERROR_01116]);
}
}
}
}
}
return skip;
}
bool PreCallValidateCmdResolveImage(core_validation::layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
IMAGE_STATE *dst_image_state) {
bool skip = false;
if (cb_node && src_image_state && dst_image_state) {
skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02541);
skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdResolveImage()", VALIDATION_ERROR_02542);
skip |= ValidateCmd(device_data, cb_node, CMD_RESOLVEIMAGE, "vkCmdResolveImage()");
skip |= insideRenderPass(device_data, cb_node, "vkCmdResolveImage()", VALIDATION_ERROR_01335);
} else {
assert(0);
}
return skip;
}
void PreCallRecordCmdResolveImage(core_validation::layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
IMAGE_STATE *dst_image_state) {
// Update bindings between images and cmd buffer
AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
std::function<bool()> function = [=]() {
return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdResolveImage()");
};
cb_node->validate_functions.push_back(function);
function = [=]() {
SetImageMemoryValid(device_data, dst_image_state, true);
return false;
};
cb_node->validate_functions.push_back(function);
UpdateCmdBufferLastCmd(device_data, cb_node, CMD_RESOLVEIMAGE);
}
bool PreCallValidateCmdBlitImage(core_validation::layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
IMAGE_STATE *dst_image_state) {
bool skip = false;
if (cb_node && src_image_state && dst_image_state) {
skip |= ValidateImageSampleCount(device_data, src_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): srcImage",
VALIDATION_ERROR_02194);
skip |= ValidateImageSampleCount(device_data, dst_image_state, VK_SAMPLE_COUNT_1_BIT, "vkCmdBlitImage(): dstImage",
VALIDATION_ERROR_02195);
skip |= ValidateMemoryIsBoundToImage(device_data, src_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02539);
skip |= ValidateMemoryIsBoundToImage(device_data, dst_image_state, "vkCmdBlitImage()", VALIDATION_ERROR_02540);
skip |= ValidateImageUsageFlags(device_data, src_image_state, VK_IMAGE_USAGE_TRANSFER_SRC_BIT, true, VALIDATION_ERROR_02182,
"vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_SRC_BIT");
skip |= ValidateImageUsageFlags(device_data, dst_image_state, VK_IMAGE_USAGE_TRANSFER_DST_BIT, true, VALIDATION_ERROR_02186,
"vkCmdBlitImage()", "VK_IMAGE_USAGE_TRANSFER_DST_BIT");
skip |= ValidateCmd(device_data, cb_node, CMD_BLITIMAGE, "vkCmdBlitImage()");
skip |= insideRenderPass(device_data, cb_node, "vkCmdBlitImage()", VALIDATION_ERROR_01300);
} else {
assert(0);
}
return skip;
}
void PreCallRecordCmdBlitImage(core_validation::layer_data *device_data, GLOBAL_CB_NODE *cb_node, IMAGE_STATE *src_image_state,
IMAGE_STATE *dst_image_state) {
// Update bindings between images and cmd buffer
AddCommandBufferBindingImage(device_data, cb_node, src_image_state);
AddCommandBufferBindingImage(device_data, cb_node, dst_image_state);
std::function<bool()> function = [=]() { return ValidateImageMemoryIsValid(device_data, src_image_state, "vkCmdBlitImage()"); };
cb_node->validate_functions.push_back(function);
function = [=]() {
SetImageMemoryValid(device_data, dst_image_state, true);
return false;
};
cb_node->validate_functions.push_back(function);
UpdateCmdBufferLastCmd(device_data, cb_node, CMD_BLITIMAGE);
}
|