blob: a1b3bdd7435ffb3961173dd0d8bcc46989c5b7f3 [file] [log] [blame]
John Zulauf3d84f1b2020-03-09 13:33:25 -06001/*
John Zulaufab7756b2020-12-29 16:10:16 -07002 * Copyright (c) 2019-2021 Valve Corporation
3 * Copyright (c) 2019-2021 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
John Zulaufab7756b2020-12-29 16:10:16 -070018 * Author: Locke Lin <locke@lunarg.com>
19 * Author: Jeremy Gebben <jeremyg@lunarg.com>
John Zulauf9cb530d2019-09-30 14:14:10 -060020 */
21
22#pragma once
23
John Zulauf7635de32020-05-29 17:14:15 -060024#include <limits>
John Zulauf9cb530d2019-09-30 14:14:10 -060025#include <map>
26#include <memory>
27#include <unordered_map>
28#include <vulkan/vulkan.h>
29
30#include "synchronization_validation_types.h"
31#include "state_tracker.h"
32
John Zulaufd5115702021-01-18 12:34:33 -070033class AccessContext;
John Zulauffaea0ee2021-01-14 14:01:32 -070034class CommandBufferAccessContext;
John Zulaufe7f6a5e2021-01-16 14:31:18 -070035using CommandBufferAccessContextShared = std::shared_ptr<CommandBufferAccessContext>;
John Zulauf64ffe552021-02-06 10:25:07 -070036class CommandExecutionContext;
John Zulaufd5115702021-01-18 12:34:33 -070037class ResourceAccessState;
38class SyncValidator;
John Zulauf355e49b2020-04-24 15:11:15 -060039
John Zulauf2f952d22020-02-10 11:34:51 -070040enum SyncHazard {
41 NONE = 0,
42 READ_AFTER_WRITE,
43 WRITE_AFTER_READ,
44 WRITE_AFTER_WRITE,
45 READ_RACING_WRITE,
46 WRITE_RACING_WRITE,
47 WRITE_RACING_READ,
48};
John Zulauf9cb530d2019-09-30 14:14:10 -060049
John Zulauf8e3c3e92021-01-06 11:19:36 -070050enum class SyncOrdering : uint8_t {
51 kNonAttachment = 0,
52 kColorAttachment = 1,
53 kDepthStencilAttachment = 2,
54 kRaster = 3,
55 kNumOrderings = 4,
56};
57
John Zulauf9cb530d2019-09-30 14:14:10 -060058// Useful Utilites for manipulating StageAccess parameters, suitable as base class to save typing
59struct SyncStageAccess {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -070060 static inline SyncStageAccessFlags FlagBit(SyncStageAccessIndex stage_access) {
John Zulauf9cb530d2019-09-30 14:14:10 -060061 return syncStageAccessInfoByStageAccessIndex[stage_access].stage_access_bit;
62 }
John Zulauf1507ee42020-05-18 11:33:09 -060063 static inline SyncStageAccessFlags Flags(SyncStageAccessIndex stage_access) {
64 return static_cast<SyncStageAccessFlags>(FlagBit(stage_access));
65 }
John Zulauf9cb530d2019-09-30 14:14:10 -060066
Jeremy Gebbend0de1f82020-11-09 08:21:07 -070067 static bool IsRead(const SyncStageAccessFlags &stage_access_bit) { return (stage_access_bit & syncStageAccessReadMask).any(); }
John Zulauf9cb530d2019-09-30 14:14:10 -060068 static bool IsRead(SyncStageAccessIndex stage_access_index) { return IsRead(FlagBit(stage_access_index)); }
69
Jeremy Gebbend0de1f82020-11-09 08:21:07 -070070 static bool IsWrite(const SyncStageAccessFlags &stage_access_bit) {
71 return (stage_access_bit & syncStageAccessWriteMask).any();
72 }
73 static bool HasWrite(const SyncStageAccessFlags &stage_access_mask) {
74 return (stage_access_mask & syncStageAccessWriteMask).any();
75 }
John Zulauf9cb530d2019-09-30 14:14:10 -060076 static bool IsWrite(SyncStageAccessIndex stage_access_index) { return IsWrite(FlagBit(stage_access_index)); }
Jeremy Gebben40a22942020-12-22 14:22:06 -070077 static VkPipelineStageFlags2KHR PipelineStageBit(SyncStageAccessIndex stage_access_index) {
John Zulauf9cb530d2019-09-30 14:14:10 -060078 return syncStageAccessInfoByStageAccessIndex[stage_access_index].stage_mask;
79 }
Jeremy Gebben40a22942020-12-22 14:22:06 -070080 static SyncStageAccessFlags AccessScopeByStage(VkPipelineStageFlags2KHR stages);
81 static SyncStageAccessFlags AccessScopeByAccess(VkAccessFlags2KHR access);
82 static SyncStageAccessFlags AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR access);
83 static SyncStageAccessFlags AccessScope(const SyncStageAccessFlags &stage_scope, VkAccessFlags2KHR accesses) {
John Zulauf9cb530d2019-09-30 14:14:10 -060084 return stage_scope & AccessScopeByAccess(accesses);
85 }
86};
87
John Zulauffaea0ee2021-01-14 14:01:32 -070088// The resource tag is relative to the command buffer or queue in which it's found
John Zulauf5f13a792020-03-10 07:31:21 -060089struct ResourceUsageTag {
John Zulauff4aecca2021-01-05 16:21:58 -070090 using TagIndex = uint64_t;
John Zulauffaea0ee2021-01-14 14:01:32 -070091 using Count = uint32_t;
John Zulauff4aecca2021-01-05 16:21:58 -070092 constexpr static TagIndex kMaxIndex = std::numeric_limits<TagIndex>::max();
John Zulauffaea0ee2021-01-14 14:01:32 -070093 constexpr static uint32_t kMaxCount = std::numeric_limits<Count>::max();
Jeremy Gebben4bb73502020-12-14 11:17:50 -070094
John Zulauffaea0ee2021-01-14 14:01:32 -070095 TagIndex index = 0U; // the index of the command within the command buffer itself (primary or secondary)
96 CMD_TYPE command = CMD_NONE;
97 Count seq_num = 0U;
98 Count sub_command = 0U;
Jeremy Gebben4bb73502020-12-14 11:17:50 -070099
John Zulauffaea0ee2021-01-14 14:01:32 -0700100 bool operator<(const ResourceUsageTag &rhs) const { return index < rhs.index; }
John Zulauf5f13a792020-03-10 07:31:21 -0600101 bool IsBefore(const ResourceUsageTag &rhs) const { return index < rhs.index; }
John Zulauffaea0ee2021-01-14 14:01:32 -0700102 bool operator==(const ResourceUsageTag &rhs) const { return (index == rhs.index); }
John Zulaufe5da6e52020-03-18 15:32:18 -0600103 bool operator!=(const ResourceUsageTag &rhs) const { return !(*this == rhs); }
Jeremy Gebben4bb73502020-12-14 11:17:50 -0700104
John Zulauffaea0ee2021-01-14 14:01:32 -0700105 ResourceUsageTag() = default;
106 ResourceUsageTag(TagIndex index_, Count seq_num_, Count sub_command_, CMD_TYPE command_)
107 : index(index_), command(command_), seq_num(seq_num_), sub_command(sub_command_) {}
John Zulauf5f13a792020-03-10 07:31:21 -0600108};
109
John Zulauf9cb530d2019-09-30 14:14:10 -0600110struct HazardResult {
John Zulauf59e25072020-07-17 10:55:21 -0600111 std::unique_ptr<const ResourceAccessState> access_state;
112 SyncStageAccessIndex usage_index = std::numeric_limits<SyncStageAccessIndex>::max();
John Zulauf9cb530d2019-09-30 14:14:10 -0600113 SyncHazard hazard = NONE;
John Zulauf37ceaed2020-07-03 16:18:15 -0600114 SyncStageAccessFlags prior_access = 0U; // TODO -- change to a NONE enum in ...Bits
John Zulauf9cb530d2019-09-30 14:14:10 -0600115 ResourceUsageTag tag = ResourceUsageTag();
John Zulauf59e25072020-07-17 10:55:21 -0600116 void Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700117 const SyncStageAccessFlags &prior_, const ResourceUsageTag &tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -0600118};
119
Jeremy Gebben9893daf2021-01-04 10:40:50 -0700120struct SyncExecScope {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700121 VkPipelineStageFlags2KHR mask_param; // the xxxStageMask parameter passed by the caller
122 VkPipelineStageFlags2KHR
123 expanded_mask; // all stage bits covered by any 'catch all bits' in the parameter (eg. ALL_GRAPHICS_BIT).
124 VkPipelineStageFlags2KHR exec_scope; // all earlier or later stages that would be affected by a barrier using this scope.
Jeremy Gebben9893daf2021-01-04 10:40:50 -0700125 SyncStageAccessFlags valid_accesses; // all valid accesses that can be used with this scope.
126
127 SyncExecScope() : mask_param(0), expanded_mask(0), exec_scope(0), valid_accesses(0) {}
128
Jeremy Gebben40a22942020-12-22 14:22:06 -0700129 static SyncExecScope MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR src_stage_mask);
130 static SyncExecScope MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR src_stage_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -0700131};
132
John Zulauf3d84f1b2020-03-09 13:33:25 -0600133struct SyncBarrier {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700134 VkPipelineStageFlags2KHR src_exec_scope;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600135 SyncStageAccessFlags src_access_scope;
Jeremy Gebben40a22942020-12-22 14:22:06 -0700136 VkPipelineStageFlags2KHR dst_exec_scope;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600137 SyncStageAccessFlags dst_access_scope;
138 SyncBarrier() = default;
Jeremy Gebben9893daf2021-01-04 10:40:50 -0700139 SyncBarrier(const SyncBarrier &other) = default;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600140 SyncBarrier &operator=(const SyncBarrier &) = default;
Jeremy Gebben9893daf2021-01-04 10:40:50 -0700141
142 SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst);
143
144 template <typename Barrier>
145 SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst);
146
147 SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &barrier);
148
John Zulaufa0a98292020-09-18 09:30:10 -0600149 void Merge(const SyncBarrier &other) {
150 src_exec_scope |= other.src_exec_scope;
151 src_access_scope |= other.src_access_scope;
152 dst_exec_scope |= other.dst_exec_scope;
153 dst_access_scope |= other.dst_access_scope;
154 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600155};
John Zulauf69133422020-05-20 14:55:53 -0600156
John Zulauf43cc7462020-12-03 12:33:12 -0700157enum class AccessAddressType : uint32_t { kLinear = 0, kIdealized = 1, kMaxType = 1, kTypeCount = kMaxType + 1 };
158
John Zulauf4a6105a2020-11-17 15:11:05 -0700159struct SyncEventState {
160 enum IgnoreReason { NotIgnored = 0, ResetWaitRace, SetRace, MissingStageBits };
John Zulauf669dfd52021-01-27 17:15:28 -0700161 using EventPointer = std::shared_ptr<const EVENT_STATE>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700162 using ScopeMap = sparse_container::range_map<VkDeviceSize, bool>;
163 EventPointer event;
164 CMD_TYPE last_command; // Only Event commands are valid here.
165 CMD_TYPE unsynchronized_set;
Jeremy Gebben40a22942020-12-22 14:22:06 -0700166 VkPipelineStageFlags2KHR barriers;
Jeremy Gebben9893daf2021-01-04 10:40:50 -0700167 SyncExecScope scope;
John Zulauf4a6105a2020-11-17 15:11:05 -0700168 ResourceUsageTag first_scope_tag;
John Zulaufd5115702021-01-18 12:34:33 -0700169 bool destroyed;
John Zulauf4a6105a2020-11-17 15:11:05 -0700170 std::array<ScopeMap, static_cast<size_t>(AccessAddressType::kTypeCount)> first_scope;
John Zulauf669dfd52021-01-27 17:15:28 -0700171 template <typename EventPointerType>
172 SyncEventState(EventPointerType &&event_state)
173 : event(std::forward<EventPointerType>(event_state)),
John Zulaufd5115702021-01-18 12:34:33 -0700174 last_command(CMD_NONE),
175 unsynchronized_set(CMD_NONE),
176 barriers(0U),
177 scope(),
178 first_scope_tag(),
179 destroyed((event_state.get() == nullptr) || event_state->destroyed) {}
John Zulauf4a6105a2020-11-17 15:11:05 -0700180 SyncEventState() : SyncEventState(EventPointer()) {}
181 void ResetFirstScope();
182 const ScopeMap &FirstScope(AccessAddressType address_type) const { return first_scope[static_cast<size_t>(address_type)]; }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700183 IgnoreReason IsIgnoredByWait(VkPipelineStageFlags2KHR srcStageMask) const;
184 bool HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope) const;
John Zulauf4a6105a2020-11-17 15:11:05 -0700185};
John Zulaufd5115702021-01-18 12:34:33 -0700186using SyncEventStateShared = std::shared_ptr<SyncEventState>;
187using SyncEventStateConstShared = std::shared_ptr<const SyncEventState>;
John Zulauf669dfd52021-01-27 17:15:28 -0700188class SyncEventsContext {
189 public:
190 using Map = std::unordered_map<const EVENT_STATE *, SyncEventStateShared>;
191 using iterator = Map::iterator;
192 using const_iterator = Map::const_iterator;
193
194 SyncEventState *GetFromShared(const SyncEventState::EventPointer &event_state) {
195 const auto find_it = map_.find(event_state.get());
196 if (find_it == map_.end()) {
John Zulauf6ce24372021-01-30 05:56:25 -0700197 if (!event_state.get()) return nullptr;
198
John Zulauf669dfd52021-01-27 17:15:28 -0700199 const auto *event_plain_ptr = event_state.get();
200 auto sync_state = SyncEventStateShared(new SyncEventState(event_state));
201 auto insert_pair = map_.insert(std::make_pair(event_plain_ptr, std::move(sync_state)));
202 return insert_pair.first->second.get();
203 }
204 return find_it->second.get();
205 }
206
207 const SyncEventState *Get(const EVENT_STATE *event_state) const {
208 const auto find_it = map_.find(event_state);
209 if (find_it == map_.end()) {
210 return nullptr;
211 }
212 return find_it->second.get();
213 }
John Zulauf6ce24372021-01-30 05:56:25 -0700214 const SyncEventState *Get(const SyncEventState::EventPointer &event_state) const { return Get(event_state.get()); }
John Zulauf669dfd52021-01-27 17:15:28 -0700215
216 // stl style naming for range-for support
217 inline iterator begin() { return map_.begin(); }
218 inline const_iterator begin() const { return map_.begin(); }
219 inline iterator end() { return map_.end(); }
220 inline const_iterator end() const { return map_.end(); }
221
222 void Destroy(const EVENT_STATE *event_state) {
223 auto sync_it = map_.find(event_state);
224 if (sync_it != map_.end()) {
225 sync_it->second->destroyed = true;
226 map_.erase(sync_it);
227 }
228 }
229 void Clear() { map_.clear(); }
230
231 private:
232 Map map_;
233};
John Zulauf4a6105a2020-11-17 15:11:05 -0700234
John Zulauf69133422020-05-20 14:55:53 -0600235// To represent ordering guarantees such as rasterization and store
John Zulauf3d84f1b2020-03-09 13:33:25 -0600236
John Zulauf9cb530d2019-09-30 14:14:10 -0600237class ResourceAccessState : public SyncStageAccess {
238 protected:
John Zulauf8e3c3e92021-01-06 11:19:36 -0700239 struct OrderingBarrier {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700240 VkPipelineStageFlags2KHR exec_scope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700241 SyncStageAccessFlags access_scope;
242 OrderingBarrier() = default;
Jeremy Gebben40a22942020-12-22 14:22:06 -0700243 OrderingBarrier(VkPipelineStageFlags2KHR es, SyncStageAccessFlags as) : exec_scope(es), access_scope(as) {}
John Zulauf8e3c3e92021-01-06 11:19:36 -0700244 OrderingBarrier &operator=(const OrderingBarrier &) = default;
245 };
246 using OrderingBarriers = std::array<OrderingBarrier, static_cast<size_t>(SyncOrdering::kNumOrderings)>;
247
John Zulauffaea0ee2021-01-14 14:01:32 -0700248 struct FirstAccess {
249 ResourceUsageTag tag;
250 SyncStageAccessIndex usage_index;
251 SyncOrdering ordering_rule;
252 FirstAccess(const ResourceUsageTag &tag_, SyncStageAccessIndex usage_index_, SyncOrdering ordering_rule_)
253 : tag(tag_), usage_index(usage_index_), ordering_rule(ordering_rule_){};
254 FirstAccess(const FirstAccess &other) = default;
255 FirstAccess(FirstAccess &&other) = default;
256 FirstAccess &operator=(const FirstAccess &rhs) = default;
257 FirstAccess &operator=(FirstAccess &&rhs) = default;
258
259 bool operator==(const FirstAccess &rhs) const {
260 return (tag == rhs.tag) && (usage_index == rhs.usage_index) && (ordering_rule == rhs.ordering_rule);
261 }
262 };
263 using FirstAccesses = small_vector<FirstAccess, 3>;
264
John Zulauf9cb530d2019-09-30 14:14:10 -0600265 // Mutliple read operations can be simlutaneously (and independently) synchronized,
266 // given the only the second execution scope creates a dependency chain, we have to track each,
267 // but only up to one per pipeline stage (as another read from the *same* stage become more recent,
268 // and applicable one for hazard detection
269 struct ReadState {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700270 VkPipelineStageFlags2KHR stage; // The stage of this read
John Zulauf37ceaed2020-07-03 16:18:15 -0600271 SyncStageAccessFlags access; // TODO: Change to FlagBits when we have a None bit enum
John Zulauff51fbb62020-10-02 14:43:24 -0600272 // TODO: Revisit whether this needs to support multiple reads per stage
Jeremy Gebben40a22942020-12-22 14:22:06 -0700273 VkPipelineStageFlags2KHR barriers; // all applicable barriered stages
John Zulauf9cb530d2019-09-30 14:14:10 -0600274 ResourceUsageTag tag;
Jeremy Gebben40a22942020-12-22 14:22:06 -0700275 VkPipelineStageFlags2KHR pending_dep_chain; // Should be zero except during barrier application
276 // Excluded from comparison
John Zulauf89311b42020-09-29 16:28:47 -0600277 ReadState() = default;
Jeremy Gebben40a22942020-12-22 14:22:06 -0700278 ReadState(VkPipelineStageFlags2KHR stage_, SyncStageAccessFlags access_, VkPipelineStageFlags2KHR barriers_,
John Zulaufab7756b2020-12-29 16:10:16 -0700279 const ResourceUsageTag &tag_)
280 : stage(stage_), access(access_), barriers(barriers_), tag(tag_), pending_dep_chain(0) {}
John Zulaufe5da6e52020-03-18 15:32:18 -0600281 bool operator==(const ReadState &rhs) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600282 bool same = (stage == rhs.stage) && (access == rhs.access) && (barriers == rhs.barriers) && (tag == rhs.tag);
John Zulaufe5da6e52020-03-18 15:32:18 -0600283 return same;
284 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700285 bool IsReadBarrierHazard(VkPipelineStageFlags2KHR src_exec_scope) const {
John Zulauf4a6105a2020-11-17 15:11:05 -0700286 // If the read stage is not in the src sync scope
287 // *AND* not execution chained with an existing sync barrier (that's the or)
288 // then the barrier access is unsafe (R/W after R)
289 return (src_exec_scope & (stage | barriers)) == 0;
290 }
291
John Zulaufe5da6e52020-03-18 15:32:18 -0600292 bool operator!=(const ReadState &rhs) const { return !(*this == rhs); }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700293 inline void Set(VkPipelineStageFlags2KHR stage_, const SyncStageAccessFlags &access_, VkPipelineStageFlags2KHR barriers_,
John Zulauf4285ee92020-09-23 10:20:52 -0600294 const ResourceUsageTag &tag_) {
295 stage = stage_;
296 access = access_;
297 barriers = barriers_;
298 tag = tag_;
John Zulauf89311b42020-09-29 16:28:47 -0600299 pending_dep_chain = 0; // If this is a new read, we aren't applying a barrier set.
John Zulauf4285ee92020-09-23 10:20:52 -0600300 }
John Zulauf9cb530d2019-09-30 14:14:10 -0600301 };
302
303 public:
304 HazardResult DetectHazard(SyncStageAccessIndex usage_index) const;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700305 HazardResult DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600306
Jeremy Gebben40a22942020-12-22 14:22:06 -0700307 HazardResult DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR source_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700308 const SyncStageAccessFlags &source_access_scope) const;
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700309 HazardResult DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const;
Jeremy Gebben40a22942020-12-22 14:22:06 -0700310 HazardResult DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR source_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -0700311 const SyncStageAccessFlags &source_access_scope, const ResourceUsageTag &event_tag) const;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600312
John Zulauf8e3c3e92021-01-06 11:19:36 -0700313 void Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700314 void SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag);
John Zulauf5f13a792020-03-10 07:31:21 -0600315 void Resolve(const ResourceAccessState &other);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600316 void ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition);
317 void ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag);
John Zulauf89311b42020-09-29 16:28:47 -0600318 void ApplyBarrier(const SyncBarrier &barrier, bool layout_transition);
John Zulauf4a6105a2020-11-17 15:11:05 -0700319 void ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition);
John Zulauf89311b42020-09-29 16:28:47 -0600320 void ApplyPendingBarriers(const ResourceUsageTag &tag);
John Zulauf9cb530d2019-09-30 14:14:10 -0600321
322 ResourceAccessState()
John Zulauf355e49b2020-04-24 15:11:15 -0600323 : write_barriers(~SyncStageAccessFlags(0)),
324 write_dependency_chain(0),
John Zulauf355e49b2020-04-24 15:11:15 -0600325 write_tag(),
John Zulaufd14743a2020-07-03 09:42:39 -0600326 last_write(0),
John Zulauff51fbb62020-10-02 14:43:24 -0600327 input_attachment_read(false),
John Zulauf361fb532020-07-22 10:45:39 -0600328 last_read_stages(0),
John Zulauf89311b42020-09-29 16:28:47 -0600329 read_execution_barriers(0),
330 pending_write_dep_chain(0),
331 pending_layout_transition(false),
John Zulauffaea0ee2021-01-14 14:01:32 -0700332 pending_write_barriers(0),
333 first_accesses_(),
334 first_read_stages_(0U) {}
John Zulauf9cb530d2019-09-30 14:14:10 -0600335
John Zulaufb02c1eb2020-10-06 16:33:36 -0600336 bool HasPendingState() const {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700337 return (0 != pending_layout_transition) || pending_write_barriers.any() || (0 != pending_write_dep_chain);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600338 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600339 bool HasWriteOp() const { return last_write != 0; }
John Zulaufe5da6e52020-03-18 15:32:18 -0600340 bool operator==(const ResourceAccessState &rhs) const {
341 bool same = (write_barriers == rhs.write_barriers) && (write_dependency_chain == rhs.write_dependency_chain) &&
John Zulaufab7756b2020-12-29 16:10:16 -0700342 (last_reads == rhs.last_reads) && (last_read_stages == rhs.last_read_stages) && (write_tag == rhs.write_tag) &&
343 (input_attachment_read == rhs.input_attachment_read) &&
John Zulauffaea0ee2021-01-14 14:01:32 -0700344 (read_execution_barriers == rhs.read_execution_barriers) && (first_accesses_ == rhs.first_accesses_);
John Zulaufe5da6e52020-03-18 15:32:18 -0600345 return same;
346 }
347 bool operator!=(const ResourceAccessState &rhs) const { return !(*this == rhs); }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700348 VkPipelineStageFlags2KHR GetReadBarriers(const SyncStageAccessFlags &usage) const;
John Zulauf59e25072020-07-17 10:55:21 -0600349 SyncStageAccessFlags GetWriteBarriers() const { return write_barriers; }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700350 bool InSourceScopeOrChain(VkPipelineStageFlags2KHR src_exec_scope, SyncStageAccessFlags src_access_scope) const {
John Zulauf4a6105a2020-11-17 15:11:05 -0700351 return ReadInSourceScopeOrChain(src_exec_scope) || WriteInSourceScopeOrChain(src_exec_scope, src_access_scope);
352 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600353
John Zulauf9cb530d2019-09-30 14:14:10 -0600354 private:
Jeremy Gebben40a22942020-12-22 14:22:06 -0700355 static constexpr VkPipelineStageFlags2KHR kInvalidAttachmentStage = ~VkPipelineStageFlags2KHR(0);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700356 bool IsWriteHazard(SyncStageAccessFlags usage) const { return (usage & ~write_barriers).any(); }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700357 bool IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const;
358 bool IsWriteBarrierHazard(VkPipelineStageFlags2KHR src_exec_scope, const SyncStageAccessFlags &src_access_scope) const {
John Zulauf4a6105a2020-11-17 15:11:05 -0700359 // If the previous write is *not* in the 1st access scope
360 // *AND* the current barrier is not in the dependency chain
361 // *AND* the there is no prior memory barrier for the previous write in the dependency chain
362 // then the barrier access is unsafe (R/W after W)
363 return ((last_write & src_access_scope) == 0) && ((src_exec_scope & write_dependency_chain) == 0) && (write_barriers == 0);
364 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700365 bool ReadInSourceScopeOrChain(VkPipelineStageFlags2KHR src_exec_scope) const {
John Zulauf4a6105a2020-11-17 15:11:05 -0700366 return (0 != (src_exec_scope & (last_read_stages | read_execution_barriers)));
367 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700368 bool WriteInSourceScopeOrChain(VkPipelineStageFlags2KHR src_exec_scope, SyncStageAccessFlags src_access_scope) const {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700369 return (src_access_scope & last_write).any() || (write_dependency_chain & src_exec_scope);
John Zulaufa0a98292020-09-18 09:30:10 -0600370 }
John Zulaufd14743a2020-07-03 09:42:39 -0600371
Jeremy Gebben40a22942020-12-22 14:22:06 -0700372 static bool IsReadHazard(VkPipelineStageFlags2KHR stage_mask, const VkPipelineStageFlags2KHR barriers) {
John Zulaufd14743a2020-07-03 09:42:39 -0600373 return stage_mask != (stage_mask & barriers);
374 }
375
Jeremy Gebben40a22942020-12-22 14:22:06 -0700376 bool IsReadHazard(VkPipelineStageFlags2KHR stage_mask, const ReadState &read_access) const {
John Zulaufd14743a2020-07-03 09:42:39 -0600377 return IsReadHazard(stage_mask, read_access.barriers);
John Zulauf0cb5be22020-01-23 12:18:22 -0700378 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700379 VkPipelineStageFlags2KHR GetOrderedStages(const OrderingBarrier &ordering) const;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700380
John Zulauffaea0ee2021-01-14 14:01:32 -0700381 void UpdateFirst(const ResourceUsageTag &tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule);
382
John Zulauf8e3c3e92021-01-06 11:19:36 -0700383 static const OrderingBarrier &GetOrderingRules(SyncOrdering ordering_enum) {
384 return kOrderingRules[static_cast<size_t>(ordering_enum)];
385 }
John Zulaufd14743a2020-07-03 09:42:39 -0600386
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700387 // TODO: Add a NONE (zero) enum to SyncStageAccessFlags for input_attachment_read and last_write
John Zulaufd14743a2020-07-03 09:42:39 -0600388
John Zulauf9cb530d2019-09-30 14:14:10 -0600389 // With reads, each must be "safe" relative to it's prior write, so we need only
390 // save the most recent write operation (as anything *transitively* unsafe would arleady
391 // be included
392 SyncStageAccessFlags write_barriers; // union of applicable barrier masks since last write
Jeremy Gebben40a22942020-12-22 14:22:06 -0700393 VkPipelineStageFlags2KHR write_dependency_chain; // intiially zero, but accumulating the dstStages of barriers if they chain.
John Zulauf9cb530d2019-09-30 14:14:10 -0600394 ResourceUsageTag write_tag;
John Zulauf355e49b2020-04-24 15:11:15 -0600395 SyncStageAccessFlags last_write; // only the most recent write
John Zulauf9cb530d2019-09-30 14:14:10 -0600396
John Zulauff51fbb62020-10-02 14:43:24 -0600397 // TODO Input Attachment cleanup for multiple reads in a given stage
398 // Tracks whether the fragment shader read is input attachment read
399 bool input_attachment_read;
John Zulaufd14743a2020-07-03 09:42:39 -0600400
Jeremy Gebben40a22942020-12-22 14:22:06 -0700401 VkPipelineStageFlags2KHR last_read_stages;
402 VkPipelineStageFlags2KHR read_execution_barriers;
John Zulaufab7756b2020-12-29 16:10:16 -0700403 small_vector<ReadState, 3> last_reads;
John Zulauf89311b42020-09-29 16:28:47 -0600404
405 // Pending execution state to support independent parallel barriers
Jeremy Gebben40a22942020-12-22 14:22:06 -0700406 VkPipelineStageFlags2KHR pending_write_dep_chain;
John Zulauf89311b42020-09-29 16:28:47 -0600407 bool pending_layout_transition;
408 SyncStageAccessFlags pending_write_barriers;
John Zulauffaea0ee2021-01-14 14:01:32 -0700409 FirstAccesses first_accesses_;
Jeremy Gebben40a22942020-12-22 14:22:06 -0700410 VkPipelineStageFlags2KHR first_read_stages_;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700411
412 static OrderingBarriers kOrderingRules;
John Zulauf9cb530d2019-09-30 14:14:10 -0600413};
414
John Zulauf16adfc92020-04-08 10:28:33 -0600415using ResourceAccessRangeMap = sparse_container::range_map<VkDeviceSize, ResourceAccessState>;
John Zulauf5c5e88d2019-12-26 11:22:02 -0700416using ResourceAccessRange = typename ResourceAccessRangeMap::key_type;
John Zulauf355e49b2020-04-24 15:11:15 -0600417using ResourceRangeMergeIterator = sparse_container::parallel_iterator<ResourceAccessRangeMap, const ResourceAccessRangeMap>;
John Zulauf9cb530d2019-09-30 14:14:10 -0600418
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700419using SyncMemoryBarrier = SyncBarrier;
420struct SyncBufferMemoryBarrier {
421 using Buffer = std::shared_ptr<const BUFFER_STATE>;
422 Buffer buffer;
423 SyncBarrier barrier;
424 ResourceAccessRange range;
John Zulaufd5115702021-01-18 12:34:33 -0700425 bool IsLayoutTransition() const { return false; }
426 const ResourceAccessRange &Range() const { return range; };
427 const BUFFER_STATE *GetState() const { return buffer.get(); }
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700428 SyncBufferMemoryBarrier(const Buffer &buffer_, const SyncBarrier &barrier_, const ResourceAccessRange &range_)
429 : buffer(buffer_), barrier(barrier_), range(range_) {}
430 SyncBufferMemoryBarrier() = default;
431};
432
433struct SyncImageMemoryBarrier {
434 using Image = std::shared_ptr<const IMAGE_STATE>;
John Zulaufd5115702021-01-18 12:34:33 -0700435 struct SubImageRange {
436 VkImageSubresourceRange subresource_range;
437 VkOffset3D offset;
438 VkExtent3D extent;
439 };
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700440 Image image;
441 uint32_t index;
442 SyncBarrier barrier;
443 VkImageLayout old_layout;
444 VkImageLayout new_layout;
John Zulaufd5115702021-01-18 12:34:33 -0700445 SubImageRange range;
446
447 bool IsLayoutTransition() const { return old_layout != new_layout; }
448 const SubImageRange &Range() const { return range; };
449 const IMAGE_STATE *GetState() const { return image.get(); }
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700450 SyncImageMemoryBarrier(const Image &image_, uint32_t index_, const SyncBarrier &barrier_, VkImageLayout old_layout_,
451 VkImageLayout new_layout_, const VkImageSubresourceRange &subresource_range_)
452 : image(image_),
453 index(index_),
454 barrier(barrier_),
455 old_layout(old_layout_),
456 new_layout(new_layout_),
John Zulaufd5115702021-01-18 12:34:33 -0700457 range({subresource_range_, VkOffset3D{0, 0, 0}, image->createInfo.extent}) {}
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700458 SyncImageMemoryBarrier() = default;
459};
460
461class SyncOpBase {
462 public:
John Zulauf64ffe552021-02-06 10:25:07 -0700463 SyncOpBase() : cmd_(CMD_NONE), name_override_(nullptr) {}
464 SyncOpBase(CMD_TYPE cmd, const char *name_override = nullptr) : cmd_(cmd), name_override_(name_override) {}
465 const char *CmdName() const { return name_override_ ? name_override_ : CommandTypeString(cmd_); }
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700466 virtual bool Validate(const CommandBufferAccessContext &cb_context) const = 0;
John Zulauf36ef9282021-02-02 11:47:24 -0700467 virtual void Record(CommandBufferAccessContext *cb_context) const = 0;
468
469 protected:
470 CMD_TYPE cmd_;
John Zulauf64ffe552021-02-06 10:25:07 -0700471 // Some promoted commands alias CMD_TYPE for KHR and non-KHR versions, also callers to preserve the cmd name as needed
472 const char *name_override_;
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700473};
474
John Zulaufd5115702021-01-18 12:34:33 -0700475class SyncOpBarriers : public SyncOpBase {
476 protected:
477 template <typename Barriers, typename FunctorFactory>
478 static void ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
479 AccessContext *context);
480 template <typename Barriers, typename FunctorFactory>
481 static void ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
482 AccessContext *access_context);
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700483
John Zulauf36ef9282021-02-02 11:47:24 -0700484 SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkPipelineStageFlags srcStageMask,
John Zulaufd5115702021-01-18 12:34:33 -0700485 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
486 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
487 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
488 const VkImageMemoryBarrier *pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700489
490 protected:
491 void MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
492 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers);
493 void MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src, const SyncExecScope &dst,
494 VkDependencyFlags dependencyFlags, uint32_t bufferMemoryBarrierCount,
495 const VkBufferMemoryBarrier *pBufferMemoryBarriers);
496 void MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src, const SyncExecScope &dst,
497 VkDependencyFlags dependencyFlags, uint32_t imageMemoryBarrierCount,
498 const VkImageMemoryBarrier *pImageMemoryBarriers);
499
500 VkDependencyFlags dependency_flags_;
501 SyncExecScope src_exec_scope_;
502 SyncExecScope dst_exec_scope_;
503 std::vector<SyncMemoryBarrier> memory_barriers_;
504 std::vector<SyncBufferMemoryBarrier> buffer_memory_barriers_;
505 std::vector<SyncImageMemoryBarrier> image_memory_barriers_;
506};
507
John Zulaufd5115702021-01-18 12:34:33 -0700508class SyncOpPipelineBarrier : public SyncOpBarriers {
509 public:
John Zulauf36ef9282021-02-02 11:47:24 -0700510 SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
511 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
512 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -0700513 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
514 const VkImageMemoryBarrier *pImageMemoryBarriers);
515 bool Validate(const CommandBufferAccessContext &cb_context) const override;
John Zulauf36ef9282021-02-02 11:47:24 -0700516 void Record(CommandBufferAccessContext *cb_context) const override;
John Zulaufd5115702021-01-18 12:34:33 -0700517};
518
519class SyncOpWaitEvents : public SyncOpBarriers {
520 public:
John Zulauf36ef9282021-02-02 11:47:24 -0700521 SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
522 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
523 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -0700524 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
525 const VkImageMemoryBarrier *pImageMemoryBarriers);
526 bool Validate(const CommandBufferAccessContext &cb_context) const override;
John Zulauf36ef9282021-02-02 11:47:24 -0700527 void Record(CommandBufferAccessContext *cb_context) const override;
John Zulaufd5115702021-01-18 12:34:33 -0700528
529 protected:
530 // TODO PHASE2 This is the wrong thing to use for "replay".. as the event state will have moved on since the record
531 // TODO PHASE2 May need to capture by value w.r.t. "first use" or build up in calling/enqueue context through replay.
John Zulauf669dfd52021-01-27 17:15:28 -0700532 std::vector<std::shared_ptr<const EVENT_STATE>> events_;
533 void MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events);
John Zulaufd5115702021-01-18 12:34:33 -0700534};
535
John Zulauf6ce24372021-01-30 05:56:25 -0700536class SyncOpResetEvent : public SyncOpBase {
537 public:
John Zulauf36ef9282021-02-02 11:47:24 -0700538 SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
539 VkPipelineStageFlags stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -0700540 bool Validate(const CommandBufferAccessContext &cb_context) const override;
John Zulauf36ef9282021-02-02 11:47:24 -0700541 void Record(CommandBufferAccessContext *cb_context) const override;
John Zulauf6ce24372021-01-30 05:56:25 -0700542
543 private:
544 std::shared_ptr<const EVENT_STATE> event_;
545 SyncExecScope exec_scope_;
546};
547
548class SyncOpSetEvent : public SyncOpBase {
549 public:
John Zulauf36ef9282021-02-02 11:47:24 -0700550 SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
551 VkPipelineStageFlags stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -0700552 bool Validate(const CommandBufferAccessContext &cb_context) const override;
John Zulauf36ef9282021-02-02 11:47:24 -0700553 void Record(CommandBufferAccessContext *cb_context) const override;
John Zulauf6ce24372021-01-30 05:56:25 -0700554
555 private:
556 std::shared_ptr<const EVENT_STATE> event_;
557 SyncExecScope src_exec_scope_;
558};
John Zulauf64ffe552021-02-06 10:25:07 -0700559
560class SyncOpBeginRenderPass : public SyncOpBase {
561 public:
562 SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkRenderPassBeginInfo *pRenderPassBegin,
563 const VkSubpassBeginInfo *pSubpassBeginInfo, const char *command_name = nullptr);
564 bool Validate(const CommandBufferAccessContext &cb_context) const override;
565 void Record(CommandBufferAccessContext *cb_context) const override;
566
567 protected:
568 safe_VkRenderPassBeginInfo renderpass_begin_info_;
569 safe_VkSubpassBeginInfo subpass_begin_info_;
570 std::vector<std::shared_ptr<const IMAGE_VIEW_STATE>> shared_attachments_;
571 std::vector<const IMAGE_VIEW_STATE *> attachments_;
572 std::shared_ptr<const RENDER_PASS_STATE> rp_state_;
573};
574
575class SyncOpNextSubpass : public SyncOpBase {
576 public:
577 SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
578 const VkSubpassEndInfo *pSubpassEndInfo, const char *name_override = nullptr);
579 bool Validate(const CommandBufferAccessContext &cb_context) const override;
580 void Record(CommandBufferAccessContext *cb_context) const override;
581
582 protected:
583 safe_VkSubpassBeginInfo subpass_begin_info_;
584 safe_VkSubpassEndInfo subpass_end_info_;
585};
586
587class SyncOpEndRenderPass : public SyncOpBase {
588 public:
589 SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo,
590 const char *name_override = nullptr);
591 bool Validate(const CommandBufferAccessContext &cb_context) const override;
592 void Record(CommandBufferAccessContext *cb_context) const override;
593
594 protected:
595 safe_VkSubpassEndInfo subpass_end_info_;
596};
597
John Zulauf540266b2020-04-06 18:54:53 -0600598class AccessContext {
John Zulauf5c5e88d2019-12-26 11:22:02 -0700599 public:
John Zulauf69133422020-05-20 14:55:53 -0600600 enum DetectOptions : uint32_t {
John Zulauf355e49b2020-04-24 15:11:15 -0600601 kDetectPrevious = 1U << 0,
602 kDetectAsync = 1U << 1,
603 kDetectAll = (kDetectPrevious | kDetectAsync)
John Zulauf16adfc92020-04-08 10:28:33 -0600604 };
John Zulauf43cc7462020-12-03 12:33:12 -0700605 using MapArray = std::array<ResourceAccessRangeMap, static_cast<size_t>(AccessAddressType::kTypeCount)>;
John Zulauf16adfc92020-04-08 10:28:33 -0600606
John Zulaufbaea94f2020-09-15 17:55:16 -0600607 // WIP TODO WIP Multi-dep -- change track back to support barrier vector, not just last.
John Zulauf3d84f1b2020-03-09 13:33:25 -0600608 struct TrackBack {
John Zulaufa0a98292020-09-18 09:30:10 -0600609 std::vector<SyncBarrier> barriers;
John Zulauf1a224292020-06-30 14:52:13 -0600610 const AccessContext *context;
John Zulaufbaea94f2020-09-15 17:55:16 -0600611 TrackBack(const AccessContext *context_, VkQueueFlags queue_flags_,
John Zulaufa0a98292020-09-18 09:30:10 -0600612 const std::vector<const VkSubpassDependency2 *> &subpass_dependencies_)
613 : barriers(), context(context_) {
614 barriers.reserve(subpass_dependencies_.size());
615 for (const VkSubpassDependency2 *dependency : subpass_dependencies_) {
616 assert(dependency);
617 barriers.emplace_back(queue_flags_, *dependency);
618 }
619 }
620
John Zulauf3d84f1b2020-03-09 13:33:25 -0600621 TrackBack &operator=(const TrackBack &) = default;
622 TrackBack() = default;
623 };
John Zulauf5c5e88d2019-12-26 11:22:02 -0700624
John Zulauf355e49b2020-04-24 15:11:15 -0600625 HazardResult DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index, const ResourceAccessRange &range) const;
John Zulauf540266b2020-04-06 18:54:53 -0600626 HazardResult DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf3d84f1b2020-03-09 13:33:25 -0600627 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
628 const VkExtent3D &extent) const;
John Zulauf69133422020-05-20 14:55:53 -0600629 template <typename Detector>
630 HazardResult DetectHazard(Detector &detector, const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range,
631 const VkOffset3D &offset, const VkExtent3D &extent, DetectOptions options) const;
John Zulauf1507ee42020-05-18 11:33:09 -0600632 HazardResult DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
633 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
634 const VkExtent3D &extent) const;
John Zulauf69133422020-05-20 14:55:53 -0600635 HazardResult DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -0700636 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -0600637 const VkOffset3D &offset, const VkExtent3D &extent) const;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700638 HazardResult DetectHazard(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulaufb027cdb2020-05-21 14:25:22 -0600639 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask = 0U) const;
Jeremy Gebben40a22942020-12-22 14:22:06 -0700640 HazardResult DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700641 const SyncStageAccessFlags &src_access_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -0700642 const VkImageSubresourceRange &subresource_range, const SyncEventState &sync_event,
643 DetectOptions options) const;
Jeremy Gebben40a22942020-12-22 14:22:06 -0700644 HazardResult DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -0700645 const SyncStageAccessFlags &src_access_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700646 const VkImageSubresourceRange &subresource_range, DetectOptions options) const;
Jeremy Gebben40a22942020-12-22 14:22:06 -0700647 HazardResult DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700648 const SyncStageAccessFlags &src_stage_accesses,
649 const VkImageMemoryBarrier &barrier) const;
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700650 HazardResult DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const;
John Zulauf7635de32020-05-29 17:14:15 -0600651 HazardResult DetectSubpassTransitionHazard(const TrackBack &track_back, const IMAGE_VIEW_STATE *attach_view) const;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600652
John Zulaufb02c1eb2020-10-06 16:33:36 -0600653 void RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
654 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const ResourceUsageTag &tag);
655
John Zulaufe5da6e52020-03-18 15:32:18 -0600656 const TrackBack &GetDstExternalTrackBack() const { return dst_external_; }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600657 void Reset() {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600658 prev_.clear();
John Zulauf355e49b2020-04-24 15:11:15 -0600659 prev_by_subpass_.clear();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600660 async_.clear();
John Zulaufe5da6e52020-03-18 15:32:18 -0600661 src_external_ = TrackBack();
John Zulaufa0a98292020-09-18 09:30:10 -0600662 dst_external_ = TrackBack();
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700663 start_tag_ = ResourceUsageTag();
John Zulauf16adfc92020-04-08 10:28:33 -0600664 for (auto &map : access_state_maps_) {
665 map.clear();
666 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600667 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600668
669 // Follow the context previous to access the access state, supporting "lazy" import into the context. Not intended for
670 // subpass layout transition, as the pending state handling is more complex
John Zulauf5f13a792020-03-10 07:31:21 -0600671 // TODO: See if returning the lower_bound would be useful from a performance POV -- look at the lower_bound overhead
672 // Would need to add a "hint" overload to parallel_iterator::invalidate_[AB] call, if so.
John Zulauf43cc7462020-12-03 12:33:12 -0700673 void ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range, ResourceAccessRangeMap *descent_map,
John Zulauf355e49b2020-04-24 15:11:15 -0600674 const ResourceAccessState *infill_state) const;
John Zulauf4a6105a2020-11-17 15:11:05 -0700675 void ResolvePreviousAccesses();
John Zulaufb02c1eb2020-10-06 16:33:36 -0600676 template <typename BarrierAction>
677 void ResolveAccessRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -0700678 BarrierAction &barrier_action, AccessAddressType address_type, ResourceAccessRangeMap *descent_map,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600679 const ResourceAccessState *infill_state) const;
680 template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700681 void ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600682 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
683 bool recur_to_infill = true) const;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600684
John Zulauf8e3c3e92021-01-06 11:19:36 -0700685 void UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
686 const ResourceAccessRange &range, const ResourceUsageTag &tag);
687 void UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -0600688 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset, const VkExtent3D &extent,
689 const ResourceUsageTag &tag);
John Zulauf8e3c3e92021-01-06 11:19:36 -0700690 void UpdateAccessState(const IMAGE_VIEW_STATE *view, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
691 const VkOffset3D &offset, const VkExtent3D &extent, VkImageAspectFlags aspect_mask,
692 const ResourceUsageTag &tag);
693 void UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf3d84f1b2020-03-09 13:33:25 -0600694 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset, const VkExtent3D &extent,
695 const ResourceUsageTag &tag);
John Zulauf7635de32020-05-29 17:14:15 -0600696 void UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
697 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
698 const ResourceUsageTag &tag);
John Zulaufaff20662020-06-01 14:07:58 -0600699 void UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
700 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, uint32_t subpass,
701 const ResourceUsageTag &tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600702
John Zulauf540266b2020-04-06 18:54:53 -0600703 void ResolveChildContexts(const std::vector<AccessContext> &contexts);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600704
John Zulauf540266b2020-04-06 18:54:53 -0600705 template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -0600706 void UpdateResourceAccess(const BUFFER_STATE &buffer, const ResourceAccessRange &range, const Action action);
John Zulauf540266b2020-04-06 18:54:53 -0600707 template <typename Action>
John Zulauf89311b42020-09-29 16:28:47 -0600708 void UpdateResourceAccess(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range, const Action action);
John Zulauf540266b2020-04-06 18:54:53 -0600709
710 template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -0700711 void ApplyToContext(const Action &barrier_action);
John Zulauf43cc7462020-12-03 12:33:12 -0700712 static AccessAddressType ImageAddressType(const IMAGE_STATE &image);
John Zulauf16adfc92020-04-08 10:28:33 -0600713
John Zulauf540266b2020-04-06 18:54:53 -0600714 AccessContext(uint32_t subpass, VkQueueFlags queue_flags, const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600715 const std::vector<AccessContext> &contexts, const AccessContext *external_context);
John Zulauf540266b2020-04-06 18:54:53 -0600716
717 AccessContext() { Reset(); }
John Zulauf7635de32020-05-29 17:14:15 -0600718 AccessContext(const AccessContext &copy_from) = default;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600719
John Zulauf43cc7462020-12-03 12:33:12 -0700720 ResourceAccessRangeMap &GetAccessStateMap(AccessAddressType type) { return access_state_maps_[static_cast<size_t>(type)]; }
721 const ResourceAccessRangeMap &GetAccessStateMap(AccessAddressType type) const {
722 return access_state_maps_[static_cast<size_t>(type)];
723 }
724 ResourceAccessRangeMap &GetLinearMap() { return GetAccessStateMap(AccessAddressType::kLinear); }
725 const ResourceAccessRangeMap &GetLinearMap() const { return GetAccessStateMap(AccessAddressType::kLinear); }
726 ResourceAccessRangeMap &GetIdealizedMap() { return GetAccessStateMap(AccessAddressType::kIdealized); }
727 const ResourceAccessRangeMap &GetIdealizedMap() const { return GetAccessStateMap(AccessAddressType::kIdealized); }
John Zulauf355e49b2020-04-24 15:11:15 -0600728 const TrackBack *GetTrackBackFromSubpass(uint32_t subpass) const {
729 if (subpass == VK_SUBPASS_EXTERNAL) {
730 return &src_external_;
731 } else {
732 assert(subpass < prev_by_subpass_.size());
733 return prev_by_subpass_[subpass];
734 }
735 }
John Zulauf16adfc92020-04-08 10:28:33 -0600736
John Zulauf64ffe552021-02-06 10:25:07 -0700737 bool ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauffaea0ee2021-01-14 14:01:32 -0700738 const VkRect2D &render_area, uint32_t subpass,
739 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name) const;
John Zulauf64ffe552021-02-06 10:25:07 -0700740 bool ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauffaea0ee2021-01-14 14:01:32 -0700741 const VkRect2D &render_area, uint32_t subpass,
742 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name) const;
John Zulauf64ffe552021-02-06 10:25:07 -0700743 bool ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauffaea0ee2021-01-14 14:01:32 -0700744 const VkRect2D &render_area, uint32_t subpass,
745 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const char *func_name) const;
John Zulauf64ffe552021-02-06 10:25:07 -0700746 bool ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauffaea0ee2021-01-14 14:01:32 -0700747 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
748 const char *func_name, uint32_t subpass) const;
John Zulauf1507ee42020-05-18 11:33:09 -0600749
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700750 void SetStartTag(const ResourceUsageTag &tag) { start_tag_ = tag; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700751 template <typename Action>
752 void ForAll(Action &&action);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700753
John Zulauf3d84f1b2020-03-09 13:33:25 -0600754 private:
755 template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700756 HazardResult DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600757 DetectOptions options) const;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600758 template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700759 HazardResult DetectAsyncHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range) const;
John Zulauf5f13a792020-03-10 07:31:21 -0600760 template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700761 HazardResult DetectPreviousHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range) const;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700762 void UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
763 const ResourceAccessRange &range, const ResourceUsageTag &tag);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600764
765 MapArray access_state_maps_;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600766 std::vector<TrackBack> prev_;
John Zulauf355e49b2020-04-24 15:11:15 -0600767 std::vector<TrackBack *> prev_by_subpass_;
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700768 std::vector<const AccessContext *> async_;
John Zulaufe5da6e52020-03-18 15:32:18 -0600769 TrackBack src_external_;
770 TrackBack dst_external_;
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700771 ResourceUsageTag start_tag_;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600772};
773
John Zulauf355e49b2020-04-24 15:11:15 -0600774class RenderPassAccessContext {
775 public:
John Zulauf64ffe552021-02-06 10:25:07 -0700776 RenderPassAccessContext() : rp_state_(nullptr), render_area_(VkRect2D()), current_subpass_(0) {}
777 RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area, VkQueueFlags queue_flags,
778 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const AccessContext *external_context);
John Zulauf355e49b2020-04-24 15:11:15 -0600779
John Zulauf64ffe552021-02-06 10:25:07 -0700780 bool ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
781 const char *func_name) const;
782 void RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag &tag);
783 bool ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *command_name) const;
784 bool ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const;
785 bool ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context, const char *func_name) const;
John Zulauf355e49b2020-04-24 15:11:15 -0600786
787 void RecordLayoutTransitions(const ResourceUsageTag &tag);
John Zulauf64ffe552021-02-06 10:25:07 -0700788 void RecordLoadOperations(const ResourceUsageTag &tag);
789 void RecordBeginRenderPass(const ResourceUsageTag &tag);
790 void RecordNextSubpass(const ResourceUsageTag &prev_subpass_tag, const ResourceUsageTag &next_subpass_tag);
791 void RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag &tag);
John Zulauf355e49b2020-04-24 15:11:15 -0600792
John Zulauf540266b2020-04-06 18:54:53 -0600793 AccessContext &CurrentContext() { return subpass_contexts_[current_subpass_]; }
794 const AccessContext &CurrentContext() const { return subpass_contexts_[current_subpass_]; }
John Zulauf355e49b2020-04-24 15:11:15 -0600795 const std::vector<AccessContext> &GetContexts() const { return subpass_contexts_; }
796 uint32_t GetCurrentSubpass() const { return current_subpass_; }
797 const RENDER_PASS_STATE *GetRenderPassState() const { return rp_state_; }
John Zulauf64ffe552021-02-06 10:25:07 -0700798 AccessContext *CreateStoreResolveProxy() const;
John Zulauf355e49b2020-04-24 15:11:15 -0600799
800 private:
John Zulauf355e49b2020-04-24 15:11:15 -0600801 const RENDER_PASS_STATE *rp_state_;
John Zulauf64ffe552021-02-06 10:25:07 -0700802 const VkRect2D render_area_;
John Zulauf355e49b2020-04-24 15:11:15 -0600803 uint32_t current_subpass_;
804 std::vector<AccessContext> subpass_contexts_;
805 std::vector<const IMAGE_VIEW_STATE *> attachment_views_;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600806};
807
John Zulauf64ffe552021-02-06 10:25:07 -0700808// Command execution context is the base class for command buffer and queue contexts
809// Preventing unintented leakage of subclass specific state, storing enough information
810// for message logging.
811// TODO: determine where to draw the design split for tag tracking (is there anything command to Queues and CB's)
812class CommandExecutionContext {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600813 public:
John Zulauf64ffe552021-02-06 10:25:07 -0700814 CommandExecutionContext() : sync_state_(nullptr) {}
815 CommandExecutionContext(SyncValidator *sync_validator) : sync_state_(sync_validator) {}
816 virtual ~CommandExecutionContext() = default;
817 const SyncValidator &GetSyncState() const {
818 assert(sync_state_);
819 return *sync_state_;
820 }
821 SyncValidator &GetSyncState() {
822 assert(sync_state_);
823 return *sync_state_;
824 }
825 virtual std::string FormatUsage(const HazardResult &hazard) const = 0;
826
827 protected:
828 SyncValidator *sync_state_;
829};
830
831class CommandBufferAccessContext : public CommandExecutionContext {
832 public:
833 CommandBufferAccessContext(SyncValidator *sync_validator = nullptr)
834 : CommandExecutionContext(sync_validator),
835 access_index_(0),
John Zulauffaea0ee2021-01-14 14:01:32 -0700836 command_number_(0),
837 subcommand_number_(0),
John Zulauf355e49b2020-04-24 15:11:15 -0600838 reset_count_(0),
839 render_pass_contexts_(),
840 cb_access_context_(),
841 current_context_(&cb_access_context_),
John Zulauf3d84f1b2020-03-09 13:33:25 -0600842 current_renderpass_context_(),
843 cb_state_(),
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700844 queue_flags_(),
John Zulauf669dfd52021-01-27 17:15:28 -0700845 events_context_(),
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700846 destroyed_(false) {}
John Zulauf355e49b2020-04-24 15:11:15 -0600847 CommandBufferAccessContext(SyncValidator &sync_validator, std::shared_ptr<CMD_BUFFER_STATE> &cb_state, VkQueueFlags queue_flags)
John Zulauf64ffe552021-02-06 10:25:07 -0700848 : CommandBufferAccessContext(&sync_validator) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600849 cb_state_ = cb_state;
850 queue_flags_ = queue_flags;
851 }
John Zulauf64ffe552021-02-06 10:25:07 -0700852 ~CommandBufferAccessContext() override = default;
853 CommandExecutionContext &GetExecutionContext() { return *this; }
854 const CommandExecutionContext &GetExecutionContext() const { return *this; }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700855
856 void Reset() {
John Zulauffaea0ee2021-01-14 14:01:32 -0700857 access_index_ = 0;
John Zulauf355e49b2020-04-24 15:11:15 -0600858 command_number_ = 0;
John Zulauffaea0ee2021-01-14 14:01:32 -0700859 subcommand_number_ = 0;
John Zulauf355e49b2020-04-24 15:11:15 -0600860 reset_count_++;
861 cb_access_context_.Reset();
John Zulauf3d84f1b2020-03-09 13:33:25 -0600862 render_pass_contexts_.clear();
John Zulauf355e49b2020-04-24 15:11:15 -0600863 current_context_ = &cb_access_context_;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600864 current_renderpass_context_ = nullptr;
John Zulauf669dfd52021-01-27 17:15:28 -0700865 events_context_.Clear();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700866 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700867 void MarkDestroyed() { destroyed_ = true; }
868 bool IsDestroyed() const { return destroyed_; }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700869
John Zulauf64ffe552021-02-06 10:25:07 -0700870 std::string FormatUsage(const HazardResult &hazard) const override;
John Zulauf540266b2020-04-06 18:54:53 -0600871 AccessContext *GetCurrentAccessContext() { return current_context_; }
John Zulauf669dfd52021-01-27 17:15:28 -0700872 SyncEventsContext *GetCurrentEventsContext() { return &events_context_; }
John Zulauf64ffe552021-02-06 10:25:07 -0700873 RenderPassAccessContext *GetCurrentRenderPassContext() { return current_renderpass_context_; }
John Zulauf540266b2020-04-06 18:54:53 -0600874 const AccessContext *GetCurrentAccessContext() const { return current_context_; }
John Zulauf64ffe552021-02-06 10:25:07 -0700875 const SyncEventsContext *GetCurrentEventsContext() const { return &events_context_; }
876 const RenderPassAccessContext *GetCurrentRenderPassContext() const { return current_renderpass_context_; }
877 void RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
878 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views, const ResourceUsageTag &tag);
Jeremy Gebben9893daf2021-01-04 10:40:50 -0700879 void ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst);
John Zulaufd5115702021-01-18 12:34:33 -0700880
locke-lunarg61870c22020-06-09 14:51:50 -0600881 bool ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint, const char *func_name) const;
882 void RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint, const ResourceUsageTag &tag);
883 bool ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const;
884 void RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag);
885 bool ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const;
886 void RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag);
887 bool ValidateDrawSubpassAttachment(const char *func_name) const;
888 void RecordDrawSubpassAttachment(const ResourceUsageTag &tag);
John Zulauf64ffe552021-02-06 10:25:07 -0700889 void RecordNextSubpass(CMD_TYPE command);
890 void RecordEndRenderPass(CMD_TYPE command);
John Zulauf4a6105a2020-11-17 15:11:05 -0700891 void RecordDestroyEvent(VkEvent event);
John Zulauf49beb112020-11-04 16:06:31 -0700892
John Zulauf3d84f1b2020-03-09 13:33:25 -0600893 const CMD_BUFFER_STATE *GetCommandBufferState() const { return cb_state_.get(); }
894 VkQueueFlags GetQueueFlags() const { return queue_flags_; }
John Zulauffaea0ee2021-01-14 14:01:32 -0700895 inline ResourceUsageTag::TagIndex NextAccessIndex() { return access_index_++; }
896
897 inline ResourceUsageTag NextSubcommandTag(CMD_TYPE command) {
898 ResourceUsageTag next(NextAccessIndex(), command_number_, subcommand_number_++, command);
899 return next;
900 }
901
John Zulauf355e49b2020-04-24 15:11:15 -0600902 inline ResourceUsageTag NextCommandTag(CMD_TYPE command) {
John Zulauf355e49b2020-04-24 15:11:15 -0600903 command_number_++;
John Zulauffaea0ee2021-01-14 14:01:32 -0700904 subcommand_number_ = 0;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700905 // The lowest bit is a sub-command number used to separate operations at the end of the previous renderpass
906 // from the start of the new one in VkCmdNextRenderpass().
John Zulauffaea0ee2021-01-14 14:01:32 -0700907 ResourceUsageTag next(NextAccessIndex(), command_number_, subcommand_number_, command);
John Zulauf355e49b2020-04-24 15:11:15 -0600908 return next;
909 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600910
John Zulauffaea0ee2021-01-14 14:01:32 -0700911 const CMD_BUFFER_STATE &GetCBState() const {
912 assert(cb_state_);
913 return *(cb_state_.get());
914 }
915 CMD_BUFFER_STATE &GetCBState() {
916 assert(cb_state_);
917 return *(cb_state_.get());
918 }
John Zulauffaea0ee2021-01-14 14:01:32 -0700919
John Zulauf3d84f1b2020-03-09 13:33:25 -0600920 private:
John Zulauffaea0ee2021-01-14 14:01:32 -0700921 ResourceUsageTag::TagIndex access_index_;
John Zulauf355e49b2020-04-24 15:11:15 -0600922 uint32_t command_number_;
John Zulauffaea0ee2021-01-14 14:01:32 -0700923 uint32_t subcommand_number_;
John Zulauf355e49b2020-04-24 15:11:15 -0600924 uint32_t reset_count_;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600925 std::vector<RenderPassAccessContext> render_pass_contexts_;
John Zulauf355e49b2020-04-24 15:11:15 -0600926 AccessContext cb_access_context_;
John Zulauf540266b2020-04-06 18:54:53 -0600927 AccessContext *current_context_;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600928 RenderPassAccessContext *current_renderpass_context_;
929 std::shared_ptr<CMD_BUFFER_STATE> cb_state_;
John Zulauf355e49b2020-04-24 15:11:15 -0600930
John Zulauf3d84f1b2020-03-09 13:33:25 -0600931 VkQueueFlags queue_flags_;
John Zulauf669dfd52021-01-27 17:15:28 -0700932 SyncEventsContext events_context_;
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700933 bool destroyed_;
John Zulauf9cb530d2019-09-30 14:14:10 -0600934};
935
936class SyncValidator : public ValidationStateTracker, public SyncStageAccess {
937 public:
938 SyncValidator() { container_type = LayerObjectTypeSyncValidation; }
939 using StateTracker = ValidationStateTracker;
940
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700941 std::unordered_map<VkCommandBuffer, CommandBufferAccessContextShared> cb_access_state;
942
943 CommandBufferAccessContextShared GetAccessContextImpl(VkCommandBuffer command_buffer, bool do_insert) {
John Zulauf9cb530d2019-09-30 14:14:10 -0600944 auto found_it = cb_access_state.find(command_buffer);
945 if (found_it == cb_access_state.end()) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700946 if (!do_insert) return CommandBufferAccessContextShared();
John Zulauf9cb530d2019-09-30 14:14:10 -0600947 // If we don't have one, make it.
John Zulauf3d84f1b2020-03-09 13:33:25 -0600948 auto cb_state = GetShared<CMD_BUFFER_STATE>(command_buffer);
949 assert(cb_state.get());
950 auto queue_flags = GetQueueFlags(*cb_state);
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700951 std::shared_ptr<CommandBufferAccessContext> context(new CommandBufferAccessContext(*this, cb_state, queue_flags));
John Zulauf3d84f1b2020-03-09 13:33:25 -0600952 auto insert_pair = cb_access_state.insert(std::make_pair(command_buffer, std::move(context)));
John Zulauf9cb530d2019-09-30 14:14:10 -0600953 found_it = insert_pair.first;
954 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700955 return found_it->second;
John Zulauf9cb530d2019-09-30 14:14:10 -0600956 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700957
John Zulauf3d84f1b2020-03-09 13:33:25 -0600958 CommandBufferAccessContext *GetAccessContext(VkCommandBuffer command_buffer) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700959 return GetAccessContextImpl(command_buffer, true).get(); // true -> do_insert on not found
John Zulauf9cb530d2019-09-30 14:14:10 -0600960 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600961 CommandBufferAccessContext *GetAccessContextNoInsert(VkCommandBuffer command_buffer) {
John Zulaufe7f6a5e2021-01-16 14:31:18 -0700962 return GetAccessContextImpl(command_buffer, false).get(); // false -> don't do_insert on not found
963 }
964
965 CommandBufferAccessContextShared GetAccessContextShared(VkCommandBuffer command_buffer) {
966 return GetAccessContextImpl(command_buffer, true); // true -> do_insert on not found
967 }
968 CommandBufferAccessContextShared GetAccessContextSharedNoInsert(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600969 return GetAccessContextImpl(command_buffer, false); // false -> don't do_insert on not found
John Zulauf9cb530d2019-09-30 14:14:10 -0600970 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600971
972 const CommandBufferAccessContext *GetAccessContext(VkCommandBuffer command_buffer) const {
John Zulauf9cb530d2019-09-30 14:14:10 -0600973 const auto found_it = cb_access_state.find(command_buffer);
974 if (found_it == cb_access_state.end()) {
975 return nullptr;
976 }
977 return found_it->second.get();
978 }
979
John Zulaufd1f85d42020-04-15 12:23:15 -0600980 void ResetCommandBufferCallback(VkCommandBuffer command_buffer);
981 void FreeCommandBufferCallback(VkCommandBuffer command_buffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600982 void RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -0700983 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name = nullptr);
984 void RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
985 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE command, const char *command_name = nullptr);
986 void RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
987 const char *cmd_name = nullptr);
John Zulauf33fc1d52020-07-17 11:01:10 -0600988 bool SupressedBoundDescriptorWAW(const HazardResult &hazard) const;
John Zulauf9cb530d2019-09-30 14:14:10 -0600989
990 void PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
Jeremy Gebbenf8924692020-10-28 16:27:14 -0600991 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) override;
John Zulauf9cb530d2019-09-30 14:14:10 -0600992
John Zulauf355e49b2020-04-24 15:11:15 -0600993 bool ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
John Zulauf64ffe552021-02-06 10:25:07 -0700994 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd, const char *cmd_name = nullptr) const;
John Zulauf355e49b2020-04-24 15:11:15 -0600995
996 bool PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Jeremy Gebbenf8924692020-10-28 16:27:14 -0600997 VkSubpassContents contents) const override;
John Zulauf355e49b2020-04-24 15:11:15 -0600998
999 bool PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001000 const VkSubpassBeginInfo *pSubpassBeginInfo) const override;
John Zulauf355e49b2020-04-24 15:11:15 -06001001
1002 bool PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08001003 const VkSubpassBeginInfo *pSubpassBeginInfo) const override;
John Zulauf355e49b2020-04-24 15:11:15 -06001004
John Zulauf9cb530d2019-09-30 14:14:10 -06001005 bool PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, uint32_t regionCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001006 const VkBufferCopy *pRegions) const override;
John Zulauf9cb530d2019-09-30 14:14:10 -06001007
1008 void PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer, uint32_t regionCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001009 const VkBufferCopy *pRegions) override;
John Zulauf9cb530d2019-09-30 14:14:10 -06001010
John Zulauf4a6105a2020-11-17 15:11:05 -07001011 void PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) override;
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001012 bool PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) const override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001013
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001014 void PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001015
John Zulauf5c5e88d2019-12-26 11:22:02 -07001016 bool PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1017 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001018 const VkImageCopy *pRegions) const override;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001019
1020 void PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001021 VkImageLayout dstImageLayout, uint32_t regionCount, const VkImageCopy *pRegions) override;
John Zulauf5c5e88d2019-12-26 11:22:02 -07001022
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001023 bool PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) const override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001024
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001025 void PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001026
John Zulauf9cb530d2019-09-30 14:14:10 -06001027 bool PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1028 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1029 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1030 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1031 uint32_t imageMemoryBarrierCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001032 const VkImageMemoryBarrier *pImageMemoryBarriers) const override;
John Zulauf9cb530d2019-09-30 14:14:10 -06001033
1034 void PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
1035 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
1036 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1037 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001038 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) override;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001039
1040 void PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001041 VkResult result) override;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001042
1043 void PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001044 VkSubpassContents contents) override;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001045 void PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001046 const VkSubpassBeginInfo *pSubpassBeginInfo) override;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001047 void PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001048 const VkSubpassBeginInfo *pSubpassBeginInfo) override;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001049
Mike Schuchardt2df08912020-12-15 16:28:09 -08001050 bool ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
John Zulauf64ffe552021-02-06 10:25:07 -07001051 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd, const char *cmd_name = nullptr) const;
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001052 bool PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const override;
Mike Schuchardt2df08912020-12-15 16:28:09 -08001053 bool PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1054 const VkSubpassEndInfo *pSubpassEndInfo) const override;
1055 bool PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
1056 const VkSubpassEndInfo *pSubpassEndInfo) const override;
John Zulauf355e49b2020-04-24 15:11:15 -06001057
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001058 void PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) override;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001059 void PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001060 const VkSubpassEndInfo *pSubpassEndInfo) override;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001061 void PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001062 const VkSubpassEndInfo *pSubpassEndInfo) override;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001063
John Zulauf64ffe552021-02-06 10:25:07 -07001064 bool ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd,
1065 const char *cmd_name = nullptr) const;
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001066 bool PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const override;
Mike Schuchardt2df08912020-12-15 16:28:09 -08001067 bool PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const override;
1068 bool PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const override;
John Zulauf355e49b2020-04-24 15:11:15 -06001069
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001070 void PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) override;
1071 void PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) override;
1072 void PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001073
1074 template <typename BufferImageCopyRegionType>
1075 bool ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
1076 VkImageLayout dstImageLayout, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
1077 CopyCommandVersion version) const;
locke-lunarga19c71d2020-03-02 18:17:04 -07001078 bool PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
1079 VkImageLayout dstImageLayout, uint32_t regionCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001080 const VkBufferImageCopy *pRegions) const override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001081 bool PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001082 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const override;
locke-lunarga19c71d2020-03-02 18:17:04 -07001083
Jeff Leger178b1e52020-10-05 12:22:23 -04001084 template <typename BufferImageCopyRegionType>
1085 void RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
1086 VkImageLayout dstImageLayout, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
1087 CopyCommandVersion version);
locke-lunarga19c71d2020-03-02 18:17:04 -07001088 void PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001089 VkImageLayout dstImageLayout, uint32_t regionCount, const VkBufferImageCopy *pRegions) override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001090 void PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001091 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) override;
locke-lunarga19c71d2020-03-02 18:17:04 -07001092
Jeff Leger178b1e52020-10-05 12:22:23 -04001093 template <typename BufferImageCopyRegionType>
1094 bool ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1095 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
1096 CopyCommandVersion version) const;
locke-lunarga19c71d2020-03-02 18:17:04 -07001097 bool PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001098 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) const override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001099 bool PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001100 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const override;
locke-lunarga19c71d2020-03-02 18:17:04 -07001101
Jeff Leger178b1e52020-10-05 12:22:23 -04001102 template <typename BufferImageCopyRegionType>
1103 void RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1104 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
1105 CopyCommandVersion version);
locke-lunarga19c71d2020-03-02 18:17:04 -07001106 void PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001107 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001108 void PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001109 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001110
1111 template <typename RegionType>
1112 bool ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
1113 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions, VkFilter filter,
1114 const char *apiName) const;
locke-lunarga19c71d2020-03-02 18:17:04 -07001115
1116 bool PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1117 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001118 const VkImageBlit *pRegions, VkFilter filter) const override;
1119 bool PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) const override;
locke-lunarga19c71d2020-03-02 18:17:04 -07001120
Jeff Leger178b1e52020-10-05 12:22:23 -04001121 template <typename RegionType>
1122 void RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
1123 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions, VkFilter filter,
1124 ResourceUsageTag tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07001125 void PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout, VkImage dstImage,
1126 VkImageLayout dstImageLayout, uint32_t regionCount, const VkImageBlit *pRegions,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001127 VkFilter filter) override;
1128 void PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) override;
locke-lunarg36ba2592020-04-03 09:42:04 -06001129
John Zulauffaea0ee2021-01-14 14:01:32 -07001130 bool ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
1131 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
1132 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
locke-lunarg61870c22020-06-09 14:51:50 -06001133 const char *function) const;
1134 void RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
1135 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount, uint32_t stride);
locke-lunarg36ba2592020-04-03 09:42:04 -06001136
John Zulauffaea0ee2021-01-14 14:01:32 -07001137 bool ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
1138 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, const char *function) const;
locke-lunarg61870c22020-06-09 14:51:50 -06001139 void RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset);
locke-lunarg93d68af2020-05-12 17:18:03 -06001140
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001141 bool PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const override;
1142 void PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001143
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001144 bool PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const override;
1145 void PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001146
1147 bool PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001148 uint32_t firstInstance) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001149 void PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount, uint32_t firstVertex,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001150 uint32_t firstInstance) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001151
1152 bool PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001153 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001154 void PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001155 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001156
1157 bool PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t drawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001158 uint32_t stride) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001159 void PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, uint32_t drawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001160 uint32_t stride) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001161
1162 bool PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001163 uint32_t drawCount, uint32_t stride) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001164 void PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001165 uint32_t drawCount, uint32_t stride) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001166
locke-lunargff255f92020-05-13 18:53:52 -06001167 bool ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset, VkBuffer countBuffer,
1168 VkDeviceSize countBufferOffset, uint32_t maxDrawCount, uint32_t stride,
1169 const char *function) const;
locke-lunarge1a67022020-04-29 00:15:36 -06001170 bool PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1171 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001172 uint32_t stride) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001173 void PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1174 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001175 uint32_t stride) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001176 bool PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1177 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001178 uint32_t stride) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001179 void PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1180 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001181 uint32_t stride) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001182 bool PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1183 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001184 uint32_t stride) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001185 void PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1186 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001187 uint32_t stride) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001188
locke-lunargff255f92020-05-13 18:53:52 -06001189 bool ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1190 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
1191 uint32_t stride, const char *function) const;
locke-lunarge1a67022020-04-29 00:15:36 -06001192 bool PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1193 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001194 uint32_t stride) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001195 void PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1196 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001197 uint32_t stride) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001198 bool PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1199 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001200 uint32_t stride) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001201 void PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1202 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001203 uint32_t stride) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001204 bool PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1205 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001206 uint32_t stride) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001207 void PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
1208 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001209 uint32_t stride) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001210
1211 bool PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
1212 const VkClearColorValue *pColor, uint32_t rangeCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001213 const VkImageSubresourceRange *pRanges) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001214 void PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
1215 const VkClearColorValue *pColor, uint32_t rangeCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001216 const VkImageSubresourceRange *pRanges) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001217
1218 bool PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
1219 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001220 const VkImageSubresourceRange *pRanges) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001221 void PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
1222 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001223 const VkImageSubresourceRange *pRanges) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001224
1225 bool PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
1226 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001227 VkDeviceSize stride, VkQueryResultFlags flags) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001228 void PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
1229 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize stride,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001230 VkQueryResultFlags flags) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001231
1232 bool PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize size,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001233 uint32_t data) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001234 void PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset, VkDeviceSize size,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001235 uint32_t data) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001236
1237 bool PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1238 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001239 const VkImageResolve *pRegions) const override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001240
locke-lunarge1a67022020-04-29 00:15:36 -06001241 void PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
1242 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001243 const VkImageResolve *pRegions) override;
locke-lunarge1a67022020-04-29 00:15:36 -06001244
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001245 bool PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo) const override;
1246 void PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo) override;
Jeff Leger178b1e52020-10-05 12:22:23 -04001247
locke-lunarge1a67022020-04-29 00:15:36 -06001248 bool PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001249 VkDeviceSize dataSize, const void *pData) const override;
locke-lunarge1a67022020-04-29 00:15:36 -06001250 void PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001251 VkDeviceSize dataSize, const void *pData) override;
locke-lunargff255f92020-05-13 18:53:52 -06001252
1253 bool PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001254 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const override;
locke-lunargff255f92020-05-13 18:53:52 -06001255 void PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
Jeremy Gebbenf8924692020-10-28 16:27:14 -06001256 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) override;
John Zulauf49beb112020-11-04 16:06:31 -07001257
1258 bool PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const override;
1259 void PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) override;
1260
1261 bool PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const override;
1262 void PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) override;
1263
1264 bool PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
1265 VkPipelineStageFlags sourceStageMask, VkPipelineStageFlags dstStageMask,
1266 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1267 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1268 uint32_t imageMemoryBarrierCount,
1269 const VkImageMemoryBarrier *pImageMemoryBarriers) const override;
1270 void PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
1271 VkPipelineStageFlags sourceStageMask, VkPipelineStageFlags dstStageMask,
1272 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
1273 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
1274 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers) override;
John Zulauf9cb530d2019-09-30 14:14:10 -06001275};