forked from KhronosGroup/Vulkan-ValidationLayers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
synchronization_validation.h
1487 lines (1293 loc) · 89.3 KB
/
synchronization_validation.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2019-2021 Valve Corporation
* Copyright (c) 2019-2021 LunarG, 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: John Zulauf <[email protected]>
* Author: Locke Lin <[email protected]>
* Author: Jeremy Gebben <[email protected]>
*/
#pragma once
#include <limits>
#include <memory>
#include <vulkan/vulkan.h>
#include "synchronization_validation_types.h"
#include "state_tracker.h"
#include "cmd_buffer_state.h"
#include "render_pass_state.h"
class AccessContext;
class CommandBufferAccessContext;
using CommandBufferAccessContextShared = std::shared_ptr<CommandBufferAccessContext>;
class CommandExecutionContext;
class ResourceAccessState;
struct ResourceFirstAccess;
class SyncValidator;
using ImageRangeEncoder = subresource_adapter::ImageRangeEncoder;
using ImageRangeGen = subresource_adapter::ImageRangeGenerator;
enum SyncHazard {
NONE = 0,
READ_AFTER_WRITE,
WRITE_AFTER_READ,
WRITE_AFTER_WRITE,
READ_RACING_WRITE,
WRITE_RACING_WRITE,
WRITE_RACING_READ,
};
enum class SyncOrdering : uint8_t {
kNonAttachment = 0,
kColorAttachment = 1,
kDepthStencilAttachment = 2,
kRaster = 3,
kNumOrderings = 4,
};
// Useful Utilites for manipulating StageAccess parameters, suitable as base class to save typing
struct SyncStageAccess {
static inline SyncStageAccessFlags FlagBit(SyncStageAccessIndex stage_access) {
return syncStageAccessInfoByStageAccessIndex[stage_access].stage_access_bit;
}
static inline SyncStageAccessFlags Flags(SyncStageAccessIndex stage_access) {
return static_cast<SyncStageAccessFlags>(FlagBit(stage_access));
}
static bool IsRead(const SyncStageAccessFlags &stage_access_bit) { return (stage_access_bit & syncStageAccessReadMask).any(); }
static bool IsRead(SyncStageAccessIndex stage_access_index) { return IsRead(FlagBit(stage_access_index)); }
static bool IsWrite(const SyncStageAccessFlags &stage_access_bit) {
return (stage_access_bit & syncStageAccessWriteMask).any();
}
static bool HasWrite(const SyncStageAccessFlags &stage_access_mask) {
return (stage_access_mask & syncStageAccessWriteMask).any();
}
static bool IsWrite(SyncStageAccessIndex stage_access_index) { return IsWrite(FlagBit(stage_access_index)); }
static VkPipelineStageFlags2KHR PipelineStageBit(SyncStageAccessIndex stage_access_index) {
return syncStageAccessInfoByStageAccessIndex[stage_access_index].stage_mask;
}
static SyncStageAccessFlags AccessScopeByStage(VkPipelineStageFlags2KHR stages);
static SyncStageAccessFlags AccessScopeByAccess(VkAccessFlags2KHR access);
static SyncStageAccessFlags AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR access);
static SyncStageAccessFlags AccessScope(const SyncStageAccessFlags &stage_scope, VkAccessFlags2KHR accesses) {
return stage_scope & AccessScopeByAccess(accesses);
}
};
struct ResourceUsageRecord {
using TagIndex = size_t;
using Count = uint32_t;
constexpr static TagIndex kMaxIndex = std::numeric_limits<TagIndex>::max();
constexpr static Count kMaxCount = std::numeric_limits<Count>::max();
CMD_TYPE command = CMD_NONE;
Count seq_num = 0U;
Count sub_command = 0U;
// This is somewhat repetitive, but it prevents the need for Exec/Submit time touchup, after which usage records can be
// from different command buffers and resets.
const CMD_BUFFER_STATE *cb_state = nullptr; // plain pointer as a shared pointer is held by the context storing this record
Count reset_count;
ResourceUsageRecord() = default;
ResourceUsageRecord(CMD_TYPE command_, Count seq_num_, Count sub_command_, const CMD_BUFFER_STATE *cb_state_,
Count reset_count_)
: command(command_), seq_num(seq_num_), sub_command(sub_command_), cb_state(cb_state_), reset_count(reset_count_) {}
};
// The resource tag index is relative to the command buffer or queue in which it's found
using ResourceUsageTag = ResourceUsageRecord::TagIndex;
using ResourceUsageRange = sparse_container::range<ResourceUsageTag>;
struct HazardResult {
std::unique_ptr<const ResourceAccessState> access_state;
std::unique_ptr<const ResourceFirstAccess> recorded_access;
SyncStageAccessIndex usage_index = std::numeric_limits<SyncStageAccessIndex>::max();
SyncHazard hazard = NONE;
SyncStageAccessFlags prior_access = 0U; // TODO -- change to a NONE enum in ...Bits
ResourceUsageTag tag = ResourceUsageTag();
void Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
const SyncStageAccessFlags &prior_, ResourceUsageTag tag_);
void AddRecordedAccess(const ResourceFirstAccess &first_access);
};
struct SyncExecScope {
VkPipelineStageFlags2KHR mask_param; // the xxxStageMask parameter passed by the caller
VkPipelineStageFlags2KHR
expanded_mask; // all stage bits covered by any 'catch all bits' in the parameter (eg. ALL_GRAPHICS_BIT).
VkPipelineStageFlags2KHR exec_scope; // all earlier or later stages that would be affected by a barrier using this scope.
SyncStageAccessFlags valid_accesses; // all valid accesses that can be used with this scope.
SyncExecScope() : mask_param(0), expanded_mask(0), exec_scope(0), valid_accesses(0) {}
static SyncExecScope MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR src_stage_mask);
static SyncExecScope MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR src_stage_mask);
};
struct SyncBarrier {
SyncExecScope src_exec_scope;
SyncStageAccessFlags src_access_scope;
SyncExecScope dst_exec_scope;
SyncStageAccessFlags dst_access_scope;
SyncBarrier() = default;
SyncBarrier(const SyncBarrier &other) = default;
SyncBarrier &operator=(const SyncBarrier &) = default;
SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst);
template <typename Barrier>
SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst);
SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier);
// template constructor for sync2 barriers
template <typename Barrier>
SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier);
void Merge(const SyncBarrier &other) {
// Note that after merge, only the exec_scope and access_scope fields are fully valid
// TODO: Do we need to update any of the other fields? Merging has limited application.
src_exec_scope.exec_scope |= other.src_exec_scope.exec_scope;
src_access_scope |= other.src_access_scope;
dst_exec_scope.exec_scope |= other.dst_exec_scope.exec_scope;
dst_access_scope |= other.dst_access_scope;
}
};
enum class AccessAddressType : uint32_t { kLinear = 0, kIdealized = 1, kMaxType = 1, kTypeCount = kMaxType + 1 };
struct SyncEventState {
enum IgnoreReason { NotIgnored = 0, ResetWaitRace, Reset2WaitRace, SetRace, MissingStageBits, SetVsWait2 };
using EventPointer = std::shared_ptr<const EVENT_STATE>;
using ScopeMap = sparse_container::range_map<VkDeviceSize, bool>;
EventPointer event;
CMD_TYPE last_command; // Only Event commands are valid here.
ResourceUsageTag last_command_tag; // Needed to filter replay validation
CMD_TYPE unsynchronized_set;
VkPipelineStageFlags2KHR barriers;
SyncExecScope scope;
ResourceUsageTag first_scope_tag;
bool first_scope_set;
bool destroyed;
std::array<ScopeMap, static_cast<size_t>(AccessAddressType::kTypeCount)> first_scope;
template <typename EventPointerType>
SyncEventState(EventPointerType &&event_state)
: event(std::forward<EventPointerType>(event_state)),
last_command(CMD_NONE),
last_command_tag(0),
unsynchronized_set(CMD_NONE),
barriers(0U),
scope(),
first_scope_tag(),
first_scope_set(false),
destroyed((event_state.get() == nullptr) || event_state->Destroyed()) {}
SyncEventState() : SyncEventState(EventPointer()) {}
void ResetFirstScope();
const ScopeMap &FirstScope(AccessAddressType address_type) const { return first_scope[static_cast<size_t>(address_type)]; }
IgnoreReason IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const;
bool HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope) const;
};
using SyncEventStateShared = std::shared_ptr<SyncEventState>;
using SyncEventStateConstShared = std::shared_ptr<const SyncEventState>;
class SyncEventsContext {
public:
using Map = layer_data::unordered_map<const EVENT_STATE *, SyncEventStateShared>;
using iterator = Map::iterator;
using const_iterator = Map::const_iterator;
SyncEventState *GetFromShared(const SyncEventState::EventPointer &event_state) {
const auto find_it = map_.find(event_state.get());
if (find_it == map_.end()) {
if (!event_state.get()) return nullptr;
const auto *event_plain_ptr = event_state.get();
auto sync_state = SyncEventStateShared(new SyncEventState(event_state));
auto insert_pair = map_.emplace(event_plain_ptr, sync_state);
return insert_pair.first->second.get();
}
return find_it->second.get();
}
const SyncEventState *Get(const EVENT_STATE *event_state) const {
const auto find_it = map_.find(event_state);
if (find_it == map_.end()) {
return nullptr;
}
return find_it->second.get();
}
const SyncEventState *Get(const SyncEventState::EventPointer &event_state) const { return Get(event_state.get()); }
void ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst);
// stl style naming for range-for support
inline iterator begin() { return map_.begin(); }
inline const_iterator begin() const { return map_.begin(); }
inline iterator end() { return map_.end(); }
inline const_iterator end() const { return map_.end(); }
void Destroy(const EVENT_STATE *event_state) {
auto sync_it = map_.find(event_state);
if (sync_it != map_.end()) {
sync_it->second->destroyed = true;
map_.erase(sync_it);
}
}
void Clear() { map_.clear(); }
private:
Map map_;
};
struct ResourceFirstAccess {
ResourceUsageTag tag;
SyncStageAccessIndex usage_index;
SyncOrdering ordering_rule;
ResourceFirstAccess(ResourceUsageTag tag_, SyncStageAccessIndex usage_index_, SyncOrdering ordering_rule_)
: tag(tag_), usage_index(usage_index_), ordering_rule(ordering_rule_){};
ResourceFirstAccess(const ResourceFirstAccess &other) = default;
ResourceFirstAccess(ResourceFirstAccess &&other) = default;
ResourceFirstAccess &operator=(const ResourceFirstAccess &rhs) = default;
ResourceFirstAccess &operator=(ResourceFirstAccess &&rhs) = default;
bool operator==(const ResourceFirstAccess &rhs) const {
return (tag == rhs.tag) && (usage_index == rhs.usage_index) && (ordering_rule == rhs.ordering_rule);
}
};
class ResourceAccessState : public SyncStageAccess {
protected:
struct OrderingBarrier {
VkPipelineStageFlags2KHR exec_scope;
SyncStageAccessFlags access_scope;
OrderingBarrier() = default;
OrderingBarrier(VkPipelineStageFlags2KHR es, SyncStageAccessFlags as) : exec_scope(es), access_scope(as) {}
OrderingBarrier &operator=(const OrderingBarrier &) = default;
OrderingBarrier &operator|=(const OrderingBarrier &rhs) {
exec_scope |= rhs.exec_scope;
access_scope |= rhs.access_scope;
return *this;
}
};
using OrderingBarriers = std::array<OrderingBarrier, static_cast<size_t>(SyncOrdering::kNumOrderings)>;
using FirstAccesses = small_vector<ResourceFirstAccess, 3>;
// Mutliple read operations can be simlutaneously (and independently) synchronized,
// given the only the second execution scope creates a dependency chain, we have to track each,
// but only up to one per pipeline stage (as another read from the *same* stage become more recent,
// and applicable one for hazard detection
struct ReadState {
VkPipelineStageFlags2KHR stage; // The stage of this read
SyncStageAccessFlags access; // TODO: Change to FlagBits when we have a None bit enum
// TODO: Revisit whether this needs to support multiple reads per stage
VkPipelineStageFlags2KHR barriers; // all applicable barriered stages
ResourceUsageTag tag;
VkPipelineStageFlags2KHR pending_dep_chain; // Should be zero except during barrier application
// Excluded from comparison
ReadState() = default;
ReadState(VkPipelineStageFlags2KHR stage_, SyncStageAccessFlags access_, VkPipelineStageFlags2KHR barriers_,
ResourceUsageTag tag_)
: stage(stage_), access(access_), barriers(barriers_), tag(tag_), pending_dep_chain(0) {}
bool operator==(const ReadState &rhs) const {
bool same = (stage == rhs.stage) && (access == rhs.access) && (barriers == rhs.barriers) && (tag == rhs.tag);
return same;
}
bool IsReadBarrierHazard(VkPipelineStageFlags2KHR src_exec_scope) const {
// If the read stage is not in the src sync scope
// *AND* not execution chained with an existing sync barrier (that's the or)
// then the barrier access is unsafe (R/W after R)
return (src_exec_scope & (stage | barriers)) == 0;
}
bool operator!=(const ReadState &rhs) const { return !(*this == rhs); }
inline void Set(VkPipelineStageFlags2KHR stage_, const SyncStageAccessFlags &access_, VkPipelineStageFlags2KHR barriers_,
ResourceUsageTag tag_) {
stage = stage_;
access = access_;
barriers = barriers_;
tag = tag_;
pending_dep_chain = 0; // If this is a new read, we aren't applying a barrier set.
}
};
public:
HazardResult DetectHazard(SyncStageAccessIndex usage_index) const;
HazardResult DetectHazard(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) const;
HazardResult DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const;
HazardResult DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const;
HazardResult DetectAsyncHazard(SyncStageAccessIndex usage_index, ResourceUsageTag start_tag) const;
HazardResult DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
ResourceUsageTag start_tag) const;
HazardResult DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR source_exec_scope,
const SyncStageAccessFlags &source_access_scope) const;
HazardResult DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR source_exec_scope,
const SyncStageAccessFlags &source_access_scope, ResourceUsageTag event_tag) const;
void Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, ResourceUsageTag tag);
void SetWrite(const SyncStageAccessFlags &usage_bit, ResourceUsageTag tag);
void Resolve(const ResourceAccessState &other);
void ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition);
void ApplyBarriers(const std::vector<SyncBarrier> &barriers, ResourceUsageTag tag);
void ApplyBarrier(const SyncBarrier &barrier, bool layout_transition);
void ApplyBarrier(ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition);
void ApplyPendingBarriers(ResourceUsageTag tag);
bool FirstAccessInTagRange(const ResourceUsageRange &tag_range) const;
void OffsetTag(ResourceUsageTag offset) {
if (last_write.any()) write_tag += offset;
for (auto &read_access : last_reads) {
read_access.tag += offset;
}
for (auto &first : first_accesses_) {
first.tag += offset;
}
}
ResourceAccessState()
: write_barriers(~SyncStageAccessFlags(0)),
write_dependency_chain(0),
write_tag(),
last_write(0),
input_attachment_read(false),
last_read_stages(0),
read_execution_barriers(0),
pending_write_dep_chain(0),
pending_layout_transition(false),
pending_write_barriers(0),
pending_layout_ordering_(),
first_accesses_(),
first_read_stages_(0U),
first_write_layout_ordering_() {}
bool HasPendingState() const {
return (0 != pending_layout_transition) || pending_write_barriers.any() || (0 != pending_write_dep_chain);
}
bool HasWriteOp() const { return last_write != 0; }
bool operator==(const ResourceAccessState &rhs) const {
bool same = (write_barriers == rhs.write_barriers) && (write_dependency_chain == rhs.write_dependency_chain) &&
(last_reads == rhs.last_reads) && (last_read_stages == rhs.last_read_stages) && (write_tag == rhs.write_tag) &&
(input_attachment_read == rhs.input_attachment_read) &&
(read_execution_barriers == rhs.read_execution_barriers) && (first_accesses_ == rhs.first_accesses_);
return same;
}
bool operator!=(const ResourceAccessState &rhs) const { return !(*this == rhs); }
VkPipelineStageFlags2KHR GetReadBarriers(const SyncStageAccessFlags &usage) const;
SyncStageAccessFlags GetWriteBarriers() const { return write_barriers; }
bool InSourceScopeOrChain(VkPipelineStageFlags2KHR src_exec_scope, SyncStageAccessFlags src_access_scope) const {
return ReadInSourceScopeOrChain(src_exec_scope) || WriteInSourceScopeOrChain(src_exec_scope, src_access_scope);
}
private:
static constexpr VkPipelineStageFlags2KHR kInvalidAttachmentStage = ~VkPipelineStageFlags2KHR(0);
bool IsWriteHazard(SyncStageAccessFlags usage) const { return (usage & ~write_barriers).any(); }
bool IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const;
bool IsWriteBarrierHazard(VkPipelineStageFlags2KHR src_exec_scope, const SyncStageAccessFlags &src_access_scope) const {
// If the previous write is *not* a layout transition
// *AND* is *not* in the 1st access scope
// *AND* the current barrier is not in the dependency chain
// *AND* the there is no prior memory barrier for the previous write in the dependency chain
// then the barrier access is unsafe (R/W after W)
return (last_write != SYNC_IMAGE_LAYOUT_TRANSITION_BIT) && (last_write & src_access_scope).none() &&
(((src_exec_scope & write_dependency_chain) == 0) || (write_barriers & src_access_scope).none());
}
bool ReadInSourceScopeOrChain(VkPipelineStageFlags2KHR src_exec_scope) const {
return (0 != (src_exec_scope & (last_read_stages | read_execution_barriers)));
}
bool WriteInSourceScopeOrChain(VkPipelineStageFlags2KHR src_exec_scope, SyncStageAccessFlags src_access_scope) const {
return (src_access_scope & last_write).any() || (write_dependency_chain & src_exec_scope);
}
static bool IsReadHazard(VkPipelineStageFlags2KHR stage_mask, const VkPipelineStageFlags2KHR barriers) {
return stage_mask != (stage_mask & barriers);
}
bool IsReadHazard(VkPipelineStageFlags2KHR stage_mask, const ReadState &read_access) const {
return IsReadHazard(stage_mask, read_access.barriers);
}
VkPipelineStageFlags2KHR GetOrderedStages(const OrderingBarrier &ordering) const;
void UpdateFirst(ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule);
void TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering);
static const OrderingBarrier &GetOrderingRules(SyncOrdering ordering_enum) {
return kOrderingRules[static_cast<size_t>(ordering_enum)];
}
// TODO: Add a NONE (zero) enum to SyncStageAccessFlags for input_attachment_read and last_write
// With reads, each must be "safe" relative to it's prior write, so we need only
// save the most recent write operation (as anything *transitively* unsafe would arleady
// be included
SyncStageAccessFlags write_barriers; // union of applicable barrier masks since last write
VkPipelineStageFlags2KHR write_dependency_chain; // intiially zero, but accumulating the dstStages of barriers if they chain.
ResourceUsageTag write_tag;
SyncStageAccessFlags last_write; // only the most recent write
// TODO Input Attachment cleanup for multiple reads in a given stage
// Tracks whether the fragment shader read is input attachment read
bool input_attachment_read;
VkPipelineStageFlags2KHR last_read_stages;
VkPipelineStageFlags2KHR read_execution_barriers;
small_vector<ReadState, 3, uint32_t> last_reads;
// Pending execution state to support independent parallel barriers
VkPipelineStageFlags2KHR pending_write_dep_chain;
bool pending_layout_transition;
SyncStageAccessFlags pending_write_barriers;
OrderingBarrier pending_layout_ordering_;
FirstAccesses first_accesses_;
VkPipelineStageFlags2KHR first_read_stages_;
OrderingBarrier first_write_layout_ordering_;
static OrderingBarriers kOrderingRules;
};
using ResourceAccessStateFunction = std::function<void(ResourceAccessState *)>;
using ResourceAccessStateConstFunction = std::function<void(const ResourceAccessState &)>;
using ResourceAccessRangeMap = sparse_container::range_map<VkDeviceSize, ResourceAccessState>;
using ResourceAccessRange = typename ResourceAccessRangeMap::key_type;
using ResourceAccessRangeIndex = typename ResourceAccessRange::index_type;
using ResourceRangeMergeIterator = sparse_container::parallel_iterator<ResourceAccessRangeMap, const ResourceAccessRangeMap>;
class AttachmentViewGen {
public:
enum Gen { kViewSubresource = 0, kRenderArea = 1, kDepthOnlyRenderArea = 2, kStencilOnlyRenderArea = 3, kGenSize = 4 };
AttachmentViewGen(const IMAGE_VIEW_STATE *view_, const VkOffset3D &offset, const VkExtent3D &extent);
AttachmentViewGen(const AttachmentViewGen &other) = default;
AttachmentViewGen(AttachmentViewGen &&other) = default;
AccessAddressType GetAddressType() const;
const IMAGE_VIEW_STATE *GetViewState() const { return view_; }
const ImageRangeGen *GetRangeGen(Gen type) const;
bool IsValid() const { return gen_store_[Gen::kViewSubresource]; }
Gen GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const;
private:
using RangeGenStore = layer_data::optional<ImageRangeGen>;
const IMAGE_VIEW_STATE *view_ = nullptr;
VkImageAspectFlags view_mask_ = 0U;
std::array<RangeGenStore, Gen::kGenSize> gen_store_;
};
using AttachmentViewGenVector = std::vector<AttachmentViewGen>;
using SyncMemoryBarrier = SyncBarrier;
struct SyncBufferMemoryBarrier {
using Buffer = std::shared_ptr<const BUFFER_STATE>;
Buffer buffer;
SyncBarrier barrier;
ResourceAccessRange range;
bool IsLayoutTransition() const { return false; }
const ResourceAccessRange &Range() const { return range; };
const BUFFER_STATE *GetState() const { return buffer.get(); }
SyncBufferMemoryBarrier(const Buffer &buffer_, const SyncBarrier &barrier_, const ResourceAccessRange &range_)
: buffer(buffer_), barrier(barrier_), range(range_) {}
SyncBufferMemoryBarrier() = default;
};
struct SyncImageMemoryBarrier {
using Image = std::shared_ptr<const IMAGE_STATE>;
Image image;
uint32_t index;
SyncBarrier barrier;
VkImageLayout old_layout;
VkImageLayout new_layout;
VkImageSubresourceRange range;
bool IsLayoutTransition() const { return old_layout != new_layout; }
const VkImageSubresourceRange &Range() const { return range; };
const IMAGE_STATE *GetState() const { return image.get(); }
SyncImageMemoryBarrier(const Image &image_, uint32_t index_, const SyncBarrier &barrier_, VkImageLayout old_layout_,
VkImageLayout new_layout_, const VkImageSubresourceRange &subresource_range_)
: image(image_),
index(index_),
barrier(barrier_),
old_layout(old_layout_),
new_layout(new_layout_),
range(subresource_range_) {}
SyncImageMemoryBarrier() = default;
};
class SyncOpBase {
public:
SyncOpBase() : cmd_(CMD_NONE) {}
SyncOpBase(CMD_TYPE cmd) : cmd_(cmd) {}
virtual ~SyncOpBase() = default;
const char *CmdName() const { return CommandTypeString(cmd_); }
virtual bool Validate(const CommandBufferAccessContext &cb_context) const = 0;
virtual ResourceUsageTag Record(CommandBufferAccessContext *cb_context) const = 0;
virtual bool ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const = 0;
virtual void DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const = 0;
protected:
CMD_TYPE cmd_;
};
class SyncOpBarriers : public SyncOpBase {
protected:
template <typename Barriers, typename FunctorFactory>
static void ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, ResourceUsageTag tag,
AccessContext *context);
template <typename Barriers, typename FunctorFactory>
static void ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, ResourceUsageTag tag,
AccessContext *access_context);
SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkPipelineStageFlags srcStageMask,
VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
const VkImageMemoryBarrier *pImageMemoryBarriers);
SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
const VkDependencyInfoKHR *pDependencyInfo);
~SyncOpBarriers() override = default;
protected:
struct BarrierSet {
VkDependencyFlags dependency_flags;
SyncExecScope src_exec_scope;
SyncExecScope dst_exec_scope;
std::vector<SyncMemoryBarrier> memory_barriers;
std::vector<SyncBufferMemoryBarrier> buffer_memory_barriers;
std::vector<SyncImageMemoryBarrier> image_memory_barriers;
bool single_exec_scope;
void MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers);
void MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src, const SyncExecScope &dst,
VkDependencyFlags dependencyFlags, uint32_t bufferMemoryBarrierCount,
const VkBufferMemoryBarrier *pBufferMemoryBarriers);
void MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src, const SyncExecScope &dst,
VkDependencyFlags dependencyFlags, uint32_t imageMemoryBarrierCount,
const VkImageMemoryBarrier *pImageMemoryBarriers);
void MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags, uint32_t barrier_count,
const VkMemoryBarrier2KHR *barriers);
void MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
uint32_t barrier_count, const VkBufferMemoryBarrier2KHR *barriers);
void MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
uint32_t barrier_count, const VkImageMemoryBarrier2KHR *barriers);
};
std::vector<BarrierSet> barriers_;
};
class SyncOpPipelineBarrier : public SyncOpBarriers {
public:
SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
const VkImageMemoryBarrier *pImageMemoryBarriers);
SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
const VkDependencyInfoKHR &pDependencyInfo);
~SyncOpPipelineBarrier() override = default;
bool Validate(const CommandBufferAccessContext &cb_context) const override;
ResourceUsageTag Record(CommandBufferAccessContext *cb_context) const override;
bool ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const override;
void DoRecord(ResourceUsageTag recorded_tag, AccessContext *access_context, SyncEventsContext *events_context) const override;
};
class SyncOpWaitEvents : public SyncOpBarriers {
public:
SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
const VkImageMemoryBarrier *pImageMemoryBarriers);
SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo);
~SyncOpWaitEvents() override = default;
bool Validate(const CommandBufferAccessContext &cb_context) const override;
ResourceUsageTag Record(CommandBufferAccessContext *cb_context) const override;
bool ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const override;
void DoRecord(ResourceUsageTag recorded_tag, AccessContext *access_context, SyncEventsContext *events_context) const override;
protected:
static const char *const kIgnored;
bool DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const;
// TODO PHASE2 This is the wrong thing to use for "replay".. as the event state will have moved on since the record
// TODO PHASE2 May need to capture by value w.r.t. "first use" or build up in calling/enqueue context through replay.
std::vector<std::shared_ptr<const EVENT_STATE>> events_;
void MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events);
};
class SyncOpResetEvent : public SyncOpBase {
public:
SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
VkPipelineStageFlags2KHR stageMask);
~SyncOpResetEvent() override = default;
bool Validate(const CommandBufferAccessContext &cb_context) const override;
ResourceUsageTag Record(CommandBufferAccessContext *cb_context) const override;
bool ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const override;
void DoRecord(ResourceUsageTag recorded_tag, AccessContext *access_context, SyncEventsContext *events_context) const override;
private:
bool DoValidate(const CommandBufferAccessContext& cb_context, const ResourceUsageTag base_tag) const;
std::shared_ptr<const EVENT_STATE> event_;
SyncExecScope exec_scope_;
};
class SyncOpSetEvent : public SyncOpBase {
public:
SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
VkPipelineStageFlags2KHR stageMask);
SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
const VkDependencyInfoKHR &dep_info);
~SyncOpSetEvent() override = default;
bool Validate(const CommandBufferAccessContext &cb_context) const override;
ResourceUsageTag Record(CommandBufferAccessContext *cb_context) const override;
bool ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const override;
private:
bool DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const;
void DoRecord(ResourceUsageTag recorded_tag, AccessContext *access_context, SyncEventsContext *events_context) const override;
std::shared_ptr<const EVENT_STATE> event_;
SyncExecScope src_exec_scope_;
// Note that the dep info is *not* dehandled, but retained for comparison with a future WaitEvents2
std::shared_ptr<safe_VkDependencyInfoKHR> dep_info_;
};
class SyncOpBeginRenderPass : public SyncOpBase {
public:
SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkRenderPassBeginInfo *pRenderPassBegin,
const VkSubpassBeginInfo *pSubpassBeginInfo);
~SyncOpBeginRenderPass() override = default;
bool Validate(const CommandBufferAccessContext &cb_context) const override;
ResourceUsageTag Record(CommandBufferAccessContext *cb_context) const override;
bool ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const override;
void DoRecord(ResourceUsageTag recorded_tag, AccessContext *access_context, SyncEventsContext *events_context) const override;
protected:
safe_VkRenderPassBeginInfo renderpass_begin_info_;
safe_VkSubpassBeginInfo subpass_begin_info_;
std::vector<std::shared_ptr<const IMAGE_VIEW_STATE>> shared_attachments_;
std::vector<const IMAGE_VIEW_STATE *> attachments_;
std::shared_ptr<const RENDER_PASS_STATE> rp_state_;
};
class SyncOpNextSubpass : public SyncOpBase {
public:
SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
const VkSubpassEndInfo *pSubpassEndInfo);
~SyncOpNextSubpass() override = default;
bool Validate(const CommandBufferAccessContext &cb_context) const override;
ResourceUsageTag Record(CommandBufferAccessContext *cb_context) const override;
bool ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const override;
void DoRecord(ResourceUsageTag recorded_tag, AccessContext *access_context, SyncEventsContext *events_context) const override;
protected:
safe_VkSubpassBeginInfo subpass_begin_info_;
safe_VkSubpassEndInfo subpass_end_info_;
};
class SyncOpEndRenderPass : public SyncOpBase {
public:
SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo);
~SyncOpEndRenderPass() override = default;
bool Validate(const CommandBufferAccessContext &cb_context) const override;
ResourceUsageTag Record(CommandBufferAccessContext *cb_context) const override;
bool ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const override;
void DoRecord(ResourceUsageTag recorded_tag, AccessContext *access_context, SyncEventsContext *events_context) const override;
protected:
safe_VkSubpassEndInfo subpass_end_info_;
};
class AccessContext {
public:
enum DetectOptions : uint32_t {
kDetectPrevious = 1U << 0,
kDetectAsync = 1U << 1,
kDetectAll = (kDetectPrevious | kDetectAsync)
};
using MapArray = std::array<ResourceAccessRangeMap, static_cast<size_t>(AccessAddressType::kTypeCount)>;
struct TrackBack {
std::vector<SyncBarrier> barriers;
const AccessContext *context;
TrackBack(const AccessContext *context_, VkQueueFlags queue_flags_,
const std::vector<const VkSubpassDependency2 *> &subpass_dependencies_)
: barriers(), context(context_) {
barriers.reserve(subpass_dependencies_.size());
for (const VkSubpassDependency2 *dependency : subpass_dependencies_) {
assert(dependency);
barriers.emplace_back(queue_flags_, *dependency);
}
}
TrackBack &operator=(const TrackBack &) = default;
TrackBack() = default;
};
HazardResult DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index, const ResourceAccessRange &range) const;
HazardResult DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
const VkExtent3D &extent) const;
template <typename Detector>
HazardResult DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
DetectOptions options) const;
template <typename Detector>
HazardResult DetectHazard(Detector &detector, const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
const VkOffset3D &offset, const VkExtent3D &extent, DetectOptions options) const;
template <typename Detector>
HazardResult DetectHazard(Detector &detector, const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
DetectOptions options) const;
HazardResult DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
const VkImageSubresourceRange &subresource_range) const;
HazardResult DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const;
HazardResult DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
const VkOffset3D &offset, const VkExtent3D &extent) const;
HazardResult DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
const SyncStageAccessFlags &src_access_scope,
const VkImageSubresourceRange &subresource_range, const SyncEventState &sync_event,
DetectOptions options) const;
HazardResult DetectImageBarrierHazard(const AttachmentViewGen &attachment_view, const SyncBarrier &barrier,
DetectOptions options) const;
HazardResult DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
const SyncStageAccessFlags &src_access_scope,
const VkImageSubresourceRange &subresource_range, DetectOptions options) const;
HazardResult DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
const SyncStageAccessFlags &src_stage_accesses,
const VkImageMemoryBarrier &barrier) const;
HazardResult DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const;
HazardResult DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const;
void RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
const AttachmentViewGenVector &attachment_views, ResourceUsageTag tag);
HazardResult DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context) const;
const TrackBack &GetDstExternalTrackBack() const { return dst_external_; }
void Reset() {
prev_.clear();
prev_by_subpass_.clear();
async_.clear();
src_external_ = nullptr;
dst_external_ = TrackBack();
start_tag_ = ResourceUsageTag();
for (auto &map : access_state_maps_) {
map.clear();
}
}
// Follow the context previous to access the access state, supporting "lazy" import into the context. Not intended for
// subpass layout transition, as the pending state handling is more complex
// TODO: See if returning the lower_bound would be useful from a performance POV -- look at the lower_bound overhead
// Would need to add a "hint" overload to parallel_iterator::invalidate_[AB] call, if so.
template <typename BarrierAction>
void ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
const ResourceAccessState *infill_state, const BarrierAction &previous_barrie) const;
void ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
const ResourceAccessState *infill_state,
const ResourceAccessStateFunction *previous_barrier = nullptr) const;
void ResolvePreviousAccesses();
template <typename BarrierAction>
void ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, BarrierAction &barrier_action,
ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state) const;
template <typename BarrierAction>
void ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
bool recur_to_infill = true) const;
void UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
const ResourceAccessRange &range, ResourceUsageTag tag);
void UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag);
void UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset, const VkExtent3D &extent,
ResourceUsageTag tag);
void UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
SyncOrdering ordering_rule, ResourceUsageTag tag);
void UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
const VkImageSubresourceLayers &subresource, const VkOffset3D &offset, const VkExtent3D &extent,
ResourceUsageTag tag);
void UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
uint32_t subpass, ResourceUsageTag tag);
void UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
uint32_t subpass, ResourceUsageTag tag);
void ResolveChildContexts(const std::vector<AccessContext> &contexts);
void ImportAsyncContexts(const AccessContext &from);
template <typename Action, typename RangeGen>
void ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg);
template <typename Action>
void ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action);
template <typename Action>
void ApplyToContext(const Action &barrier_action);
static AccessAddressType ImageAddressType(const IMAGE_STATE &image);
AccessContext(uint32_t subpass, VkQueueFlags queue_flags, const std::vector<SubpassDependencyGraphNode> &dependencies,
const std::vector<AccessContext> &contexts, const AccessContext *external_context);
AccessContext() { Reset(); }
AccessContext(const AccessContext ©_from) = default;
ResourceAccessRangeMap &GetAccessStateMap(AccessAddressType type) { return access_state_maps_[static_cast<size_t>(type)]; }
const ResourceAccessRangeMap &GetAccessStateMap(AccessAddressType type) const {
return access_state_maps_[static_cast<size_t>(type)];
}
ResourceAccessRangeMap &GetLinearMap() { return GetAccessStateMap(AccessAddressType::kLinear); }
const ResourceAccessRangeMap &GetLinearMap() const { return GetAccessStateMap(AccessAddressType::kLinear); }
ResourceAccessRangeMap &GetIdealizedMap() { return GetAccessStateMap(AccessAddressType::kIdealized); }
const ResourceAccessRangeMap &GetIdealizedMap() const { return GetAccessStateMap(AccessAddressType::kIdealized); }
const TrackBack *GetTrackBackFromSubpass(uint32_t subpass) const {
if (subpass == VK_SUBPASS_EXTERNAL) {
return src_external_;
} else {
assert(subpass < prev_by_subpass_.size());
return prev_by_subpass_[subpass];
}
}
bool ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
const VkRect2D &render_area, uint32_t subpass, const AttachmentViewGenVector &attachment_views,
const char *func_name) const;
bool ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
const VkRect2D &render_area, uint32_t subpass, const AttachmentViewGenVector &attachment_views,
const char *func_name) const;
bool ValidateStoreOperation(const CommandExecutionContext &ex_context,
const RENDER_PASS_STATE &rp_state,
const VkRect2D &render_area, uint32_t subpass, const AttachmentViewGenVector &attachment_views,
const char *func_name) const;
bool ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
const char *func_name, uint32_t subpass) const;
void SetStartTag(ResourceUsageTag tag) { start_tag_ = tag; }
template <typename Action>
void ForAll(Action &&action);
private:
template <typename Detector>
HazardResult DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
DetectOptions options) const;
template <typename Detector>
HazardResult DetectAsyncHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range) const;
template <typename Detector>
HazardResult DetectPreviousHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range) const;
void UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
const ResourceAccessRange &range, ResourceUsageTag tag);
MapArray access_state_maps_;
std::vector<TrackBack> prev_;
std::vector<TrackBack *> prev_by_subpass_;
std::vector<const AccessContext *> async_;
TrackBack *src_external_;
TrackBack dst_external_;
ResourceUsageTag start_tag_;
};
class RenderPassAccessContext {
public:
static AttachmentViewGenVector CreateAttachmentViewGen(const VkRect2D &render_area,
const std::vector<const IMAGE_VIEW_STATE *> &attachment_views);
RenderPassAccessContext() : rp_state_(nullptr), render_area_(VkRect2D()), current_subpass_(0) {}
RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area, VkQueueFlags queue_flags,
const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const AccessContext *external_context);
bool ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
const char *func_name) const;
void RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, ResourceUsageTag tag);
bool ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *command_name) const;
bool ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const;
bool ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context, const char *func_name) const;
void RecordLayoutTransitions(ResourceUsageTag tag);
void RecordLoadOperations(ResourceUsageTag tag);
void RecordBeginRenderPass(ResourceUsageTag tag);
void RecordNextSubpass(ResourceUsageTag prev_subpass_tag, ResourceUsageTag next_subpass_tag);
void RecordEndRenderPass(AccessContext *external_context, ResourceUsageTag tag);
AccessContext &CurrentContext() { return subpass_contexts_[current_subpass_]; }
const AccessContext &CurrentContext() const { return subpass_contexts_[current_subpass_]; }
const std::vector<AccessContext> &GetContexts() const { return subpass_contexts_; }
uint32_t GetCurrentSubpass() const { return current_subpass_; }
const RENDER_PASS_STATE *GetRenderPassState() const { return rp_state_; }
AccessContext *CreateStoreResolveProxy() const;
private:
const RENDER_PASS_STATE *rp_state_;
const VkRect2D render_area_;
uint32_t current_subpass_;
std::vector<AccessContext> subpass_contexts_;
AttachmentViewGenVector attachment_views_;
};
// Command execution context is the base class for command buffer and queue contexts
// Preventing unintented leakage of subclass specific state, storing enough information
// for message logging.
// TODO: determine where to draw the design split for tag tracking (is there anything command to Queues and CB's)
class CommandExecutionContext {
public:
CommandExecutionContext() : sync_state_(nullptr) {}
CommandExecutionContext(SyncValidator *sync_validator) : sync_state_(sync_validator) {}
virtual ~CommandExecutionContext() = default;
const SyncValidator &GetSyncState() const {
assert(sync_state_);
return *sync_state_;
}
SyncValidator &GetSyncState() {
assert(sync_state_);
return *sync_state_;
}
virtual std::string FormatUsage(ResourceUsageTag tag) const = 0;
virtual std::string FormatUsage(const ResourceFirstAccess &access) const = 0;
virtual std::string FormatUsage(const HazardResult &hazard) const = 0;
protected:
SyncValidator *sync_state_;
};
class CommandBufferAccessContext : public CommandExecutionContext {
public:
using SyncOpPointer = std::shared_ptr<SyncOpBase>;
struct SyncOpEntry {
ResourceUsageTag tag;
SyncOpPointer sync_op;
SyncOpEntry(ResourceUsageTag tag_, SyncOpPointer &&sync_op_) : tag(tag_), sync_op(std::move(sync_op_)) {}
SyncOpEntry() = default;
SyncOpEntry(const SyncOpEntry &other) = default;
};
CommandBufferAccessContext(SyncValidator *sync_validator = nullptr)
: CommandExecutionContext(sync_validator),
cb_state_(),
queue_flags_(),
destroyed_(false),
access_log_(),
cbs_referenced_(),
command_number_(0),
subcommand_number_(0),
reset_count_(0),
cb_access_context_(),
current_context_(&cb_access_context_),
events_context_(),
render_pass_contexts_(),
current_renderpass_context_(),
sync_ops_() {}