blob: 498d07429b30326c2d4dc14844103e1999b96d0b [file] [log] [blame]
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001/* Copyright (c) 2019-2022 The Khronos Group Inc.
2 * Copyright (c) 2019-2022 Valve Corporation
3 * Copyright (c) 2019-2022 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#include <limits>
23#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060024#include <memory>
25#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060026#include "synchronization_validation.h"
Jeremy Gebben5f585ae2021-02-02 09:03:06 -070027#include "sync_utils.h"
John Zulauf9cb530d2019-09-30 14:14:10 -060028
John Zulaufea943c52022-02-22 11:05:17 -070029// Utilities to DRY up Get... calls
30template <typename Map, typename Key = typename Map::key_type, typename RetVal = layer_data::optional<typename Map::mapped_type>>
31RetVal GetMappedOptional(const Map &map, const Key &key) {
32 RetVal ret_val;
33 auto it = map.find(key);
34 if (it != map.cend()) {
35 ret_val.emplace(it->second);
36 }
37 return ret_val;
38}
39template <typename Map, typename Fn>
40typename Map::mapped_type GetMapped(const Map &map, const typename Map::key_type &key, Fn &&default_factory) {
41 auto value = GetMappedOptional(map, key);
42 return (value) ? *value : default_factory();
43}
44
45template <typename Map, typename Fn>
John Zulauf397e68b2022-04-19 11:44:07 -060046typename Map::mapped_type GetMappedInsert(Map &map, const typename Map::key_type &key, Fn &&emplace_factory) {
John Zulaufea943c52022-02-22 11:05:17 -070047 auto value = GetMappedOptional(map, key);
48 if (value) {
49 return *value;
50 }
John Zulauf397e68b2022-04-19 11:44:07 -060051 auto insert_it = map.emplace(std::make_pair(key, emplace_factory()));
John Zulaufea943c52022-02-22 11:05:17 -070052 assert(insert_it.second);
53
54 return insert_it.first->second;
55}
56
57template <typename Map, typename Key = typename Map::key_type, typename Mapped = typename Map::mapped_type,
58 typename Value = typename Mapped::element_type>
59Value *GetMappedPlainFromShared(const Map &map, const Key &key) {
60 auto value = GetMappedOptional<Map, Key>(map, key);
61 if (value) return value->get();
62 return nullptr;
63}
64
Jeremy Gebben6fbf8242021-06-21 09:14:46 -060065static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.Binding(); }
John Zulauf264cce02021-02-05 14:40:47 -070066
John Zulauf29d00532021-03-04 13:28:54 -070067static bool SimpleBinding(const IMAGE_STATE &image_state) {
Jeremy Gebben62c3bf42021-07-21 15:38:24 -060068 bool simple =
Jeremy Gebben82e11d52021-07-26 09:19:37 -060069 SimpleBinding(static_cast<const BINDABLE &>(image_state)) || image_state.IsSwapchainImage() || image_state.bind_swapchain;
John Zulauf29d00532021-03-04 13:28:54 -070070
71 // If it's not simple we must have an encoder.
72 assert(!simple || image_state.fragment_encoder.get());
73 return simple;
74}
75
John Zulauf4fa68462021-04-26 21:04:22 -060076static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
77static const std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
John Zulauf43cc7462020-12-03 12:33:12 -070078 AccessAddressType::kLinear, AccessAddressType::kIdealized};
79
John Zulaufd5115702021-01-18 12:34:33 -070080static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070081static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
82 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
83}
John Zulaufd5115702021-01-18 12:34:33 -070084
John Zulauf9cb530d2019-09-30 14:14:10 -060085static const char *string_SyncHazardVUID(SyncHazard hazard) {
86 switch (hazard) {
87 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070088 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060089 break;
90 case SyncHazard::READ_AFTER_WRITE:
91 return "SYNC-HAZARD-READ_AFTER_WRITE";
92 break;
93 case SyncHazard::WRITE_AFTER_READ:
94 return "SYNC-HAZARD-WRITE_AFTER_READ";
95 break;
96 case SyncHazard::WRITE_AFTER_WRITE:
97 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
98 break;
John Zulauf2f952d22020-02-10 11:34:51 -070099 case SyncHazard::READ_RACING_WRITE:
100 return "SYNC-HAZARD-READ-RACING-WRITE";
101 break;
102 case SyncHazard::WRITE_RACING_WRITE:
103 return "SYNC-HAZARD-WRITE-RACING-WRITE";
104 break;
105 case SyncHazard::WRITE_RACING_READ:
106 return "SYNC-HAZARD-WRITE-RACING-READ";
107 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600108 default:
109 assert(0);
110 }
111 return "SYNC-HAZARD-INVALID";
112}
113
John Zulauf59e25072020-07-17 10:55:21 -0600114static bool IsHazardVsRead(SyncHazard hazard) {
115 switch (hazard) {
116 case SyncHazard::NONE:
117 return false;
118 break;
119 case SyncHazard::READ_AFTER_WRITE:
120 return false;
121 break;
122 case SyncHazard::WRITE_AFTER_READ:
123 return true;
124 break;
125 case SyncHazard::WRITE_AFTER_WRITE:
126 return false;
127 break;
128 case SyncHazard::READ_RACING_WRITE:
129 return false;
130 break;
131 case SyncHazard::WRITE_RACING_WRITE:
132 return false;
133 break;
134 case SyncHazard::WRITE_RACING_READ:
135 return true;
136 break;
137 default:
138 assert(0);
139 }
140 return false;
141}
142
John Zulauf9cb530d2019-09-30 14:14:10 -0600143static const char *string_SyncHazard(SyncHazard hazard) {
144 switch (hazard) {
145 case SyncHazard::NONE:
146 return "NONR";
147 break;
148 case SyncHazard::READ_AFTER_WRITE:
149 return "READ_AFTER_WRITE";
150 break;
151 case SyncHazard::WRITE_AFTER_READ:
152 return "WRITE_AFTER_READ";
153 break;
154 case SyncHazard::WRITE_AFTER_WRITE:
155 return "WRITE_AFTER_WRITE";
156 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700157 case SyncHazard::READ_RACING_WRITE:
158 return "READ_RACING_WRITE";
159 break;
160 case SyncHazard::WRITE_RACING_WRITE:
161 return "WRITE_RACING_WRITE";
162 break;
163 case SyncHazard::WRITE_RACING_READ:
164 return "WRITE_RACING_READ";
165 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600166 default:
167 assert(0);
168 }
169 return "INVALID HAZARD";
170}
171
John Zulauf37ceaed2020-07-03 16:18:15 -0600172static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
173 // Return the info for the first bit found
174 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700175 for (size_t i = 0; i < flags.size(); i++) {
176 if (flags.test(i)) {
177 info = &syncStageAccessInfoByStageAccessIndex[i];
178 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600179 }
180 }
181 return info;
182}
183
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700184static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600185 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700186 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600187 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700188 } else {
189 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
190 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
191 if ((flags & info.stage_access_bit).any()) {
192 if (!out_str.empty()) {
193 out_str.append(sep);
194 }
195 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600196 }
John Zulauf59e25072020-07-17 10:55:21 -0600197 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700198 if (out_str.length() == 0) {
199 out_str.append("Unhandled SyncStageAccess");
200 }
John Zulauf59e25072020-07-17 10:55:21 -0600201 }
202 return out_str;
203}
204
John Zulauf397e68b2022-04-19 11:44:07 -0600205std::ostream &operator<<(std::ostream &out, const ResourceUsageRecord &record) {
206 out << "command: " << CommandTypeString(record.command);
207 out << ", seq_no: " << record.seq_num;
208 if (record.sub_command != 0) {
209 out << ", subcmd: " << record.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700210 }
John Zulauf397e68b2022-04-19 11:44:07 -0600211 return out;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700212}
John Zulauf397e68b2022-04-19 11:44:07 -0600213
John Zulauf4fa68462021-04-26 21:04:22 -0600214static std::string string_UsageIndex(SyncStageAccessIndex usage_index) {
215 const char *stage_access_name = "INVALID_STAGE_ACCESS";
216 if (usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size())) {
217 stage_access_name = syncStageAccessInfoByStageAccessIndex[usage_index].name;
218 }
219 return std::string(stage_access_name);
220}
221
John Zulauf397e68b2022-04-19 11:44:07 -0600222struct SyncNodeFormatter {
223 const debug_report_data *report_data;
224 const BASE_NODE *node;
225 const char *label;
226
227 SyncNodeFormatter(const SyncValidator &sync_state, const CMD_BUFFER_STATE *cb_state)
228 : report_data(sync_state.report_data), node(cb_state), label("command_buffer") {}
229 SyncNodeFormatter(const SyncValidator &sync_state, const QUEUE_STATE *q_state)
230 : report_data(sync_state.report_data), node(q_state), label("queue") {}
231};
232
233std::ostream &operator<<(std::ostream &out, const SyncNodeFormatter &formater) {
234 if (formater.node) {
235 out << ", " << formater.label << ": " << formater.report_data->FormatHandle(formater.node->Handle()).c_str();
236 if (formater.node->Destroyed()) {
237 out << " (destroyed)";
238 }
239 } else {
240 out << ", " << formater.label << ": null handle";
241 }
242 return out;
243}
244
245std::ostream &operator<<(std::ostream &out, const HazardResult &hazard) {
246 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
247 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
248 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
249 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
250 out << "(";
251 if (!hazard.recorded_access.get()) {
252 // if we have a recorded usage the usage is reported from the recorded contexts point of view
253 out << "usage: " << usage_info.name << ", ";
254 }
255 out << "prior_usage: " << stage_access_name;
256 if (IsHazardVsRead(hazard.hazard)) {
257 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
258 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
259 } else {
260 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
261 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
262 }
263 return out;
264}
265
John Zulauf4fa68462021-04-26 21:04:22 -0600266struct NoopBarrierAction {
267 explicit NoopBarrierAction() {}
268 void operator()(ResourceAccessState *access) const {}
John Zulauf5c628d02021-05-04 15:46:36 -0600269 const bool layout_transition = false;
John Zulauf4fa68462021-04-26 21:04:22 -0600270};
271
272// NOTE: Make sure the proxy doesn't outlive from, as the proxy is pointing directly to access contexts owned by from.
273CommandBufferAccessContext::CommandBufferAccessContext(const CommandBufferAccessContext &from, AsProxyContext dummy)
274 : CommandBufferAccessContext(from.sync_state_) {
275 // Copy only the needed fields out of from for a temporary, proxy command buffer context
276 cb_state_ = from.cb_state_;
277 queue_flags_ = from.queue_flags_;
278 destroyed_ = from.destroyed_;
279 access_log_ = from.access_log_; // potentially large, but no choice given tagging lookup.
280 command_number_ = from.command_number_;
281 subcommand_number_ = from.subcommand_number_;
282 reset_count_ = from.reset_count_;
283
284 const auto *from_context = from.GetCurrentAccessContext();
285 assert(from_context);
286
287 // Construct a fully resolved single access context out of from
288 const NoopBarrierAction noop_barrier;
289 for (AccessAddressType address_type : kAddressTypes) {
290 from_context->ResolveAccessRange(address_type, kFullRange, noop_barrier,
291 &cb_access_context_.GetAccessStateMap(address_type), nullptr);
292 }
293 // The proxy has flatten the current render pass context (if any), but the async contexts are needed for hazard detection
294 cb_access_context_.ImportAsyncContexts(*from_context);
295
296 events_context_ = from.events_context_;
297
298 // We don't want to copy the full render_pass_context_ history just for the proxy.
299}
300
301std::string CommandBufferAccessContext::FormatUsage(const ResourceUsageTag tag) const {
John Zulauf397e68b2022-04-19 11:44:07 -0600302 if (tag >= access_log_.size()) return std::string();
303
John Zulauf4fa68462021-04-26 21:04:22 -0600304 std::stringstream out;
305 assert(tag < access_log_.size());
306 const auto &record = access_log_[tag];
John Zulauf397e68b2022-04-19 11:44:07 -0600307 out << record;
308 if (cb_state_.get() != record.cb_state) {
309 out << SyncNodeFormatter(*sync_state_, record.cb_state);
John Zulauf4fa68462021-04-26 21:04:22 -0600310 }
John Zulaufd142c9a2022-04-12 14:22:44 -0600311 out << ", reset_no: " << std::to_string(record.reset_count);
John Zulauf4fa68462021-04-26 21:04:22 -0600312 return out.str();
313}
John Zulauf397e68b2022-04-19 11:44:07 -0600314
John Zulauf4fa68462021-04-26 21:04:22 -0600315std::string CommandBufferAccessContext::FormatUsage(const ResourceFirstAccess &access) const {
316 std::stringstream out;
317 out << "(recorded_usage: " << string_UsageIndex(access.usage_index);
318 out << ", " << FormatUsage(access.tag) << ")";
319 return out.str();
320}
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700321
John Zulauf397e68b2022-04-19 11:44:07 -0600322std::string CommandExecutionContext::FormatHazard(const HazardResult &hazard) const {
John Zulauf1dae9192020-06-16 15:46:44 -0600323 std::stringstream out;
John Zulauf397e68b2022-04-19 11:44:07 -0600324 out << hazard;
325 out << ", " << FormatUsage(hazard.tag) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600326 return out.str();
327}
328
John Zulaufd14743a2020-07-03 09:42:39 -0600329// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
330// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
331// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700332static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700333static const SyncStageAccessFlags kColorAttachmentAccessScope =
334 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
335 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
336 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
337 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700338static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
339 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT_KHR | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700340static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
341 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
342 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
343 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700344static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700345static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600346
John Zulauf8e3c3e92021-01-06 11:19:36 -0700347ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700348 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700349 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
350 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
351 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
352
John Zulaufee984022022-04-13 16:39:50 -0600353// Sometimes we have an internal access conflict, and we using the kInvalidTag to set and detect in temporary/proxy contexts
354static const ResourceUsageTag kInvalidTag(ResourceUsageRecord::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600355
Jeremy Gebben62c3bf42021-07-21 15:38:24 -0600356static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) { return bindable.GetFakeBaseAddress(); }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600357
locke-lunarg3c038002020-04-30 23:08:08 -0600358inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
359 if (size == VK_WHOLE_SIZE) {
360 return (whole_size - offset);
361 }
362 return size;
363}
364
John Zulauf3e86bf02020-09-12 10:47:57 -0600365static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
366 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
367}
368
John Zulauf16adfc92020-04-08 10:28:33 -0600369template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600370static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600371 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
372}
373
John Zulauf355e49b2020-04-24 15:11:15 -0600374static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600375
John Zulauf3e86bf02020-09-12 10:47:57 -0600376static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
377 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
378}
379
380static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
381 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
382}
383
John Zulauf4a6105a2020-11-17 15:11:05 -0700384// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
385//
John Zulauf10f1f522020-12-18 12:00:35 -0700386// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
387//
John Zulauf4a6105a2020-11-17 15:11:05 -0700388// Usage:
389// Constructor() -- initializes the generator to point to the begin of the space declared.
390// * -- the current range of the generator empty signfies end
391// ++ -- advance to the next non-empty range (or end)
392
393// A wrapper for a single range with the same semantics as the actual generators below
394template <typename KeyType>
395class SingleRangeGenerator {
396 public:
397 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700398 const KeyType &operator*() const { return current_; }
399 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700400 SingleRangeGenerator &operator++() {
401 current_ = KeyType(); // just one real range
402 return *this;
403 }
404
405 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
406
407 private:
408 SingleRangeGenerator() = default;
409 const KeyType range_;
410 KeyType current_;
411};
412
John Zulaufae842002021-04-15 18:20:55 -0600413// Generate the ranges that are the intersection of range and the entries in the RangeMap
414template <typename RangeMap, typename KeyType = typename RangeMap::key_type>
415class MapRangesRangeGenerator {
John Zulauf4a6105a2020-11-17 15:11:05 -0700416 public:
John Zulaufd5115702021-01-18 12:34:33 -0700417 // Default constructed is safe to dereference for "empty" test, but for no other operation.
John Zulaufae842002021-04-15 18:20:55 -0600418 MapRangesRangeGenerator() : range_(), map_(nullptr), map_pos_(), current_() {
John Zulaufd5115702021-01-18 12:34:33 -0700419 // Default construction for KeyType *must* be empty range
420 assert(current_.empty());
421 }
John Zulaufae842002021-04-15 18:20:55 -0600422 MapRangesRangeGenerator(const RangeMap &filter, const KeyType &range) : range_(range), map_(&filter), map_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700423 SeekBegin();
424 }
John Zulaufae842002021-04-15 18:20:55 -0600425 MapRangesRangeGenerator(const MapRangesRangeGenerator &from) = default;
John Zulaufd5115702021-01-18 12:34:33 -0700426
John Zulauf4a6105a2020-11-17 15:11:05 -0700427 const KeyType &operator*() const { return current_; }
428 const KeyType *operator->() const { return &current_; }
John Zulaufae842002021-04-15 18:20:55 -0600429 MapRangesRangeGenerator &operator++() {
430 ++map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700431 UpdateCurrent();
432 return *this;
433 }
434
John Zulaufae842002021-04-15 18:20:55 -0600435 bool operator==(const MapRangesRangeGenerator &other) const { return current_ == other.current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700436
John Zulaufae842002021-04-15 18:20:55 -0600437 protected:
John Zulauf4a6105a2020-11-17 15:11:05 -0700438 void UpdateCurrent() {
John Zulaufae842002021-04-15 18:20:55 -0600439 if (map_pos_ != map_->cend()) {
440 current_ = range_ & map_pos_->first;
John Zulauf4a6105a2020-11-17 15:11:05 -0700441 } else {
442 current_ = KeyType();
443 }
444 }
445 void SeekBegin() {
John Zulaufae842002021-04-15 18:20:55 -0600446 map_pos_ = map_->lower_bound(range_);
John Zulauf4a6105a2020-11-17 15:11:05 -0700447 UpdateCurrent();
448 }
John Zulaufae842002021-04-15 18:20:55 -0600449
450 // Adding this functionality here, to avoid gratuitous Base:: qualifiers in the derived class
451 // Note: Not exposed in this classes public interface to encourage using a consistent ++/empty generator semantic
452 template <typename Pred>
453 MapRangesRangeGenerator &PredicatedIncrement(Pred &pred) {
454 do {
455 ++map_pos_;
456 } while (map_pos_ != map_->cend() && map_pos_->first.intersects(range_) && !pred(map_pos_));
457 UpdateCurrent();
458 return *this;
459 }
460
John Zulauf4a6105a2020-11-17 15:11:05 -0700461 const KeyType range_;
John Zulaufae842002021-04-15 18:20:55 -0600462 const RangeMap *map_;
463 typename RangeMap::const_iterator map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700464 KeyType current_;
465};
John Zulaufd5115702021-01-18 12:34:33 -0700466using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulaufae842002021-04-15 18:20:55 -0600467using EventSimpleRangeGenerator = MapRangesRangeGenerator<SyncEventState::ScopeMap>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700468
John Zulaufae842002021-04-15 18:20:55 -0600469// Generate the ranges for entries meeting the predicate that are the intersection of range and the entries in the RangeMap
470template <typename RangeMap, typename Predicate, typename KeyType = typename RangeMap::key_type>
471class PredicatedMapRangesRangeGenerator : public MapRangesRangeGenerator<RangeMap, KeyType> {
472 public:
473 using Base = MapRangesRangeGenerator<RangeMap, KeyType>;
474 // Default constructed is safe to dereference for "empty" test, but for no other operation.
475 PredicatedMapRangesRangeGenerator() : Base(), pred_() {}
476 PredicatedMapRangesRangeGenerator(const RangeMap &filter, const KeyType &range, Predicate pred)
477 : Base(filter, range), pred_(pred) {}
478 PredicatedMapRangesRangeGenerator(const PredicatedMapRangesRangeGenerator &from) = default;
479
480 PredicatedMapRangesRangeGenerator &operator++() {
481 Base::PredicatedIncrement(pred_);
482 return *this;
483 }
484
485 protected:
486 Predicate pred_;
487};
John Zulauf4a6105a2020-11-17 15:11:05 -0700488
489// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulaufae842002021-04-15 18:20:55 -0600490// Templated to allow for different Range generators or map sources...
491template <typename RangeMap, typename RangeGen, typename KeyType = typename RangeMap::key_type>
John Zulauf4a6105a2020-11-17 15:11:05 -0700492class FilteredGeneratorGenerator {
493 public:
John Zulaufd5115702021-01-18 12:34:33 -0700494 // Default constructed is safe to dereference for "empty" test, but for no other operation.
495 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
496 // Default construction for KeyType *must* be empty range
497 assert(current_.empty());
498 }
John Zulaufae842002021-04-15 18:20:55 -0600499 FilteredGeneratorGenerator(const RangeMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700500 SeekBegin();
501 }
John Zulaufd5115702021-01-18 12:34:33 -0700502 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700503 const KeyType &operator*() const { return current_; }
504 const KeyType *operator->() const { return &current_; }
505 FilteredGeneratorGenerator &operator++() {
506 KeyType gen_range = GenRange();
507 KeyType filter_range = FilterRange();
508 current_ = KeyType();
509 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
510 if (gen_range.end > filter_range.end) {
511 // if the generated range is beyond the filter_range, advance the filter range
512 filter_range = AdvanceFilter();
513 } else {
514 gen_range = AdvanceGen();
515 }
516 current_ = gen_range & filter_range;
517 }
518 return *this;
519 }
520
521 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
522
523 private:
524 KeyType AdvanceFilter() {
525 ++filter_pos_;
526 auto filter_range = FilterRange();
527 if (filter_range.valid()) {
528 FastForwardGen(filter_range);
529 }
530 return filter_range;
531 }
532 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700533 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700534 auto gen_range = GenRange();
535 if (gen_range.valid()) {
536 FastForwardFilter(gen_range);
537 }
538 return gen_range;
539 }
540
541 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700542 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700543
544 KeyType FastForwardFilter(const KeyType &range) {
545 auto filter_range = FilterRange();
546 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700547 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700548 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
549 if (retry_count < kRetryLimit) {
550 ++filter_pos_;
551 filter_range = FilterRange();
552 retry_count++;
553 } else {
554 // Okay we've tried walking, do a seek.
555 filter_pos_ = filter_->lower_bound(range);
556 break;
557 }
558 }
559 return FilterRange();
560 }
561
562 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
563 // faster.
564 KeyType FastForwardGen(const KeyType &range) {
565 auto gen_range = GenRange();
566 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700567 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700568 gen_range = GenRange();
569 }
570 return gen_range;
571 }
572
573 void SeekBegin() {
574 auto gen_range = GenRange();
575 if (gen_range.empty()) {
576 current_ = KeyType();
577 filter_pos_ = filter_->cend();
578 } else {
579 filter_pos_ = filter_->lower_bound(gen_range);
580 current_ = gen_range & FilterRange();
581 }
582 }
583
John Zulaufae842002021-04-15 18:20:55 -0600584 const RangeMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700585 RangeGen gen_;
John Zulaufae842002021-04-15 18:20:55 -0600586 typename RangeMap::const_iterator filter_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700587 KeyType current_;
588};
589
590using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
591
John Zulauf5c5e88d2019-12-26 11:22:02 -0700592
John Zulauf3e86bf02020-09-12 10:47:57 -0600593ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
594 VkDeviceSize stride) {
595 VkDeviceSize range_start = offset + first_index * stride;
596 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600597 if (count == UINT32_MAX) {
598 range_size = buf_whole_size - range_start;
599 } else {
600 range_size = count * stride;
601 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600602 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600603}
604
locke-lunarg654e3692020-06-04 17:19:15 -0600605SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
606 VkShaderStageFlagBits stage_flag) {
607 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
608 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
609 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
610 }
611 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
612 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
613 assert(0);
614 }
615 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
616 return stage_access->second.uniform_read;
617 }
618
619 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
620 // Because if write hazard happens, read hazard might or might not happen.
621 // But if write hazard doesn't happen, read hazard is impossible to happen.
622 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700623 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600624 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700625 // TODO: sampled_read
626 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600627}
628
locke-lunarg37047832020-06-12 13:44:45 -0600629bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
630 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
631 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
632 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
633 ? true
634 : false;
635}
636
637bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
638 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
639 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
640 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
641 ? true
642 : false;
643}
644
John Zulauf355e49b2020-04-24 15:11:15 -0600645// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600646template <typename Action>
647static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
648 Action &action) {
649 // At this point the "apply over range" logic only supports a single memory binding
650 if (!SimpleBinding(image_state)) return;
651 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600652 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700653 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
654 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600655 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700656 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600657 }
658}
659
John Zulauf7635de32020-05-29 17:14:15 -0600660// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
661// Used by both validation and record operations
662//
663// The signature for Action() reflect the needs of both uses.
664template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700665void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
666 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600667 const auto &rp_ci = rp_state.createInfo;
668 const auto *attachment_ci = rp_ci.pAttachments;
669 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
670
671 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
672 const auto *color_attachments = subpass_ci.pColorAttachments;
673 const auto *color_resolve = subpass_ci.pResolveAttachments;
674 if (color_resolve && color_attachments) {
675 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
676 const auto &color_attach = color_attachments[i].attachment;
677 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
678 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
679 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700680 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
681 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600682 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700683 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
684 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600685 }
686 }
687 }
688
689 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700690 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600691 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
692 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
693 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
694 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
695 const auto src_ci = attachment_ci[src_at];
696 // The formats are required to match so we can pick either
697 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
698 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
699 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600700
701 // Figure out which aspects are actually touched during resolve operations
702 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700703 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600704 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600705 aspect_string = "depth/stencil";
706 } else if (resolve_depth) {
707 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700708 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600709 aspect_string = "depth";
710 } else if (resolve_stencil) {
711 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700712 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600713 aspect_string = "stencil";
714 }
715
John Zulaufd0ec59f2021-03-13 14:25:08 -0700716 if (aspect_string) {
717 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
718 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
719 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
720 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600721 }
722 }
723}
724
725// Action for validating resolve operations
726class ValidateResolveAction {
727 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700728 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulaufbb890452021-12-14 11:30:18 -0700729 const CommandExecutionContext &exec_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600730 : render_pass_(render_pass),
731 subpass_(subpass),
732 context_(context),
John Zulaufbb890452021-12-14 11:30:18 -0700733 exec_context_(exec_context),
John Zulauf7635de32020-05-29 17:14:15 -0600734 func_name_(func_name),
735 skip_(false) {}
736 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700737 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
738 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600739 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700740 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600741 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700742 skip_ |=
John Zulaufbb890452021-12-14 11:30:18 -0700743 exec_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
744 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
745 " to resolve attachment %" PRIu32 ". Access info %s.",
746 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf397e68b2022-04-19 11:44:07 -0600747 attachment_name, src_at, dst_at, exec_context_.FormatHazard(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600748 }
749 }
750 // Providing a mechanism for the constructing caller to get the result of the validation
751 bool GetSkip() const { return skip_; }
752
753 private:
754 VkRenderPass render_pass_;
755 const uint32_t subpass_;
756 const AccessContext &context_;
John Zulaufbb890452021-12-14 11:30:18 -0700757 const CommandExecutionContext &exec_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600758 const char *func_name_;
759 bool skip_;
760};
761
762// Update action for resolve operations
763class UpdateStateResolveAction {
764 public:
John Zulauf14940722021-04-12 15:19:02 -0600765 UpdateStateResolveAction(AccessContext &context, ResourceUsageTag tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700766 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
767 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600768 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700769 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600770 }
771
772 private:
773 AccessContext &context_;
John Zulauf14940722021-04-12 15:19:02 -0600774 const ResourceUsageTag tag_;
John Zulauf7635de32020-05-29 17:14:15 -0600775};
776
John Zulauf59e25072020-07-17 10:55:21 -0600777void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
John Zulauf14940722021-04-12 15:19:02 -0600778 const SyncStageAccessFlags &prior_, const ResourceUsageTag tag_) {
John Zulauf4fa68462021-04-26 21:04:22 -0600779 access_state = layer_data::make_unique<const ResourceAccessState>(*access_state_);
John Zulauf59e25072020-07-17 10:55:21 -0600780 usage_index = usage_index_;
781 hazard = hazard_;
782 prior_access = prior_;
783 tag = tag_;
784}
785
John Zulauf4fa68462021-04-26 21:04:22 -0600786void HazardResult::AddRecordedAccess(const ResourceFirstAccess &first_access) {
787 recorded_access = layer_data::make_unique<const ResourceFirstAccess>(first_access);
788}
789
John Zulauf540266b2020-04-06 18:54:53 -0600790AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
791 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600792 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600793 Reset();
794 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700795 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
796 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600797 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600798 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600799 const auto prev_pass = prev_dep.first->pass;
800 const auto &prev_barriers = prev_dep.second;
801 assert(prev_dep.second.size());
802 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
803 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700804 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600805
806 async_.reserve(subpass_dep.async.size());
807 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700808 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600809 }
John Zulauf22aefed2021-03-11 18:14:35 -0700810 if (has_barrier_from_external) {
811 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
812 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
813 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600814 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600815 if (subpass_dep.barrier_to_external.size()) {
816 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600817 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700818}
819
John Zulauf5f13a792020-03-10 07:31:21 -0600820template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700821HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600822 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600823 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600824 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600825
826 HazardResult hazard;
827 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
828 hazard = detector.Detect(prev);
829 }
830 return hazard;
831}
832
John Zulauf4a6105a2020-11-17 15:11:05 -0700833template <typename Action>
834void AccessContext::ForAll(Action &&action) {
835 for (const auto address_type : kAddressTypes) {
836 auto &accesses = GetAccessStateMap(address_type);
837 for (const auto &access : accesses) {
838 action(address_type, access);
839 }
840 }
841}
842
John Zulauf3d84f1b2020-03-09 13:33:25 -0600843// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
844// the DAG of the contexts (for example subpasses)
845template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700846HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600847 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600848 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600849
John Zulauf1a224292020-06-30 14:52:13 -0600850 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600851 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
852 // so we'll check these first
853 for (const auto &async_context : async_) {
854 hazard = async_context->DetectAsyncHazard(type, detector, range);
855 if (hazard.hazard) return hazard;
856 }
John Zulauf5f13a792020-03-10 07:31:21 -0600857 }
858
John Zulauf1a224292020-06-30 14:52:13 -0600859 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600860
John Zulauf69133422020-05-20 14:55:53 -0600861 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600862 const auto the_end = accesses.cend(); // End is not invalidated
863 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600864 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600865
John Zulauf3cafbf72021-03-26 16:55:19 -0600866 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600867 // Cover any leading gap, or gap between entries
868 if (detect_prev) {
869 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
870 // Cover any leading gap, or gap between entries
871 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600872 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600873 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600874 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600875 if (hazard.hazard) return hazard;
876 }
John Zulauf69133422020-05-20 14:55:53 -0600877 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
878 gap.begin = pos->first.end;
879 }
880
881 hazard = detector.Detect(pos);
882 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600883 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600884 }
885
886 if (detect_prev) {
887 // Detect in the trailing empty as needed
888 gap.end = range.end;
889 if (gap.non_empty()) {
890 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600891 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600892 }
893
894 return hazard;
895}
896
897// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
898template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700899HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
900 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600901 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600902 auto pos = accesses.lower_bound(range);
903 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600904
John Zulauf3d84f1b2020-03-09 13:33:25 -0600905 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600906 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700907 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600908 if (hazard.hazard) break;
909 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600910 }
John Zulauf16adfc92020-04-08 10:28:33 -0600911
John Zulauf3d84f1b2020-03-09 13:33:25 -0600912 return hazard;
913}
914
John Zulaufb02c1eb2020-10-06 16:33:36 -0600915struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700916 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600917 void operator()(ResourceAccessState *access) const {
918 assert(access);
919 access->ApplyBarriers(barriers, true);
920 }
921 const std::vector<SyncBarrier> &barriers;
922};
923
John Zulauf22aefed2021-03-11 18:14:35 -0700924struct ApplyTrackbackStackAction {
925 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
926 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
927 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600928 void operator()(ResourceAccessState *access) const {
929 assert(access);
930 assert(!access->HasPendingState());
931 access->ApplyBarriers(barriers, false);
John Zulaufee984022022-04-13 16:39:50 -0600932 // NOTE: We can use invalid tag, as these barriers do no include layout transitions (would assert in SetWrite)
933 access->ApplyPendingBarriers(kInvalidTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700934 if (previous_barrier) {
935 assert(bool(*previous_barrier));
936 (*previous_barrier)(access);
937 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600938 }
939 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700940 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600941};
942
943// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
944// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
945// *different* map from dest.
946// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
947// range [first, last)
948template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600949static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
950 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600951 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600952 auto at = entry;
953 for (auto pos = first; pos != last; ++pos) {
954 // Every member of the input iterator range must fit within the remaining portion of entry
955 assert(at->first.includes(pos->first));
956 assert(at != dest->end());
957 // Trim up at to the same size as the entry to resolve
958 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600959 auto access = pos->second; // intentional copy
960 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600961 at->second.Resolve(access);
962 ++at; // Go to the remaining unused section of entry
963 }
964}
965
John Zulaufa0a98292020-09-18 09:30:10 -0600966static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
967 SyncBarrier merged = {};
968 for (const auto &barrier : barriers) {
969 merged.Merge(barrier);
970 }
971 return merged;
972}
973
John Zulaufb02c1eb2020-10-06 16:33:36 -0600974template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700975void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600976 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
977 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600978 if (!range.non_empty()) return;
979
John Zulauf355e49b2020-04-24 15:11:15 -0600980 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
981 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600982 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600983 if (current->pos_B->valid) {
984 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600985 auto access = src_pos->second; // intentional copy
986 barrier_action(&access);
987
John Zulauf16adfc92020-04-08 10:28:33 -0600988 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600989 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
990 trimmed->second.Resolve(access);
991 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600992 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600993 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600994 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600995 }
John Zulauf16adfc92020-04-08 10:28:33 -0600996 } else {
997 // we have to descend to fill this gap
998 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700999 ResourceAccessRange recurrence_range = current_range;
1000 // The current context is empty for the current range, so recur to fill the gap.
1001 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
1002 // is not valid, to minimize that recurrence
1003 if (current->pos_B.at_end()) {
1004 // Do the remainder here....
1005 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -06001006 } else {
John Zulauf22aefed2021-03-11 18:14:35 -07001007 // Recur only over the range until B becomes valid (within the limits of range).
1008 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -06001009 }
John Zulauf22aefed2021-03-11 18:14:35 -07001010 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
1011
John Zulauf355e49b2020-04-24 15:11:15 -06001012 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
1013 // iterator of the outer while.
1014
1015 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
1016 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
1017 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -07001018 const auto seek_to = recurrence_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
locke-lunarg88dbb542020-06-23 22:05:42 -06001019 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -06001020 current.seek(seek_to);
1021 } else if (!current->pos_A->valid && infill_state) {
1022 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
1023 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
1024 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -06001025 }
John Zulauf5f13a792020-03-10 07:31:21 -06001026 }
ziga-lunargf0e27ad2022-03-28 00:44:12 +02001027 if (current->range.non_empty()) {
1028 ++current;
1029 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001030 }
John Zulauf1a224292020-06-30 14:52:13 -06001031
1032 // Infill if range goes passed both the current and resolve map prior contents
1033 if (recur_to_infill && (current->range.end < range.end)) {
1034 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -07001035 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -06001036 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001037}
1038
John Zulauf22aefed2021-03-11 18:14:35 -07001039template <typename BarrierAction>
1040void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
1041 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
1042 const BarrierAction &previous_barrier) const {
1043 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
1044 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
1045}
1046
John Zulauf43cc7462020-12-03 12:33:12 -07001047void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -07001048 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
1049 const ResourceAccessStateFunction *previous_barrier) const {
1050 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -06001051 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -07001052 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
1053 ResourceAccessState state_copy;
1054 if (previous_barrier) {
1055 assert(bool(*previous_barrier));
1056 state_copy = *infill_state;
1057 (*previous_barrier)(&state_copy);
1058 infill_state = &state_copy;
1059 }
1060 sparse_container::update_range_value(*descent_map, range, *infill_state,
1061 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -06001062 }
1063 } else {
1064 // Look for something to fill the gap further along.
1065 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -07001066 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufbb890452021-12-14 11:30:18 -07001067 prev_dep.source_subpass->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001068 }
John Zulauf5f13a792020-03-10 07:31:21 -06001069 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001070}
1071
John Zulauf4a6105a2020-11-17 15:11:05 -07001072// Non-lazy import of all accesses, WaitEvents needs this.
1073void AccessContext::ResolvePreviousAccesses() {
1074 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -07001075 if (!prev_.size()) return; // If no previous contexts, nothing to do
1076
John Zulauf4a6105a2020-11-17 15:11:05 -07001077 for (const auto address_type : kAddressTypes) {
1078 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
1079 }
1080}
1081
John Zulauf43cc7462020-12-03 12:33:12 -07001082AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
1083 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -06001084}
1085
John Zulauf1507ee42020-05-18 11:33:09 -06001086static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001087 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1088 ? SYNC_ACCESS_INDEX_NONE
1089 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
1090 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001091 return stage_access;
1092}
1093static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001094 const auto stage_access =
1095 (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1096 ? SYNC_ACCESS_INDEX_NONE
1097 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
1098 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001099 return stage_access;
1100}
1101
John Zulauf7635de32020-05-29 17:14:15 -06001102// Caller must manage returned pointer
1103static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001104 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -06001105 auto *proxy = new AccessContext(context);
John Zulaufee984022022-04-13 16:39:50 -06001106 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kInvalidTag);
1107 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kInvalidTag);
John Zulauf7635de32020-05-29 17:14:15 -06001108 return proxy;
1109}
1110
John Zulaufb02c1eb2020-10-06 16:33:36 -06001111template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001112void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1113 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
1114 const ResourceAccessState *infill_state) const {
1115 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1116 if (!attachment_gen) return;
1117
1118 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1119 const AccessAddressType address_type = view_gen.GetAddressType();
1120 for (; range_gen->non_empty(); ++range_gen) {
1121 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001122 }
John Zulauf62f10592020-04-03 12:20:02 -06001123}
1124
John Zulauf7635de32020-05-29 17:14:15 -06001125// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulaufbb890452021-12-14 11:30:18 -07001126bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &exec_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001127 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001128 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001129 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001130 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
1131 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
1132 // those affects have not been recorded yet.
1133 //
1134 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1135 // to apply and only copy then, if this proves a hot spot.
1136 std::unique_ptr<AccessContext> proxy_for_prev;
1137 TrackBack proxy_track_back;
1138
John Zulauf355e49b2020-04-24 15:11:15 -06001139 const auto &transitions = rp_state.subpass_transitions[subpass];
1140 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001141 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1142
1143 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001144 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001145 if (prev_needs_proxy) {
1146 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001147 proxy_for_prev.reset(
John Zulaufbb890452021-12-14 11:30:18 -07001148 CreateStoreResolveProxyContext(*track_back->source_subpass, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -06001149 proxy_track_back = *track_back;
John Zulaufbb890452021-12-14 11:30:18 -07001150 proxy_track_back.source_subpass = proxy_for_prev.get();
John Zulauf7635de32020-05-29 17:14:15 -06001151 }
1152 track_back = &proxy_track_back;
1153 }
1154 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001155 if (hazard.hazard) {
John Zulaufee984022022-04-13 16:39:50 -06001156 if (hazard.tag == kInvalidTag) {
John Zulaufbb890452021-12-14 11:30:18 -07001157 skip |= exec_context.GetSyncState().LogError(
John Zulaufee984022022-04-13 16:39:50 -06001158 rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
1159 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1160 " image layout transition (old_layout: %s, new_layout: %s) after store/resolve operation in subpass %" PRIu32,
1161 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1162 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout), transition.prev_pass);
1163 } else {
John Zulaufbb890452021-12-14 11:30:18 -07001164 skip |= exec_context.GetSyncState().LogError(
John Zulaufee984022022-04-13 16:39:50 -06001165 rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
1166 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1167 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1168 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1169 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf397e68b2022-04-19 11:44:07 -06001170 exec_context.FormatHazard(hazard).c_str());
John Zulaufee984022022-04-13 16:39:50 -06001171 }
John Zulauf355e49b2020-04-24 15:11:15 -06001172 }
1173 }
1174 return skip;
1175}
1176
John Zulaufbb890452021-12-14 11:30:18 -07001177bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &exec_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001178 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001179 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001180 bool skip = false;
1181 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001182
John Zulauf1507ee42020-05-18 11:33:09 -06001183 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1184 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001185 const auto &view_gen = attachment_views[i];
1186 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001187 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001188
1189 // Need check in the following way
1190 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1191 // vs. transition
1192 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1193 // for each aspect loaded.
1194
1195 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001196 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001197 const bool is_color = !(has_depth || has_stencil);
1198
1199 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001200 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001201
John Zulaufaff20662020-06-01 14:07:58 -06001202 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001203 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001204
John Zulaufb02c1eb2020-10-06 16:33:36 -06001205 bool checked_stencil = false;
John Zulauf57261402021-08-13 11:32:06 -06001206 if (is_color && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001207 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001208 aspect = "color";
1209 } else {
John Zulauf57261402021-08-13 11:32:06 -06001210 if (has_depth && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001211 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1212 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001213 aspect = "depth";
1214 }
John Zulauf57261402021-08-13 11:32:06 -06001215 if (!hazard.hazard && has_stencil && (stencil_load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001216 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1217 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001218 aspect = "stencil";
1219 checked_stencil = true;
1220 }
1221 }
1222
1223 if (hazard.hazard) {
1224 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulaufbb890452021-12-14 11:30:18 -07001225 const auto &sync_state = exec_context.GetSyncState();
John Zulaufee984022022-04-13 16:39:50 -06001226 if (hazard.tag == kInvalidTag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001227 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001228 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001229 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1230 " aspect %s during load with loadOp %s.",
1231 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1232 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001233 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001234 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001235 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001236 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf397e68b2022-04-19 11:44:07 -06001237 exec_context.FormatHazard(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001238 }
1239 }
1240 }
1241 }
1242 return skip;
1243}
1244
John Zulaufaff20662020-06-01 14:07:58 -06001245// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1246// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1247// store is part of the same Next/End operation.
1248// The latter is handled in layout transistion validation directly
John Zulaufbb890452021-12-14 11:30:18 -07001249bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &exec_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001250 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001251 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001252 bool skip = false;
1253 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001254
1255 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1256 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001257 const AttachmentViewGen &view_gen = attachment_views[i];
1258 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001259 const auto &ci = attachment_ci[i];
1260
1261 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1262 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1263 // sake, we treat DONT_CARE as writing.
1264 const bool has_depth = FormatHasDepth(ci.format);
1265 const bool has_stencil = FormatHasStencil(ci.format);
1266 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001267 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001268 if (!has_stencil && !store_op_stores) continue;
1269
1270 HazardResult hazard;
1271 const char *aspect = nullptr;
1272 bool checked_stencil = false;
1273 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001274 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1275 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001276 aspect = "color";
1277 } else {
John Zulauf57261402021-08-13 11:32:06 -06001278 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001279 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001280 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1281 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001282 aspect = "depth";
1283 }
1284 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001285 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1286 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001287 aspect = "stencil";
1288 checked_stencil = true;
1289 }
1290 }
1291
1292 if (hazard.hazard) {
1293 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1294 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulauf397e68b2022-04-19 11:44:07 -06001295 skip |= exec_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
1296 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1297 " %s aspect during store with %s %s. Access info %s",
1298 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
1299 op_type_string, store_op_string,
1300 exec_context.FormatHazard(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001301 }
1302 }
1303 }
1304 return skip;
1305}
1306
John Zulaufbb890452021-12-14 11:30:18 -07001307bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &exec_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001308 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1309 const char *func_name, uint32_t subpass) const {
John Zulaufbb890452021-12-14 11:30:18 -07001310 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, exec_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001311 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001312 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001313}
1314
John Zulauf06f6f1e2022-04-19 15:28:11 -06001315AccessContext::TrackBack *AccessContext::AddTrackBack(const AccessContext *context, const SyncBarrier &barrier) {
1316 prev_.emplace_back(context, barrier);
1317 return &prev_.back();
1318}
1319
1320void AccessContext::AddAsyncContext(const AccessContext *context) { async_.emplace_back(context); }
1321
John Zulauf3d84f1b2020-03-09 13:33:25 -06001322class HazardDetector {
1323 SyncStageAccessIndex usage_index_;
1324
1325 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001326 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf14940722021-04-12 15:19:02 -06001327 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001328 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001329 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001330 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001331};
1332
John Zulauf69133422020-05-20 14:55:53 -06001333class HazardDetectorWithOrdering {
1334 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001335 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001336
1337 public:
1338 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001339 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001340 }
John Zulauf14940722021-04-12 15:19:02 -06001341 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001342 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001343 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001344 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001345};
1346
John Zulauf16adfc92020-04-08 10:28:33 -06001347HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001348 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001349 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001350 const auto base_address = ResourceBaseAddress(buffer);
1351 HazardDetector detector(usage_index);
1352 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001353}
1354
John Zulauf69133422020-05-20 14:55:53 -06001355template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001356HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1357 DetectOptions options) const {
1358 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1359 if (!attachment_gen) return HazardResult();
1360
1361 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1362 const auto address_type = view_gen.GetAddressType();
1363 for (; range_gen->non_empty(); ++range_gen) {
1364 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1365 if (hazard.hazard) return hazard;
1366 }
1367
1368 return HazardResult();
1369}
1370
1371template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001372HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1373 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1374 const VkExtent3D &extent, DetectOptions options) const {
1375 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001376 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001377 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1378 base_address);
1379 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001380 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001381 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001382 if (hazard.hazard) return hazard;
1383 }
1384 return HazardResult();
1385}
John Zulauf110413c2021-03-20 05:38:38 -06001386template <typename Detector>
1387HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1388 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1389 if (!SimpleBinding(image)) return HazardResult();
1390 const auto base_address = ResourceBaseAddress(image);
1391 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1392 const auto address_type = ImageAddressType(image);
1393 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001394 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1395 if (hazard.hazard) return hazard;
1396 }
1397 return HazardResult();
1398}
John Zulauf69133422020-05-20 14:55:53 -06001399
John Zulauf540266b2020-04-06 18:54:53 -06001400HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1401 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1402 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001403 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1404 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001405 HazardDetector detector(current_usage);
1406 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001407}
1408
1409HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001410 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001411 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001412 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001413}
1414
John Zulaufd0ec59f2021-03-13 14:25:08 -07001415HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1416 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1417 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1418 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1419}
1420
John Zulauf69133422020-05-20 14:55:53 -06001421HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001422 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001423 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001424 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001425 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001426}
1427
John Zulauf3d84f1b2020-03-09 13:33:25 -06001428class BarrierHazardDetector {
1429 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001430 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001431 SyncStageAccessFlags src_access_scope)
1432 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1433
John Zulauf5f13a792020-03-10 07:31:21 -06001434 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1435 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001436 }
John Zulauf14940722021-04-12 15:19:02 -06001437 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001438 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001439 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001440 }
1441
1442 private:
1443 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001444 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001445 SyncStageAccessFlags src_access_scope_;
1446};
1447
John Zulauf4a6105a2020-11-17 15:11:05 -07001448class EventBarrierHazardDetector {
1449 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001450 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001451 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
John Zulauf14940722021-04-12 15:19:02 -06001452 ResourceUsageTag scope_tag)
John Zulauf4a6105a2020-11-17 15:11:05 -07001453 : usage_index_(usage_index),
1454 src_exec_scope_(src_exec_scope),
1455 src_access_scope_(src_access_scope),
1456 event_scope_(event_scope),
1457 scope_pos_(event_scope.cbegin()),
1458 scope_end_(event_scope.cend()),
1459 scope_tag_(scope_tag) {}
1460
1461 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1462 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1463 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1464 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1465 if (scope_pos_ == scope_end_) return HazardResult();
1466 if (!scope_pos_->first.intersects(pos->first)) {
1467 event_scope_.lower_bound(pos->first);
1468 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1469 }
1470
1471 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1472 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1473 }
John Zulauf14940722021-04-12 15:19:02 -06001474 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07001475 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1476 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1477 }
1478
1479 private:
1480 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001481 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001482 SyncStageAccessFlags src_access_scope_;
1483 const SyncEventState::ScopeMap &event_scope_;
1484 SyncEventState::ScopeMap::const_iterator scope_pos_;
1485 SyncEventState::ScopeMap::const_iterator scope_end_;
John Zulauf14940722021-04-12 15:19:02 -06001486 const ResourceUsageTag scope_tag_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001487};
1488
Jeremy Gebben40a22942020-12-22 14:22:06 -07001489HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001490 const SyncStageAccessFlags &src_access_scope,
1491 const VkImageSubresourceRange &subresource_range,
1492 const SyncEventState &sync_event, DetectOptions options) const {
1493 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1494 // first access scope map to use, and there's no easy way to plumb it in below.
1495 const auto address_type = ImageAddressType(image);
1496 const auto &event_scope = sync_event.FirstScope(address_type);
1497
1498 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1499 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001500 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001501}
1502
John Zulaufd0ec59f2021-03-13 14:25:08 -07001503HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1504 DetectOptions options) const {
1505 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1506 barrier.src_access_scope);
1507 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1508}
1509
Jeremy Gebben40a22942020-12-22 14:22:06 -07001510HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001511 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001512 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001513 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001514 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001515 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001516}
1517
Jeremy Gebben40a22942020-12-22 14:22:06 -07001518HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001519 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001520 const VkImageMemoryBarrier &barrier) const {
1521 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1522 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1523 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1524}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001525HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001526 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001527 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001528}
John Zulauf355e49b2020-04-24 15:11:15 -06001529
John Zulauf9cb530d2019-09-30 14:14:10 -06001530template <typename Flags, typename Map>
1531SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1532 SyncStageAccessFlags scope = 0;
1533 for (const auto &bit_scope : map) {
1534 if (flag_mask < bit_scope.first) break;
1535
1536 if (flag_mask & bit_scope.first) {
1537 scope |= bit_scope.second;
1538 }
1539 }
1540 return scope;
1541}
1542
Jeremy Gebben40a22942020-12-22 14:22:06 -07001543SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001544 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1545}
1546
Jeremy Gebben40a22942020-12-22 14:22:06 -07001547SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1548 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001549}
1550
Jeremy Gebben40a22942020-12-22 14:22:06 -07001551// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1552SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001553 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1554 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1555 // of the union of all stage/access types for all the stages and the same unions for the access mask...
John Zulauf9cb530d2019-09-30 14:14:10 -06001556 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1557}
1558
1559template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001560void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001561 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1562 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001563 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001564 auto pos = accesses->lower_bound(range);
1565 if (pos == accesses->end() || !pos->first.intersects(range)) {
1566 // The range is empty, fill it with a default value.
1567 pos = action.Infill(accesses, pos, range);
1568 } else if (range.begin < pos->first.begin) {
1569 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001570 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001571 } else if (pos->first.begin < range.begin) {
1572 // Trim the beginning if needed
1573 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1574 ++pos;
1575 }
1576
1577 const auto the_end = accesses->end();
1578 while ((pos != the_end) && pos->first.intersects(range)) {
1579 if (pos->first.end > range.end) {
1580 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1581 }
1582
1583 pos = action(accesses, pos);
1584 if (pos == the_end) break;
1585
1586 auto next = pos;
1587 ++next;
1588 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1589 // Need to infill if next is disjoint
1590 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001591 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001592 next = action.Infill(accesses, next, new_range);
1593 }
1594 pos = next;
1595 }
1596}
John Zulaufd5115702021-01-18 12:34:33 -07001597
1598// Give a comparable interface for range generators and ranges
1599template <typename Action>
1600inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1601 assert(range);
1602 UpdateMemoryAccessState(accesses, *range, action);
1603}
1604
John Zulauf4a6105a2020-11-17 15:11:05 -07001605template <typename Action, typename RangeGen>
1606void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1607 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001608 RangeGen &range_gen = *range_gen_arg; // Non-const references must be * by style requirement but deref-ing * iterator is a pain
John Zulauf4a6105a2020-11-17 15:11:05 -07001609 for (; range_gen->non_empty(); ++range_gen) {
1610 UpdateMemoryAccessState(accesses, *range_gen, action);
1611 }
1612}
John Zulauf9cb530d2019-09-30 14:14:10 -06001613
John Zulaufd0ec59f2021-03-13 14:25:08 -07001614template <typename Action, typename RangeGen>
1615void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1616 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1617 for (; range_gen->non_empty(); ++range_gen) {
1618 UpdateMemoryAccessState(accesses, *range_gen, action);
1619 }
1620}
John Zulauf9cb530d2019-09-30 14:14:10 -06001621struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001622 using Iterator = ResourceAccessRangeMap::iterator;
1623 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001624 // this is only called on gaps, and never returns a gap.
1625 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001626 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001627 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001628 }
John Zulauf5f13a792020-03-10 07:31:21 -06001629
John Zulauf5c5e88d2019-12-26 11:22:02 -07001630 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001631 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001632 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001633 return pos;
1634 }
1635
John Zulauf43cc7462020-12-03 12:33:12 -07001636 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf14940722021-04-12 15:19:02 -06001637 SyncOrdering ordering_rule_, ResourceUsageTag tag_)
John Zulauf8e3c3e92021-01-06 11:19:36 -07001638 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001639 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001640 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001641 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001642 const SyncOrdering ordering_rule;
John Zulauf14940722021-04-12 15:19:02 -06001643 const ResourceUsageTag tag;
John Zulauf9cb530d2019-09-30 14:14:10 -06001644};
1645
John Zulauf4a6105a2020-11-17 15:11:05 -07001646// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001647struct PipelineBarrierOp {
1648 SyncBarrier barrier;
1649 bool layout_transition;
1650 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1651 : barrier(barrier_), layout_transition(layout_transition_) {}
1652 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001653 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001654 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1655};
John Zulauf4a6105a2020-11-17 15:11:05 -07001656// The barrier operation for wait events
1657struct WaitEventBarrierOp {
John Zulauf14940722021-04-12 15:19:02 -06001658 ResourceUsageTag scope_tag;
John Zulauf4a6105a2020-11-17 15:11:05 -07001659 SyncBarrier barrier;
1660 bool layout_transition;
John Zulauf14940722021-04-12 15:19:02 -06001661 WaitEventBarrierOp(const ResourceUsageTag scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1662 : scope_tag(scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
John Zulauf4a6105a2020-11-17 15:11:05 -07001663 WaitEventBarrierOp() = default;
John Zulauf14940722021-04-12 15:19:02 -06001664 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(scope_tag, barrier, layout_transition); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001665};
John Zulauf1e331ec2020-12-04 18:29:38 -07001666
John Zulauf4a6105a2020-11-17 15:11:05 -07001667// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1668// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1669// of a collection is known/present.
John Zulauf5c628d02021-05-04 15:46:36 -06001670template <typename BarrierOp, typename OpVector = std::vector<BarrierOp>>
John Zulauf89311b42020-09-29 16:28:47 -06001671class ApplyBarrierOpsFunctor {
1672 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001673 using Iterator = ResourceAccessRangeMap::iterator;
John Zulauf5c628d02021-05-04 15:46:36 -06001674 // Only called with a gap, and pos at the lower_bound(range)
1675 inline Iterator Infill(ResourceAccessRangeMap *accesses, const Iterator &pos, const ResourceAccessRange &range) const {
1676 if (!infill_default_) {
1677 return pos;
1678 }
1679 ResourceAccessState default_state;
1680 auto inserted = accesses->insert(pos, std::make_pair(range, default_state));
1681 return inserted;
1682 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001683
John Zulauf5c628d02021-05-04 15:46:36 -06001684 Iterator operator()(ResourceAccessRangeMap *accesses, const Iterator &pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001685 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001686 for (const auto &op : barrier_ops_) {
1687 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001688 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001689
John Zulauf89311b42020-09-29 16:28:47 -06001690 if (resolve_) {
1691 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1692 // another walk
1693 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001694 }
1695 return pos;
1696 }
1697
John Zulauf89311b42020-09-29 16:28:47 -06001698 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf5c628d02021-05-04 15:46:36 -06001699 ApplyBarrierOpsFunctor(bool resolve, typename OpVector::size_type size_hint, ResourceUsageTag tag)
1700 : resolve_(resolve), infill_default_(false), barrier_ops_(), tag_(tag) {
John Zulaufd5115702021-01-18 12:34:33 -07001701 barrier_ops_.reserve(size_hint);
1702 }
John Zulauf5c628d02021-05-04 15:46:36 -06001703 void EmplaceBack(const BarrierOp &op) {
1704 barrier_ops_.emplace_back(op);
1705 infill_default_ |= op.layout_transition;
1706 }
John Zulauf89311b42020-09-29 16:28:47 -06001707
1708 private:
1709 bool resolve_;
John Zulauf5c628d02021-05-04 15:46:36 -06001710 bool infill_default_;
1711 OpVector barrier_ops_;
John Zulauf14940722021-04-12 15:19:02 -06001712 const ResourceUsageTag tag_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001713};
1714
John Zulauf4a6105a2020-11-17 15:11:05 -07001715// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1716// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1717template <typename BarrierOp>
John Zulauf5c628d02021-05-04 15:46:36 -06001718class ApplyBarrierFunctor : public ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>> {
1719 using Base = ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>>;
1720
John Zulauf4a6105a2020-11-17 15:11:05 -07001721 public:
John Zulaufee984022022-04-13 16:39:50 -06001722 ApplyBarrierFunctor(const BarrierOp &barrier_op) : Base(false, 1, kInvalidTag) { Base::EmplaceBack(barrier_op); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001723};
1724
John Zulauf1e331ec2020-12-04 18:29:38 -07001725// This functor resolves the pendinging state.
John Zulauf5c628d02021-05-04 15:46:36 -06001726class ResolvePendingBarrierFunctor : public ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>> {
1727 using Base = ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>>;
1728
John Zulauf1e331ec2020-12-04 18:29:38 -07001729 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001730 ResolvePendingBarrierFunctor(ResourceUsageTag tag) : Base(true, 0, tag) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001731};
1732
John Zulauf8e3c3e92021-01-06 11:19:36 -07001733void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001734 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001735 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001736 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001737}
1738
John Zulauf8e3c3e92021-01-06 11:19:36 -07001739void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001740 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001741 if (!SimpleBinding(buffer)) return;
1742 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001743 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001744}
John Zulauf355e49b2020-04-24 15:11:15 -06001745
John Zulauf8e3c3e92021-01-06 11:19:36 -07001746void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001747 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1748 if (!SimpleBinding(image)) return;
1749 const auto base_address = ResourceBaseAddress(image);
1750 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1751 const auto address_type = ImageAddressType(image);
1752 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1753 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1754}
1755void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001756 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001757 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001758 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001759 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001760 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1761 base_address);
1762 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001763 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001764 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001765}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001766
1767void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
John Zulauf14940722021-04-12 15:19:02 -06001768 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001769 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1770 if (!gen) return;
1771 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1772 const auto address_type = view_gen.GetAddressType();
1773 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1774 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001775}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001776
John Zulauf8e3c3e92021-01-06 11:19:36 -07001777void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001778 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001779 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001780 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1781 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001782 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001783}
1784
John Zulaufd0ec59f2021-03-13 14:25:08 -07001785template <typename Action, typename RangeGen>
1786void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1787 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1788 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001789}
1790
1791template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001792void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1793 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1794 if (!gen) return;
1795 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001796}
1797
John Zulaufd0ec59f2021-03-13 14:25:08 -07001798void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1799 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001800 const ResourceUsageTag tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001801 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001802 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001803}
1804
John Zulaufd0ec59f2021-03-13 14:25:08 -07001805void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06001806 uint32_t subpass, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001807 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001808
1809 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1810 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001811 const auto &view_gen = attachment_views[i];
1812 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001813
1814 const auto &ci = attachment_ci[i];
1815 const bool has_depth = FormatHasDepth(ci.format);
1816 const bool has_stencil = FormatHasStencil(ci.format);
1817 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001818 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001819
1820 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001821 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1822 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001823 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001824 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001825 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1826 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001827 }
John Zulauf57261402021-08-13 11:32:06 -06001828 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001829 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001830 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1831 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001832 }
1833 }
1834 }
1835 }
1836}
1837
John Zulauf540266b2020-04-06 18:54:53 -06001838template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001839void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001840 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001841 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001842 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001843 }
1844}
1845
1846void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001847 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1848 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001849 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001850 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001851 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001852 }
1853 }
1854}
1855
John Zulauf4fa68462021-04-26 21:04:22 -06001856// Caller must ensure that lifespan of this is less than from
1857void AccessContext::ImportAsyncContexts(const AccessContext &from) { async_ = from.async_; }
1858
John Zulauf355e49b2020-04-24 15:11:15 -06001859// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001860HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1861 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001862
John Zulauf355e49b2020-04-24 15:11:15 -06001863 // We should never ask for a transition from a context we don't have
John Zulaufbb890452021-12-14 11:30:18 -07001864 assert(track_back.source_subpass);
John Zulauf355e49b2020-04-24 15:11:15 -06001865
1866 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001867 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1868 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufbb890452021-12-14 11:30:18 -07001869 HazardResult hazard = track_back.source_subpass->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001870 if (!hazard.hazard) {
1871 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001872 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001873 }
John Zulaufa0a98292020-09-18 09:30:10 -06001874
John Zulauf355e49b2020-04-24 15:11:15 -06001875 return hazard;
1876}
1877
John Zulaufb02c1eb2020-10-06 16:33:36 -06001878void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001879 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001880 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001881 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001882 for (const auto &transition : transitions) {
1883 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001884 const auto &view_gen = attachment_views[transition.attachment];
1885 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001886
1887 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1888 assert(trackback);
1889
1890 // Import the attachments into the current context
John Zulaufbb890452021-12-14 11:30:18 -07001891 const auto *prev_context = trackback->source_subpass;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001892 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001893 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001894 auto &target_map = GetAccessStateMap(address_type);
1895 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001896 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1897 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001898 }
1899
John Zulauf86356ca2020-10-19 11:46:41 -06001900 // If there were no transitions skip this global map walk
1901 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001902 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001903 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001904 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001905}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001906
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001907void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulauf669dfd52021-01-27 17:15:28 -07001908 auto *events_context = GetCurrentEventsContext();
1909 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06001910 events_context->ApplyBarrier(src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001911}
1912
locke-lunarg61870c22020-06-09 14:51:50 -06001913bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1914 const char *func_name) const {
1915 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001916 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001917 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001918 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001919 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001920 return skip;
1921 }
1922
1923 using DescriptorClass = cvdescriptorset::DescriptorClass;
1924 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1925 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001926 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1927
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001928 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001929 const auto raster_state = pipe->RasterizationState();
1930 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001931 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001932 }
locke-lunarg61870c22020-06-09 14:51:50 -06001933 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001934 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001935 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001936 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001937 const auto descriptor_type = binding_it.GetType();
1938 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1939 auto array_idx = 0;
1940
1941 if (binding_it.IsVariableDescriptorCount()) {
1942 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1943 }
1944 SyncStageAccessIndex sync_index =
1945 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1946
1947 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1948 uint32_t index = i - index_range.start;
1949 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1950 switch (descriptor->GetClass()) {
1951 case DescriptorClass::ImageSampler:
1952 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001953 if (descriptor->Invalid()) {
1954 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001955 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07001956
1957 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
1958 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1959 const auto *img_view_state = image_descriptor->GetImageViewState();
1960 VkImageLayout image_layout = image_descriptor->GetImageLayout();
1961
John Zulauf361fb532020-07-22 10:45:39 -06001962 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001963 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1964 // Descriptors, so we do not have to worry about depth slicing here.
1965 // See: VUID 00343
1966 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001967 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001968 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001969
1970 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1971 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1972 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001973 // Input attachments are subject to raster ordering rules
1974 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001975 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001976 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001977 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001978 }
John Zulauf110413c2021-03-20 05:38:38 -06001979
John Zulauf33fc1d52020-07-17 11:01:10 -06001980 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001981 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001982 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001983 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1984 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001985 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001986 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1987 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1988 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001989 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1990 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
John Zulauf397e68b2022-04-19 11:44:07 -06001991 set_binding.first.binding, index, FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001992 }
1993 break;
1994 }
1995 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001996 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
1997 if (texel_descriptor->Invalid()) {
1998 continue;
1999 }
2000 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
2001 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002002 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06002003 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06002004 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002005 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002006 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06002007 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
2008 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002009 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
2010 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
2011 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06002012 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002013 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauf397e68b2022-04-19 11:44:07 -06002014 FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002015 }
2016 break;
2017 }
2018 case DescriptorClass::GeneralBuffer: {
2019 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07002020 if (buffer_descriptor->Invalid()) {
2021 continue;
2022 }
2023 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06002024 const ResourceAccessRange range =
2025 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06002026 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06002027 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002028 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002029 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06002030 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
2031 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002032 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2033 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
2034 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06002035 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002036 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauf397e68b2022-04-19 11:44:07 -06002037 FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002038 }
2039 break;
2040 }
2041 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2042 default:
2043 break;
2044 }
2045 }
2046 }
2047 }
2048 return skip;
2049}
2050
2051void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
John Zulauf14940722021-04-12 15:19:02 -06002052 const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002053 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06002054 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002055 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002056 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06002057 return;
2058 }
2059
2060 using DescriptorClass = cvdescriptorset::DescriptorClass;
2061 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2062 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06002063 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2064
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002065 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002066 const auto raster_state = pipe->RasterizationState();
2067 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06002068 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002069 }
locke-lunarg61870c22020-06-09 14:51:50 -06002070 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07002071 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002072 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002073 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06002074 const auto descriptor_type = binding_it.GetType();
2075 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2076 auto array_idx = 0;
2077
2078 if (binding_it.IsVariableDescriptorCount()) {
2079 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2080 }
2081 SyncStageAccessIndex sync_index =
2082 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2083
2084 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2085 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2086 switch (descriptor->GetClass()) {
2087 case DescriptorClass::ImageSampler:
2088 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002089 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
2090 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
2091 if (image_descriptor->Invalid()) {
2092 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002093 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07002094 const auto *img_view_state = image_descriptor->GetImageViewState();
Jeremy Gebben11a68a32021-07-29 11:59:22 -06002095 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
2096 // Descriptors, so we do not have to worry about depth slicing here.
2097 // See: VUID 00343
2098 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06002099 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002100 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06002101 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2102 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2103 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
2104 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002105 } else {
John Zulauf110413c2021-03-20 05:38:38 -06002106 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
2107 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002108 }
locke-lunarg61870c22020-06-09 14:51:50 -06002109 break;
2110 }
2111 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002112 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
2113 if (texel_descriptor->Invalid()) {
2114 continue;
2115 }
2116 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
2117 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002118 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002119 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002120 break;
2121 }
2122 case DescriptorClass::GeneralBuffer: {
2123 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07002124 if (buffer_descriptor->Invalid()) {
2125 continue;
2126 }
2127 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06002128 const ResourceAccessRange range =
2129 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002130 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002131 break;
2132 }
2133 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2134 default:
2135 break;
2136 }
2137 }
2138 }
2139 }
2140}
2141
2142bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2143 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002144 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002145 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002146 return skip;
2147 }
2148
2149 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2150 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002151 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002152
2153 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002154 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002155 if (binding_description.binding < binding_buffers_size) {
2156 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002157 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002158
locke-lunarg1ae57d62020-11-18 10:49:19 -07002159 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002160 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2161 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002162 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002163 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002164 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002165 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
John Zulauf397e68b2022-04-19 11:44:07 -06002166 func_name, string_SyncHazard(hazard.hazard),
2167 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2168 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002169 }
2170 }
2171 }
2172 return skip;
2173}
2174
John Zulauf14940722021-04-12 15:19:02 -06002175void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002176 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002177 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002178 return;
2179 }
2180 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2181 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002182 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002183
2184 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002185 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002186 if (binding_description.binding < binding_buffers_size) {
2187 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002188 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002189
locke-lunarg1ae57d62020-11-18 10:49:19 -07002190 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002191 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2192 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002193 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2194 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002195 }
2196 }
2197}
2198
2199bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2200 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002201 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002202 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002203 }
locke-lunarg61870c22020-06-09 14:51:50 -06002204
locke-lunarg1ae57d62020-11-18 10:49:19 -07002205 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002206 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002207 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2208 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002209 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002210 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002211 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002212 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2213 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06002214 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002215 }
2216
2217 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2218 // We will detect more accurate range in the future.
2219 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2220 return skip;
2221}
2222
John Zulauf14940722021-04-12 15:19:02 -06002223void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002224 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) return;
locke-lunarg61870c22020-06-09 14:51:50 -06002225
locke-lunarg1ae57d62020-11-18 10:49:19 -07002226 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002227 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002228 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2229 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002230 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002231
2232 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2233 // We will detect more accurate range in the future.
2234 RecordDrawVertex(UINT32_MAX, 0, tag);
2235}
2236
2237bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002238 bool skip = false;
2239 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002240 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002241 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002242}
2243
John Zulauf14940722021-04-12 15:19:02 -06002244void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002245 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002246 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002247 }
locke-lunarg61870c22020-06-09 14:51:50 -06002248}
2249
John Zulauf41a9c7c2021-12-07 15:59:53 -07002250ResourceUsageTag CommandBufferAccessContext::RecordBeginRenderPass(CMD_TYPE cmd, const RENDER_PASS_STATE &rp_state,
2251 const VkRect2D &render_area,
2252 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
John Zulauf355e49b2020-04-24 15:11:15 -06002253 // Create an access context the current renderpass.
John Zulauf41a9c7c2021-12-07 15:59:53 -07002254 const auto barrier_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2255 const auto load_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kLoadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07002256 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002257 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002258 current_renderpass_context_->RecordBeginRenderPass(barrier_tag, load_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002259 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002260 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002261}
2262
John Zulauf41a9c7c2021-12-07 15:59:53 -07002263ResourceUsageTag CommandBufferAccessContext::RecordNextSubpass(const CMD_TYPE cmd) {
John Zulauf16adfc92020-04-08 10:28:33 -06002264 assert(current_renderpass_context_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002265 if (!current_renderpass_context_) return NextCommandTag(cmd);
2266
2267 auto store_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kStoreOp);
2268 auto barrier_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2269 auto load_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kLoadOp);
2270
2271 current_renderpass_context_->RecordNextSubpass(store_tag, barrier_tag, load_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002272 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002273 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002274}
2275
John Zulauf41a9c7c2021-12-07 15:59:53 -07002276ResourceUsageTag CommandBufferAccessContext::RecordEndRenderPass(const CMD_TYPE cmd) {
John Zulauf16adfc92020-04-08 10:28:33 -06002277 assert(current_renderpass_context_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002278 if (!current_renderpass_context_) return NextCommandTag(cmd);
John Zulauf16adfc92020-04-08 10:28:33 -06002279
John Zulauf41a9c7c2021-12-07 15:59:53 -07002280 auto store_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kStoreOp);
2281 auto barrier_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2282
2283 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, store_tag, barrier_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002284 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002285 current_renderpass_context_ = nullptr;
John Zulauf41a9c7c2021-12-07 15:59:53 -07002286 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002287}
2288
John Zulauf4a6105a2020-11-17 15:11:05 -07002289void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2290 // Erase is okay with the key not being
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002291 auto event_state = sync_state_->Get<EVENT_STATE>(event);
John Zulauf669dfd52021-01-27 17:15:28 -07002292 if (event_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002293 GetCurrentEventsContext()->Destroy(event_state.get());
John Zulaufd5115702021-01-18 12:34:33 -07002294 }
2295}
2296
John Zulaufae842002021-04-15 18:20:55 -06002297// The is the recorded cb context
John Zulaufbb890452021-12-14 11:30:18 -07002298bool CommandBufferAccessContext::ValidateFirstUse(CommandExecutionContext *proxy_context, const char *func_name,
John Zulauf4fa68462021-04-26 21:04:22 -06002299 uint32_t index) const {
2300 assert(proxy_context);
2301 auto *events_context = proxy_context->GetCurrentEventsContext();
2302 auto *access_context = proxy_context->GetCurrentAccessContext();
2303 const ResourceUsageTag base_tag = proxy_context->GetTagLimit();
John Zulaufae842002021-04-15 18:20:55 -06002304 bool skip = false;
2305 ResourceUsageRange tag_range = {0, 0};
2306 const AccessContext *recorded_context = GetCurrentAccessContext();
2307 assert(recorded_context);
2308 HazardResult hazard;
John Zulaufbb890452021-12-14 11:30:18 -07002309 auto log_msg = [this](const HazardResult &hazard, const CommandExecutionContext &exec_context, const char *func_name,
John Zulaufae842002021-04-15 18:20:55 -06002310 uint32_t index) {
John Zulaufbb890452021-12-14 11:30:18 -07002311 const auto handle = exec_context.Handle();
John Zulaufae842002021-04-15 18:20:55 -06002312 const auto recorded_handle = cb_state_->commandBuffer();
John Zulauf4fa68462021-04-26 21:04:22 -06002313 const auto *report_data = sync_state_->report_data;
John Zulaufbb890452021-12-14 11:30:18 -07002314 return sync_state_->LogError(handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf4fa68462021-04-26 21:04:22 -06002315 "%s: Hazard %s for entry %" PRIu32 ", %s, Recorded access info %s. Access info %s.", func_name,
2316 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(recorded_handle).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06002317 FormatUsage(*hazard.recorded_access).c_str(), exec_context.FormatHazard(hazard).c_str());
John Zulaufae842002021-04-15 18:20:55 -06002318 };
John Zulaufbb890452021-12-14 11:30:18 -07002319 const ReplayTrackbackBarriersAction *replay_context = nullptr;
John Zulaufae842002021-04-15 18:20:55 -06002320 for (const auto &sync_op : sync_ops_) {
John Zulauf4fa68462021-04-26 21:04:22 -06002321 // we update the range to any include layout transition first use writes,
2322 // as they are stored along with the source scope (as effective barrier) when recorded
2323 tag_range.end = sync_op.tag + 1;
John Zulauf610e28c2021-08-03 17:46:23 -06002324 skip |= sync_op.sync_op->ReplayValidate(sync_op.tag, *this, base_tag, proxy_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002325
John Zulaufbb890452021-12-14 11:30:18 -07002326 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context, replay_context);
John Zulaufae842002021-04-15 18:20:55 -06002327 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002328 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002329 }
2330 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
John Zulauf4fa68462021-04-26 21:04:22 -06002331 // Record the barrier into the proxy context.
John Zulaufbb890452021-12-14 11:30:18 -07002332 sync_op.sync_op->ReplayRecord(base_tag + sync_op.tag, access_context, events_context);
2333 replay_context = sync_op.sync_op->GetReplayTrackback();
John Zulauf4fa68462021-04-26 21:04:22 -06002334 tag_range.begin = tag_range.end;
John Zulaufae842002021-04-15 18:20:55 -06002335 }
2336
John Zulaufbb890452021-12-14 11:30:18 -07002337 // Renderpasses may not cross command buffer boundaries
2338 assert(replay_context == nullptr);
2339
John Zulaufae842002021-04-15 18:20:55 -06002340 // and anything after the last syncop
John Zulaufae842002021-04-15 18:20:55 -06002341 tag_range.end = ResourceUsageRecord::kMaxIndex;
John Zulaufbb890452021-12-14 11:30:18 -07002342 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context, replay_context);
John Zulaufae842002021-04-15 18:20:55 -06002343 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002344 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002345 }
2346
2347 return skip;
2348}
2349
John Zulauf06f6f1e2022-04-19 15:28:11 -06002350void CommandExecutionContext::RecordExecutedCommandBuffer(const CommandBufferAccessContext &recorded_cb_context, CMD_TYPE cmd) {
John Zulauf4fa68462021-04-26 21:04:22 -06002351 auto *events_context = GetCurrentEventsContext();
2352 auto *access_context = GetCurrentAccessContext();
2353 const AccessContext *recorded_context = recorded_cb_context.GetCurrentAccessContext();
2354 assert(recorded_context);
2355
2356 // Just run through the barriers ignoring the usage from the recorded context, as Resolve will overwrite outdated state
2357 const ResourceUsageTag base_tag = GetTagLimit();
John Zulauf06f6f1e2022-04-19 15:28:11 -06002358 for (const auto &sync_op : recorded_cb_context.GetSyncOps()) {
John Zulauf4fa68462021-04-26 21:04:22 -06002359 // we update the range to any include layout transition first use writes,
2360 // as they are stored along with the source scope (as effective barrier) when recorded
John Zulaufbb890452021-12-14 11:30:18 -07002361 sync_op.sync_op->ReplayRecord(base_tag + sync_op.tag, access_context, events_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002362 }
2363
2364 ResourceUsageRange tag_range = ImportRecordedAccessLog(recorded_cb_context);
2365 assert(base_tag == tag_range.begin); // to ensure the to offset calculation agree
2366 ResolveRecordedContext(*recorded_context, tag_range.begin);
2367}
2368
John Zulauf3c788ef2022-02-22 12:12:30 -07002369void CommandExecutionContext::ResolveRecordedContext(const AccessContext &recorded_context, ResourceUsageTag offset) {
John Zulauf4fa68462021-04-26 21:04:22 -06002370 auto tag_offset = [offset](ResourceAccessState *access) { access->OffsetTag(offset); };
2371
2372 auto *access_context = GetCurrentAccessContext();
2373 for (auto address_type : kAddressTypes) {
2374 recorded_context.ResolveAccessRange(address_type, kFullRange, tag_offset, &access_context->GetAccessStateMap(address_type),
2375 nullptr, false);
2376 }
2377}
2378
John Zulauf3c788ef2022-02-22 12:12:30 -07002379ResourceUsageRange CommandExecutionContext::ImportRecordedAccessLog(const CommandBufferAccessContext &recorded_context) {
John Zulauf4fa68462021-04-26 21:04:22 -06002380 // The execution references ensure lifespan for the referenced child CB's...
2381 ResourceUsageRange tag_range(GetTagLimit(), 0);
John Zulauf3c788ef2022-02-22 12:12:30 -07002382 InsertRecordedAccessLogEntries(recorded_context);
2383 tag_range.end = GetTagLimit();
John Zulauf4fa68462021-04-26 21:04:22 -06002384 return tag_range;
2385}
2386
John Zulauf3c788ef2022-02-22 12:12:30 -07002387void CommandBufferAccessContext::InsertRecordedAccessLogEntries(const CommandBufferAccessContext &recorded_context) {
2388 cbs_referenced_.emplace(recorded_context.GetCBStateShared());
2389 access_log_.insert(access_log_.end(), recorded_context.access_log_.cbegin(), recorded_context.access_log_.end());
2390}
2391
John Zulauf41a9c7c2021-12-07 15:59:53 -07002392ResourceUsageTag CommandBufferAccessContext::NextSubcommandTag(CMD_TYPE command, ResourceUsageRecord::SubcommandType subcommand) {
2393 ResourceUsageTag next = access_log_.size();
2394 access_log_.emplace_back(command, command_number_, subcommand, ++subcommand_number_, cb_state_.get(), reset_count_);
2395 return next;
2396}
2397
2398ResourceUsageTag CommandBufferAccessContext::NextCommandTag(CMD_TYPE command, ResourceUsageRecord::SubcommandType subcommand) {
2399 command_number_++;
2400 subcommand_number_ = 0;
2401 ResourceUsageTag next = access_log_.size();
2402 access_log_.emplace_back(command, command_number_, subcommand, subcommand_number_, cb_state_.get(), reset_count_);
2403 return next;
2404}
2405
2406ResourceUsageTag CommandBufferAccessContext::NextIndexedCommandTag(CMD_TYPE command, uint32_t index) {
2407 if (index == 0) {
2408 return NextCommandTag(command, ResourceUsageRecord::SubcommandType::kIndex);
2409 }
2410 return NextSubcommandTag(command, ResourceUsageRecord::SubcommandType::kIndex);
2411}
2412
John Zulaufbb890452021-12-14 11:30:18 -07002413void CommandBufferAccessContext::RecordSyncOp(SyncOpPointer &&sync_op) {
2414 auto tag = sync_op->Record(this);
2415 // As renderpass operations can have side effects on the command buffer access context,
2416 // update the sync operation to record these if any.
2417 if (current_renderpass_context_) {
2418 const auto &rpc = *current_renderpass_context_;
2419 sync_op->SetReplayContext(rpc.GetCurrentSubpass(), rpc.GetReplayContext());
2420 }
2421 sync_ops_.emplace_back(tag, std::move(sync_op));
2422}
2423
John Zulaufae842002021-04-15 18:20:55 -06002424class HazardDetectFirstUse {
2425 public:
John Zulaufbb890452021-12-14 11:30:18 -07002426 HazardDetectFirstUse(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2427 const ReplayTrackbackBarriersAction *replay_barrier)
2428 : recorded_use_(recorded_use), tag_range_(tag_range), replay_barrier_(replay_barrier) {}
John Zulaufae842002021-04-15 18:20:55 -06002429 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulaufbb890452021-12-14 11:30:18 -07002430 if (replay_barrier_) {
2431 // Intentional copy to apply the replay barrier
2432 auto access = pos->second;
2433 (*replay_barrier_)(&access);
2434 return access.DetectHazard(recorded_use_, tag_range_);
2435 }
John Zulaufae842002021-04-15 18:20:55 -06002436 return pos->second.DetectHazard(recorded_use_, tag_range_);
2437 }
2438 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
2439 return pos->second.DetectAsyncHazard(recorded_use_, tag_range_, start_tag);
2440 }
2441
2442 private:
2443 const ResourceAccessState &recorded_use_;
2444 const ResourceUsageRange &tag_range_;
John Zulaufbb890452021-12-14 11:30:18 -07002445 const ReplayTrackbackBarriersAction *replay_barrier_;
John Zulaufae842002021-04-15 18:20:55 -06002446};
2447
2448// This is called with the *recorded* command buffers access context, with the *active* access context pass in, againsts which
2449// hazards will be detected
John Zulaufbb890452021-12-14 11:30:18 -07002450HazardResult AccessContext::DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context,
2451 const ReplayTrackbackBarriersAction *replay_barrier) const {
John Zulaufae842002021-04-15 18:20:55 -06002452 HazardResult hazard;
2453 for (const auto address_type : kAddressTypes) {
2454 const auto &recorded_access_map = GetAccessStateMap(address_type);
2455 for (const auto &recorded_access : recorded_access_map) {
2456 // Cull any entries not in the current tag range
2457 if (!recorded_access.second.FirstAccessInTagRange(tag_range)) continue;
John Zulaufbb890452021-12-14 11:30:18 -07002458 HazardDetectFirstUse detector(recorded_access.second, tag_range, replay_barrier);
John Zulaufae842002021-04-15 18:20:55 -06002459 hazard = access_context.DetectHazard(address_type, detector, recorded_access.first, DetectOptions::kDetectAll);
2460 if (hazard.hazard) break;
2461 }
2462 }
2463
2464 return hazard;
2465}
2466
John Zulaufbb890452021-12-14 11:30:18 -07002467bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &exec_context,
2468 const CMD_BUFFER_STATE &cmd, const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002469 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07002470 const auto &sync_state = exec_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002471 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002472 if (!pipe) {
2473 return skip;
2474 }
2475
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002476 const auto raster_state = pipe->RasterizationState();
2477 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002478 return skip;
2479 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002480 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002481 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002482
John Zulauf1a224292020-06-30 14:52:13 -06002483 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002484 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002485 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2486 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002487 if (location >= subpass.colorAttachmentCount ||
2488 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002489 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002490 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002491 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2492 if (!view_gen.IsValid()) continue;
2493 HazardResult hazard =
2494 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2495 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002496 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002497 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002498 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002499 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002500 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002501 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002502 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf397e68b2022-04-19 11:44:07 -06002503 location, exec_context.FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002504 }
2505 }
2506 }
locke-lunarg37047832020-06-12 13:44:45 -06002507
2508 // PHASE1 TODO: Add layout based read/vs. write selection.
2509 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002510 const auto ds_state = pipe->DepthStencilState();
2511 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002512
2513 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2514 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2515 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002516 bool depth_write = false, stencil_write = false;
2517
2518 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002519 if (!FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable && ds_state->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002520 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2521 depth_write = true;
2522 }
2523 // PHASE1 TODO: It needs to check if stencil is writable.
2524 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2525 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2526 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002527 if (!FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002528 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2529 stencil_write = true;
2530 }
2531
2532 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2533 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002534 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2535 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2536 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002537 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002538 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002539 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002540 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002541 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002542 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2543 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf397e68b2022-04-19 11:44:07 -06002544 exec_context.FormatHazard(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002545 }
2546 }
2547 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002548 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2549 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2550 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002551 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002552 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002553 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002554 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002555 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002556 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2557 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf397e68b2022-04-19 11:44:07 -06002558 exec_context.FormatHazard(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002559 }
locke-lunarg61870c22020-06-09 14:51:50 -06002560 }
2561 }
2562 return skip;
2563}
2564
John Zulauf14940722021-04-12 15:19:02 -06002565void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002566 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002567 if (!pipe) {
2568 return;
2569 }
2570
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002571 const auto *raster_state = pipe->RasterizationState();
2572 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002573 return;
2574 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002575 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002576 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002577
John Zulauf1a224292020-06-30 14:52:13 -06002578 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002579 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002580 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2581 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002582 if (location >= subpass.colorAttachmentCount ||
2583 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002584 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002585 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002586 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2587 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2588 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2589 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002590 }
2591 }
locke-lunarg37047832020-06-12 13:44:45 -06002592
2593 // PHASE1 TODO: Add layout based read/vs. write selection.
2594 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002595 const auto *ds_state = pipe->DepthStencilState();
2596 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002597 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2598 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2599 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002600 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002601 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2602 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002603
2604 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002605 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable &&
2606 ds_state->depthWriteEnable && IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
locke-lunarg37047832020-06-12 13:44:45 -06002607 depth_write = true;
2608 }
2609 // PHASE1 TODO: It needs to check if stencil is writable.
2610 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2611 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2612 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002613 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002614 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2615 stencil_write = true;
2616 }
2617
John Zulaufd0ec59f2021-03-13 14:25:08 -07002618 if (depth_write || stencil_write) {
2619 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2620 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2621 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2622 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002623 }
locke-lunarg61870c22020-06-09 14:51:50 -06002624 }
2625}
2626
John Zulaufbb890452021-12-14 11:30:18 -07002627bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &exec_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002628 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002629 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07002630 skip |= CurrentContext().ValidateResolveOperations(exec_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002631 current_subpass_);
John Zulaufbb890452021-12-14 11:30:18 -07002632 skip |= CurrentContext().ValidateStoreOperation(exec_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002633 func_name);
2634
John Zulauf355e49b2020-04-24 15:11:15 -06002635 const auto next_subpass = current_subpass_ + 1;
ziga-lunarg31a3e772022-03-22 11:48:46 +01002636 if (next_subpass >= subpass_contexts_.size()) {
2637 return skip;
2638 }
John Zulauf1507ee42020-05-18 11:33:09 -06002639 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002640 skip |=
John Zulaufbb890452021-12-14 11:30:18 -07002641 next_context.ValidateLayoutTransitions(exec_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002642 if (!skip) {
2643 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2644 // on a copy of the (empty) next context.
2645 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2646 AccessContext temp_context(next_context);
John Zulaufee984022022-04-13 16:39:50 -06002647 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kInvalidTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002648 skip |=
John Zulaufbb890452021-12-14 11:30:18 -07002649 temp_context.ValidateLoadOperation(exec_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002650 }
John Zulauf7635de32020-05-29 17:14:15 -06002651 return skip;
2652}
John Zulaufbb890452021-12-14 11:30:18 -07002653bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &exec_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002654 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002655 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07002656 skip |= CurrentContext().ValidateResolveOperations(exec_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002657 current_subpass_);
John Zulaufbb890452021-12-14 11:30:18 -07002658 skip |= CurrentContext().ValidateStoreOperation(exec_context, *rp_state_, render_area_, current_subpass_,
John Zulaufd0ec59f2021-03-13 14:25:08 -07002659
2660 attachment_views_, func_name);
John Zulaufbb890452021-12-14 11:30:18 -07002661 skip |= ValidateFinalSubpassLayoutTransitions(exec_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002662 return skip;
2663}
2664
John Zulauf64ffe552021-02-06 10:25:07 -07002665AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002666 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002667}
2668
John Zulaufbb890452021-12-14 11:30:18 -07002669bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &exec_context,
John Zulauf64ffe552021-02-06 10:25:07 -07002670 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002671 bool skip = false;
2672
John Zulauf7635de32020-05-29 17:14:15 -06002673 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2674 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2675 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2676 // to apply and only copy then, if this proves a hot spot.
2677 std::unique_ptr<AccessContext> proxy_for_current;
2678
John Zulauf355e49b2020-04-24 15:11:15 -06002679 // Validate the "finalLayout" transitions to external
2680 // Get them from where there we're hidding in the extra entry.
2681 const auto &final_transitions = rp_state_->subpass_transitions.back();
2682 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002683 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002684 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufbb890452021-12-14 11:30:18 -07002685 assert(trackback.source_subpass); // Transitions are given implicit transitions if the StateTracker is working correctly
2686 auto *context = trackback.source_subpass;
John Zulauf7635de32020-05-29 17:14:15 -06002687
2688 if (transition.prev_pass == current_subpass_) {
2689 if (!proxy_for_current) {
2690 // We haven't recorded resolve ofor the current_subpass, so we need to copy current and update it *as if*
John Zulauf64ffe552021-02-06 10:25:07 -07002691 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002692 }
2693 context = proxy_for_current.get();
2694 }
2695
John Zulaufa0a98292020-09-18 09:30:10 -06002696 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2697 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002698 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002699 if (hazard.hazard) {
John Zulaufee984022022-04-13 16:39:50 -06002700 if (hazard.tag == kInvalidTag) {
2701 // Hazard vs. ILT
John Zulaufbb890452021-12-14 11:30:18 -07002702 skip |= exec_context.GetSyncState().LogError(
John Zulaufee984022022-04-13 16:39:50 -06002703 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
2704 "%s: Hazard %s vs. store/resolve operations in subpass %" PRIu32 " for attachment %" PRIu32
2705 " final image layout transition (old_layout: %s, new_layout: %s).",
2706 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2707 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout));
2708 } else {
John Zulaufbb890452021-12-14 11:30:18 -07002709 skip |= exec_context.GetSyncState().LogError(
John Zulaufee984022022-04-13 16:39:50 -06002710 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
2711 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2712 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2713 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2714 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf397e68b2022-04-19 11:44:07 -06002715 exec_context.FormatHazard(hazard).c_str());
John Zulaufee984022022-04-13 16:39:50 -06002716 }
John Zulauf355e49b2020-04-24 15:11:15 -06002717 }
2718 }
2719 return skip;
2720}
2721
John Zulauf14940722021-04-12 15:19:02 -06002722void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002723 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002724 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002725}
2726
John Zulauf14940722021-04-12 15:19:02 -06002727void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002728 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2729 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002730
2731 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2732 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002733 const AttachmentViewGen &view_gen = attachment_views_[i];
2734 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002735
2736 const auto &ci = attachment_ci[i];
2737 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002738 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002739 const bool is_color = !(has_depth || has_stencil);
2740
2741 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002742 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2743 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2744 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2745 SyncOrdering::kColorAttachment, tag);
2746 }
John Zulauf1507ee42020-05-18 11:33:09 -06002747 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002748 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002749 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2750 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2751 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2752 SyncOrdering::kDepthStencilAttachment, tag);
2753 }
John Zulauf1507ee42020-05-18 11:33:09 -06002754 }
2755 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002756 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2757 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2758 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2759 SyncOrdering::kDepthStencilAttachment, tag);
2760 }
John Zulauf1507ee42020-05-18 11:33:09 -06002761 }
2762 }
2763 }
2764 }
2765}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002766AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2767 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2768 AttachmentViewGenVector view_gens;
2769 VkExtent3D extent = CastTo3D(render_area.extent);
2770 VkOffset3D offset = CastTo3D(render_area.offset);
2771 view_gens.reserve(attachment_views.size());
2772 for (const auto *view : attachment_views) {
2773 view_gens.emplace_back(view, offset, extent);
2774 }
2775 return view_gens;
2776}
John Zulauf64ffe552021-02-06 10:25:07 -07002777RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2778 VkQueueFlags queue_flags,
2779 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2780 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002781 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002782 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002783 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulaufbb890452021-12-14 11:30:18 -07002784 replay_context_ = std::make_shared<ReplayRenderpassContext>();
2785 auto &replay_subpass_contexts = replay_context_->subpass_contexts;
2786 replay_subpass_contexts.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002787 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002788 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulaufbb890452021-12-14 11:30:18 -07002789 replay_subpass_contexts.emplace_back(queue_flags, rp_state_->subpass_dependencies[pass], replay_subpass_contexts);
John Zulauf355e49b2020-04-24 15:11:15 -06002790 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002791 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002792}
John Zulauf41a9c7c2021-12-07 15:59:53 -07002793void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag barrier_tag, const ResourceUsageTag load_tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002794 assert(0 == current_subpass_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002795 subpass_contexts_[current_subpass_].SetStartTag(barrier_tag);
2796 RecordLayoutTransitions(barrier_tag);
2797 RecordLoadOperations(load_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002798}
John Zulauf1507ee42020-05-18 11:33:09 -06002799
John Zulauf41a9c7c2021-12-07 15:59:53 -07002800void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag store_tag, const ResourceUsageTag barrier_tag,
2801 const ResourceUsageTag load_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002802 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulauf41a9c7c2021-12-07 15:59:53 -07002803 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
2804 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002805
ziga-lunarg31a3e772022-03-22 11:48:46 +01002806 if (current_subpass_ + 1 >= subpass_contexts_.size()) {
2807 return;
2808 }
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002809 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2810 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002811 current_subpass_++;
John Zulauf41a9c7c2021-12-07 15:59:53 -07002812 subpass_contexts_[current_subpass_].SetStartTag(barrier_tag);
2813 RecordLayoutTransitions(barrier_tag);
2814 RecordLoadOperations(load_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002815}
2816
John Zulauf41a9c7c2021-12-07 15:59:53 -07002817void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag store_tag,
2818 const ResourceUsageTag barrier_tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002819 // Add the resolve and store accesses
John Zulauf41a9c7c2021-12-07 15:59:53 -07002820 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
2821 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002822
John Zulauf355e49b2020-04-24 15:11:15 -06002823 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002824 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002825
2826 // Add the "finalLayout" transitions to external
2827 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002828 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2829 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2830 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002831 const auto &final_transitions = rp_state_->subpass_transitions.back();
2832 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002833 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002834 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufbb890452021-12-14 11:30:18 -07002835 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.source_subpass);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002836 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), barrier_tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002837 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002838 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002839 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002840 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002841 }
2842}
2843
John Zulauf06f6f1e2022-04-19 15:28:11 -06002844SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param,
2845 const VkPipelineStageFlags2KHR disabled_feature_mask) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002846 SyncExecScope result;
2847 result.mask_param = mask_param;
John Zulauf06f6f1e2022-04-19 15:28:11 -06002848 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags, disabled_feature_mask);
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002849 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002850 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2851 return result;
2852}
2853
Jeremy Gebben40a22942020-12-22 14:22:06 -07002854SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002855 SyncExecScope result;
2856 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002857 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2858 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002859 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2860 return result;
2861}
2862
2863SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002864 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002865 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002866 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002867 dst_access_scope = 0;
2868}
2869
2870template <typename Barrier>
2871SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002872 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002873 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002874 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002875 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2876}
2877
2878SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002879 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2880 if (barrier) {
2881 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002882 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002883 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002884
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002885 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002886 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002887 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2888
2889 } else {
2890 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002891 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002892 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2893
2894 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002895 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002896 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2897 }
2898}
2899
2900template <typename Barrier>
2901SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2902 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2903 src_exec_scope = src.exec_scope;
2904 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2905
2906 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002907 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002908 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002909}
2910
John Zulaufb02c1eb2020-10-06 16:33:36 -06002911// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2912void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2913 for (const auto &barrier : barriers) {
2914 ApplyBarrier(barrier, layout_transition);
2915 }
2916}
2917
John Zulauf89311b42020-09-29 16:28:47 -06002918// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2919// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2920// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufbb890452021-12-14 11:30:18 -07002921void ResourceAccessState::ApplyBarriersImmediate(const std::vector<SyncBarrier> &barriers) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002922 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002923 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002924 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002925 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002926 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002927 }
John Zulaufbb890452021-12-14 11:30:18 -07002928 ApplyPendingBarriers(kInvalidTag); // There can't be any need for this tag
John Zulauf3d84f1b2020-03-09 13:33:25 -06002929}
John Zulauf9cb530d2019-09-30 14:14:10 -06002930HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2931 HazardResult hazard;
2932 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002933 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002934 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002935 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002936 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002937 }
2938 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002939 // Write operation:
2940 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2941 // If reads exists -- test only against them because either:
2942 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2943 // * the read weren't hazards, and thus if the write is safe w.r.t. the reads, no hazard vs. last_write is possible if
2944 // the current write happens after the reads, so just test the write against the reades
2945 // Otherwise test against last_write
2946 //
2947 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002948 if (last_reads.size()) {
2949 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002950 if (IsReadHazard(usage_stage, read_access)) {
2951 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2952 break;
2953 }
2954 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002955 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002956 // Write-After-Write check -- if we have a previous write to test against
2957 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002958 }
2959 }
2960 return hazard;
2961}
2962
John Zulauf4fa68462021-04-26 21:04:22 -06002963HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002964 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002965 return DetectHazard(usage_index, ordering);
2966}
2967
2968HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002969 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2970 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002971 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002972 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002973 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2974 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002975 if (IsRead(usage_bit)) {
2976 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2977 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2978 if (is_raw_hazard) {
2979 // NOTE: we know last_write is non-zero
2980 // See if the ordering rules save us from the simple RAW check above
2981 // First check to see if the current usage is covered by the ordering rules
2982 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2983 const bool usage_is_ordered =
2984 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2985 if (usage_is_ordered) {
2986 // Now see of the most recent write (or a subsequent read) are ordered
2987 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2988 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002989 }
2990 }
John Zulauf4285ee92020-09-23 10:20:52 -06002991 if (is_raw_hazard) {
2992 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2993 }
John Zulauf5c628d02021-05-04 15:46:36 -06002994 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2995 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2996 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002997 } else {
2998 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002999 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07003000 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06003001 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07003002 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06003003 if (usage_write_is_ordered) {
3004 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
3005 ordered_stages = GetOrderedStages(ordering);
3006 }
3007 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
3008 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003009 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06003010 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
3011 if (IsReadHazard(usage_stage, read_access)) {
3012 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3013 break;
3014 }
John Zulaufd14743a2020-07-03 09:42:39 -06003015 }
3016 }
John Zulauf2a344ca2021-09-09 17:07:19 -06003017 } else if (last_write.any() && !(last_write_is_ordered && usage_write_is_ordered)) {
3018 bool ilt_ilt_hazard = false;
3019 if ((usage_index == SYNC_IMAGE_LAYOUT_TRANSITION) && (usage_bit == last_write)) {
3020 // ILT after ILT is a special case where we check the 2nd access scope of the first ILT against the first access
3021 // scope of the second ILT, which has been passed (smuggled?) in the ordering barrier
3022 ilt_ilt_hazard = !(write_barriers & ordering.access_scope).any();
3023 }
3024 if (ilt_ilt_hazard || IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003025 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06003026 }
John Zulauf69133422020-05-20 14:55:53 -06003027 }
3028 }
3029 return hazard;
3030}
3031
John Zulaufae842002021-04-15 18:20:55 -06003032HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
3033 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06003034 using Size = FirstAccesses::size_type;
3035 const auto &recorded_accesses = recorded_use.first_accesses_;
3036 Size count = recorded_accesses.size();
3037 if (count) {
3038 const auto &last_access = recorded_accesses.back();
3039 bool do_write_last = IsWrite(last_access.usage_index);
3040 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06003041
John Zulauf4fa68462021-04-26 21:04:22 -06003042 for (Size i = 0; i < count; ++count) {
3043 const auto &first = recorded_accesses[i];
3044 // Skip and quit logic
3045 if (first.tag < tag_range.begin) continue;
3046 if (first.tag >= tag_range.end) {
3047 do_write_last = false; // ignore last since we know it can't be in tag_range
3048 break;
3049 }
3050
3051 hazard = DetectHazard(first.usage_index, first.ordering_rule);
3052 if (hazard.hazard) {
3053 hazard.AddRecordedAccess(first);
3054 break;
3055 }
3056 }
3057
3058 if (do_write_last && tag_range.includes(last_access.tag)) {
3059 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
3060 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
3061 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
3062 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
3063 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
3064 // the barrier that applies them
3065 barrier |= recorded_use.first_write_layout_ordering_;
3066 }
3067 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
3068 // the active context
3069 if (recorded_use.first_read_stages_) {
3070 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
3071 // reads in the active context are not "most recent" as all recorded context operations are *after* them
3072 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
3073 // active context.
3074 barrier.exec_scope |= recorded_use.first_read_stages_;
3075 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
3076 barrier.access_scope |= FlagBit(last_access.usage_index);
3077 }
3078 hazard = DetectHazard(last_access.usage_index, barrier);
3079 if (hazard.hazard) {
3080 hazard.AddRecordedAccess(last_access);
3081 }
3082 }
John Zulaufae842002021-04-15 18:20:55 -06003083 }
3084 return hazard;
3085}
3086
John Zulauf2f952d22020-02-10 11:34:51 -07003087// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06003088HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07003089 HazardResult hazard;
3090 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003091 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
3092 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
3093 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07003094 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06003095 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06003096 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003097 }
3098 } else {
John Zulauf14940722021-04-12 15:19:02 -06003099 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06003100 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07003101 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003102 // Any reads during the other subpass will conflict with this write, so we need to check them all.
John Zulaufab7756b2020-12-29 16:10:16 -07003103 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003104 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003105 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003106 break;
3107 }
3108 }
John Zulauf2f952d22020-02-10 11:34:51 -07003109 }
3110 }
3111 return hazard;
3112}
3113
John Zulaufae842002021-04-15 18:20:55 -06003114HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
3115 ResourceUsageTag start_tag) const {
3116 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06003117 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06003118 // Skip and quit logic
3119 if (first.tag < tag_range.begin) continue;
3120 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06003121
3122 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06003123 if (hazard.hazard) {
3124 hazard.AddRecordedAccess(first);
3125 break;
3126 }
John Zulaufae842002021-04-15 18:20:55 -06003127 }
3128 return hazard;
3129}
3130
Jeremy Gebben40a22942020-12-22 14:22:06 -07003131HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003132 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003133 // Only supporting image layout transitions for now
3134 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3135 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003136 // only test for WAW if there no intervening read operations.
3137 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07003138 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06003139 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07003140 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003141 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003142 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003143 break;
3144 }
3145 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003146 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3147 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3148 }
3149
3150 return hazard;
3151}
3152
Jeremy Gebben40a22942020-12-22 14:22:06 -07003153HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07003154 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06003155 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07003156 // Only supporting image layout transitions for now
3157 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3158 HazardResult hazard;
3159 // only test for WAW if there no intervening read operations.
3160 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3161
John Zulaufab7756b2020-12-29 16:10:16 -07003162 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003163 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3164 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003165 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003166 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003167 // The read is in the events first synchronization scope, so we use a barrier hazard check
3168 // If the read stage is not in the src sync scope
3169 // *AND* not execution chained with an existing sync barrier (that's the or)
3170 // then the barrier access is unsafe (R/W after R)
3171 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3172 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3173 break;
3174 }
3175 } else {
3176 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3177 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3178 }
3179 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003180 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003181 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
John Zulauf14940722021-04-12 15:19:02 -06003182 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003183 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3184 // So do a normal barrier hazard check
3185 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3186 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3187 }
3188 } else {
3189 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003190 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3191 }
John Zulaufd14743a2020-07-03 09:42:39 -06003192 }
John Zulauf361fb532020-07-22 10:45:39 -06003193
John Zulauf0cb5be22020-01-23 12:18:22 -07003194 return hazard;
3195}
3196
John Zulauf5f13a792020-03-10 07:31:21 -06003197// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3198// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3199// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3200void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003201 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003202 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3203 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003204 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003205 } else if (other.write_tag == write_tag) {
3206 // In the *equals* case for write operations, we merged the write barriers and the read state (but without the
John Zulauf5f13a792020-03-10 07:31:21 -06003207 // dependency chaining logic or any stage expansion)
3208 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003209 pending_write_barriers |= other.pending_write_barriers;
3210 pending_layout_transition |= other.pending_layout_transition;
3211 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003212 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003213
John Zulaufd14743a2020-07-03 09:42:39 -06003214 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003215 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003216 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003217 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003218 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003219 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003220 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003221 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3222 // but we should wait on profiling data for that.
3223 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003224 auto &my_read = last_reads[my_read_index];
3225 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003226 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003227 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003228 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003229 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003230 my_read.pending_dep_chain = other_read.pending_dep_chain;
3231 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3232 // May require tracking more than one access per stage.
3233 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003234 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003235 // Since I'm overwriting the fragement stage read, also update the input attachment info
3236 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003237 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003238 }
John Zulauf14940722021-04-12 15:19:02 -06003239 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003240 // The read tags match so merge the barriers
3241 my_read.barriers |= other_read.barriers;
3242 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003243 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003244
John Zulauf5f13a792020-03-10 07:31:21 -06003245 break;
3246 }
3247 }
3248 } else {
3249 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003250 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003251 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003252 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003253 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003254 }
John Zulauf5f13a792020-03-10 07:31:21 -06003255 }
3256 }
John Zulauf361fb532020-07-22 10:45:39 -06003257 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003258 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3259 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003260
3261 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3262 // of the copy and other into this using the update first logic.
3263 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3264 // of the other first_accesses... )
3265 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3266 FirstAccesses firsts(std::move(first_accesses_));
3267 first_accesses_.clear();
3268 first_read_stages_ = 0U;
3269 auto a = firsts.begin();
3270 auto a_end = firsts.end();
3271 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003272 // TODO: Determine whether some tag offset will be needed for PHASE II
3273 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003274 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3275 ++a;
3276 }
3277 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3278 }
3279 for (; a != a_end; ++a) {
3280 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3281 }
3282 }
John Zulauf5f13a792020-03-10 07:31:21 -06003283}
3284
John Zulauf14940722021-04-12 15:19:02 -06003285void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003286 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3287 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003288 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003289 // Mulitple outstanding reads may be of interest and do dependency chains independently
3290 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3291 const auto usage_stage = PipelineStageBit(usage_index);
3292 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003293 for (auto &read_access : last_reads) {
3294 if (read_access.stage == usage_stage) {
3295 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003296 break;
3297 }
3298 }
3299 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003300 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003301 last_read_stages |= usage_stage;
3302 }
John Zulauf4285ee92020-09-23 10:20:52 -06003303
3304 // Fragment shader reads come in two flavors, and we need to track if the one we're tracking is the special one.
Jeremy Gebben40a22942020-12-22 14:22:06 -07003305 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003306 // TODO Revisit re: multiple reads for a given stage
3307 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003308 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003309 } else {
3310 // Assume write
3311 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003312 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003313 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003314 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003315}
John Zulauf5f13a792020-03-10 07:31:21 -06003316
John Zulauf89311b42020-09-29 16:28:47 -06003317// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3318// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3319// We can overwrite them as *this* write is now after them.
3320//
3321// Note: intentionally ignore pending barriers and chains (i.e. don't apply or clear them), let ApplyPendingBarriers handle them.
John Zulauf14940722021-04-12 15:19:02 -06003322void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003323 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003324 last_read_stages = 0;
3325 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003326 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003327
3328 write_barriers = 0;
3329 write_dependency_chain = 0;
3330 write_tag = tag;
3331 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003332}
3333
John Zulauf89311b42020-09-29 16:28:47 -06003334// Apply the memory barrier without updating the existing barriers. The execution barrier
3335// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3336// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3337// replace the current write barriers or add to them, so accumulate to pending as well.
3338void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3339 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3340 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003341 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3342 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3343 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3344 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003345 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003346 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003347 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003348 if (layout_transition) {
3349 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3350 }
John Zulaufa0a98292020-09-18 09:30:10 -06003351 }
John Zulauf89311b42020-09-29 16:28:47 -06003352 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3353 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003354
John Zulauf89311b42020-09-29 16:28:47 -06003355 if (!pending_layout_transition) {
3356 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3357 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003358 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003359 // The | implements the "dependency chain" logic for this access, as the barriers field stores the second sync scope
John Zulaufc523bf62021-02-16 08:20:34 -07003360 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3361 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003362 }
3363 }
John Zulaufa0a98292020-09-18 09:30:10 -06003364 }
John Zulaufa0a98292020-09-18 09:30:10 -06003365}
3366
John Zulauf4a6105a2020-11-17 15:11:05 -07003367// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3368// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003369void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003370 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3371 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3372 // in order to know if it's in the excecution scope
3373 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3374 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3375 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003376 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003377 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003378 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003379 if (layout_transition) {
3380 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3381 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003382 }
3383 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3384 pending_layout_transition |= layout_transition;
3385
3386 if (!pending_layout_transition) {
3387 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3388 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003389 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003390 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3391 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3392 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3393 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3394 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3395 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3396 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003397 if ((read_access.tag < scope_tag) && (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers))) {
John Zulaufc523bf62021-02-16 08:20:34 -07003398 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003399 }
3400 }
3401 }
3402}
John Zulauf14940722021-04-12 15:19:02 -06003403void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003404 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003405 // SetWrite clobbers the last_reads array, and thus we don't have to clear the read_state out.
John Zulauf89311b42020-09-29 16:28:47 -06003406 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003407 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003408 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3409 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003410 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003411 }
John Zulauf89311b42020-09-29 16:28:47 -06003412
3413 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003414 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003415 for (auto &read_access : last_reads) {
3416 read_access.barriers |= read_access.pending_dep_chain;
3417 read_execution_barriers |= read_access.barriers;
3418 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003419 }
3420
3421 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3422 write_dependency_chain |= pending_write_dep_chain;
3423 write_barriers |= pending_write_barriers;
3424 pending_write_dep_chain = 0;
3425 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003426}
3427
John Zulaufae842002021-04-15 18:20:55 -06003428bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3429 if (!first_accesses_.size()) return false;
3430 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3431 return tag_range.intersects(first_access_range);
3432}
3433
John Zulauf59e25072020-07-17 10:55:21 -06003434// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003435VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3436 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003437
John Zulaufab7756b2020-12-29 16:10:16 -07003438 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003439 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003440 barriers = read_access.barriers;
3441 break;
John Zulauf59e25072020-07-17 10:55:21 -06003442 }
3443 }
John Zulauf4285ee92020-09-23 10:20:52 -06003444
John Zulauf59e25072020-07-17 10:55:21 -06003445 return barriers;
3446}
3447
Jeremy Gebben40a22942020-12-22 14:22:06 -07003448inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003449 assert(IsRead(usage));
3450 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3451 // * the previous reads are not hazards, and thus last_write must be visible and available to
3452 // any reads that happen after.
3453 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3454 // the current read will be also not be a hazard, thus reporting a hazard here adds no needed information.
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003455 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003456}
3457
Jeremy Gebben40a22942020-12-22 14:22:06 -07003458VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003459 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003460 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003461 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003462 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003463 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003464 // If we have an input attachment in last_reads and input attachments are ordered we all that stage
Jeremy Gebben40a22942020-12-22 14:22:06 -07003465 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003466 }
3467
3468 return ordered_stages;
3469}
3470
John Zulauf14940722021-04-12 15:19:02 -06003471void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003472 // Only record until we record a write.
3473 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003474 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003475 if (0 == (usage_stage & first_read_stages_)) {
3476 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003477 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003478 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003479 if (0 == (read_execution_barriers & usage_stage)) {
3480 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3481 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3482 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003483 }
3484 }
3485}
3486
John Zulauf4fa68462021-04-26 21:04:22 -06003487void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3488 // Only call this after recording an image layout transition
3489 assert(first_accesses_.size());
3490 if (first_accesses_.back().tag == tag) {
3491 // If this layout transition is the the first write, add the additional ordering rules that guard the ILT
Samuel Iglesias Gonsálvez9b4660b2021-10-21 08:50:39 +02003492 assert(first_accesses_.back().usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
John Zulauf4fa68462021-04-26 21:04:22 -06003493 first_write_layout_ordering_ = layout_ordering;
3494 }
3495}
3496
John Zulaufee984022022-04-13 16:39:50 -06003497void ResourceAccessState::ReadState::Set(VkPipelineStageFlags2KHR stage_, const SyncStageAccessFlags &access_,
3498 VkPipelineStageFlags2KHR barriers_, ResourceUsageTag tag_) {
3499 stage = stage_;
3500 access = access_;
3501 barriers = barriers_;
3502 tag = tag_;
3503 pending_dep_chain = 0; // If this is a new read, we aren't applying a barrier set.
3504}
3505
John Zulaufea943c52022-02-22 11:05:17 -07003506std::shared_ptr<CommandBufferAccessContext> SyncValidator::AccessContextFactory(VkCommandBuffer command_buffer) {
3507 // If we don't have one, make it.
3508 auto cb_state = Get<CMD_BUFFER_STATE>(command_buffer);
3509 assert(cb_state.get());
3510 auto queue_flags = cb_state->GetQueueFlags();
3511 return std::make_shared<CommandBufferAccessContext>(*this, cb_state, queue_flags);
3512}
3513
3514inline std::shared_ptr<CommandBufferAccessContext> SyncValidator::GetAccessContextShared(VkCommandBuffer command_buffer) {
3515 return GetMappedInsert(cb_access_state, command_buffer,
3516 [this, command_buffer]() { return AccessContextFactory(command_buffer); });
3517}
3518
3519std::shared_ptr<const CommandBufferAccessContext> SyncValidator::GetAccessContextShared(VkCommandBuffer command_buffer) const {
3520 return GetMapped(cb_access_state, command_buffer, []() { return std::shared_ptr<CommandBufferAccessContext>(); });
3521}
3522
3523const CommandBufferAccessContext *SyncValidator::GetAccessContext(VkCommandBuffer command_buffer) const {
3524 return GetMappedPlainFromShared(cb_access_state, command_buffer);
3525}
3526
3527CommandBufferAccessContext *SyncValidator::GetAccessContext(VkCommandBuffer command_buffer) {
3528 return GetAccessContextShared(command_buffer).get();
3529}
3530
3531CommandBufferAccessContext *SyncValidator::GetAccessContextNoInsert(VkCommandBuffer command_buffer) {
3532 return GetMappedPlainFromShared(cb_access_state, command_buffer);
3533}
3534
John Zulaufd1f85d42020-04-15 12:23:15 -06003535void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003536 auto *access_context = GetAccessContextNoInsert(command_buffer);
3537 if (access_context) {
3538 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003539 }
3540}
3541
John Zulaufd1f85d42020-04-15 12:23:15 -06003542void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3543 auto access_found = cb_access_state.find(command_buffer);
3544 if (access_found != cb_access_state.end()) {
3545 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003546 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003547 cb_access_state.erase(access_found);
3548 }
3549}
3550
John Zulauf9cb530d2019-09-30 14:14:10 -06003551bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3552 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3553 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003554 const auto *cb_context = GetAccessContext(commandBuffer);
3555 assert(cb_context);
3556 if (!cb_context) return skip;
3557 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003558
John Zulauf3d84f1b2020-03-09 13:33:25 -06003559 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003560 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3561 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003562
3563 for (uint32_t region = 0; region < regionCount; region++) {
3564 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003565 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003566 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003567 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003568 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003569 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003570 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003571 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06003572 cb_context->FormatHazard(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003573 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003574 }
John Zulauf16adfc92020-04-08 10:28:33 -06003575 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003576 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003577 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003578 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003579 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003580 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003581 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06003582 cb_context->FormatHazard(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003583 }
3584 }
3585 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003586 }
3587 return skip;
3588}
3589
3590void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3591 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003592 auto *cb_context = GetAccessContext(commandBuffer);
3593 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003594 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003595 auto *context = cb_context->GetCurrentAccessContext();
3596
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003597 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3598 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003599
3600 for (uint32_t region = 0; region < regionCount; region++) {
3601 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003602 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003603 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003604 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003605 }
John Zulauf16adfc92020-04-08 10:28:33 -06003606 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003607 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003608 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003609 }
3610 }
3611}
3612
John Zulauf4a6105a2020-11-17 15:11:05 -07003613void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3614 // Clear out events from the command buffer contexts
3615 for (auto &cb_context : cb_access_state) {
3616 cb_context.second->RecordDestroyEvent(event);
3617 }
3618}
3619
Tony-LunarGef035472021-11-02 10:23:33 -06003620bool SyncValidator::ValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos,
3621 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003622 bool skip = false;
3623 const auto *cb_context = GetAccessContext(commandBuffer);
3624 assert(cb_context);
3625 if (!cb_context) return skip;
3626 const auto *context = cb_context->GetCurrentAccessContext();
Tony-LunarGef035472021-11-02 10:23:33 -06003627 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003628
3629 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003630 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3631 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003632
3633 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3634 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3635 if (src_buffer) {
3636 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003637 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003638 if (hazard.hazard) {
3639 // TODO -- add tag information to log msg when useful.
3640 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003641 "%s(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003642 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06003643 region, cb_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003644 }
3645 }
3646 if (dst_buffer && !skip) {
3647 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003648 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003649 if (hazard.hazard) {
3650 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003651 "%s(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003652 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06003653 region, cb_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003654 }
3655 }
3656 if (skip) break;
3657 }
3658 return skip;
3659}
3660
Tony-LunarGef035472021-11-02 10:23:33 -06003661bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3662 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3663 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3664}
3665
3666bool SyncValidator::PreCallValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) const {
3667 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3668}
3669
3670void SyncValidator::RecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003671 auto *cb_context = GetAccessContext(commandBuffer);
3672 assert(cb_context);
Tony-LunarGef035472021-11-02 10:23:33 -06003673 const auto tag = cb_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003674 auto *context = cb_context->GetCurrentAccessContext();
3675
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003676 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3677 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003678
3679 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3680 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3681 if (src_buffer) {
3682 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003683 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003684 }
3685 if (dst_buffer) {
3686 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003687 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003688 }
3689 }
3690}
3691
Tony-LunarGef035472021-11-02 10:23:33 -06003692void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3693 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3694}
3695
3696void SyncValidator::PreCallRecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) {
3697 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3698}
3699
John Zulauf5c5e88d2019-12-26 11:22:02 -07003700bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3701 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3702 const VkImageCopy *pRegions) const {
3703 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003704 const auto *cb_access_context = GetAccessContext(commandBuffer);
3705 assert(cb_access_context);
3706 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003707
John Zulauf3d84f1b2020-03-09 13:33:25 -06003708 const auto *context = cb_access_context->GetCurrentAccessContext();
3709 assert(context);
3710 if (!context) return skip;
3711
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003712 auto src_image = Get<IMAGE_STATE>(srcImage);
3713 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003714 for (uint32_t region = 0; region < regionCount; region++) {
3715 const auto &copy_region = pRegions[region];
3716 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003717 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003718 copy_region.srcOffset, copy_region.extent);
3719 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003720 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003721 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003722 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06003723 cb_access_context->FormatHazard(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003724 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003725 }
3726
3727 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003728 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003729 copy_region.dstOffset, copy_region.extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003730 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003731 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003732 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003733 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06003734 cb_access_context->FormatHazard(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003735 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003736 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003737 }
3738 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003739
John Zulauf5c5e88d2019-12-26 11:22:02 -07003740 return skip;
3741}
3742
3743void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3744 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3745 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003746 auto *cb_access_context = GetAccessContext(commandBuffer);
3747 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003748 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003749 auto *context = cb_access_context->GetCurrentAccessContext();
3750 assert(context);
3751
Jeremy Gebben9f537102021-10-05 16:37:12 -06003752 auto src_image = Get<IMAGE_STATE>(srcImage);
3753 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003754
3755 for (uint32_t region = 0; region < regionCount; region++) {
3756 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003757 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003758 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003759 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003760 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003761 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003762 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003763 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003764 }
3765 }
3766}
3767
Tony-LunarGb61514a2021-11-02 12:36:51 -06003768bool SyncValidator::ValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo,
3769 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003770 bool skip = false;
3771 const auto *cb_access_context = GetAccessContext(commandBuffer);
3772 assert(cb_access_context);
3773 if (!cb_access_context) return skip;
3774
3775 const auto *context = cb_access_context->GetCurrentAccessContext();
3776 assert(context);
3777 if (!context) return skip;
3778
Tony-LunarGb61514a2021-11-02 12:36:51 -06003779 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003780 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3781 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003782
Jeff Leger178b1e52020-10-05 12:22:23 -04003783 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3784 const auto &copy_region = pCopyImageInfo->pRegions[region];
3785 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003786 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003787 copy_region.srcOffset, copy_region.extent);
3788 if (hazard.hazard) {
3789 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003790 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003791 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06003792 region, cb_access_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003793 }
3794 }
3795
3796 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003797 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003798 copy_region.dstOffset, copy_region.extent);
Jeff Leger178b1e52020-10-05 12:22:23 -04003799 if (hazard.hazard) {
3800 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003801 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003802 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06003803 region, cb_access_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003804 }
3805 if (skip) break;
3806 }
3807 }
3808
3809 return skip;
3810}
3811
Tony-LunarGb61514a2021-11-02 12:36:51 -06003812bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3813 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3814 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3815}
3816
3817bool SyncValidator::PreCallValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) const {
3818 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3819}
3820
3821void SyncValidator::RecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003822 auto *cb_access_context = GetAccessContext(commandBuffer);
3823 assert(cb_access_context);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003824 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003825 auto *context = cb_access_context->GetCurrentAccessContext();
3826 assert(context);
3827
Jeremy Gebben9f537102021-10-05 16:37:12 -06003828 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3829 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04003830
3831 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3832 const auto &copy_region = pCopyImageInfo->pRegions[region];
3833 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003834 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003835 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003836 }
3837 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003838 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003839 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003840 }
3841 }
3842}
3843
Tony-LunarGb61514a2021-11-02 12:36:51 -06003844void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3845 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3846}
3847
3848void SyncValidator::PreCallRecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) {
3849 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3850}
3851
John Zulauf9cb530d2019-09-30 14:14:10 -06003852bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3853 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3854 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3855 uint32_t bufferMemoryBarrierCount,
3856 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3857 uint32_t imageMemoryBarrierCount,
3858 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3859 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003860 const auto *cb_access_context = GetAccessContext(commandBuffer);
3861 assert(cb_access_context);
3862 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003863
John Zulauf36ef9282021-02-02 11:47:24 -07003864 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3865 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3866 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3867 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003868 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003869 return skip;
3870}
3871
3872void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3873 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3874 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3875 uint32_t bufferMemoryBarrierCount,
3876 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3877 uint32_t imageMemoryBarrierCount,
3878 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003879 auto *cb_access_context = GetAccessContext(commandBuffer);
3880 assert(cb_access_context);
3881 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003882
John Zulauf1bf30522021-09-03 15:39:06 -06003883 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(),
3884 srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount,
3885 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3886 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06003887}
3888
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003889bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3890 const VkDependencyInfoKHR *pDependencyInfo) const {
3891 bool skip = false;
3892 const auto *cb_access_context = GetAccessContext(commandBuffer);
3893 assert(cb_access_context);
3894 if (!cb_access_context) return skip;
3895
3896 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3897 skip = pipeline_barrier.Validate(*cb_access_context);
3898 return skip;
3899}
3900
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003901bool SyncValidator::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
3902 const VkDependencyInfo *pDependencyInfo) const {
3903 bool skip = false;
3904 const auto *cb_access_context = GetAccessContext(commandBuffer);
3905 assert(cb_access_context);
3906 if (!cb_access_context) return skip;
3907
3908 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3909 skip = pipeline_barrier.Validate(*cb_access_context);
3910 return skip;
3911}
3912
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003913void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3914 auto *cb_access_context = GetAccessContext(commandBuffer);
3915 assert(cb_access_context);
3916 if (!cb_access_context) return;
3917
John Zulauf1bf30522021-09-03 15:39:06 -06003918 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(),
3919 *pDependencyInfo);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003920}
3921
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003922void SyncValidator::PreCallRecordCmdPipelineBarrier2(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) {
3923 auto *cb_access_context = GetAccessContext(commandBuffer);
3924 assert(cb_access_context);
3925 if (!cb_access_context) return;
3926
3927 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(),
3928 *pDependencyInfo);
3929}
3930
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003931void SyncValidator::CreateDevice(const VkDeviceCreateInfo *pCreateInfo) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003932 // The state tracker sets up the device state
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003933 StateTracker::CreateDevice(pCreateInfo);
John Zulauf9cb530d2019-09-30 14:14:10 -06003934
John Zulauf5f13a792020-03-10 07:31:21 -06003935 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3936 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003937 // TODO: Find a good way to do this hooklessly.
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003938 SetCommandBufferResetCallback([this](VkCommandBuffer command_buffer) -> void { ResetCommandBufferCallback(command_buffer); });
3939 SetCommandBufferFreeCallback([this](VkCommandBuffer command_buffer) -> void { FreeCommandBufferCallback(command_buffer); });
John Zulauf9cb530d2019-09-30 14:14:10 -06003940}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003941
John Zulauf355e49b2020-04-24 15:11:15 -06003942bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003943 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003944 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003945 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003946 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003947 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003948 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003949 }
John Zulauf355e49b2020-04-24 15:11:15 -06003950 return skip;
3951}
3952
3953bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3954 VkSubpassContents contents) const {
3955 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003956 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003957 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003958 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003959 return skip;
3960}
3961
3962bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003963 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003964 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003965 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003966 return skip;
3967}
3968
3969bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3970 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003971 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003972 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003973 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003974 return skip;
3975}
3976
John Zulauf3d84f1b2020-03-09 13:33:25 -06003977void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3978 VkResult result) {
3979 // The state tracker sets up the command buffer state
3980 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3981
3982 // Create/initialize the structure that trackers accesses at the command buffer scope.
3983 auto cb_access_context = GetAccessContext(commandBuffer);
3984 assert(cb_access_context);
3985 cb_access_context->Reset();
3986}
3987
3988void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003989 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003990 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003991 if (cb_context) {
John Zulaufbb890452021-12-14 11:30:18 -07003992 cb_context->RecordSyncOp<SyncOpBeginRenderPass>(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003993 }
3994}
3995
3996void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3997 VkSubpassContents contents) {
3998 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003999 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06004000 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06004001 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004002}
4003
4004void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
4005 const VkSubpassBeginInfo *pSubpassBeginInfo) {
4006 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06004007 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004008}
4009
4010void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
4011 const VkRenderPassBeginInfo *pRenderPassBegin,
4012 const VkSubpassBeginInfo *pSubpassBeginInfo) {
4013 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07004014 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06004015}
4016
Mike Schuchardt2df08912020-12-15 16:28:09 -08004017bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07004018 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004019 bool skip = false;
4020
4021 auto cb_context = GetAccessContext(commandBuffer);
4022 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07004023 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07004024 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07004025 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06004026}
4027
4028bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
4029 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07004030 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07004031 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06004032 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07004033 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
4034 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06004035 return skip;
4036}
4037
Mike Schuchardt2df08912020-12-15 16:28:09 -08004038bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
4039 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004040 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07004041 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06004042 return skip;
4043}
4044
4045bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
4046 const VkSubpassEndInfo *pSubpassEndInfo) const {
4047 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07004048 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06004049 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06004050}
4051
4052void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07004053 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06004054 auto cb_context = GetAccessContext(commandBuffer);
4055 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07004056 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06004057
John Zulaufbb890452021-12-14 11:30:18 -07004058 cb_context->RecordSyncOp<SyncOpNextSubpass>(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004059}
4060
4061void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
4062 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07004063 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06004064 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06004065 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004066}
4067
4068void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
4069 const VkSubpassEndInfo *pSubpassEndInfo) {
4070 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06004071 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004072}
4073
4074void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
4075 const VkSubpassEndInfo *pSubpassEndInfo) {
4076 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07004077 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004078}
4079
sfricke-samsung85584a72021-09-30 21:43:38 -07004080bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
4081 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004082 bool skip = false;
4083
4084 auto cb_context = GetAccessContext(commandBuffer);
4085 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07004086 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06004087
sfricke-samsung85584a72021-09-30 21:43:38 -07004088 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07004089 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06004090 return skip;
4091}
4092
4093bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
4094 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07004095 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06004096 return skip;
4097}
4098
Mike Schuchardt2df08912020-12-15 16:28:09 -08004099bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004100 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07004101 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06004102 return skip;
4103}
4104
4105bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08004106 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004107 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07004108 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06004109 return skip;
4110}
4111
sfricke-samsung85584a72021-09-30 21:43:38 -07004112void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06004113 // Resolve the all subpass contexts to the command buffer contexts
4114 auto cb_context = GetAccessContext(commandBuffer);
4115 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07004116 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06004117
John Zulaufbb890452021-12-14 11:30:18 -07004118 cb_context->RecordSyncOp<SyncOpEndRenderPass>(cmd, *this, pSubpassEndInfo);
John Zulaufe5da6e52020-03-18 15:32:18 -06004119}
John Zulauf3d84f1b2020-03-09 13:33:25 -06004120
John Zulauf33fc1d52020-07-17 11:01:10 -06004121// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
4122// updates to a resource which do not conflict at the byte level.
4123// TODO: Revisit this rule to see if it needs to be tighter or looser
4124// TODO: Add programatic control over suppression heuristics
4125bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
4126 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
4127}
4128
John Zulauf3d84f1b2020-03-09 13:33:25 -06004129void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004130 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06004131 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004132}
4133
4134void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004135 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004136 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004137}
4138
4139void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07004140 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06004141 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004142}
locke-lunarga19c71d2020-03-02 18:17:04 -07004143
sfricke-samsung71f04e32022-03-16 01:21:21 -05004144template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004145bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004146 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4147 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004148 bool skip = false;
4149 const auto *cb_access_context = GetAccessContext(commandBuffer);
4150 assert(cb_access_context);
4151 if (!cb_access_context) return skip;
4152
Tony Barbour845d29b2021-11-09 11:43:14 -07004153 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004154
locke-lunarga19c71d2020-03-02 18:17:04 -07004155 const auto *context = cb_access_context->GetCurrentAccessContext();
4156 assert(context);
4157 if (!context) return skip;
4158
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004159 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4160 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004161
4162 for (uint32_t region = 0; region < regionCount; region++) {
4163 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07004164 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07004165 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004166 if (src_buffer) {
4167 ResourceAccessRange src_range =
4168 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004169 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07004170 if (hazard.hazard) {
4171 // PHASE1 TODO -- add tag information to log msg when useful.
4172 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
4173 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4174 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004175 cb_access_context->FormatHazard(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004176 }
4177 }
4178
Jeremy Gebben40a22942020-12-22 14:22:06 -07004179 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07004180 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004181 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004182 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004183 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004184 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004185 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004186 }
4187 if (skip) break;
4188 }
4189 if (skip) break;
4190 }
4191 return skip;
4192}
4193
Jeff Leger178b1e52020-10-05 12:22:23 -04004194bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4195 VkImageLayout dstImageLayout, uint32_t regionCount,
4196 const VkBufferImageCopy *pRegions) const {
4197 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
Tony Barbour845d29b2021-11-09 11:43:14 -07004198 CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004199}
4200
4201bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4202 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4203 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4204 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004205 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4206}
4207
4208bool SyncValidator::PreCallValidateCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4209 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) const {
4210 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4211 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4212 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004213}
4214
sfricke-samsung71f04e32022-03-16 01:21:21 -05004215template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004216void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004217 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4218 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004219 auto *cb_access_context = GetAccessContext(commandBuffer);
4220 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004221
Jeff Leger178b1e52020-10-05 12:22:23 -04004222 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004223 auto *context = cb_access_context->GetCurrentAccessContext();
4224 assert(context);
4225
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004226 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4227 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004228
4229 for (uint32_t region = 0; region < regionCount; region++) {
4230 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07004231 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004232 if (src_buffer) {
4233 ResourceAccessRange src_range =
4234 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004235 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004236 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07004237 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004238 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004239 }
4240 }
4241}
4242
Jeff Leger178b1e52020-10-05 12:22:23 -04004243void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4244 VkImageLayout dstImageLayout, uint32_t regionCount,
4245 const VkBufferImageCopy *pRegions) {
4246 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
Tony Barbour845d29b2021-11-09 11:43:14 -07004247 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004248}
4249
4250void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4251 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4252 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4253 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4254 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004255 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4256}
4257
4258void SyncValidator::PreCallRecordCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4259 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) {
4260 StateTracker::PreCallRecordCmdCopyBufferToImage2(commandBuffer, pCopyBufferToImageInfo);
4261 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4262 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4263 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004264}
4265
sfricke-samsung71f04e32022-03-16 01:21:21 -05004266template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004267bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004268 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
4269 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004270 bool skip = false;
4271 const auto *cb_access_context = GetAccessContext(commandBuffer);
4272 assert(cb_access_context);
4273 if (!cb_access_context) return skip;
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004274 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004275
locke-lunarga19c71d2020-03-02 18:17:04 -07004276 const auto *context = cb_access_context->GetCurrentAccessContext();
4277 assert(context);
4278 if (!context) return skip;
4279
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004280 auto src_image = Get<IMAGE_STATE>(srcImage);
4281 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004282 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004283 for (uint32_t region = 0; region < regionCount; region++) {
4284 const auto &copy_region = pRegions[region];
4285 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004286 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004287 copy_region.imageOffset, copy_region.imageExtent);
4288 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004289 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004290 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004291 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004292 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004293 }
John Zulauf477700e2021-01-06 11:41:49 -07004294 if (dst_mem) {
4295 ResourceAccessRange dst_range =
4296 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004297 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004298 if (hazard.hazard) {
4299 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4300 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4301 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004302 cb_access_context->FormatHazard(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004303 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004304 }
4305 }
4306 if (skip) break;
4307 }
4308 return skip;
4309}
4310
Jeff Leger178b1e52020-10-05 12:22:23 -04004311bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4312 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4313 const VkBufferImageCopy *pRegions) const {
4314 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004315 CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004316}
4317
4318bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4319 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4320 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4321 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004322 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4323}
4324
4325bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4326 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) const {
4327 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4328 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4329 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004330}
4331
sfricke-samsung71f04e32022-03-16 01:21:21 -05004332template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004333void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004334 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004335 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004336 auto *cb_access_context = GetAccessContext(commandBuffer);
4337 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004338
Jeff Leger178b1e52020-10-05 12:22:23 -04004339 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004340 auto *context = cb_access_context->GetCurrentAccessContext();
4341 assert(context);
4342
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004343 auto src_image = Get<IMAGE_STATE>(srcImage);
Jeremy Gebben9f537102021-10-05 16:37:12 -06004344 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004345 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004346 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004347
4348 for (uint32_t region = 0; region < regionCount; region++) {
4349 const auto &copy_region = pRegions[region];
4350 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004351 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004352 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004353 if (dst_buffer) {
4354 ResourceAccessRange dst_range =
4355 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004356 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004357 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004358 }
4359 }
4360}
4361
Jeff Leger178b1e52020-10-05 12:22:23 -04004362void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4363 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4364 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004365 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004366}
4367
4368void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4369 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4370 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4371 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4372 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004373 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4374}
4375
4376void SyncValidator::PreCallRecordCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4377 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) {
4378 StateTracker::PreCallRecordCmdCopyImageToBuffer2(commandBuffer, pCopyImageToBufferInfo);
4379 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4380 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4381 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004382}
4383
4384template <typename RegionType>
4385bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4386 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4387 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004388 bool skip = false;
4389 const auto *cb_access_context = GetAccessContext(commandBuffer);
4390 assert(cb_access_context);
4391 if (!cb_access_context) return skip;
4392
4393 const auto *context = cb_access_context->GetCurrentAccessContext();
4394 assert(context);
4395 if (!context) return skip;
4396
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004397 auto src_image = Get<IMAGE_STATE>(srcImage);
4398 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004399
4400 for (uint32_t region = 0; region < regionCount; region++) {
4401 const auto &blit_region = pRegions[region];
4402 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004403 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4404 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4405 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4406 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4407 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4408 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004409 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004410 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004411 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004412 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004413 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004414 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004415 }
4416 }
4417
4418 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004419 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4420 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4421 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4422 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4423 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4424 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004425 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004426 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004427 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004428 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004429 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004430 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004431 }
4432 if (skip) break;
4433 }
4434 }
4435
4436 return skip;
4437}
4438
Jeff Leger178b1e52020-10-05 12:22:23 -04004439bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4440 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4441 const VkImageBlit *pRegions, VkFilter filter) const {
4442 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4443 "vkCmdBlitImage");
4444}
4445
4446bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4447 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4448 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4449 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4450 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4451}
4452
Tony-LunarG542ae912021-11-04 16:06:44 -06004453bool SyncValidator::PreCallValidateCmdBlitImage2(VkCommandBuffer commandBuffer,
4454 const VkBlitImageInfo2 *pBlitImageInfo) const {
4455 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4456 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4457 pBlitImageInfo->filter, "vkCmdBlitImage2");
4458}
4459
Jeff Leger178b1e52020-10-05 12:22:23 -04004460template <typename RegionType>
4461void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4462 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4463 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004464 auto *cb_access_context = GetAccessContext(commandBuffer);
4465 assert(cb_access_context);
4466 auto *context = cb_access_context->GetCurrentAccessContext();
4467 assert(context);
4468
Jeremy Gebben9f537102021-10-05 16:37:12 -06004469 auto src_image = Get<IMAGE_STATE>(srcImage);
4470 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004471
4472 for (uint32_t region = 0; region < regionCount; region++) {
4473 const auto &blit_region = pRegions[region];
4474 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004475 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4476 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4477 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4478 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4479 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4480 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004481 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004482 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004483 }
4484 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004485 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4486 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4487 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4488 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4489 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4490 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004491 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004492 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004493 }
4494 }
4495}
locke-lunarg36ba2592020-04-03 09:42:04 -06004496
Jeff Leger178b1e52020-10-05 12:22:23 -04004497void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4498 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4499 const VkImageBlit *pRegions, VkFilter filter) {
4500 auto *cb_access_context = GetAccessContext(commandBuffer);
4501 assert(cb_access_context);
4502 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4503 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4504 pRegions, filter);
4505 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4506}
4507
4508void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4509 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4510 auto *cb_access_context = GetAccessContext(commandBuffer);
4511 assert(cb_access_context);
4512 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4513 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4514 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4515 pBlitImageInfo->filter, tag);
4516}
4517
Tony-LunarG542ae912021-11-04 16:06:44 -06004518void SyncValidator::PreCallRecordCmdBlitImage2(VkCommandBuffer commandBuffer, const VkBlitImageInfo2 *pBlitImageInfo) {
4519 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4520 auto *cb_access_context = GetAccessContext(commandBuffer);
4521 assert(cb_access_context);
4522 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2);
4523 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4524 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4525 pBlitImageInfo->filter, tag);
4526}
4527
John Zulauffaea0ee2021-01-14 14:01:32 -07004528bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4529 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4530 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4531 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004532 bool skip = false;
4533 if (drawCount == 0) return skip;
4534
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004535 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004536 VkDeviceSize size = struct_size;
4537 if (drawCount == 1 || stride == size) {
4538 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004539 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004540 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4541 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004542 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004543 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004544 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06004545 cb_context.FormatHazard(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004546 }
4547 } else {
4548 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004549 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004550 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4551 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004552 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004553 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4554 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06004555 cb_context.FormatHazard(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004556 break;
4557 }
4558 }
4559 }
4560 return skip;
4561}
4562
John Zulauf14940722021-04-12 15:19:02 -06004563void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004564 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4565 uint32_t stride) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004566 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004567 VkDeviceSize size = struct_size;
4568 if (drawCount == 1 || stride == size) {
4569 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004570 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004571 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004572 } else {
4573 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004574 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004575 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4576 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004577 }
4578 }
4579}
4580
John Zulauffaea0ee2021-01-14 14:01:32 -07004581bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4582 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4583 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004584 bool skip = false;
4585
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004586 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004587 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004588 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4589 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004590 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004591 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004592 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06004593 cb_context.FormatHazard(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004594 }
4595 return skip;
4596}
4597
John Zulauf14940722021-04-12 15:19:02 -06004598void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004599 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004600 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004601 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004602}
4603
locke-lunarg36ba2592020-04-03 09:42:04 -06004604bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004605 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004606 const auto *cb_access_context = GetAccessContext(commandBuffer);
4607 assert(cb_access_context);
4608 if (!cb_access_context) return skip;
4609
locke-lunarg61870c22020-06-09 14:51:50 -06004610 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004611 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004612}
4613
4614void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004615 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004616 auto *cb_access_context = GetAccessContext(commandBuffer);
4617 assert(cb_access_context);
4618 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004619
locke-lunarg61870c22020-06-09 14:51:50 -06004620 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004621}
locke-lunarge1a67022020-04-29 00:15:36 -06004622
4623bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004624 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004625 const auto *cb_access_context = GetAccessContext(commandBuffer);
4626 assert(cb_access_context);
4627 if (!cb_access_context) return skip;
4628
4629 const auto *context = cb_access_context->GetCurrentAccessContext();
4630 assert(context);
4631 if (!context) return skip;
4632
locke-lunarg61870c22020-06-09 14:51:50 -06004633 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004634 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4635 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004636 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004637}
4638
4639void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004640 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004641 auto *cb_access_context = GetAccessContext(commandBuffer);
4642 assert(cb_access_context);
4643 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4644 auto *context = cb_access_context->GetCurrentAccessContext();
4645 assert(context);
4646
locke-lunarg61870c22020-06-09 14:51:50 -06004647 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4648 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004649}
4650
4651bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4652 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004653 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004654 const auto *cb_access_context = GetAccessContext(commandBuffer);
4655 assert(cb_access_context);
4656 if (!cb_access_context) return skip;
4657
locke-lunarg61870c22020-06-09 14:51:50 -06004658 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4659 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4660 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004661 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004662}
4663
4664void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4665 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004666 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004667 auto *cb_access_context = GetAccessContext(commandBuffer);
4668 assert(cb_access_context);
4669 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004670
locke-lunarg61870c22020-06-09 14:51:50 -06004671 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4672 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4673 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004674}
4675
4676bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4677 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004678 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004679 const auto *cb_access_context = GetAccessContext(commandBuffer);
4680 assert(cb_access_context);
4681 if (!cb_access_context) return skip;
4682
locke-lunarg61870c22020-06-09 14:51:50 -06004683 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4684 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4685 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004686 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004687}
4688
4689void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4690 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004691 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004692 auto *cb_access_context = GetAccessContext(commandBuffer);
4693 assert(cb_access_context);
4694 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004695
locke-lunarg61870c22020-06-09 14:51:50 -06004696 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4697 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4698 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004699}
4700
4701bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4702 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004703 bool skip = false;
4704 if (drawCount == 0) return skip;
4705
locke-lunargff255f92020-05-13 18:53:52 -06004706 const auto *cb_access_context = GetAccessContext(commandBuffer);
4707 assert(cb_access_context);
4708 if (!cb_access_context) return skip;
4709
4710 const auto *context = cb_access_context->GetCurrentAccessContext();
4711 assert(context);
4712 if (!context) return skip;
4713
locke-lunarg61870c22020-06-09 14:51:50 -06004714 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4715 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004716 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4717 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004718
4719 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4720 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4721 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004722 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004723 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004724}
4725
4726void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4727 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004728 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004729 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004730 auto *cb_access_context = GetAccessContext(commandBuffer);
4731 assert(cb_access_context);
4732 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4733 auto *context = cb_access_context->GetCurrentAccessContext();
4734 assert(context);
4735
locke-lunarg61870c22020-06-09 14:51:50 -06004736 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4737 cb_access_context->RecordDrawSubpassAttachment(tag);
4738 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004739
4740 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4741 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4742 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004743 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004744}
4745
4746bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4747 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004748 bool skip = false;
4749 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004750 const auto *cb_access_context = GetAccessContext(commandBuffer);
4751 assert(cb_access_context);
4752 if (!cb_access_context) return skip;
4753
4754 const auto *context = cb_access_context->GetCurrentAccessContext();
4755 assert(context);
4756 if (!context) return skip;
4757
locke-lunarg61870c22020-06-09 14:51:50 -06004758 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4759 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004760 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4761 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004762
4763 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4764 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4765 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004766 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004767 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004768}
4769
4770void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4771 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004772 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004773 auto *cb_access_context = GetAccessContext(commandBuffer);
4774 assert(cb_access_context);
4775 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4776 auto *context = cb_access_context->GetCurrentAccessContext();
4777 assert(context);
4778
locke-lunarg61870c22020-06-09 14:51:50 -06004779 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4780 cb_access_context->RecordDrawSubpassAttachment(tag);
4781 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004782
4783 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4784 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4785 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004786 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004787}
4788
4789bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4790 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4791 uint32_t stride, const char *function) const {
4792 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004793 const auto *cb_access_context = GetAccessContext(commandBuffer);
4794 assert(cb_access_context);
4795 if (!cb_access_context) return skip;
4796
4797 const auto *context = cb_access_context->GetCurrentAccessContext();
4798 assert(context);
4799 if (!context) return skip;
4800
locke-lunarg61870c22020-06-09 14:51:50 -06004801 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4802 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004803 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4804 maxDrawCount, stride, function);
4805 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004806
4807 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4808 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4809 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004810 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004811 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004812}
4813
4814bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4815 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4816 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004817 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4818 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004819}
4820
sfricke-samsung85584a72021-09-30 21:43:38 -07004821void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4822 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4823 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004824 auto *cb_access_context = GetAccessContext(commandBuffer);
4825 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004826 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004827 auto *context = cb_access_context->GetCurrentAccessContext();
4828 assert(context);
4829
locke-lunarg61870c22020-06-09 14:51:50 -06004830 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4831 cb_access_context->RecordDrawSubpassAttachment(tag);
4832 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4833 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004834
4835 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4836 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4837 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004838 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004839}
4840
sfricke-samsung85584a72021-09-30 21:43:38 -07004841void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4842 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4843 uint32_t stride) {
4844 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4845 stride);
4846 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4847 CMD_DRAWINDIRECTCOUNT);
4848}
locke-lunarge1a67022020-04-29 00:15:36 -06004849bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4850 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4851 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004852 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4853 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004854}
4855
4856void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4857 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4858 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004859 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4860 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004861 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4862 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004863}
4864
4865bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4866 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4867 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004868 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4869 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004870}
4871
4872void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4873 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4874 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004875 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4876 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004877 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4878 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004879}
4880
4881bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4882 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4883 uint32_t stride, const char *function) const {
4884 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004885 const auto *cb_access_context = GetAccessContext(commandBuffer);
4886 assert(cb_access_context);
4887 if (!cb_access_context) return skip;
4888
4889 const auto *context = cb_access_context->GetCurrentAccessContext();
4890 assert(context);
4891 if (!context) return skip;
4892
locke-lunarg61870c22020-06-09 14:51:50 -06004893 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4894 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004895 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4896 offset, maxDrawCount, stride, function);
4897 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004898
4899 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4900 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4901 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004902 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004903 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004904}
4905
4906bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4907 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4908 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004909 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4910 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004911}
4912
sfricke-samsung85584a72021-09-30 21:43:38 -07004913void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4914 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4915 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004916 auto *cb_access_context = GetAccessContext(commandBuffer);
4917 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004918 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004919 auto *context = cb_access_context->GetCurrentAccessContext();
4920 assert(context);
4921
locke-lunarg61870c22020-06-09 14:51:50 -06004922 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4923 cb_access_context->RecordDrawSubpassAttachment(tag);
4924 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4925 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004926
4927 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4928 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004929 // We will update the index and vertex buffer in SubmitQueue in the future.
4930 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004931}
4932
sfricke-samsung85584a72021-09-30 21:43:38 -07004933void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4934 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4935 uint32_t maxDrawCount, uint32_t stride) {
4936 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4937 maxDrawCount, stride);
4938 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4939 CMD_DRAWINDEXEDINDIRECTCOUNT);
4940}
4941
locke-lunarge1a67022020-04-29 00:15:36 -06004942bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4943 VkDeviceSize offset, VkBuffer countBuffer,
4944 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4945 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004946 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4947 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004948}
4949
4950void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4951 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4952 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004953 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4954 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004955 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4956 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004957}
4958
4959bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4960 VkDeviceSize offset, VkBuffer countBuffer,
4961 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4962 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004963 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4964 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004965}
4966
4967void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4968 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4969 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004970 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4971 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004972 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4973 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004974}
4975
4976bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4977 const VkClearColorValue *pColor, uint32_t rangeCount,
4978 const VkImageSubresourceRange *pRanges) const {
4979 bool skip = false;
4980 const auto *cb_access_context = GetAccessContext(commandBuffer);
4981 assert(cb_access_context);
4982 if (!cb_access_context) return skip;
4983
4984 const auto *context = cb_access_context->GetCurrentAccessContext();
4985 assert(context);
4986 if (!context) return skip;
4987
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004988 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004989
4990 for (uint32_t index = 0; index < rangeCount; index++) {
4991 const auto &range = pRanges[index];
4992 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004993 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004994 if (hazard.hazard) {
4995 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004996 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004997 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf397e68b2022-04-19 11:44:07 -06004998 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004999 }
5000 }
5001 }
5002 return skip;
5003}
5004
5005void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
5006 const VkClearColorValue *pColor, uint32_t rangeCount,
5007 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005008 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06005009 auto *cb_access_context = GetAccessContext(commandBuffer);
5010 assert(cb_access_context);
5011 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
5012 auto *context = cb_access_context->GetCurrentAccessContext();
5013 assert(context);
5014
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005015 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06005016
5017 for (uint32_t index = 0; index < rangeCount; index++) {
5018 const auto &range = pRanges[index];
5019 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06005020 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005021 }
5022 }
5023}
5024
5025bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
5026 VkImageLayout imageLayout,
5027 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
5028 const VkImageSubresourceRange *pRanges) const {
5029 bool skip = false;
5030 const auto *cb_access_context = GetAccessContext(commandBuffer);
5031 assert(cb_access_context);
5032 if (!cb_access_context) return skip;
5033
5034 const auto *context = cb_access_context->GetCurrentAccessContext();
5035 assert(context);
5036 if (!context) return skip;
5037
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005038 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06005039
5040 for (uint32_t index = 0; index < rangeCount; index++) {
5041 const auto &range = pRanges[index];
5042 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06005043 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005044 if (hazard.hazard) {
5045 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005046 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005047 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf397e68b2022-04-19 11:44:07 -06005048 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005049 }
5050 }
5051 }
5052 return skip;
5053}
5054
5055void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
5056 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
5057 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005058 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06005059 auto *cb_access_context = GetAccessContext(commandBuffer);
5060 assert(cb_access_context);
5061 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
5062 auto *context = cb_access_context->GetCurrentAccessContext();
5063 assert(context);
5064
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005065 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06005066
5067 for (uint32_t index = 0; index < rangeCount; index++) {
5068 const auto &range = pRanges[index];
5069 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06005070 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005071 }
5072 }
5073}
5074
5075bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
5076 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
5077 VkDeviceSize dstOffset, VkDeviceSize stride,
5078 VkQueryResultFlags flags) const {
5079 bool skip = false;
5080 const auto *cb_access_context = GetAccessContext(commandBuffer);
5081 assert(cb_access_context);
5082 if (!cb_access_context) return skip;
5083
5084 const auto *context = cb_access_context->GetCurrentAccessContext();
5085 assert(context);
5086 if (!context) return skip;
5087
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005088 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005089
5090 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005091 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005092 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005093 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005094 skip |=
5095 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5096 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf397e68b2022-04-19 11:44:07 -06005097 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005098 }
5099 }
locke-lunargff255f92020-05-13 18:53:52 -06005100
5101 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06005102 return skip;
5103}
5104
5105void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
5106 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5107 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005108 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
5109 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06005110 auto *cb_access_context = GetAccessContext(commandBuffer);
5111 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06005112 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06005113 auto *context = cb_access_context->GetCurrentAccessContext();
5114 assert(context);
5115
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005116 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005117
5118 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005119 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005120 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005121 }
locke-lunargff255f92020-05-13 18:53:52 -06005122
5123 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06005124}
5125
5126bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5127 VkDeviceSize size, uint32_t data) const {
5128 bool skip = false;
5129 const auto *cb_access_context = GetAccessContext(commandBuffer);
5130 assert(cb_access_context);
5131 if (!cb_access_context) return skip;
5132
5133 const auto *context = cb_access_context->GetCurrentAccessContext();
5134 assert(context);
5135 if (!context) return skip;
5136
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005137 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005138
5139 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005140 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005141 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005142 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005143 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005144 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf397e68b2022-04-19 11:44:07 -06005145 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005146 }
5147 }
5148 return skip;
5149}
5150
5151void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5152 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005153 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06005154 auto *cb_access_context = GetAccessContext(commandBuffer);
5155 assert(cb_access_context);
5156 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
5157 auto *context = cb_access_context->GetCurrentAccessContext();
5158 assert(context);
5159
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005160 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005161
5162 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005163 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005164 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005165 }
5166}
5167
5168bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5169 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5170 const VkImageResolve *pRegions) const {
5171 bool skip = false;
5172 const auto *cb_access_context = GetAccessContext(commandBuffer);
5173 assert(cb_access_context);
5174 if (!cb_access_context) return skip;
5175
5176 const auto *context = cb_access_context->GetCurrentAccessContext();
5177 assert(context);
5178 if (!context) return skip;
5179
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005180 auto src_image = Get<IMAGE_STATE>(srcImage);
5181 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005182
5183 for (uint32_t region = 0; region < regionCount; region++) {
5184 const auto &resolve_region = pRegions[region];
5185 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005186 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005187 resolve_region.srcOffset, resolve_region.extent);
5188 if (hazard.hazard) {
5189 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005190 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005191 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06005192 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005193 }
5194 }
5195
5196 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005197 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005198 resolve_region.dstOffset, resolve_region.extent);
5199 if (hazard.hazard) {
5200 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005201 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005202 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06005203 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005204 }
5205 if (skip) break;
5206 }
5207 }
5208
5209 return skip;
5210}
5211
5212void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5213 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5214 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005215 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5216 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005217 auto *cb_access_context = GetAccessContext(commandBuffer);
5218 assert(cb_access_context);
5219 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5220 auto *context = cb_access_context->GetCurrentAccessContext();
5221 assert(context);
5222
Jeremy Gebben9f537102021-10-05 16:37:12 -06005223 auto src_image = Get<IMAGE_STATE>(srcImage);
5224 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005225
5226 for (uint32_t region = 0; region < regionCount; region++) {
5227 const auto &resolve_region = pRegions[region];
5228 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005229 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005230 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005231 }
5232 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005233 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005234 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005235 }
5236 }
5237}
5238
Tony-LunarG562fc102021-11-12 13:58:35 -07005239bool SyncValidator::ValidateCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5240 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04005241 bool skip = false;
5242 const auto *cb_access_context = GetAccessContext(commandBuffer);
5243 assert(cb_access_context);
5244 if (!cb_access_context) return skip;
5245
5246 const auto *context = cb_access_context->GetCurrentAccessContext();
5247 assert(context);
5248 if (!context) return skip;
5249
Tony-LunarG562fc102021-11-12 13:58:35 -07005250 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005251 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5252 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005253
5254 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5255 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5256 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005257 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005258 resolve_region.srcOffset, resolve_region.extent);
5259 if (hazard.hazard) {
5260 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005261 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005262 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06005263 region, cb_access_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005264 }
5265 }
5266
5267 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005268 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005269 resolve_region.dstOffset, resolve_region.extent);
5270 if (hazard.hazard) {
5271 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005272 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005273 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06005274 region, cb_access_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005275 }
5276 if (skip) break;
5277 }
5278 }
5279
5280 return skip;
5281}
5282
Tony-LunarG562fc102021-11-12 13:58:35 -07005283bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5284 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5285 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5286}
5287
5288bool SyncValidator::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
5289 const VkResolveImageInfo2 *pResolveImageInfo) const {
5290 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5291}
5292
5293void SyncValidator::RecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5294 CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04005295 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5296 auto *cb_access_context = GetAccessContext(commandBuffer);
5297 assert(cb_access_context);
Tony-LunarG562fc102021-11-12 13:58:35 -07005298 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04005299 auto *context = cb_access_context->GetCurrentAccessContext();
5300 assert(context);
5301
Jeremy Gebben9f537102021-10-05 16:37:12 -06005302 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5303 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005304
5305 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5306 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5307 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005308 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005309 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005310 }
5311 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005312 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005313 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005314 }
5315 }
5316}
5317
Tony-LunarG562fc102021-11-12 13:58:35 -07005318void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5319 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5320 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5321}
5322
5323void SyncValidator::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2 *pResolveImageInfo) {
5324 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5325}
5326
locke-lunarge1a67022020-04-29 00:15:36 -06005327bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5328 VkDeviceSize dataSize, const void *pData) const {
5329 bool skip = false;
5330 const auto *cb_access_context = GetAccessContext(commandBuffer);
5331 assert(cb_access_context);
5332 if (!cb_access_context) return skip;
5333
5334 const auto *context = cb_access_context->GetCurrentAccessContext();
5335 assert(context);
5336 if (!context) return skip;
5337
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005338 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005339
5340 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005341 // VK_WHOLE_SIZE not allowed
5342 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005343 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005344 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005345 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005346 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf397e68b2022-04-19 11:44:07 -06005347 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005348 }
5349 }
5350 return skip;
5351}
5352
5353void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5354 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005355 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005356 auto *cb_access_context = GetAccessContext(commandBuffer);
5357 assert(cb_access_context);
5358 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5359 auto *context = cb_access_context->GetCurrentAccessContext();
5360 assert(context);
5361
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005362 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005363
5364 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005365 // VK_WHOLE_SIZE not allowed
5366 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005367 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005368 }
5369}
locke-lunargff255f92020-05-13 18:53:52 -06005370
5371bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5372 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5373 bool skip = false;
5374 const auto *cb_access_context = GetAccessContext(commandBuffer);
5375 assert(cb_access_context);
5376 if (!cb_access_context) return skip;
5377
5378 const auto *context = cb_access_context->GetCurrentAccessContext();
5379 assert(context);
5380 if (!context) return skip;
5381
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005382 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005383
5384 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005385 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005386 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005387 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005388 skip |=
5389 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5390 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf397e68b2022-04-19 11:44:07 -06005391 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatHazard(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005392 }
5393 }
5394 return skip;
5395}
5396
5397void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5398 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005399 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005400 auto *cb_access_context = GetAccessContext(commandBuffer);
5401 assert(cb_access_context);
5402 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5403 auto *context = cb_access_context->GetCurrentAccessContext();
5404 assert(context);
5405
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005406 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005407
5408 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005409 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005410 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005411 }
5412}
John Zulauf49beb112020-11-04 16:06:31 -07005413
5414bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5415 bool skip = false;
5416 const auto *cb_context = GetAccessContext(commandBuffer);
5417 assert(cb_context);
5418 if (!cb_context) return skip;
5419
John Zulauf36ef9282021-02-02 11:47:24 -07005420 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005421 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005422}
5423
5424void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5425 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5426 auto *cb_context = GetAccessContext(commandBuffer);
5427 assert(cb_context);
5428 if (!cb_context) return;
John Zulauf1bf30522021-09-03 15:39:06 -06005429 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005430}
5431
John Zulauf4edde622021-02-15 08:54:50 -07005432bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5433 const VkDependencyInfoKHR *pDependencyInfo) const {
5434 bool skip = false;
5435 const auto *cb_context = GetAccessContext(commandBuffer);
5436 assert(cb_context);
5437 if (!cb_context || !pDependencyInfo) return skip;
5438
5439 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5440 return set_event_op.Validate(*cb_context);
5441}
5442
Tony-LunarGc43525f2021-11-15 16:12:38 -07005443bool SyncValidator::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5444 const VkDependencyInfo *pDependencyInfo) const {
5445 bool skip = false;
5446 const auto *cb_context = GetAccessContext(commandBuffer);
5447 assert(cb_context);
5448 if (!cb_context || !pDependencyInfo) return skip;
5449
5450 SyncOpSetEvent set_event_op(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5451 return set_event_op.Validate(*cb_context);
5452}
5453
John Zulauf4edde622021-02-15 08:54:50 -07005454void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5455 const VkDependencyInfoKHR *pDependencyInfo) {
5456 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5457 auto *cb_context = GetAccessContext(commandBuffer);
5458 assert(cb_context);
5459 if (!cb_context || !pDependencyInfo) return;
5460
John Zulauf1bf30522021-09-03 15:39:06 -06005461 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
John Zulauf4edde622021-02-15 08:54:50 -07005462}
5463
Tony-LunarGc43525f2021-11-15 16:12:38 -07005464void SyncValidator::PostCallRecordCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5465 const VkDependencyInfo *pDependencyInfo) {
5466 StateTracker::PostCallRecordCmdSetEvent2(commandBuffer, event, pDependencyInfo);
5467 auto *cb_context = GetAccessContext(commandBuffer);
5468 assert(cb_context);
5469 if (!cb_context || !pDependencyInfo) return;
5470
5471 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5472}
5473
John Zulauf49beb112020-11-04 16:06:31 -07005474bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5475 VkPipelineStageFlags stageMask) const {
5476 bool skip = false;
5477 const auto *cb_context = GetAccessContext(commandBuffer);
5478 assert(cb_context);
5479 if (!cb_context) return skip;
5480
John Zulauf36ef9282021-02-02 11:47:24 -07005481 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005482 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005483}
5484
5485void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5486 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5487 auto *cb_context = GetAccessContext(commandBuffer);
5488 assert(cb_context);
5489 if (!cb_context) return;
5490
John Zulauf1bf30522021-09-03 15:39:06 -06005491 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005492}
5493
John Zulauf4edde622021-02-15 08:54:50 -07005494bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5495 VkPipelineStageFlags2KHR stageMask) const {
5496 bool skip = false;
5497 const auto *cb_context = GetAccessContext(commandBuffer);
5498 assert(cb_context);
5499 if (!cb_context) return skip;
5500
5501 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5502 return reset_event_op.Validate(*cb_context);
5503}
5504
Tony-LunarGa2662db2021-11-16 07:26:24 -07005505bool SyncValidator::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5506 VkPipelineStageFlags2 stageMask) const {
5507 bool skip = false;
5508 const auto *cb_context = GetAccessContext(commandBuffer);
5509 assert(cb_context);
5510 if (!cb_context) return skip;
5511
5512 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5513 return reset_event_op.Validate(*cb_context);
5514}
5515
John Zulauf4edde622021-02-15 08:54:50 -07005516void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5517 VkPipelineStageFlags2KHR stageMask) {
5518 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5519 auto *cb_context = GetAccessContext(commandBuffer);
5520 assert(cb_context);
5521 if (!cb_context) return;
5522
John Zulauf1bf30522021-09-03 15:39:06 -06005523 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4edde622021-02-15 08:54:50 -07005524}
5525
Tony-LunarGa2662db2021-11-16 07:26:24 -07005526void SyncValidator::PostCallRecordCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags2 stageMask) {
5527 StateTracker::PostCallRecordCmdResetEvent2(commandBuffer, event, stageMask);
5528 auto *cb_context = GetAccessContext(commandBuffer);
5529 assert(cb_context);
5530 if (!cb_context) return;
5531
5532 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5533}
5534
John Zulauf49beb112020-11-04 16:06:31 -07005535bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5536 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5537 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5538 uint32_t bufferMemoryBarrierCount,
5539 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5540 uint32_t imageMemoryBarrierCount,
5541 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5542 bool skip = false;
5543 const auto *cb_context = GetAccessContext(commandBuffer);
5544 assert(cb_context);
5545 if (!cb_context) return skip;
5546
John Zulauf36ef9282021-02-02 11:47:24 -07005547 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5548 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5549 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005550 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005551}
5552
5553void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5554 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5555 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5556 uint32_t bufferMemoryBarrierCount,
5557 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5558 uint32_t imageMemoryBarrierCount,
5559 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5560 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5561 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5562 imageMemoryBarrierCount, pImageMemoryBarriers);
5563
5564 auto *cb_context = GetAccessContext(commandBuffer);
5565 assert(cb_context);
5566 if (!cb_context) return;
5567
John Zulauf1bf30522021-09-03 15:39:06 -06005568 cb_context->RecordSyncOp<SyncOpWaitEvents>(
John Zulauf610e28c2021-08-03 17:46:23 -06005569 CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
John Zulauf1bf30522021-09-03 15:39:06 -06005570 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf4a6105a2020-11-17 15:11:05 -07005571}
5572
John Zulauf4edde622021-02-15 08:54:50 -07005573bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5574 const VkDependencyInfoKHR *pDependencyInfos) const {
5575 bool skip = false;
5576 const auto *cb_context = GetAccessContext(commandBuffer);
5577 assert(cb_context);
5578 if (!cb_context) return skip;
5579
5580 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5581 skip |= wait_events_op.Validate(*cb_context);
5582 return skip;
5583}
5584
5585void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5586 const VkDependencyInfoKHR *pDependencyInfos) {
5587 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5588
5589 auto *cb_context = GetAccessContext(commandBuffer);
5590 assert(cb_context);
5591 if (!cb_context) return;
5592
John Zulauf1bf30522021-09-03 15:39:06 -06005593 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5594 pDependencyInfos);
John Zulauf4edde622021-02-15 08:54:50 -07005595}
5596
Tony-LunarG1364cf52021-11-17 16:10:11 -07005597bool SyncValidator::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5598 const VkDependencyInfo *pDependencyInfos) const {
5599 bool skip = false;
5600 const auto *cb_context = GetAccessContext(commandBuffer);
5601 assert(cb_context);
5602 if (!cb_context) return skip;
5603
5604 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5605 skip |= wait_events_op.Validate(*cb_context);
5606 return skip;
5607}
5608
5609void SyncValidator::PostCallRecordCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5610 const VkDependencyInfo *pDependencyInfos) {
5611 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5612
5613 auto *cb_context = GetAccessContext(commandBuffer);
5614 assert(cb_context);
5615 if (!cb_context) return;
5616
5617 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5618 pDependencyInfos);
5619}
5620
John Zulauf4a6105a2020-11-17 15:11:05 -07005621void SyncEventState::ResetFirstScope() {
5622 for (const auto address_type : kAddressTypes) {
5623 first_scope[static_cast<size_t>(address_type)].clear();
5624 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005625 scope = SyncExecScope();
John Zulauf78b1f892021-09-20 15:02:09 -06005626 first_scope_set = false;
5627 first_scope_tag = 0;
John Zulauf4a6105a2020-11-17 15:11:05 -07005628}
5629
5630// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005631SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005632 IgnoreReason reason = NotIgnored;
5633
Tony-LunarG1364cf52021-11-17 16:10:11 -07005634 if ((CMD_WAITEVENTS2KHR == cmd || CMD_WAITEVENTS2 == cmd) && (CMD_SETEVENT == last_command)) {
John Zulauf4edde622021-02-15 08:54:50 -07005635 reason = SetVsWait2;
5636 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5637 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005638 } else if (unsynchronized_set) {
5639 reason = SetRace;
John Zulauf78b1f892021-09-20 15:02:09 -06005640 } else if (first_scope_set) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005641 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005642 if (missing_bits) reason = MissingStageBits;
5643 }
5644
5645 return reason;
5646}
5647
Jeremy Gebben40a22942020-12-22 14:22:06 -07005648bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005649 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5650 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5651 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005652}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005653
John Zulaufbb890452021-12-14 11:30:18 -07005654void SyncOpBase::SetReplayContext(uint32_t subpass, ReplayContextPtr &&replay) {
5655 subpass_ = subpass;
5656 replay_context_ = std::move(replay);
5657}
5658
5659const ReplayTrackbackBarriersAction *SyncOpBase::GetReplayTrackback() const {
5660 if (replay_context_) {
5661 assert(subpass_ < replay_context_->subpass_contexts.size());
5662 return &replay_context_->subpass_contexts[subpass_];
5663 }
5664 return nullptr;
5665}
5666
John Zulauf36ef9282021-02-02 11:47:24 -07005667SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5668 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5669 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005670 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5671 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5672 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005673 : SyncOpBase(cmd), barriers_(1) {
5674 auto &barrier_set = barriers_[0];
5675 barrier_set.dependency_flags = dependencyFlags;
5676 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5677 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005678 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005679 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5680 pMemoryBarriers);
5681 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5682 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5683 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5684 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005685}
5686
John Zulauf4edde622021-02-15 08:54:50 -07005687SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5688 const VkDependencyInfoKHR *dep_infos)
5689 : SyncOpBase(cmd), barriers_(event_count) {
5690 for (uint32_t i = 0; i < event_count; i++) {
5691 const auto &dep_info = dep_infos[i];
5692 auto &barrier_set = barriers_[i];
5693 barrier_set.dependency_flags = dep_info.dependencyFlags;
5694 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5695 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5696 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5697 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5698 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5699 dep_info.pMemoryBarriers);
5700 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5701 dep_info.pBufferMemoryBarriers);
5702 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5703 dep_info.pImageMemoryBarriers);
5704 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005705}
5706
John Zulauf36ef9282021-02-02 11:47:24 -07005707SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005708 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5709 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5710 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5711 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5712 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005713 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005714 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5715
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005716SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5717 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005718 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005719
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005720bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5721 bool skip = false;
5722 const auto *context = cb_context.GetCurrentAccessContext();
5723 assert(context);
5724 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005725 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5726
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005727 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005728 const auto &barrier_set = barriers_[0];
5729 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5730 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5731 const auto *image_state = image_barrier.image.get();
5732 if (!image_state) continue;
5733 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5734 if (hazard.hazard) {
5735 // PHASE1 TODO -- add tag information to log msg when useful.
5736 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005737 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005738 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5739 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5740 string_SyncHazard(hazard.hazard), image_barrier.index,
5741 sync_state.report_data->FormatHandle(image_handle).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06005742 cb_context.FormatHazard(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005743 }
5744 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005745 return skip;
5746}
5747
John Zulaufd5115702021-01-18 12:34:33 -07005748struct SyncOpPipelineBarrierFunctorFactory {
5749 using BarrierOpFunctor = PipelineBarrierOp;
5750 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5751 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5752 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5753 using BufferRange = ResourceAccessRange;
5754 using ImageRange = subresource_adapter::ImageRangeGenerator;
5755 using GlobalRange = ResourceAccessRange;
5756
5757 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5758 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5759 }
John Zulauf14940722021-04-12 15:19:02 -06005760 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005761 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5762 }
5763 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5764 return GlobalBarrierOpFunctor(barrier, false);
5765 }
5766
5767 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5768 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5769 const auto base_address = ResourceBaseAddress(buffer);
5770 return (range + base_address);
5771 }
John Zulauf110413c2021-03-20 05:38:38 -06005772 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005773 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005774
5775 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005776 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005777 return range_gen;
5778 }
5779 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5780};
5781
5782template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005783void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005784 AccessContext *context) {
5785 for (const auto &barrier : barriers) {
5786 const auto *state = barrier.GetState();
5787 if (state) {
5788 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5789 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5790 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5791 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5792 }
5793 }
5794}
5795
5796template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005797void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005798 AccessContext *access_context) {
5799 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5800 for (const auto &barrier : barriers) {
5801 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5802 }
5803 for (const auto address_type : kAddressTypes) {
5804 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5805 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5806 }
5807}
5808
John Zulauf8eda1562021-04-13 17:06:41 -06005809ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005810 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005811 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005812 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufbb890452021-12-14 11:30:18 -07005813 ReplayRecord(tag, access_context, events_context);
John Zulauf4fa68462021-04-26 21:04:22 -06005814 return tag;
5815}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005816
John Zulaufbb890452021-12-14 11:30:18 -07005817void SyncOpPipelineBarrier::ReplayRecord(const ResourceUsageTag tag, AccessContext *access_context,
5818 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005819 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005820 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5821 assert(barriers_.size() == 1);
5822 const auto &barrier_set = barriers_[0];
5823 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5824 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5825 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005826 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005827 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005828 } else {
5829 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005830 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005831 }
5832 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005833}
5834
John Zulauf8eda1562021-04-13 17:06:41 -06005835bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07005836 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005837 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5838 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005839 return false;
5840}
5841
John Zulauf4edde622021-02-15 08:54:50 -07005842void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5843 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5844 const VkMemoryBarrier *barriers) {
5845 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005846 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005847 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005848 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005849 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005850 }
5851 if (0 == memory_barrier_count) {
5852 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005853 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005854 }
John Zulauf4edde622021-02-15 08:54:50 -07005855 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005856}
5857
John Zulauf4edde622021-02-15 08:54:50 -07005858void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5859 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5860 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5861 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005862 for (uint32_t index = 0; index < barrier_count; index++) {
5863 const auto &barrier = barriers[index];
Jeremy Gebben9f537102021-10-05 16:37:12 -06005864 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005865 if (buffer) {
5866 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5867 const auto range = MakeRange(barrier.offset, barrier_size);
5868 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005869 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005870 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005871 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005872 }
5873 }
5874}
5875
John Zulauf4edde622021-02-15 08:54:50 -07005876void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005877 uint32_t memory_barrier_count, const VkMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005878 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005879 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005880 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005881 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5882 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5883 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005884 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005885 }
John Zulauf4edde622021-02-15 08:54:50 -07005886 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005887}
5888
John Zulauf4edde622021-02-15 08:54:50 -07005889void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5890 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005891 const VkBufferMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005892 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005893 for (uint32_t index = 0; index < barrier_count; index++) {
5894 const auto &barrier = barriers[index];
5895 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5896 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9f537102021-10-05 16:37:12 -06005897 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005898 if (buffer) {
5899 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5900 const auto range = MakeRange(barrier.offset, barrier_size);
5901 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005902 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005903 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005904 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005905 }
5906 }
5907}
5908
John Zulauf4edde622021-02-15 08:54:50 -07005909void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5910 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5911 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5912 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005913 for (uint32_t index = 0; index < barrier_count; index++) {
5914 const auto &barrier = barriers[index];
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005915 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005916 if (image) {
5917 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5918 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005919 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005920 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005921 image_memory_barriers.emplace_back();
5922 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005923 }
5924 }
5925}
John Zulaufd5115702021-01-18 12:34:33 -07005926
John Zulauf4edde622021-02-15 08:54:50 -07005927void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5928 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005929 const VkImageMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005930 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005931 for (uint32_t index = 0; index < barrier_count; index++) {
5932 const auto &barrier = barriers[index];
5933 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5934 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005935 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005936 if (image) {
5937 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5938 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005939 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005940 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005941 image_memory_barriers.emplace_back();
5942 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005943 }
5944 }
5945}
5946
John Zulauf36ef9282021-02-02 11:47:24 -07005947SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005948 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5949 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5950 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5951 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005952 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005953 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5954 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005955 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005956}
5957
John Zulauf4edde622021-02-15 08:54:50 -07005958SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5959 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5960 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5961 MakeEventsList(sync_state, eventCount, pEvents);
5962 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5963}
5964
John Zulauf610e28c2021-08-03 17:46:23 -06005965const char *const SyncOpWaitEvents::kIgnored = "Wait operation is ignored for this event.";
5966
John Zulaufd5115702021-01-18 12:34:33 -07005967bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005968 bool skip = false;
5969 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005970 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005971
John Zulauf610e28c2021-08-03 17:46:23 -06005972 // This is only interesting at record and not replay (Execute/Submit) time.
John Zulauf4edde622021-02-15 08:54:50 -07005973 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5974 const auto &barrier_set = barriers_[barrier_set_index];
5975 if (barrier_set.single_exec_scope) {
5976 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5977 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5978 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5979 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5980 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5981 } else {
5982 const auto &barriers = barrier_set.memory_barriers;
5983 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5984 const auto &barrier = barriers[barrier_index];
5985 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5986 const std::string vuid =
5987 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5988 skip =
5989 sync_state.LogInfo(command_buffer_handle, vuid,
5990 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5991 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5992 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5993 }
5994 }
5995 }
5996 }
John Zulaufd5115702021-01-18 12:34:33 -07005997 }
5998
John Zulauf610e28c2021-08-03 17:46:23 -06005999 // The rest is common to record time and replay time.
6000 skip |= DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6001 return skip;
6002}
6003
John Zulaufbb890452021-12-14 11:30:18 -07006004bool SyncOpWaitEvents::DoValidate(const CommandExecutionContext &exec_context, const ResourceUsageTag base_tag) const {
John Zulauf610e28c2021-08-03 17:46:23 -06006005 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07006006 const auto &sync_state = exec_context.GetSyncState();
John Zulauf610e28c2021-08-03 17:46:23 -06006007
Jeremy Gebben40a22942020-12-22 14:22:06 -07006008 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07006009 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07006010 bool events_not_found = false;
John Zulaufbb890452021-12-14 11:30:18 -07006011 const auto *events_context = exec_context.GetCurrentEventsContext();
John Zulauf669dfd52021-01-27 17:15:28 -07006012 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07006013 size_t barrier_set_index = 0;
6014 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06006015 for (const auto &event : events_) {
6016 const auto *sync_event = events_context->Get(event.get());
6017 const auto &barrier_set = barriers_[barrier_set_index];
6018 if (!sync_event) {
6019 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
6020 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
6021 // new validation error... wait without previously submitted set event...
6022 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
John Zulauf4edde622021-02-15 08:54:50 -07006023 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06006024 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07006025 }
John Zulauf610e28c2021-08-03 17:46:23 -06006026
6027 // For replay calls, don't revalidate "same command buffer" events
6028 if (sync_event->last_command_tag > base_tag) continue;
6029
John Zulauf78394fc2021-07-12 15:41:40 -06006030 const auto event_handle = sync_event->event->event();
6031 // TODO add "destroyed" checks
6032
John Zulauf78b1f892021-09-20 15:02:09 -06006033 if (sync_event->first_scope_set) {
6034 // Only accumulate barrier and event stages if there is a pending set in the current context
6035 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
6036 event_stage_masks |= sync_event->scope.mask_param;
6037 }
6038
John Zulauf78394fc2021-07-12 15:41:40 -06006039 const auto &src_exec_scope = barrier_set.src_exec_scope;
John Zulauf78b1f892021-09-20 15:02:09 -06006040
John Zulauf78394fc2021-07-12 15:41:40 -06006041 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
6042 if (ignore_reason) {
6043 switch (ignore_reason) {
6044 case SyncEventState::ResetWaitRace:
6045 case SyncEventState::Reset2WaitRace: {
6046 // Four permuations of Reset and Wait calls...
6047 const char *vuid =
6048 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
6049 if (ignore_reason == SyncEventState::Reset2WaitRace) {
Tony-LunarG279601c2021-11-16 10:50:51 -07006050 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2-event-03831"
6051 : "VUID-vkCmdResetEvent2-event-03832";
John Zulauf78394fc2021-07-12 15:41:40 -06006052 }
6053 const char *const message =
6054 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
6055 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
6056 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
John Zulauf610e28c2021-08-03 17:46:23 -06006057 CommandTypeString(sync_event->last_command), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06006058 break;
6059 }
6060 case SyncEventState::SetRace: {
6061 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
6062 // this event
6063 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
6064 const char *const message =
6065 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
6066 const char *const reason = "First synchronization scope is undefined.";
6067 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
6068 sync_state.report_data->FormatHandle(event_handle).c_str(),
John Zulauf610e28c2021-08-03 17:46:23 -06006069 CommandTypeString(sync_event->last_command), reason, kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06006070 break;
6071 }
6072 case SyncEventState::MissingStageBits: {
6073 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
6074 // Issue error message that event waited for is not in wait events scope
6075 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
6076 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
6077 ". Bits missing from srcStageMask %s. %s";
6078 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
6079 sync_state.report_data->FormatHandle(event_handle).c_str(),
6080 sync_event->scope.mask_param, src_exec_scope.mask_param,
John Zulauf610e28c2021-08-03 17:46:23 -06006081 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06006082 break;
6083 }
6084 case SyncEventState::SetVsWait2: {
Tony-LunarG279601c2021-11-16 10:50:51 -07006085 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2-pEvents-03837",
John Zulauf78394fc2021-07-12 15:41:40 -06006086 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
6087 sync_state.report_data->FormatHandle(event_handle).c_str(),
6088 CommandTypeString(sync_event->last_command));
6089 break;
6090 }
6091 default:
6092 assert(ignore_reason == SyncEventState::NotIgnored);
6093 }
6094 } else if (barrier_set.image_memory_barriers.size()) {
6095 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
John Zulaufbb890452021-12-14 11:30:18 -07006096 const auto *context = exec_context.GetCurrentAccessContext();
John Zulauf78394fc2021-07-12 15:41:40 -06006097 assert(context);
6098 for (const auto &image_memory_barrier : image_memory_barriers) {
6099 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
6100 const auto *image_state = image_memory_barrier.image.get();
6101 if (!image_state) continue;
6102 const auto &subresource_range = image_memory_barrier.range;
6103 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
6104 const auto hazard =
6105 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
6106 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
6107 if (hazard.hazard) {
6108 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
6109 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
6110 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
6111 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06006112 exec_context.FormatHazard(hazard).c_str());
John Zulauf78394fc2021-07-12 15:41:40 -06006113 break;
6114 }
6115 }
6116 }
6117 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
6118 // 03839
6119 barrier_set_index += barrier_set_incr;
6120 }
John Zulaufd5115702021-01-18 12:34:33 -07006121
6122 // Note that we can't check for HOST in pEvents as we don't track that set event type
John Zulauf4edde622021-02-15 08:54:50 -07006123 const auto extra_stage_bits = (barrier_mask_params & ~VK_PIPELINE_STAGE_2_HOST_BIT_KHR) & ~event_stage_masks;
John Zulaufd5115702021-01-18 12:34:33 -07006124 if (extra_stage_bits) {
6125 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07006126 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
6127 const char *const vuid =
Tony-LunarG279601c2021-11-16 10:50:51 -07006128 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07006129 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07006130 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufbb890452021-12-14 11:30:18 -07006131 const auto handle = exec_context.Handle();
John Zulaufd5115702021-01-18 12:34:33 -07006132 if (events_not_found) {
John Zulaufbb890452021-12-14 11:30:18 -07006133 skip |= sync_state.LogInfo(handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07006134 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07006135 " vkCmdSetEvent may be in previously submitted command buffer.");
6136 } else {
John Zulaufbb890452021-12-14 11:30:18 -07006137 skip |= sync_state.LogError(handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07006138 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07006139 }
6140 }
6141 return skip;
6142}
6143
6144struct SyncOpWaitEventsFunctorFactory {
6145 using BarrierOpFunctor = WaitEventBarrierOp;
6146 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
6147 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
6148 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
6149 using BufferRange = EventSimpleRangeGenerator;
6150 using ImageRange = EventImageRangeGenerator;
6151 using GlobalRange = EventSimpleRangeGenerator;
6152
6153 // Need to restrict to only valid exec and access scope for this event
6154 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
6155 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07006156 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07006157 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
6158 return barrier;
6159 }
6160 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
6161 auto barrier = RestrictToEvent(barrier_arg);
6162 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
6163 }
John Zulauf14940722021-04-12 15:19:02 -06006164 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07006165 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
6166 }
6167 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
6168 auto barrier = RestrictToEvent(barrier_arg);
6169 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
6170 }
6171
6172 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
6173 const AccessAddressType address_type = GetAccessAddressType(buffer);
6174 const auto base_address = ResourceBaseAddress(buffer);
6175 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
6176 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
6177 return filtered_range_gen;
6178 }
John Zulauf110413c2021-03-20 05:38:38 -06006179 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07006180 if (!SimpleBinding(image)) return ImageRange();
6181 const auto address_type = GetAccessAddressType(image);
6182 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06006183 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07006184 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
6185
6186 return filtered_range_gen;
6187 }
6188 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
6189 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
6190 }
6191 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
6192 SyncEventState *sync_event;
6193};
6194
John Zulauf8eda1562021-04-13 17:06:41 -06006195ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006196 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07006197 auto *access_context = cb_context->GetCurrentAccessContext();
6198 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006199 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07006200 auto *events_context = cb_context->GetCurrentEventsContext();
6201 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006202 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07006203
John Zulaufbb890452021-12-14 11:30:18 -07006204 ReplayRecord(tag, access_context, events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006205 return tag;
6206}
6207
John Zulaufbb890452021-12-14 11:30:18 -07006208void SyncOpWaitEvents::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07006209 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
6210 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
6211 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
6212 access_context->ResolvePreviousAccesses();
6213
John Zulauf4edde622021-02-15 08:54:50 -07006214 size_t barrier_set_index = 0;
6215 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
6216 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07006217 for (auto &event_shared : events_) {
6218 if (!event_shared.get()) continue;
6219 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07006220
John Zulauf4edde622021-02-15 08:54:50 -07006221 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006222 sync_event->last_command_tag = tag;
John Zulaufd5115702021-01-18 12:34:33 -07006223
John Zulauf4edde622021-02-15 08:54:50 -07006224 const auto &barrier_set = barriers_[barrier_set_index];
6225 const auto &dst = barrier_set.dst_exec_scope;
6226 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07006227 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
6228 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
6229 // of the barriers is maintained.
6230 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07006231 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
6232 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
6233 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07006234
6235 // Apply the global barrier to the event itself (for race condition tracking)
6236 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
6237 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6238 sync_event->barriers |= dst.exec_scope;
6239 } else {
6240 // We ignored this wait, so we don't have any effective synchronization barriers for it.
6241 sync_event->barriers = 0U;
6242 }
John Zulauf4edde622021-02-15 08:54:50 -07006243 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07006244 }
6245
6246 // Apply the pending barriers
6247 ResolvePendingBarrierFunctor apply_pending_action(tag);
6248 access_context->ApplyToContext(apply_pending_action);
6249}
6250
John Zulauf8eda1562021-04-13 17:06:41 -06006251bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006252 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
6253 return DoValidate(*exec_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006254}
6255
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006256bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6257 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
6258 bool skip = false;
6259 const auto *cb_access_context = GetAccessContext(commandBuffer);
6260 assert(cb_access_context);
6261 if (!cb_access_context) return skip;
6262
6263 const auto *context = cb_access_context->GetCurrentAccessContext();
6264 assert(context);
6265 if (!context) return skip;
6266
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006267 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006268
6269 if (dst_buffer) {
6270 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6271 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
6272 if (hazard.hazard) {
6273 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
6274 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
6275 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06006276 cb_access_context->FormatHazard(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006277 }
6278 }
6279 return skip;
6280}
6281
John Zulauf669dfd52021-01-27 17:15:28 -07006282void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07006283 events_.reserve(event_count);
6284 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006285 events_.emplace_back(sync_state.Get<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07006286 }
6287}
John Zulauf6ce24372021-01-30 05:56:25 -07006288
John Zulauf36ef9282021-02-02 11:47:24 -07006289SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006290 VkPipelineStageFlags2KHR stageMask)
Jeremy Gebben9f537102021-10-05 16:37:12 -06006291 : SyncOpBase(cmd), event_(sync_state.Get<EVENT_STATE>(event)), exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006292
John Zulauf1bf30522021-09-03 15:39:06 -06006293bool SyncOpResetEvent::Validate(const CommandBufferAccessContext& cb_context) const {
6294 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6295}
6296
John Zulaufbb890452021-12-14 11:30:18 -07006297bool SyncOpResetEvent::DoValidate(const CommandExecutionContext &exec_context, const ResourceUsageTag base_tag) const {
6298 auto *events_context = exec_context.GetCurrentEventsContext();
John Zulauf6ce24372021-01-30 05:56:25 -07006299 assert(events_context);
6300 bool skip = false;
6301 if (!events_context) return skip;
6302
John Zulaufbb890452021-12-14 11:30:18 -07006303 const auto &sync_state = exec_context.GetSyncState();
John Zulauf6ce24372021-01-30 05:56:25 -07006304 const auto *sync_event = events_context->Get(event_);
6305 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6306
John Zulauf1bf30522021-09-03 15:39:06 -06006307 if (sync_event->last_command_tag > base_tag) return skip; // if we validated this in recording of the secondary, don't repeat
6308
John Zulauf6ce24372021-01-30 05:56:25 -07006309 const char *const set_wait =
6310 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6311 "hazards.";
6312 const char *message = set_wait; // Only one message this call.
6313 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
6314 const char *vuid = nullptr;
6315 switch (sync_event->last_command) {
6316 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006317 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006318 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006319 // Needs a barrier between set and reset
6320 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
6321 break;
John Zulauf4edde622021-02-15 08:54:50 -07006322 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006323 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006324 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07006325 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
6326 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
6327 break;
6328 }
6329 default:
6330 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07006331 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
6332 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07006333 break;
6334 }
6335 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006336 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
6337 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006338 CommandTypeString(sync_event->last_command));
6339 }
6340 }
6341 return skip;
6342}
6343
John Zulauf8eda1562021-04-13 17:06:41 -06006344ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
6345 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006346 auto *events_context = cb_context->GetCurrentEventsContext();
6347 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006348 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006349
6350 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06006351 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006352
6353 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07006354 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006355 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006356 sync_event->unsynchronized_set = CMD_NONE;
6357 sync_event->ResetFirstScope();
6358 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06006359
6360 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006361}
6362
John Zulauf8eda1562021-04-13 17:06:41 -06006363bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006364 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
6365 return DoValidate(*exec_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006366}
6367
John Zulaufbb890452021-12-14 11:30:18 -07006368void SyncOpResetEvent::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006369
John Zulauf36ef9282021-02-02 11:47:24 -07006370SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006371 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07006372 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006373 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006374 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
6375 dep_info_() {}
6376
6377SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
6378 const VkDependencyInfoKHR &dep_info)
6379 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006380 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006381 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
Tony-LunarG273f32f2021-09-28 08:56:30 -06006382 dep_info_(new safe_VkDependencyInfo(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006383
6384bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf610e28c2021-08-03 17:46:23 -06006385 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6386}
6387bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006388 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
6389 assert(exec_context);
6390 return DoValidate(*exec_context, base_tag);
John Zulauf610e28c2021-08-03 17:46:23 -06006391}
6392
John Zulaufbb890452021-12-14 11:30:18 -07006393bool SyncOpSetEvent::DoValidate(const CommandExecutionContext &exec_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006394 bool skip = false;
6395
John Zulaufbb890452021-12-14 11:30:18 -07006396 const auto &sync_state = exec_context.GetSyncState();
6397 auto *events_context = exec_context.GetCurrentEventsContext();
John Zulauf6ce24372021-01-30 05:56:25 -07006398 assert(events_context);
6399 if (!events_context) return skip;
6400
6401 const auto *sync_event = events_context->Get(event_);
6402 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6403
John Zulauf610e28c2021-08-03 17:46:23 -06006404 if (sync_event->last_command_tag >= base_tag) return skip; // for replay we don't want to revalidate internal "last commmand"
6405
John Zulauf6ce24372021-01-30 05:56:25 -07006406 const char *const reset_set =
6407 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6408 "hazards.";
6409 const char *const wait =
6410 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6411
6412 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006413 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006414 const char *message = nullptr;
6415 switch (sync_event->last_command) {
6416 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006417 case CMD_RESETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006418 case CMD_RESETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006419 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006420 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006421 message = reset_set;
6422 break;
6423 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006424 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006425 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006426 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006427 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006428 message = reset_set;
6429 break;
6430 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006431 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006432 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006433 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006434 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006435 message = wait;
6436 break;
6437 default:
6438 // The only other valid last command that wasn't one.
6439 assert(sync_event->last_command == CMD_NONE);
6440 break;
6441 }
John Zulauf4edde622021-02-15 08:54:50 -07006442 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006443 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006444 std::string vuid("SYNC-");
6445 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006446 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6447 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006448 CommandTypeString(sync_event->last_command));
6449 }
6450 }
6451
6452 return skip;
6453}
6454
John Zulauf8eda1562021-04-13 17:06:41 -06006455ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006456 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006457 auto *events_context = cb_context->GetCurrentEventsContext();
6458 auto *access_context = cb_context->GetCurrentAccessContext();
6459 assert(events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006460 if (access_context && events_context) {
John Zulaufbb890452021-12-14 11:30:18 -07006461 ReplayRecord(tag, access_context, events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006462 }
6463 return tag;
6464}
John Zulauf6ce24372021-01-30 05:56:25 -07006465
John Zulaufbb890452021-12-14 11:30:18 -07006466void SyncOpSetEvent::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006467 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf610e28c2021-08-03 17:46:23 -06006468 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006469
6470 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6471 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6472 // any issues caused by naive scope setting here.
6473
6474 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6475 // Given:
6476 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6477 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6478
6479 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6480 sync_event->unsynchronized_set = sync_event->last_command;
6481 sync_event->ResetFirstScope();
John Zulauf78b1f892021-09-20 15:02:09 -06006482 } else if (!sync_event->first_scope_set) {
John Zulauf6ce24372021-01-30 05:56:25 -07006483 // We only set the scope if there isn't one
6484 sync_event->scope = src_exec_scope_;
6485
6486 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6487 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6488 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6489 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6490 }
6491 };
6492 access_context->ForAll(set_scope);
6493 sync_event->unsynchronized_set = CMD_NONE;
John Zulauf78b1f892021-09-20 15:02:09 -06006494 sync_event->first_scope_set = true;
John Zulauf6ce24372021-01-30 05:56:25 -07006495 sync_event->first_scope_tag = tag;
6496 }
John Zulauf4edde622021-02-15 08:54:50 -07006497 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6498 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006499 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006500 sync_event->barriers = 0U;
6501}
John Zulauf64ffe552021-02-06 10:25:07 -07006502
6503SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6504 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006505 const VkSubpassBeginInfo *pSubpassBeginInfo)
6506 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006507 if (pRenderPassBegin) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006508 rp_state_ = sync_state.Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
John Zulauf64ffe552021-02-06 10:25:07 -07006509 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006510 auto fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07006511 if (fb_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006512 shared_attachments_ = sync_state.GetAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
John Zulauf64ffe552021-02-06 10:25:07 -07006513 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6514 // Note that this a safe to presist as long as shared_attachments is not cleared
6515 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006516 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006517 attachments_.emplace_back(attachment.get());
6518 }
6519 }
6520 if (pSubpassBeginInfo) {
6521 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6522 }
6523 }
6524}
6525
6526bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6527 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6528 bool skip = false;
6529
6530 assert(rp_state_.get());
6531 if (nullptr == rp_state_.get()) return skip;
6532 auto &rp_state = *rp_state_.get();
6533
6534 const uint32_t subpass = 0;
6535
6536 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6537 // hasn't happened yet)
6538 const std::vector<AccessContext> empty_context_vector;
6539 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6540 cb_context.GetCurrentAccessContext());
6541
6542 // Validate attachment operations
6543 if (attachments_.size() == 0) return skip;
6544 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006545
6546 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6547 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6548 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6549 // operations (though it's currently a messy approach)
6550 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6551 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006552
6553 // Validate load operations if there were no layout transition hazards
6554 if (!skip) {
John Zulaufee984022022-04-13 16:39:50 -06006555 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kInvalidTag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07006556 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006557 }
6558
6559 return skip;
6560}
6561
John Zulauf8eda1562021-04-13 17:06:41 -06006562ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006563 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6564 assert(rp_state_.get());
John Zulauf41a9c7c2021-12-07 15:59:53 -07006565 if (nullptr == rp_state_.get()) return cb_context->NextCommandTag(cmd_);
6566 return cb_context->RecordBeginRenderPass(cmd_, *rp_state_.get(), renderpass_begin_info_.renderArea, attachments_);
John Zulauf64ffe552021-02-06 10:25:07 -07006567}
6568
John Zulauf8eda1562021-04-13 17:06:41 -06006569bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006570 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006571 return false;
6572}
6573
John Zulaufbb890452021-12-14 11:30:18 -07006574void SyncOpBeginRenderPass::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context,
6575 SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006576
John Zulauf64ffe552021-02-06 10:25:07 -07006577SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006578 const VkSubpassEndInfo *pSubpassEndInfo)
6579 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006580 if (pSubpassBeginInfo) {
6581 subpass_begin_info_.initialize(pSubpassBeginInfo);
6582 }
6583 if (pSubpassEndInfo) {
6584 subpass_end_info_.initialize(pSubpassEndInfo);
6585 }
6586}
6587
6588bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6589 bool skip = false;
6590 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6591 if (!renderpass_context) return skip;
6592
6593 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6594 return skip;
6595}
6596
John Zulauf8eda1562021-04-13 17:06:41 -06006597ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006598 return cb_context->RecordNextSubpass(cmd_);
John Zulauf8eda1562021-04-13 17:06:41 -06006599}
6600
6601bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006602 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006603 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006604}
6605
sfricke-samsung85584a72021-09-30 21:43:38 -07006606SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6607 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006608 if (pSubpassEndInfo) {
6609 subpass_end_info_.initialize(pSubpassEndInfo);
6610 }
6611}
6612
John Zulaufbb890452021-12-14 11:30:18 -07006613void SyncOpNextSubpass::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6614}
John Zulauf8eda1562021-04-13 17:06:41 -06006615
John Zulauf64ffe552021-02-06 10:25:07 -07006616bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6617 bool skip = false;
6618 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6619
6620 if (!renderpass_context) return skip;
6621 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6622 return skip;
6623}
6624
John Zulauf8eda1562021-04-13 17:06:41 -06006625ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006626 return cb_context->RecordEndRenderPass(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006627}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006628
John Zulauf8eda1562021-04-13 17:06:41 -06006629bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006630 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006631 return false;
6632}
6633
John Zulaufbb890452021-12-14 11:30:18 -07006634void SyncOpEndRenderPass::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context,
6635 SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006636
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006637void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6638 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6639 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6640 auto *cb_access_context = GetAccessContext(commandBuffer);
6641 assert(cb_access_context);
6642 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6643 auto *context = cb_access_context->GetCurrentAccessContext();
6644 assert(context);
6645
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006646 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006647
6648 if (dst_buffer) {
6649 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6650 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6651 }
6652}
John Zulaufd05c5842021-03-26 11:32:16 -06006653
John Zulaufae842002021-04-15 18:20:55 -06006654bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6655 const VkCommandBuffer *pCommandBuffers) const {
6656 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6657 const char *func_name = "vkCmdExecuteCommands";
6658 const auto *cb_context = GetAccessContext(commandBuffer);
6659 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006660
6661 // Heavyweight, but we need a proxy copy of the active command buffer access context
6662 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006663
6664 // Make working copies of the access and events contexts
John Zulaufae842002021-04-15 18:20:55 -06006665 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006666 proxy_cb_context.NextIndexedCommandTag(CMD_EXECUTECOMMANDS, cb_index);
6667
John Zulaufae842002021-04-15 18:20:55 -06006668 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6669 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006670
6671 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6672 assert(recorded_context);
6673 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6674
6675 // The barriers have already been applied in ValidatFirstUse
6676 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6677 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006678 }
6679
John Zulaufae842002021-04-15 18:20:55 -06006680 return skip;
6681}
6682
6683void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6684 const VkCommandBuffer *pCommandBuffers) {
6685 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006686 auto *cb_context = GetAccessContext(commandBuffer);
6687 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006688 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006689 cb_context->NextIndexedCommandTag(CMD_EXECUTECOMMANDS, cb_index);
John Zulauf4fa68462021-04-26 21:04:22 -06006690 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6691 if (!recorded_cb_context) continue;
6692 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6693 }
John Zulaufae842002021-04-15 18:20:55 -06006694}
6695
John Zulaufd0ec59f2021-03-13 14:25:08 -07006696AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6697 : view_(view), view_mask_(), gen_store_() {
6698 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6699 const IMAGE_STATE &image_state = *view_->image_state.get();
6700 const auto base_address = ResourceBaseAddress(image_state);
6701 const auto *encoder = image_state.fragment_encoder.get();
6702 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006703 // Get offset and extent for the view, accounting for possible depth slicing
6704 const VkOffset3D zero_offset = view->GetOffset();
6705 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006706 // Intentional copy
6707 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6708 view_mask_ = subres_range.aspectMask;
6709 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6710 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6711
6712 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6713 if (depth && (depth != view_mask_)) {
6714 subres_range.aspectMask = depth;
6715 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6716 }
6717 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6718 if (stencil && (stencil != view_mask_)) {
6719 subres_range.aspectMask = stencil;
6720 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6721 }
6722}
6723
6724const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6725 const ImageRangeGen *got = nullptr;
6726 switch (gen_type) {
6727 case kViewSubresource:
6728 got = &gen_store_[kViewSubresource];
6729 break;
6730 case kRenderArea:
6731 got = &gen_store_[kRenderArea];
6732 break;
6733 case kDepthOnlyRenderArea:
6734 got =
6735 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6736 break;
6737 case kStencilOnlyRenderArea:
6738 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6739 : &gen_store_[Gen::kStencilOnlyRenderArea];
6740 break;
6741 default:
6742 assert(got);
6743 }
6744 return got;
6745}
6746
6747AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6748 assert(IsValid());
6749 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6750 if (depth_op) {
6751 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6752 if (stencil_op) {
6753 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6754 return kRenderArea;
6755 }
6756 return kDepthOnlyRenderArea;
6757 }
6758 if (stencil_op) {
6759 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6760 return kStencilOnlyRenderArea;
6761 }
6762
6763 assert(depth_op || stencil_op);
6764 return kRenderArea;
6765}
6766
6767AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006768
6769void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6770 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6771 for (auto &event_pair : map_) {
6772 assert(event_pair.second); // Shouldn't be storing empty
6773 auto &sync_event = *event_pair.second;
6774 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6775 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6776 sync_event.barriers |= dst.exec_scope;
6777 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6778 }
6779 }
6780}
John Zulaufbb890452021-12-14 11:30:18 -07006781
6782ReplayTrackbackBarriersAction::ReplayTrackbackBarriersAction(VkQueueFlags queue_flags,
6783 const SubpassDependencyGraphNode &subpass_dep,
6784 const std::vector<ReplayTrackbackBarriersAction> &replay_contexts) {
6785 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
6786 trackback_barriers.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
6787 for (const auto &prev_dep : subpass_dep.prev) {
6788 const auto prev_pass = prev_dep.first->pass;
6789 const auto &prev_barriers = prev_dep.second;
6790 trackback_barriers.emplace_back(&replay_contexts[prev_pass], queue_flags, prev_barriers);
6791 }
6792 if (has_barrier_from_external) {
6793 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
6794 trackback_barriers.emplace_back(nullptr, queue_flags, subpass_dep.barrier_from_external);
6795 }
6796}
6797
6798void ReplayTrackbackBarriersAction::operator()(ResourceAccessState *access) const {
6799 if (trackback_barriers.size() == 1) {
6800 trackback_barriers[0](access);
6801 } else {
6802 ResourceAccessState resolved;
6803 for (const auto &trackback : trackback_barriers) {
6804 ResourceAccessState access_copy = *access;
6805 trackback(&access_copy);
6806 resolved.Resolve(access_copy);
6807 }
6808 *access = resolved;
6809 }
6810}
6811
6812ReplayTrackbackBarriersAction::TrackbackBarriers::TrackbackBarriers(
6813 const ReplayTrackbackBarriersAction *source_subpass_, VkQueueFlags queue_flags_,
6814 const std::vector<const VkSubpassDependency2 *> &subpass_dependencies_)
6815 : Base(source_subpass_, queue_flags_, subpass_dependencies_) {}
6816
6817void ReplayTrackbackBarriersAction::TrackbackBarriers::operator()(ResourceAccessState *access) const {
6818 if (source_subpass) {
6819 (*source_subpass)(access);
6820 }
6821 access->ApplyBarriersImmediate(barriers);
6822}