File: cc_image_layout.cpp

package info (click to toggle)
vulkan-validationlayers 1.4.335.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 51,728 kB
  • sloc: cpp: 645,254; python: 12,203; sh: 24; makefile: 24; xml: 14
file content (1249 lines) | stat: -rw-r--r-- 74,154 bytes parent folder | download
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
/* Copyright (c) 2015-2025 The Khronos Group Inc.
 * Copyright (c) 2015-2025 Valve Corporation
 * Copyright (c) 2015-2025 LunarG, Inc.
 * Copyright (C) 2015-2025 Google Inc.
 * Modifications Copyright (C) 2020-2022 Advanced Micro Devices, Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <assert.h>
#include <vector>

#include <vulkan/vk_enum_string_helper.h>
#include <vulkan/utility/vk_format_utils.h>
#include "core_checks/cc_state_tracker.h"
#include "core_checks/cc_sync_vuid_maps.h"
#include "core_checks/cc_vuid_maps.h"
#include "core_checks/core_validation.h"
#include "error_message/error_strings.h"
#include "generated/error_location_helper.h"
#include "utils/image_layout_utils.h"
#include "utils/image_utils.h"
#include "state_tracker/image_state.h"
#include "state_tracker/render_pass_state.h"
#include "state_tracker/cmd_buffer_state.h"
#include "drawdispatch/drawdispatch_vuids.h"

bool IsValidAspectMaskForFormat(VkImageAspectFlags aspect_mask, VkFormat format);

using LayoutRange = subresource_adapter::IndexRange;
using RangeGenerator = subresource_adapter::RangeGenerator;

// Utility type for checking Image layouts
struct LayoutUseCheckAndMessage {
    const static VkImageAspectFlags kDepthOrStencil = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
    const VkImageLayout expected_layout;
    const VkImageAspectFlags aspect_mask;
    const char *message;
    VkImageLayout layout;

    LayoutUseCheckAndMessage() = delete;
    LayoutUseCheckAndMessage(VkImageLayout expected, const VkImageAspectFlags aspect_mask_ = 0)
        : expected_layout{expected}, aspect_mask{aspect_mask_}, message(nullptr), layout(kInvalidLayout) {}
    bool Check(const ImageLayoutState &state) {
        message = nullptr;
        layout = kInvalidLayout;  // Success status
        if (state.current_layout != kInvalidLayout) {
            if (!ImageLayoutMatches(aspect_mask, expected_layout, state.current_layout)) {
                message = "previous known";
                layout = state.current_layout;
            }
        } else if (state.first_layout != kInvalidLayout) {
            if (!ImageLayoutMatches(aspect_mask, expected_layout, state.first_layout)) {
                if (!((state.aspect_mask & kDepthOrStencil) &&
                      ImageLayoutMatches(state.aspect_mask, expected_layout, state.first_layout))) {
                    message = "previously used";
                    layout = state.first_layout;
                }
            }
        }
        return layout == kInvalidLayout;
    }
};

bool CoreChecks::ValidateDescriptorImageLayout(const LogObjectList &objlist, const vvl::Image &image_state,
                                               VkImageAspectFlags aspect_mask, VkImageLayout explicit_layout,
                                               const CommandBufferImageLayoutMap &cb_layout_map, RangeGenerator &&range_gen,
                                               const vvl::DrawDispatchVuid &vuids,
                                               std::function<std::string()> describe_descriptor_callback) const {
    bool skip = false;
    LayoutUseCheckAndMessage layout_check(explicit_layout, aspect_mask);
    skip |= ForEachMatchingLayoutMapRange(
        cb_layout_map, std::move(range_gen),
        [this, &objlist, &image_state, &layout_check, &describe_descriptor_callback, vuids](const LayoutRange &range,
                                                                                            const ImageLayoutState &state) {
            bool local_skip = false;
            if (!layout_check.Check(state)) {
                const subresource_adapter::Subresource subresource = image_state.subresource_encoder.Decode(range.begin);
                local_skip |= LogError(vuids.image_layout_00344, objlist, vuids.loc(),
                                       "Cannot use %s (layer %" PRIu32 ", mip %" PRIu32
                                       ") with specific layout %s (specified by %s) that doesn't match the "
                                       "%s layout %s.",
                                       FormatHandle(image_state).c_str(), subresource.arrayLayer, subresource.mipLevel,
                                       string_VkImageLayout(layout_check.expected_layout), describe_descriptor_callback().c_str(),
                                       layout_check.message, string_VkImageLayout(layout_check.layout));
            }
            return local_skip;
        });
    return skip;
}

bool CoreChecks::ValidateSubresourceImageLayout(const vvl::CommandBuffer &cb_state, const vvl::Image &image_state,
                                                const VkImageSubresourceLayers &subresource_layers, int32_t depth_offset,
                                                uint32_t depth_extent, VkImageLayout explicit_layout, const Location &loc,
                                                const char *vuid) const {
    bool skip = false;
    if (disabled[image_layout_validation]) {
        return skip;
    }
    const auto image_layout_map = cb_state.GetImageLayoutMap(image_state.VkHandle());
    if (!image_layout_map) {
        return skip;
    }

    VkImageSubresourceRange normalized_subresource_range =
        image_state.NormalizeSubresourceRange(RangeFromLayers(subresource_layers));

    if (CanTransitionDepthSlices(extensions, image_state.create_info)) {
        normalized_subresource_range.baseArrayLayer = (uint32_t)depth_offset;
        normalized_subresource_range.layerCount = depth_extent;
    }

    if (!image_state.subresource_encoder.InRange(normalized_subresource_range)) {
        return skip;
    }

    RangeGenerator range_gen(image_state.subresource_encoder, normalized_subresource_range);

    LayoutUseCheckAndMessage layout_check(explicit_layout, normalized_subresource_range.aspectMask);
    skip |= ForEachMatchingLayoutMapRange(
        *image_layout_map, std::move(range_gen),
        [this, &cb_state, &image_state, &layout_check, vuid, loc](const LayoutRange &range, const ImageLayoutState &state) {
            bool local_skip = false;
            if (!layout_check.Check(state)) {
                const subresource_adapter::Subresource subresource = image_state.subresource_encoder.Decode(range.begin);
                const LogObjectList objlist(cb_state.Handle(), image_state.Handle());
                local_skip |= LogError(vuid, objlist, loc,
                                       "Cannot use %s (layer %" PRIu32 ", mip %" PRIu32
                                       ") with specific layout %s that doesn't match the "
                                       "%s layout %s.",
                                       FormatHandle(image_state).c_str(), subresource.arrayLayer, subresource.mipLevel,
                                       string_VkImageLayout(layout_check.expected_layout), layout_check.message,
                                       string_VkImageLayout(layout_check.layout));
            }
            return local_skip;
        });
    return skip;
}

bool CoreChecks::ValidateVideoImageLayout(const vvl::CommandBuffer &cb_state, const vvl::Image &image_state,
                                          const VkImageSubresourceRange &normalized_subresource_range,
                                          VkImageLayout explicit_layout, const Location &loc,
                                          const char *mismatch_layout_vuid) const {
    if (disabled[image_layout_validation]) {
        return false;
    }
    const auto image_layout_map = cb_state.GetImageLayoutMap(image_state.VkHandle());
    if (!image_layout_map) {
        return false;
    }

    RangeGenerator range_gen = image_state.subresource_encoder.InRange(normalized_subresource_range)
                                   ? RangeGenerator(image_state.subresource_encoder, normalized_subresource_range)
                                   : RangeGenerator{};

    bool skip = false;
    LayoutUseCheckAndMessage layout_check(explicit_layout, normalized_subresource_range.aspectMask);
    LayoutUseCheckAndMessage layout_check_general(VK_IMAGE_LAYOUT_GENERAL, normalized_subresource_range.aspectMask);
    skip |= ForEachMatchingLayoutMapRange(
        *image_layout_map, std::move(range_gen),
        [this, &cb_state, &image_state, &layout_check, &layout_check_general, mismatch_layout_vuid, loc](
            const LayoutRange &range, const ImageLayoutState &state) {
            bool local_skip = false;
            if (!layout_check.Check(state) && (!enabled_features.unifiedImageLayoutsVideo || !layout_check_general.Check(state))) {
                const subresource_adapter::Subresource subresource = image_state.subresource_encoder.Decode(range.begin);
                std::string expected_layout = string_VkImageLayout(layout_check.expected_layout);
                if (enabled_features.unifiedImageLayoutsVideo) {
                    expected_layout += " or VK_IMAGE_LAYOUT_GENERAL";
                }
                const LogObjectList objlist(cb_state.Handle(), image_state.Handle());
                local_skip |= LogError(mismatch_layout_vuid, objlist, loc,
                                       "Cannot use %s (layer=%" PRIu32 " mip=%" PRIu32
                                       ") with specific layout %s that doesn't match the "
                                       "%s layout %s.",
                                       FormatHandle(image_state).c_str(), subresource.arrayLayer, subresource.mipLevel,
                                       expected_layout.c_str(), layout_check.message, string_VkImageLayout(layout_check.layout));
            }
            return local_skip;
        });
    return skip;
}

void CoreChecks::TransitionFinalSubpassLayouts(vvl::CommandBuffer &cb_state) {
    auto render_pass_state = cb_state.active_render_pass.get();
    auto framebuffer_state = cb_state.active_framebuffer.get();
    if (!render_pass_state || !framebuffer_state) {
        return;
    }

    const VkRenderPassCreateInfo2 *render_pass_info = render_pass_state->create_info.ptr();
    for (uint32_t i = 0; i < render_pass_info->attachmentCount; ++i) {
        auto *view_state = cb_state.GetActiveAttachmentImageViewState(i);
        if (!view_state) continue;

        VkImageLayout stencil_layout = kInvalidLayout;
        const auto *attachment_description_stencil_layout =
            vku::FindStructInPNextChain<VkAttachmentDescriptionStencilLayout>(render_pass_info->pAttachments[i].pNext);
        if (attachment_description_stencil_layout) {
            stencil_layout = attachment_description_stencil_layout->stencilFinalLayout;
        }
        cb_state.SetImageViewLayout(*view_state, render_pass_info->pAttachments[i].finalLayout, stencil_layout);
    }
}

struct GlobalLayoutUpdater {
    bool update(VkImageLayout &dst, const ImageLayoutState &src) const {
        if (src.current_layout != kInvalidLayout && dst != src.current_layout) {
            dst = src.current_layout;
            return true;
        }
        return false;
    }

    std::optional<VkImageLayout> insert(const ImageLayoutState &src) const {
        std::optional<VkImageLayout> result;
        if (src.current_layout != kInvalidLayout) {
            result.emplace(src.current_layout);
        }
        return result;
    }
};

// This validates that the first layout specified in the command buffer for the image
// is the same as this image's global (actual/current) layout
bool CoreChecks::ValidateCmdBufImageLayouts(
    const Location &loc, const vvl::CommandBuffer &cb_state,
    vvl::unordered_map<const vvl::Image *, ImageLayoutMap> &local_image_layout_state) const {
    if (disabled[image_layout_validation]) {
        return false;
    }
    bool skip = false;
    // Iterate over the layout maps for each referenced image
    for (const auto &[image, cb_layout_map] : cb_state.image_layout_registry) {
        if (!cb_layout_map || cb_layout_map->empty()) {
            continue;
        }
        const auto image_state = Get<vvl::Image>(image);
        if (!image_state) {
            continue;
        }

        // TODO - things like ANGLE might have external images which have their layouts transitioned implicitly
        // https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/8940
        if (image_state->external_memory_handle_types != 0) {
            continue;
        }

        // Validate the initial_uses for each subresource referenced
        const auto subresource_count = image_state->subresource_encoder.SubresourceCount();
        auto it = local_image_layout_state.try_emplace(image_state.get(), subresource_count).first;
        ImageLayoutMap &local_layout_map = it->second;

        const auto *global_layout_map = image_state->layout_map.get();
        ASSERT_AND_CONTINUE(global_layout_map);
        auto global_layout_map_guard = image_state->LayoutMapReadLock();

        auto pos = cb_layout_map->begin();
        const auto end = cb_layout_map->end();
        sparse_container::parallel_iterator<const ImageLayoutMap> current_layout(local_layout_map, *global_layout_map,
                                                                                 pos->first.begin);
        while (pos != end) {
            const ImageLayoutState &cb_layout_state = pos->second;
            VkImageLayout first_layout = cb_layout_state.first_layout;
            if (first_layout == kInvalidLayout) {
                continue;
            }

            VkImageLayout image_layout = kInvalidLayout;

            if (current_layout->range.empty()) break;  // When we are past the end of data in overlay and global... stop looking
            if (current_layout->pos_A->valid) {        // pos_A denotes the overlay map in the parallel iterator
                image_layout = current_layout->pos_A->lower_bound->second;
            } else if (current_layout->pos_B->valid) {  // pos_B denotes the global map in the parallel iterator
                image_layout = current_layout->pos_B->lower_bound->second;
            }
            const auto intersected_range = pos->first & current_layout->range;
            if (first_layout == VK_IMAGE_LAYOUT_UNDEFINED) {
                // TODO: Set memory invalid which is in mem_tracker currently
            } else if (image_layout != first_layout) {
                const auto aspect_mask = image_state->subresource_encoder.Decode(intersected_range.begin).aspectMask;
                const bool matches = ImageLayoutMatches(aspect_mask, image_layout, first_layout);
                if (!matches) {
                    // We can report all the errors for the intersected range directly
                    for (auto index : vvl::range_view<decltype(intersected_range)>(intersected_range)) {
                        const auto subresource = image_state->subresource_encoder.Decode(index);
                        const LogObjectList objlist(cb_state.Handle(), image_state->Handle());
                        const vvl::DrawDispatchVuid &draw_dispatch_vuids = GetDrawDispatchVuid(vvl::Func::vkCmdDraw);
                        const char *vuid = cb_layout_state.submit_time_layout_mismatch_vuid
                                               ? cb_layout_state.submit_time_layout_mismatch_vuid
                                               : draw_dispatch_vuids.image_layout_09600;
                        skip |= LogError(
                            vuid, objlist, loc,
                            "command buffer %s expects %s (subresource: %s) to be in layout %s--instead, current layout is %s.",
                            FormatHandle(cb_state).c_str(), FormatHandle(*image_state).c_str(),
                            string_VkImageSubresource(subresource).c_str(), string_VkImageLayout(first_layout),
                            string_VkImageLayout(image_layout));
                    }
                }
            }
            if (pos->first.includes(intersected_range.end)) {
                current_layout.seek(intersected_range.end);
            } else {
                ++pos;
                if (pos != end) {
                    current_layout.seek(pos->first.begin);
                }
            }
        }
        // Update all layout set operations (which will be a subset of the initial_layouts)
        sparse_container::splice(local_layout_map, *cb_layout_map, GlobalLayoutUpdater());
    }

    return skip;
}

void CoreChecks::UpdateCmdBufImageLayouts(const vvl::CommandBuffer &cb_state) {
    for (const auto &[image, cb_layout_map] : cb_state.image_layout_registry) {
        const auto image_state = Get<vvl::Image>(image);
        if (image_state && cb_layout_map && image_state->GetId() == cb_layout_map->image_id) {
            auto guard = image_state->LayoutMapWriteLock();
            sparse_container::splice(*image_state->layout_map, *cb_layout_map, GlobalLayoutUpdater());
        }
    }
}

// ValidateLayoutVsAttachmentDescription is a general function where we can validate various state associated with the
// VkAttachmentDescription structs that are used by the sub-passes of a renderpass. Initial check is to make sure that READ_ONLY
// layout attachments don't have CLEAR as their loadOp.
bool CoreChecks::ValidateLayoutVsAttachmentDescription(const VkImageLayout first_layout, const uint32_t attachment,
                                                       const VkAttachmentDescription2 &attachment_description,
                                                       const Location &layout_loc) const {
    bool skip = false;
    const bool use_rp2 = layout_loc.function != Func::vkCreateRenderPass;

    // Verify that initial loadOp on READ_ONLY attachments is not CLEAR
    // for both loadOp and stencilLoaOp rp2 has it in 1 VU while rp1 has it in 2 VU with half behind Maintenance2 extension
    // Each is VUID is below in following order: rp2 -> rp1 with Maintenance2 -> rp1 with no extenstion
    if (attachment_description.loadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
        if (use_rp2 && ((first_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) ||
                        (first_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) ||
                        (first_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL))) {
            skip |= LogError("VUID-VkRenderPassCreateInfo2-pAttachments-02522", device, layout_loc,
                             "(%s) is an invalid for pAttachments[%d] (first attachment to have LOAD_OP_CLEAR).",
                             string_VkImageLayout(first_layout), attachment);
        } else if ((use_rp2 == false) && IsExtEnabled(extensions.vk_khr_maintenance2) &&
                   (first_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL)) {
            skip |= LogError("VUID-VkRenderPassCreateInfo-pAttachments-01566", device, layout_loc,
                             "(%s) is an invalid for pAttachments[%d] (first attachment to have LOAD_OP_CLEAR).",
                             string_VkImageLayout(first_layout), attachment);
        } else if ((use_rp2 == false) && ((first_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) ||
                                          (first_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL))) {
            skip |= LogError("VUID-VkRenderPassCreateInfo-pAttachments-00836", device, layout_loc,
                             "(%s) is an invalid for pAttachments[%d] (first attachment to have LOAD_OP_CLEAR).",
                             string_VkImageLayout(first_layout), attachment);
        }
    }

    // Same as above for loadOp, but for stencilLoadOp
    if (attachment_description.stencilLoadOp == VK_ATTACHMENT_LOAD_OP_CLEAR) {
        if (use_rp2 && ((first_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) ||
                        (first_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL) ||
                        (first_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL))) {
            skip |= LogError("VUID-VkRenderPassCreateInfo2-pAttachments-02523", device, layout_loc,
                             "(%s) is an invalid for pAttachments[%d] (first attachment to have LOAD_OP_CLEAR).",
                             string_VkImageLayout(first_layout), attachment);
        } else if ((use_rp2 == false) && IsExtEnabled(extensions.vk_khr_maintenance2) &&
                   (first_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL)) {
            skip |= LogError("VUID-VkRenderPassCreateInfo-pAttachments-01567", device, layout_loc,
                             "(%s) is an invalid for pAttachments[%d] (first attachment to have LOAD_OP_CLEAR).",
                             string_VkImageLayout(first_layout), attachment);
        } else if ((use_rp2 == false) && ((first_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) ||
                                          (first_layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL))) {
            skip |= LogError("VUID-VkRenderPassCreateInfo-pAttachments-02511", device, layout_loc,
                             "(%s) is an invalid for pAttachments[%d] (first attachment to have LOAD_OP_CLEAR).",
                             string_VkImageLayout(first_layout), attachment);
        }
    }

    return skip;
}

bool CoreChecks::ValidateMultipassRenderedToSingleSampledSampleCount(VkFramebuffer framebuffer, VkRenderPass renderpass,
                                                                     vvl::Image &image_state, VkSampleCountFlagBits msrtss_samples,
                                                                     const Location &rasterization_samples_loc) const {
    bool skip = false;
    const auto image_create_info = image_state.create_info;
    if (!image_state.image_format_properties.sampleCounts) {
        skip |= GetPhysicalDeviceImageFormatProperties(image_state, "VUID-VkRenderPassAttachmentBeginInfo-pAttachments-07010",
                                                       rasterization_samples_loc);
    }
    if (!(image_state.image_format_properties.sampleCounts & msrtss_samples)) {
        const LogObjectList objlist(renderpass, framebuffer, image_state.Handle());
        skip |= LogError("VUID-VkRenderPassAttachmentBeginInfo-pAttachments-07010", objlist, rasterization_samples_loc,
                         "is %s but is not supported with image (%s) created with\n"
                         "format: %s\n"
                         "imageType: %s\n"
                         "tiling: %s\n"
                         "usage: %s\n"
                         "flags: %s\n",
                         string_VkSampleCountFlagBits(msrtss_samples), FormatHandle(image_state).c_str(),
                         string_VkFormat(image_create_info.format), string_VkImageType(image_create_info.imageType),
                         string_VkImageTiling(image_create_info.tiling), string_VkImageUsageFlags(image_create_info.usage).c_str(),
                         string_VkImageCreateFlags(image_create_info.flags).c_str());
    }
    return skip;
}

bool CoreChecks::ValidateRenderPassLayoutAgainstFramebufferImageUsage(VkImageLayout layout, const vvl::ImageView &image_view_state,
                                                                      VkFramebuffer framebuffer, VkRenderPass renderpass,
                                                                      uint32_t attachment_index, const Location &rp_loc,
                                                                      const Location &attachment_reference_loc) const {
    bool skip = false;
    const auto &image_view = image_view_state.Handle();
    const auto *image_state = image_view_state.image_state.get();
    if (!image_state) {
        return skip;  // validated at VUID-VkRenderPassBeginInfo-framebuffer-parameter
    }
    const auto &image = image_state->Handle();
    const bool use_rp2 = rp_loc.function != Func::vkCmdBeginRenderPass;

    auto image_usage = image_state->create_info.usage;
    const auto stencil_usage_info = vku::FindStructInPNextChain<VkImageStencilUsageCreateInfo>(image_state->create_info.pNext);
    if (stencil_usage_info) {
        image_usage |= stencil_usage_info->stencilUsage;
    }

    const char *vuid = kVUIDUndefined;

    // Check for layouts that mismatch image usages in the framebuffer
    if (layout == VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL && !(image_usage & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT)) {
        vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-03094" : "VUID-vkCmdBeginRenderPass-initialLayout-00895";
        skip = true;
    } else if (layout == VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL &&
               !(image_usage & (VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT))) {
        vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-03097" : "VUID-vkCmdBeginRenderPass-initialLayout-00897";
        skip = true;
    } else if (layout == VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL && !(image_usage & VK_IMAGE_USAGE_TRANSFER_SRC_BIT)) {
        vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-03098" : "VUID-vkCmdBeginRenderPass-initialLayout-00898";
        skip = true;
    } else if (layout == VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL && !(image_usage & VK_IMAGE_USAGE_TRANSFER_DST_BIT)) {
        vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-03099" : "VUID-vkCmdBeginRenderPass-initialLayout-00899";
        skip = true;
    } else if (layout == VK_IMAGE_LAYOUT_ATTACHMENT_FEEDBACK_LOOP_OPTIMAL_EXT) {
        if (((image_usage & (VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) == 0) ||
            ((image_usage & (VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT)) == 0)) {
            vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-07002" : "VUID-vkCmdBeginRenderPass-initialLayout-07000";
            skip = true;
        } else if (!(image_usage & VK_IMAGE_USAGE_ATTACHMENT_FEEDBACK_LOOP_BIT_EXT)) {
            vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-07003" : "VUID-vkCmdBeginRenderPass-initialLayout-07001";
            skip = true;
        }
    } else if ((layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
                layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
                layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
                layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL) &&
               !(image_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
        vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-03096" : "VUID-vkCmdBeginRenderPass-initialLayout-01758";
        skip = true;
    } else if (layout == VK_IMAGE_LAYOUT_RENDERING_LOCAL_READ && !IsDynamicRenderingImageUsageValid(image_usage)) {
        vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-09538" : "VUID-vkCmdBeginRenderPass-initialLayout-09537";
        skip = true;
    } else if ((IsImageLayoutDepthOnly(layout) || IsImageLayoutStencilOnly(layout)) &&
               !(image_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
        vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-initialLayout-02844" : "VUID-vkCmdBeginRenderPass-initialLayout-02842";
        skip = true;
    }

    if (skip) {
        std::stringstream stencil_usage_message;
        if (stencil_usage_info) {
            stencil_usage_message << " (which includes " << string_VkImageUsageFlags(stencil_usage_info->stencilUsage)
                                  << "from VkImageStencilUsageCreateInfo)";
        }
        const LogObjectList objlist(image, renderpass, framebuffer, image_view);
        return LogError(vuid, objlist, rp_loc,
                        "(%s) was created with %s = %s, but %s pAttachments[%" PRIu32 "] (%s) usage is %s%s.",
                        FormatHandle(renderpass).c_str(), attachment_reference_loc.Fields().c_str(), string_VkImageLayout(layout),
                        FormatHandle(framebuffer).c_str(), attachment_index, FormatHandle(image_view).c_str(),
                        string_VkImageUsageFlags(image_usage).c_str(), stencil_usage_message.str().c_str());
    }

    return skip;
}

bool CoreChecks::ValidateRenderPassStencilLayoutAgainstFramebufferImageUsage(VkImageLayout layout,
                                                                             const vvl::ImageView &image_view_state,
                                                                             VkFramebuffer framebuffer, VkRenderPass renderpass,
                                                                             const Location &layout_loc) const {
    bool skip = false;
    const auto &image_view = image_view_state.Handle();
    const auto *image_state = image_view_state.image_state.get();
    const auto &image = image_state->Handle();
    const bool use_rp2 = layout_loc.function != Func::vkCmdBeginRenderPass;

    if (!image_state) {
        return skip;  // validated at VUID-VkRenderPassBeginInfo-framebuffer-parameter
    }
    auto image_usage = image_state->create_info.usage;
    if (const auto stencil_usage_info =
            vku::FindStructInPNextChain<VkImageStencilUsageCreateInfo>(image_state->create_info.pNext)) {
        image_usage |= stencil_usage_info->stencilUsage;
    }

    if (IsImageLayoutStencilOnly(layout) && !(image_usage & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT)) {
        const char *vuid = use_rp2 ? "VUID-vkCmdBeginRenderPass2-stencilInitialLayout-02845"
                                   : "VUID-vkCmdBeginRenderPass-stencilInitialLayout-02843";
        const LogObjectList objlist(image, renderpass, framebuffer, image_view);
        skip |= LogError(vuid, objlist, layout_loc,
                         "is %s but the image attached to %s via %s"
                         " was created with %s (not VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT).",
                         string_VkImageLayout(layout), FormatHandle(framebuffer).c_str(), FormatHandle(image_view).c_str(),
                         string_VkImageUsageFlags(image_usage).c_str());
    }

    return skip;
}

bool CoreChecks::ValidateFramebufferAndRenderPassLayouts(const vvl::CommandBuffer &cb_state,
                                                         const VkRenderPassBeginInfo &begin_info,
                                                         const vvl::RenderPass &render_pass_state,
                                                         const vvl::Framebuffer &framebuffer_state,
                                                         const Location &rp_begin_loc) const {
    bool skip = false;
    const auto *render_pass_info = render_pass_state.create_info.ptr();
    const VkRenderPass render_pass = render_pass_state.VkHandle();
    auto const &framebuffer_info = framebuffer_state.create_info;
    const VkImageView *attachments = framebuffer_info.pAttachments;

    const VkFramebuffer framebuffer = framebuffer_state.VkHandle();

    if (render_pass_info->attachmentCount != framebuffer_info.attachmentCount) {
        const LogObjectList objlist(render_pass, framebuffer_state.Handle());
        // VU bieng worked on at https://gitlab.khronos.org/vulkan/vulkan/-/issues/2267
        skip |= LogError("UNASSIGNED-CoreValidation-DrawState-InvalidRenderpass", objlist, rp_begin_loc,
                         "You cannot start a render pass using a framebuffer with a different number of attachments (%" PRIu32
                         " vs %" PRIu32 ").",
                         render_pass_info->attachmentCount, framebuffer_info.attachmentCount);
    }

    const auto *attachment_info = vku::FindStructInPNextChain<VkRenderPassAttachmentBeginInfo>(begin_info.pNext);
    if (((framebuffer_info.flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT) != 0) && attachment_info != nullptr) {
        attachments = attachment_info->pAttachments;
    }

    if (attachments == nullptr) {
        return skip;
    }

    // Have the location where the VkRenderPass is reference, and where in it's creation the error occured
    const Location rp_loc = rp_begin_loc.dot(Field::renderPass);
    // only printing Fields, but use same Function to make getting correct VUID easier
    const Location rp_create_info(rp_begin_loc.function, Field::pCreateInfo);

    for (uint32_t i = 0; i < render_pass_info->attachmentCount && i < framebuffer_info.attachmentCount; ++i) {
        const Location attachment_loc = rp_create_info.dot(Field::pAttachments, i);
        auto image_view = attachments[i];
        auto view_state = Get<vvl::ImageView>(image_view);

        if (!view_state) {
            const LogObjectList objlist(render_pass, framebuffer_state.Handle(), image_view);
            skip |= LogError("VUID-VkRenderPassBeginInfo-framebuffer-parameter", objlist, attachment_loc, "%s is invalid.",
                             FormatHandle(image_view).c_str());
            continue;
        }

        const VkImage image = view_state->create_info.image;
        const auto *image_state = view_state->image_state.get();

        if (!image_state) {
            const LogObjectList objlist(render_pass, framebuffer_state.Handle(), image_view, image);
            skip |= LogError("VUID-VkRenderPassBeginInfo-framebuffer-parameter", objlist, attachment_loc,
                             "%s references invalid image (%s).", FormatHandle(image_view).c_str(), FormatHandle(image).c_str());
            continue;
        }
        if (image_state->IsSwapchainImage() && image_state->owned_by_swapchain && !image_state->bind_swapchain) {
            const LogObjectList objlist(render_pass, framebuffer_state.Handle(), image_view, image);
            skip |= LogError("VUID-VkRenderPassBeginInfo-framebuffer-parameter", objlist, attachment_loc,
                             "%s references a swapchain image (%s) from a swapchain that has been destroyed.",
                             FormatHandle(image_view).c_str(), FormatHandle(image).c_str());
            continue;
        }
        auto attachment_initial_layout = render_pass_info->pAttachments[i].initialLayout;
        auto attachment_final_layout = render_pass_info->pAttachments[i].finalLayout;

        // Default to expecting stencil in the same layout.
        auto attachment_stencil_initial_layout = attachment_initial_layout;

        // If a separate layout is specified, look for that.
        const auto *attachment_desc_stencil_layout =
            vku::FindStructInPNextChain<VkAttachmentDescriptionStencilLayout>(render_pass_info->pAttachments[i].pNext);
        if (attachment_desc_stencil_layout) {
            attachment_stencil_initial_layout = attachment_desc_stencil_layout->stencilInitialLayout;
        }

        std::shared_ptr<const CommandBufferImageLayoutMap> image_layout_map;
        bool has_queried_map = false;

        for (uint32_t aspect_index = 0; aspect_index < 32; aspect_index++) {
            VkImageAspectFlags test_aspect = 1u << aspect_index;
            // NOTE: This part of validation works with view's GetRangeGeneratorRange() range,
            // but it has the same aspect as normalized_subresource_range so it's fine to check the latter.
            // Still one future improvement is to use view's range generator object directly to check for aspect.
            if ((view_state->normalized_subresource_range.aspectMask & test_aspect) == 0) {
                continue;
            }

            // Allow for differing depth and stencil layouts
            VkImageLayout check_layout = attachment_initial_layout;
            if (test_aspect == VK_IMAGE_ASPECT_STENCIL_BIT) {
                check_layout = attachment_stencil_initial_layout;
            }

            // If no layout information for image yet, will be checked at QueueSubmit time
            if (check_layout == VK_IMAGE_LAYOUT_UNDEFINED) {
                continue;
            }
            if (!has_queried_map) {
                image_layout_map = cb_state.GetImageLayoutMap(image_state->VkHandle());
                has_queried_map = true;
            }
            if (!image_layout_map) {
                // If no layout information for image yet, will be checked at QueueSubmit time
                continue;
            }

            // Cannot use view_state->range_generator directly since we need to modify aspectMask
            VkImageSubresourceRange image_layout_range = view_state->GetRangeGeneratorRange(device_state->extensions);
            image_layout_range.aspectMask = test_aspect;

            LayoutUseCheckAndMessage layout_check(check_layout, test_aspect);

            skip |= ForEachMatchingLayoutMapRange(
                *image_layout_map, RangeGenerator(image_state->subresource_encoder, image_layout_range),
                [this, &layout_check, i, cb = cb_state.Handle(), render_pass = render_pass,
                 framebuffer = framebuffer_state.Handle(), image = view_state->image_state->Handle(),
                 image_view = view_state->Handle(), attachment_loc,
                 rp_begin_loc](const LayoutRange &range, const ImageLayoutState &state) {
                    bool subres_skip = false;
                    if (!layout_check.Check(state)) {
                        const LogObjectList objlist(cb, render_pass, framebuffer, image, image_view);
                        const char *vuid = rp_begin_loc.function != Func::vkCmdBeginRenderPass
                                               ? "VUID-vkCmdBeginRenderPass2-initialLayout-03100"
                                               : "VUID-vkCmdBeginRenderPass-initialLayout-00900";
                        subres_skip |= LogError(vuid, objlist, attachment_loc,
                                                "You cannot start a render pass using attachment %" PRIu32
                                                " where the render pass initial layout is %s and the %s layout "
                                                "of the attachment is %s. The layouts "
                                                "must match, or the render pass initial layout for the "
                                                "attachment must be VK_IMAGE_LAYOUT_UNDEFINED.",
                                                i, string_VkImageLayout(layout_check.expected_layout), layout_check.message,
                                                string_VkImageLayout(layout_check.layout));
                    }
                    return subres_skip;
                });
        }
        skip |= ValidateRenderPassLayoutAgainstFramebufferImageUsage(
            attachment_initial_layout, *view_state, framebuffer, render_pass, i, rp_loc, attachment_loc.dot(Field::initialLayout));

        skip |= ValidateRenderPassLayoutAgainstFramebufferImageUsage(attachment_final_layout, *view_state, framebuffer, render_pass,
                                                                     i, rp_loc, attachment_loc.dot(Field::finalLayout));

        if (attachment_desc_stencil_layout != nullptr) {
            skip |= ValidateRenderPassStencilLayoutAgainstFramebufferImageUsage(
                attachment_desc_stencil_layout->stencilInitialLayout, *view_state, framebuffer, render_pass,
                attachment_loc.pNext(Struct::VkAttachmentDescriptionStencilLayout, Field::stencilInitialLayout));
            skip |= ValidateRenderPassStencilLayoutAgainstFramebufferImageUsage(
                attachment_desc_stencil_layout->stencilFinalLayout, *view_state, framebuffer, render_pass,
                attachment_loc.pNext(Struct::VkAttachmentDescriptionStencilLayout, Field::stencilFinalLayout));
        }
    }

    for (uint32_t j = 0; j < render_pass_info->subpassCount; ++j) {
        const Location subpass_loc = rp_create_info.dot(Field::pSubpasses, j);
        auto &subpass = render_pass_info->pSubpasses[j];
        const auto *ms_rendered_to_single_sampled =
            vku::FindStructInPNextChain<VkMultisampledRenderToSingleSampledInfoEXT>(render_pass_info->pSubpasses[j].pNext);
        for (uint32_t k = 0; k < render_pass_info->pSubpasses[j].inputAttachmentCount; ++k) {
            auto &attachment_ref = subpass.pInputAttachments[k];
            if (attachment_ref.attachment == VK_ATTACHMENT_UNUSED) continue;
            const Location input_loc = subpass_loc.dot(Field::pInputAttachments, k);
            auto image_view = attachments[attachment_ref.attachment];

            if (auto view_state = Get<vvl::ImageView>(image_view)) {
                skip |= ValidateRenderPassLayoutAgainstFramebufferImageUsage(attachment_ref.layout, *view_state, framebuffer,
                                                                             render_pass, attachment_ref.attachment, rp_loc,
                                                                             input_loc.dot(Field::layout));

                if (ms_rendered_to_single_sampled && ms_rendered_to_single_sampled->multisampledRenderToSingleSampledEnable) {
                    if (render_pass_info->pAttachments[attachment_ref.attachment].samples == VK_SAMPLE_COUNT_1_BIT) {
                        skip |= ValidateMultipassRenderedToSingleSampledSampleCount(
                            framebuffer, render_pass, *view_state->image_state, ms_rendered_to_single_sampled->rasterizationSamples,
                            subpass_loc.pNext(Struct::VkMultisampledRenderToSingleSampledInfoEXT, Field::rasterizationSamples));
                    }
                }
            }
        }

        for (uint32_t k = 0; k < render_pass_info->pSubpasses[j].colorAttachmentCount; ++k) {
            auto &attachment_ref = subpass.pColorAttachments[k];
            if (attachment_ref.attachment == VK_ATTACHMENT_UNUSED) continue;
            const Location color_attachment_loc = subpass_loc.dot(Field::pColorAttachments, k);
            auto image_view = attachments[attachment_ref.attachment];

            if (auto view_state = Get<vvl::ImageView>(image_view)) {
                skip |= ValidateRenderPassLayoutAgainstFramebufferImageUsage(attachment_ref.layout, *view_state, framebuffer,
                                                                             render_pass, attachment_ref.attachment, rp_loc,
                                                                             color_attachment_loc.dot(Field::layout));
                if (subpass.pResolveAttachments) {
                    skip |= ValidateRenderPassLayoutAgainstFramebufferImageUsage(
                        attachment_ref.layout, *view_state, framebuffer, render_pass, attachment_ref.attachment, rp_loc,
                        subpass_loc.dot(Field::pResolveAttachments, k).dot(Field::layout));
                }

                if (ms_rendered_to_single_sampled && ms_rendered_to_single_sampled->multisampledRenderToSingleSampledEnable) {
                    if (render_pass_info->pAttachments[attachment_ref.attachment].samples == VK_SAMPLE_COUNT_1_BIT) {
                        skip |= ValidateMultipassRenderedToSingleSampledSampleCount(
                            framebuffer, render_pass, *view_state->image_state, ms_rendered_to_single_sampled->rasterizationSamples,
                            subpass_loc.pNext(Struct::VkMultisampledRenderToSingleSampledInfoEXT, Field::rasterizationSamples));
                    }
                }
            }
        }

        if (render_pass_info->pSubpasses[j].pDepthStencilAttachment) {
            auto &attachment_ref = *subpass.pDepthStencilAttachment;
            if (attachment_ref.attachment == VK_ATTACHMENT_UNUSED) continue;
            const Location ds_loc = subpass_loc.dot(Field::pDepthStencilAttachment);
            auto image_view = attachments[attachment_ref.attachment];

            if (auto view_state = Get<vvl::ImageView>(image_view)) {
                skip |= ValidateRenderPassLayoutAgainstFramebufferImageUsage(attachment_ref.layout, *view_state, framebuffer,
                                                                             render_pass, attachment_ref.attachment, rp_loc,
                                                                             ds_loc.dot(Field::layout));

                if (const auto *stencil_layout =
                        vku::FindStructInPNextChain<VkAttachmentReferenceStencilLayout>(attachment_ref.pNext);
                    stencil_layout != nullptr) {
                    skip |= ValidateRenderPassStencilLayoutAgainstFramebufferImageUsage(
                        stencil_layout->stencilLayout, *view_state, framebuffer, render_pass,
                        ds_loc.pNext(Struct::VkAttachmentReferenceStencilLayout, Field::stencilLayout));
                }

                if (ms_rendered_to_single_sampled && ms_rendered_to_single_sampled->multisampledRenderToSingleSampledEnable) {
                    if (render_pass_info->pAttachments[attachment_ref.attachment].samples == VK_SAMPLE_COUNT_1_BIT) {
                        skip |= ValidateMultipassRenderedToSingleSampledSampleCount(
                            framebuffer, render_pass, *view_state->image_state, ms_rendered_to_single_sampled->rasterizationSamples,
                            subpass_loc.pNext(Struct::VkMultisampledRenderToSingleSampledInfoEXT, Field::rasterizationSamples));
                    }
                }
            }
        }
    }
    return skip;
}

bool CoreChecks::ValidateRenderingAttachmentCurrentLayout(const vvl::CommandBuffer &cb_state,
                                                          const VkRenderingAttachmentInfo &attachment_info,
                                                          VkImageAspectFlags aspect_mask, const Location &attachment_loc,
                                                          const char *vuid) const {
    bool skip = false;
    if (disabled[image_layout_validation]) {
        return skip;
    }
    const auto image_view_state = Get<vvl::ImageView>(attachment_info.imageView);
    if (!image_view_state) {
        return skip;
    }
    const vvl::Image &image_state = *image_view_state->image_state;
    const auto image_layout_map = cb_state.GetImageLayoutMap(image_state.VkHandle());
    if (!image_layout_map) {
        return skip;
    }

    // Cannot use view_state->range_generator directly since we need to modify aspectMask
    VkImageSubresourceRange image_layout_range = image_view_state->GetRangeGeneratorRange(device_state->extensions);
    image_layout_range.aspectMask = aspect_mask;

    LayoutUseCheckAndMessage layout_check(attachment_info.imageLayout, aspect_mask);

    skip |= ForEachMatchingLayoutMapRange(
        *image_layout_map, RangeGenerator(image_view_state->image_state->subresource_encoder, image_layout_range),
        [this, &cb_state, &image_state, &image_view_state, &layout_check, vuid, attachment_loc](const LayoutRange &range,
                                                                                                const ImageLayoutState &state) {
            bool local_skip = false;
            if (!layout_check.Check(state)) {
                const subresource_adapter::Subresource subresource = image_state.subresource_encoder.Decode(range.begin);
                const LogObjectList objlist(cb_state.Handle(), image_state.Handle(), image_view_state->Handle());
                local_skip |=
                    LogError(vuid, objlist, attachment_loc,
                             "(%s, layer %" PRIu32 ", mip %" PRIu32 ") is expected to have layout %s but %s layout is %s.",
                             FormatHandle(image_state).c_str(), subresource.arrayLayer, subresource.mipLevel,
                             string_VkImageLayout(layout_check.expected_layout), layout_check.message,
                             string_VkImageLayout(layout_check.layout));
            }
            return local_skip;
        });
    return skip;
}

void CoreChecks::TransitionAttachmentRefLayout(vvl::CommandBuffer &cb_state, const vku::safe_VkAttachmentReference2 &ref) {
    if (ref.attachment == VK_ATTACHMENT_UNUSED) return;
    vvl::ImageView *image_view = cb_state.GetActiveAttachmentImageViewState(ref.attachment);
    if (image_view) {
        VkImageLayout stencil_layout = kInvalidLayout;
        const auto *attachment_reference_stencil_layout =
            vku::FindStructInPNextChain<VkAttachmentReferenceStencilLayout>(ref.pNext);
        if (attachment_reference_stencil_layout) {
            stencil_layout = attachment_reference_stencil_layout->stencilLayout;
        }

        cb_state.SetImageViewLayout(*image_view, ref.layout, stencil_layout);
    }
}

void CoreChecks::TransitionSubpassLayouts(vvl::CommandBuffer &cb_state, const vvl::RenderPass &render_pass_state,
                                          const int subpass_index) {
    auto const &subpass = render_pass_state.create_info.pSubpasses[subpass_index];
    for (uint32_t j = 0; j < subpass.inputAttachmentCount; ++j) {
        TransitionAttachmentRefLayout(cb_state, subpass.pInputAttachments[j]);
    }
    for (uint32_t j = 0; j < subpass.colorAttachmentCount; ++j) {
        TransitionAttachmentRefLayout(cb_state, subpass.pColorAttachments[j]);
    }
    if (subpass.pDepthStencilAttachment) {
        TransitionAttachmentRefLayout(cb_state, *subpass.pDepthStencilAttachment);
    }
}

// Transition the layout state for renderpass attachments based on the BeginRenderPass() call. This includes:
// 1. Transition into initialLayout state
// 2. Transition from initialLayout to layout used in subpass 0
void CoreChecks::TransitionBeginRenderPassLayouts(vvl::CommandBuffer &cb_state, const vvl::RenderPass &render_pass_state) {
    // First record expected initialLayout as a potential initial layout usage.
    auto const rpci = render_pass_state.create_info.ptr();
    for (uint32_t i = 0; i < rpci->attachmentCount; ++i) {
        auto *view_state = cb_state.GetActiveAttachmentImageViewState(i);
        if (!view_state) continue;

        vvl::Image *image_state = view_state->image_state.get();
        ASSERT_AND_CONTINUE(image_state);

        const auto initial_layout = rpci->pAttachments[i].initialLayout;
        const auto *attachment_description_stencil_layout =
            vku::FindStructInPNextChain<VkAttachmentDescriptionStencilLayout>(rpci->pAttachments[i].pNext);
        if (attachment_description_stencil_layout) {
            const auto stencil_initial_layout = attachment_description_stencil_layout->stencilInitialLayout;
            VkImageSubresourceRange sub_range = view_state->normalized_subresource_range;
            sub_range.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
            cb_state.TrackImageFirstLayout(*image_state, sub_range, 0, 0, initial_layout);
            sub_range.aspectMask = VK_IMAGE_ASPECT_STENCIL_BIT;
            cb_state.TrackImageFirstLayout(*image_state, sub_range, 0, 0, stencil_initial_layout);
        } else {
            // If layoutStencil is kInvalidLayout (meaning no separate depth/stencil layout), image view format has both depth
            // and stencil aspects, and subresource has only one of aspect out of depth or stencil, then the missing aspect will
            // also be transitioned and thus must be included explicitly
            auto subresource_range = view_state->normalized_subresource_range;
            if (const VkFormat format = view_state->create_info.format; vkuFormatIsDepthAndStencil(format)) {
                if (subresource_range.aspectMask & (VK_IMAGE_ASPECT_STENCIL_BIT | VK_IMAGE_ASPECT_DEPTH_BIT)) {
                    subresource_range.aspectMask |= VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
                }
            }
            cb_state.TrackImageFirstLayout(*image_state, subresource_range, 0, 0, initial_layout);
        }
    }
    // Now transition for first subpass (index 0)
    TransitionSubpassLayouts(cb_state, render_pass_state, 0);
}

bool CoreChecks::ValidateClearImageLayout(const vvl::CommandBuffer &cb_state, const vvl::Image &image_state,
                                          const VkImageSubresourceRange &range, VkImageLayout dest_image_layout,
                                          const Location &loc) const {
    bool skip = false;
    if (loc.function == Func::vkCmdClearDepthStencilImage) {
        if ((dest_image_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) && (dest_image_layout != VK_IMAGE_LAYOUT_GENERAL)) {
            LogObjectList objlist(cb_state.Handle(), image_state.Handle());
            skip |= LogError("VUID-vkCmdClearDepthStencilImage-imageLayout-00012", objlist, loc,
                             "Layout for cleared image is %s but can only be TRANSFER_DST_OPTIMAL or GENERAL.",
                             string_VkImageLayout(dest_image_layout));
        }

    } else if (loc.function == Func::vkCmdClearColorImage) {
        if ((dest_image_layout != VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL) && (dest_image_layout != VK_IMAGE_LAYOUT_GENERAL) &&
            (dest_image_layout != VK_IMAGE_LAYOUT_SHARED_PRESENT_KHR)) {
            LogObjectList objlist(cb_state.Handle(), image_state.Handle());
            skip |= LogError("VUID-vkCmdClearColorImage-imageLayout-01394", objlist, loc,
                             "Layout for cleared image is %s but can only be TRANSFER_DST_OPTIMAL, SHARED_PRESENT_KHR, or GENERAL.",
                             string_VkImageLayout(dest_image_layout));
        }
    }

    // Cast to const to prevent creation at validate time.
    const auto image_layout_map = cb_state.GetImageLayoutMap(image_state.VkHandle());
    if (image_layout_map) {
        LayoutUseCheckAndMessage layout_check(dest_image_layout);
        auto normalized_isr = image_state.NormalizeSubresourceRange(range);
        // TODO: InRange check is needed here because provided range is an API input (can be arbitrary data),
        // but regular normalization does not produce correct range to track image layouts (need to use similar logic
        // that is used to initialize range encoder)
        if (image_state.subresource_encoder.InRange(normalized_isr)) {
            auto range_gen = RangeGenerator(image_state.subresource_encoder, normalized_isr);
            skip |= ForEachMatchingLayoutMapRange(
                *image_layout_map, std::move(range_gen),
                [this, &cb_state, &layout_check, loc, image = image_state.Handle()](const LayoutRange &range,
                                                                                    const ImageLayoutState &state) {
                    bool subres_skip = false;
                    if (!layout_check.Check(state)) {
                        const char *vuid = (loc.function == Func::vkCmdClearDepthStencilImage)
                                               ? "VUID-vkCmdClearDepthStencilImage-imageLayout-00011"
                                               : "VUID-vkCmdClearColorImage-imageLayout-00004";
                        LogObjectList objlist(cb_state.Handle(), image);
                        subres_skip |= LogError(vuid, objlist, loc,
                                                "Cannot clear an image whose layout is %s and doesn't match the %s layout %s.",
                                                string_VkImageLayout(layout_check.expected_layout), layout_check.message,
                                                string_VkImageLayout(layout_check.layout));
                    }
                    return subres_skip;
                });
        }
    }

    return skip;
}

bool CoreChecks::ValidateImageBarrierLayouts(const vvl::CommandBuffer &cb_state, const vvl::Image &image_state,
                                             const Location &image_loc, const ImageBarrier &image_barrier,
                                             ImageLayoutRegistry &local_layout_registry) const {
    bool skip = false;

    std::shared_ptr<CommandBufferImageLayoutMap> local_layout_map;
    auto iter = local_layout_registry.find(image_state.VkHandle());
    bool existing_local_map = false;
    if (iter == local_layout_registry.end()) {
        local_layout_map =
            std::make_shared<CommandBufferImageLayoutMap>(image_state.subresource_encoder.SubresourceCount(), image_state.GetId());
        local_layout_registry.emplace(image_state.VkHandle(), local_layout_map);
    } else if (iter->second->image_id != image_state.GetId()) {
        local_layout_map =
            std::make_shared<CommandBufferImageLayoutMap>(image_state.subresource_encoder.SubresourceCount(), image_state.GetId());
        iter->second = local_layout_map;
    } else {
        local_layout_map = iter->second;
        existing_local_map = true;
    }

    std::shared_ptr<const CommandBufferImageLayoutMap> cb_layout_map = cb_state.GetImageLayoutMap(image_state.VkHandle());
    const auto &layout_map = (existing_local_map || cb_layout_map == nullptr) ? local_layout_map : cb_layout_map;

    // Validate aspects in isolation.
    // This is required when handling separate depth-stencil layouts.
    for (uint32_t aspect_index = 0; aspect_index < 32; aspect_index++) {
        VkImageAspectFlags test_aspect = 1u << aspect_index;
        if ((image_barrier.subresourceRange.aspectMask & test_aspect) == 0) {
            continue;
        }

        // It is fine to store normalized oldLayout value inside LayoutUseCheckAndMessage. It is used only for
        // comparison and won't appear in the error message (messages require original, non-normalized layouts)
        auto old_layout = NormalizeSynchronization2Layout(image_barrier.subresourceRange.aspectMask, image_barrier.oldLayout);

        LayoutUseCheckAndMessage layout_check(old_layout, test_aspect);
        auto normalized_isr = image_state.NormalizeSubresourceRange(image_barrier.subresourceRange);
        normalized_isr.aspectMask = test_aspect;

        // TODO: InRange check is needed here because barrier's subresource range is an API input (can be arbitrary data),
        // but regular normalization does not produce correct range to track image layouts (need to use similar logic
        // that is used to initialize range encoder)
        if (image_state.subresource_encoder.InRange(normalized_isr)) {
            skip |= ForEachMatchingLayoutMapRange(
                *layout_map, RangeGenerator(image_state.subresource_encoder, normalized_isr),
                [this, &cb_state, &layout_check, &image_loc, &image_barrier, &image_state](const LayoutRange &range,
                                                                                           const ImageLayoutState &state) {
                    bool subres_skip = false;
                    if (!layout_check.Check(state)) {
                        const auto &vuid = GetImageBarrierVUID(image_loc, vvl::ImageError::kConflictingLayout);
                        const subresource_adapter::Subresource subresource = image_state.subresource_encoder.Decode(range.begin);
                        const VkImageSubresource vk_subresource = image_state.subresource_encoder.MakeVkSubresource(subresource);
                        const LogObjectList objlist(cb_state.Handle(), image_barrier.image);
                        subres_skip =
                            LogError(vuid, objlist, image_loc,
                                     "(%s) cannot transition the layout of aspect=%" PRIu32 ", level=%" PRIu32 ", layer=%" PRIu32
                                     " from %s when the "
                                     "%s layout is %s.",
                                     FormatHandle(image_barrier.image).c_str(), vk_subresource.aspectMask, vk_subresource.mipLevel,
                                     vk_subresource.arrayLayer, string_VkImageLayout(image_barrier.oldLayout), layout_check.message,
                                     string_VkImageLayout(layout_check.layout));
                    }
                    return subres_skip;
                });

            UpdateCurrentLayout(*local_layout_map, RangeGenerator(image_state.subresource_encoder, normalized_isr),
                                image_barrier.newLayout, kInvalidLayout, normalized_isr.aspectMask);
        }
    }
    return skip;
}

static std::vector<uint32_t> GetUsedColorAttachments(const vvl::CommandBuffer &cb_state) {
    std::vector<uint32_t> attachments;
    attachments.reserve(cb_state.rendering_attachments.color_locations.size());
    for (size_t i = 0; i < cb_state.rendering_attachments.color_locations.size(); ++i) {
        // VkRenderingAttachmentLocationInfo can make color attachment unused
        // by setting output location value as VK_ATTACHMENT_UNUSED
        if (cb_state.rendering_attachments.color_locations[i] != VK_ATTACHMENT_UNUSED) {
            attachments.emplace_back(uint32_t(i));
        }
    }
    return attachments;
}

bool CoreChecks::VerifyDynamicRenderingImageBarrierLayouts(const vvl::CommandBuffer &cb_state, const vvl::Image &image_state,
                                                           const VkRenderingInfo &rendering_info,
                                                           const Location &barrier_loc) const {
    bool skip = false;
    auto cb_image_layouts = cb_state.GetImageLayoutMap(image_state.VkHandle());
    if (!cb_image_layouts) {
        return skip;
    }

    // Search for attachment that uses the same image as the barrier's image
    std::shared_ptr<const vvl::ImageView> matching_attatchment_view_state;
    for (auto color_attachment_idx : GetUsedColorAttachments(cb_state)) {
        if (color_attachment_idx >= rendering_info.colorAttachmentCount) {
            continue;
        }
        const auto &color_attachment = rendering_info.pColorAttachments[color_attachment_idx];
        auto image_view_state = Get<vvl::ImageView>(color_attachment.imageView);
        if (image_view_state && image_view_state->image_state->VkHandle() == image_state.VkHandle()) {
            matching_attatchment_view_state = std::move(image_view_state);
            break;
        }
    }
    if (!matching_attatchment_view_state && rendering_info.pDepthAttachment) {
        auto image_view_state = Get<vvl::ImageView>(rendering_info.pDepthAttachment->imageView);
        if (image_view_state && image_view_state->image_state->VkHandle() == image_state.VkHandle()) {
            matching_attatchment_view_state = std::move(image_view_state);
        }
    }
    if (!matching_attatchment_view_state && rendering_info.pStencilAttachment) {
        auto image_view_state = Get<vvl::ImageView>(rendering_info.pStencilAttachment->imageView);
        if (image_view_state && image_view_state->image_state->VkHandle() == image_state.VkHandle()) {
            matching_attatchment_view_state = std::move(image_view_state);
        }
    }
    if (!matching_attatchment_view_state) {
        return skip;
    }

    // Validate layout of the found attachment
    skip |= ForEachMatchingLayoutMapRange(
        *cb_image_layouts, RangeGenerator(matching_attatchment_view_state->range_generator),
        [this, &image_state, &barrier_loc](const LayoutRange &range, const ImageLayoutState &state) {
            // Use current layout if it is specified (we tracked actual image layout transition).
            // Otherwise use expected layout (specified by various APIs): during execution the
            // correct programs must ensure the image layout is in the expected layout at this point.
            const VkImageLayout layout = (state.current_layout != kInvalidLayout) ? state.current_layout : state.first_layout;

            bool local_skip = false;
            if (layout != VK_IMAGE_LAYOUT_RENDERING_LOCAL_READ && layout != VK_IMAGE_LAYOUT_GENERAL) {
                const auto &vuid = GetDynamicRenderingBarrierVUID(barrier_loc, vvl::DynamicRenderingBarrierError::kImageLayout);
                local_skip |=
                    LogError(vuid, image_state.VkHandle(), barrier_loc, "image layout is %s.", string_VkImageLayout(layout));
            }
            return local_skip;
        });
    return skip;
}

void CoreChecks::EnqueueValidateDynamicRenderingImageBarrierLayouts(const Location barrier_loc, vvl::CommandBuffer &cb_state,
                                                                    const ImageBarrier &image_barrier) {
    if (!cb_state.active_render_pass || !cb_state.active_render_pass->UsesDynamicRendering()) {
        return;
    }
    const VkRenderingInfo &rendering_info = *cb_state.active_render_pass->dynamic_rendering_begin_rendering_info.ptr();
    std::shared_ptr<const CommandBufferImageLayoutMap> image_layout_map = cb_state.GetImageLayoutMap(image_barrier.image);

    auto &cb_sub_state = core::SubState(cb_state);

    auto process_image_view = [&image_barrier, &image_layout_map, &cb_sub_state,
                               &barrier_loc](const vvl::ImageView &image_view_state) {
        // Skip attachments that use different image than a barrier
        if (image_barrier.image != image_view_state.image_state->VkHandle()) {
            return;
        }
        // Skip images that already have image layout specified so layout validation was done at record time
        if (image_layout_map) {
            auto any_range_pred = [](const LayoutRange &, const ImageLayoutState &) { return true; };
            if (ForEachMatchingLayoutMapRange(*image_layout_map, RangeGenerator(image_view_state.range_generator),
                                              any_range_pred)) {
                return;
            }
        }
        // Enqueue distinct subresource ranges for this image.
        // Then during submit time the layouts of these subresources are validated against allowed values
        auto &enqueued_subresources = cb_sub_state.submit_validate_dynamic_rendering_barrier_subresources[image_barrier.image];
        auto it = std::find_if(enqueued_subresources.begin(), enqueued_subresources.end(), [&image_view_state](const auto &entry) {
            return entry.first == image_view_state.normalized_subresource_range;
        });
        if (it == enqueued_subresources.end()) {
            enqueued_subresources.emplace_back(
                std::make_pair(image_view_state.normalized_subresource_range, vvl::LocationCapture(barrier_loc)));
        }
    };

    for (auto color_attachment_idx : GetUsedColorAttachments(cb_state)) {
        if (color_attachment_idx >= rendering_info.colorAttachmentCount) {
            continue;
        }
        const auto &color_attachment = rendering_info.pColorAttachments[color_attachment_idx];
        if (const auto image_view_state = Get<vvl::ImageView>(color_attachment.imageView)) {
            process_image_view(*image_view_state);
        }
    }
    if (rendering_info.pDepthAttachment) {
        const AttachmentInfo &attachment =
            cb_state.active_attachments[cb_state.GetDynamicRenderingAttachmentIndex(AttachmentInfo::Type::Depth)];
        if (attachment.image_view) {
            process_image_view(*attachment.image_view);
        }
    }
    if (rendering_info.pStencilAttachment) {
        const AttachmentInfo &attachment =
            cb_state.active_attachments[cb_state.GetDynamicRenderingAttachmentIndex(AttachmentInfo::Type::Stencil)];
        if (attachment.image_view) {
            process_image_view(*attachment.image_view);
        }
    }
}

void CoreChecks::RecordTransitionImageLayout(vvl::CommandBuffer &cb_state, const ImageBarrier &mem_barrier,
                                             const vvl::Image &image_state) {
    if (enabled_features.synchronization2) {
        if (mem_barrier.oldLayout == mem_barrier.newLayout) {
            return;
        }
    }

    VkImageSubresourceRange normalized_subresource_range = image_state.NormalizeSubresourceRange(mem_barrier.subresourceRange);

    // VK_REMAINING_ARRAY_LAYERS for sliced 3d image in the context of layout transition means image's depth extent.
    if (mem_barrier.subresourceRange.layerCount == VK_REMAINING_ARRAY_LAYERS &&
        CanTransitionDepthSlices(extensions, image_state.create_info)) {
        normalized_subresource_range.layerCount =
            image_state.create_info.extent.depth - normalized_subresource_range.baseArrayLayer;
    }

    VkImageLayout old_layout = mem_barrier.oldLayout;
    if (IsQueueFamilyExternal(mem_barrier.srcQueueFamilyIndex)) {
        // Layout transitions in external instance are not tracked, so don't validate previous layout
        old_layout = VK_IMAGE_LAYOUT_UNDEFINED;
    }

    // For ownership transfers, the barrier is specified twice; as a release
    // operation on the yielding queue family, and as an acquire operation
    // on the acquiring queue family. This barrier may also include a layout
    // transition, which occurs 'between' the two operations. For validation
    // purposes it doesn't seem important which side performs the layout
    // transition, but it must not be performed twice. We'll arbitrarily
    // choose to perform it as part of the acquire operation.
    //
    // However, we still need to record initial layout for the "initial layout" validation
    if (cb_state.IsReleaseOp(mem_barrier)) {
        cb_state.TrackImageFirstLayout(image_state, normalized_subresource_range, 0, 0, old_layout);
    } else {
        cb_state.SetImageLayout(image_state, normalized_subresource_range, mem_barrier.newLayout, old_layout);
    }
}

bool CoreChecks::IsCompliantSubresourceRange(const VkImageSubresourceRange &subres_range, const vvl::Image &image_state) const {
    if (!(subres_range.layerCount) || !(subres_range.levelCount)) return false;
    if (subres_range.baseMipLevel + subres_range.levelCount > image_state.create_info.mipLevels) return false;
    if ((subres_range.baseArrayLayer + subres_range.layerCount) > image_state.create_info.arrayLayers) {
        return false;
    }
    if (!IsValidAspectMaskForFormat(subres_range.aspectMask, image_state.create_info.format)) return false;
    if (((vkuFormatPlaneCount(image_state.create_info.format) < 3) && (subres_range.aspectMask & VK_IMAGE_ASPECT_PLANE_2_BIT)) ||
        ((vkuFormatPlaneCount(image_state.create_info.format) < 2) && (subres_range.aspectMask & VK_IMAGE_ASPECT_PLANE_1_BIT))) {
        return false;
    }
    if (subres_range.aspectMask & VK_IMAGE_ASPECT_METADATA_BIT ||
        subres_range.aspectMask & VK_IMAGE_ASPECT_MEMORY_PLANE_0_BIT_EXT ||
        subres_range.aspectMask & VK_IMAGE_ASPECT_MEMORY_PLANE_1_BIT_EXT ||
        subres_range.aspectMask & VK_IMAGE_ASPECT_MEMORY_PLANE_2_BIT_EXT ||
        subres_range.aspectMask & VK_IMAGE_ASPECT_MEMORY_PLANE_3_BIT_EXT) {
        return false;
    }
    return true;
}

bool CoreChecks::ValidateHostCopyCurrentLayout(const VkImageLayout expected_layout, const VkImageSubresourceLayers &subres_layers,
                                               const vvl::Image &image_state, const Location &loc) const {
    return ValidateHostCopyCurrentLayout(expected_layout, RangeFromLayers(subres_layers), image_state, loc);
}

bool CoreChecks::ValidateHostCopyCurrentLayout(const VkImageLayout expected_layout, const VkImageSubresourceRange &validate_range,
                                               const vvl::Image &image_state, const Location &loc) const {
    bool skip = false;
    if (disabled[image_layout_validation]) return false;
    if (!image_state.layout_map) return false;
    const VkImageSubresourceRange subres_range = image_state.NormalizeSubresourceRange(validate_range);
    // RangeGenerator doesn't tolerate degenerate or invalid ranges. The error will be found and logged elsewhere
    if (!IsCompliantSubresourceRange(subres_range, image_state)) return false;

    RangeGenerator range_gen(image_state.subresource_encoder, subres_range);

    struct CheckState {
        const VkImageLayout expected_layout;
        VkImageAspectFlags aspect_mask;
        ImageLayoutMap::key_type found_range;
        VkImageLayout found_layout;
        CheckState(VkImageLayout expected_layout_, VkImageAspectFlags aspect_mask_)
            : expected_layout(expected_layout_),
              aspect_mask(aspect_mask_),
              found_range({0, 0}),
              found_layout(VK_IMAGE_LAYOUT_MAX_ENUM) {}
    };

    CheckState check_state(expected_layout, subres_range.aspectMask);

    auto guard = image_state.LayoutMapReadLock();
    ForEachMatchingLayoutMapRange(*image_state.layout_map, std::move(range_gen),
                                  [&check_state](const ImageLayoutMap::key_type &range, const VkImageLayout &layout) {
                                      bool mismatch = false;
                                      if (!ImageLayoutMatches(check_state.aspect_mask, layout, check_state.expected_layout)) {
                                          check_state.found_range = range;
                                          check_state.found_layout = layout;
                                          mismatch = true;
                                      }
                                      return mismatch;
                                  });

    if (check_state.found_range.non_empty()) {
        const VkImageSubresource subres = image_state.subresource_encoder.IndexToVkSubresource(check_state.found_range.begin);
        skip |= LogError(vvl::GetImageImageLayoutVUID(loc), image_state.Handle(), loc,
                         "is currently %s but expected to be %s for %s (subresource: %s)",
                         string_VkImageLayout(check_state.found_layout), string_VkImageLayout(expected_layout),
                         debug_report->FormatHandle(image_state.Handle()).c_str(), string_VkImageSubresource(subres).c_str());
    }
    return skip;
}