blob: ac57b935ce4c9fc397d8a7c76140502a02f537d9 [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 Zulauf3d84f1b2020-03-09 13:33:25 -06001315class HazardDetector {
1316 SyncStageAccessIndex usage_index_;
1317
1318 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001319 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf14940722021-04-12 15:19:02 -06001320 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001321 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001322 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001323 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001324};
1325
John Zulauf69133422020-05-20 14:55:53 -06001326class HazardDetectorWithOrdering {
1327 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001328 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001329
1330 public:
1331 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001332 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001333 }
John Zulauf14940722021-04-12 15:19:02 -06001334 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001335 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001336 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001337 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001338};
1339
John Zulauf16adfc92020-04-08 10:28:33 -06001340HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001341 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001342 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001343 const auto base_address = ResourceBaseAddress(buffer);
1344 HazardDetector detector(usage_index);
1345 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001346}
1347
John Zulauf69133422020-05-20 14:55:53 -06001348template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001349HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1350 DetectOptions options) const {
1351 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1352 if (!attachment_gen) return HazardResult();
1353
1354 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1355 const auto address_type = view_gen.GetAddressType();
1356 for (; range_gen->non_empty(); ++range_gen) {
1357 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1358 if (hazard.hazard) return hazard;
1359 }
1360
1361 return HazardResult();
1362}
1363
1364template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001365HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1366 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1367 const VkExtent3D &extent, DetectOptions options) const {
1368 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001369 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001370 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1371 base_address);
1372 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001373 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001374 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001375 if (hazard.hazard) return hazard;
1376 }
1377 return HazardResult();
1378}
John Zulauf110413c2021-03-20 05:38:38 -06001379template <typename Detector>
1380HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1381 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1382 if (!SimpleBinding(image)) return HazardResult();
1383 const auto base_address = ResourceBaseAddress(image);
1384 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1385 const auto address_type = ImageAddressType(image);
1386 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001387 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1388 if (hazard.hazard) return hazard;
1389 }
1390 return HazardResult();
1391}
John Zulauf69133422020-05-20 14:55:53 -06001392
John Zulauf540266b2020-04-06 18:54:53 -06001393HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1394 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1395 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001396 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1397 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001398 HazardDetector detector(current_usage);
1399 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001400}
1401
1402HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001403 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001404 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001405 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001406}
1407
John Zulaufd0ec59f2021-03-13 14:25:08 -07001408HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1409 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1410 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1411 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1412}
1413
John Zulauf69133422020-05-20 14:55:53 -06001414HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001415 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001416 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001417 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001418 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001419}
1420
John Zulauf3d84f1b2020-03-09 13:33:25 -06001421class BarrierHazardDetector {
1422 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001423 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001424 SyncStageAccessFlags src_access_scope)
1425 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1426
John Zulauf5f13a792020-03-10 07:31:21 -06001427 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1428 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001429 }
John Zulauf14940722021-04-12 15:19:02 -06001430 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001431 // 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 -07001432 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001433 }
1434
1435 private:
1436 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001437 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001438 SyncStageAccessFlags src_access_scope_;
1439};
1440
John Zulauf4a6105a2020-11-17 15:11:05 -07001441class EventBarrierHazardDetector {
1442 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001443 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001444 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
John Zulauf14940722021-04-12 15:19:02 -06001445 ResourceUsageTag scope_tag)
John Zulauf4a6105a2020-11-17 15:11:05 -07001446 : usage_index_(usage_index),
1447 src_exec_scope_(src_exec_scope),
1448 src_access_scope_(src_access_scope),
1449 event_scope_(event_scope),
1450 scope_pos_(event_scope.cbegin()),
1451 scope_end_(event_scope.cend()),
1452 scope_tag_(scope_tag) {}
1453
1454 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1455 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1456 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1457 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1458 if (scope_pos_ == scope_end_) return HazardResult();
1459 if (!scope_pos_->first.intersects(pos->first)) {
1460 event_scope_.lower_bound(pos->first);
1461 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1462 }
1463
1464 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1465 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1466 }
John Zulauf14940722021-04-12 15:19:02 -06001467 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07001468 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1469 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1470 }
1471
1472 private:
1473 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001474 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001475 SyncStageAccessFlags src_access_scope_;
1476 const SyncEventState::ScopeMap &event_scope_;
1477 SyncEventState::ScopeMap::const_iterator scope_pos_;
1478 SyncEventState::ScopeMap::const_iterator scope_end_;
John Zulauf14940722021-04-12 15:19:02 -06001479 const ResourceUsageTag scope_tag_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001480};
1481
Jeremy Gebben40a22942020-12-22 14:22:06 -07001482HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001483 const SyncStageAccessFlags &src_access_scope,
1484 const VkImageSubresourceRange &subresource_range,
1485 const SyncEventState &sync_event, DetectOptions options) const {
1486 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1487 // first access scope map to use, and there's no easy way to plumb it in below.
1488 const auto address_type = ImageAddressType(image);
1489 const auto &event_scope = sync_event.FirstScope(address_type);
1490
1491 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1492 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001493 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001494}
1495
John Zulaufd0ec59f2021-03-13 14:25:08 -07001496HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1497 DetectOptions options) const {
1498 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1499 barrier.src_access_scope);
1500 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1501}
1502
Jeremy Gebben40a22942020-12-22 14:22:06 -07001503HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001504 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001505 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001506 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001507 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001508 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001509}
1510
Jeremy Gebben40a22942020-12-22 14:22:06 -07001511HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001512 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001513 const VkImageMemoryBarrier &barrier) const {
1514 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1515 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1516 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1517}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001518HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001519 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001520 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001521}
John Zulauf355e49b2020-04-24 15:11:15 -06001522
John Zulauf9cb530d2019-09-30 14:14:10 -06001523template <typename Flags, typename Map>
1524SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1525 SyncStageAccessFlags scope = 0;
1526 for (const auto &bit_scope : map) {
1527 if (flag_mask < bit_scope.first) break;
1528
1529 if (flag_mask & bit_scope.first) {
1530 scope |= bit_scope.second;
1531 }
1532 }
1533 return scope;
1534}
1535
Jeremy Gebben40a22942020-12-22 14:22:06 -07001536SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001537 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1538}
1539
Jeremy Gebben40a22942020-12-22 14:22:06 -07001540SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1541 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001542}
1543
Jeremy Gebben40a22942020-12-22 14:22:06 -07001544// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1545SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001546 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1547 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1548 // 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 -06001549 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1550}
1551
1552template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001553void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001554 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1555 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001556 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001557 auto pos = accesses->lower_bound(range);
1558 if (pos == accesses->end() || !pos->first.intersects(range)) {
1559 // The range is empty, fill it with a default value.
1560 pos = action.Infill(accesses, pos, range);
1561 } else if (range.begin < pos->first.begin) {
1562 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001563 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001564 } else if (pos->first.begin < range.begin) {
1565 // Trim the beginning if needed
1566 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1567 ++pos;
1568 }
1569
1570 const auto the_end = accesses->end();
1571 while ((pos != the_end) && pos->first.intersects(range)) {
1572 if (pos->first.end > range.end) {
1573 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1574 }
1575
1576 pos = action(accesses, pos);
1577 if (pos == the_end) break;
1578
1579 auto next = pos;
1580 ++next;
1581 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1582 // Need to infill if next is disjoint
1583 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001584 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001585 next = action.Infill(accesses, next, new_range);
1586 }
1587 pos = next;
1588 }
1589}
John Zulaufd5115702021-01-18 12:34:33 -07001590
1591// Give a comparable interface for range generators and ranges
1592template <typename Action>
1593inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1594 assert(range);
1595 UpdateMemoryAccessState(accesses, *range, action);
1596}
1597
John Zulauf4a6105a2020-11-17 15:11:05 -07001598template <typename Action, typename RangeGen>
1599void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1600 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001601 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 -07001602 for (; range_gen->non_empty(); ++range_gen) {
1603 UpdateMemoryAccessState(accesses, *range_gen, action);
1604 }
1605}
John Zulauf9cb530d2019-09-30 14:14:10 -06001606
John Zulaufd0ec59f2021-03-13 14:25:08 -07001607template <typename Action, typename RangeGen>
1608void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1609 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1610 for (; range_gen->non_empty(); ++range_gen) {
1611 UpdateMemoryAccessState(accesses, *range_gen, action);
1612 }
1613}
John Zulauf9cb530d2019-09-30 14:14:10 -06001614struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001615 using Iterator = ResourceAccessRangeMap::iterator;
1616 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001617 // this is only called on gaps, and never returns a gap.
1618 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001619 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001620 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001621 }
John Zulauf5f13a792020-03-10 07:31:21 -06001622
John Zulauf5c5e88d2019-12-26 11:22:02 -07001623 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001624 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001625 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001626 return pos;
1627 }
1628
John Zulauf43cc7462020-12-03 12:33:12 -07001629 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf14940722021-04-12 15:19:02 -06001630 SyncOrdering ordering_rule_, ResourceUsageTag tag_)
John Zulauf8e3c3e92021-01-06 11:19:36 -07001631 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001632 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001633 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001634 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001635 const SyncOrdering ordering_rule;
John Zulauf14940722021-04-12 15:19:02 -06001636 const ResourceUsageTag tag;
John Zulauf9cb530d2019-09-30 14:14:10 -06001637};
1638
John Zulauf4a6105a2020-11-17 15:11:05 -07001639// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001640struct PipelineBarrierOp {
1641 SyncBarrier barrier;
1642 bool layout_transition;
1643 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1644 : barrier(barrier_), layout_transition(layout_transition_) {}
1645 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001646 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001647 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1648};
John Zulauf4a6105a2020-11-17 15:11:05 -07001649// The barrier operation for wait events
1650struct WaitEventBarrierOp {
John Zulauf14940722021-04-12 15:19:02 -06001651 ResourceUsageTag scope_tag;
John Zulauf4a6105a2020-11-17 15:11:05 -07001652 SyncBarrier barrier;
1653 bool layout_transition;
John Zulauf14940722021-04-12 15:19:02 -06001654 WaitEventBarrierOp(const ResourceUsageTag scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1655 : scope_tag(scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
John Zulauf4a6105a2020-11-17 15:11:05 -07001656 WaitEventBarrierOp() = default;
John Zulauf14940722021-04-12 15:19:02 -06001657 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(scope_tag, barrier, layout_transition); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001658};
John Zulauf1e331ec2020-12-04 18:29:38 -07001659
John Zulauf4a6105a2020-11-17 15:11:05 -07001660// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1661// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1662// of a collection is known/present.
John Zulauf5c628d02021-05-04 15:46:36 -06001663template <typename BarrierOp, typename OpVector = std::vector<BarrierOp>>
John Zulauf89311b42020-09-29 16:28:47 -06001664class ApplyBarrierOpsFunctor {
1665 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001666 using Iterator = ResourceAccessRangeMap::iterator;
John Zulauf5c628d02021-05-04 15:46:36 -06001667 // Only called with a gap, and pos at the lower_bound(range)
1668 inline Iterator Infill(ResourceAccessRangeMap *accesses, const Iterator &pos, const ResourceAccessRange &range) const {
1669 if (!infill_default_) {
1670 return pos;
1671 }
1672 ResourceAccessState default_state;
1673 auto inserted = accesses->insert(pos, std::make_pair(range, default_state));
1674 return inserted;
1675 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001676
John Zulauf5c628d02021-05-04 15:46:36 -06001677 Iterator operator()(ResourceAccessRangeMap *accesses, const Iterator &pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001678 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001679 for (const auto &op : barrier_ops_) {
1680 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001681 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001682
John Zulauf89311b42020-09-29 16:28:47 -06001683 if (resolve_) {
1684 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1685 // another walk
1686 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001687 }
1688 return pos;
1689 }
1690
John Zulauf89311b42020-09-29 16:28:47 -06001691 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf5c628d02021-05-04 15:46:36 -06001692 ApplyBarrierOpsFunctor(bool resolve, typename OpVector::size_type size_hint, ResourceUsageTag tag)
1693 : resolve_(resolve), infill_default_(false), barrier_ops_(), tag_(tag) {
John Zulaufd5115702021-01-18 12:34:33 -07001694 barrier_ops_.reserve(size_hint);
1695 }
John Zulauf5c628d02021-05-04 15:46:36 -06001696 void EmplaceBack(const BarrierOp &op) {
1697 barrier_ops_.emplace_back(op);
1698 infill_default_ |= op.layout_transition;
1699 }
John Zulauf89311b42020-09-29 16:28:47 -06001700
1701 private:
1702 bool resolve_;
John Zulauf5c628d02021-05-04 15:46:36 -06001703 bool infill_default_;
1704 OpVector barrier_ops_;
John Zulauf14940722021-04-12 15:19:02 -06001705 const ResourceUsageTag tag_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001706};
1707
John Zulauf4a6105a2020-11-17 15:11:05 -07001708// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1709// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1710template <typename BarrierOp>
John Zulauf5c628d02021-05-04 15:46:36 -06001711class ApplyBarrierFunctor : public ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>> {
1712 using Base = ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>>;
1713
John Zulauf4a6105a2020-11-17 15:11:05 -07001714 public:
John Zulaufee984022022-04-13 16:39:50 -06001715 ApplyBarrierFunctor(const BarrierOp &barrier_op) : Base(false, 1, kInvalidTag) { Base::EmplaceBack(barrier_op); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001716};
1717
John Zulauf1e331ec2020-12-04 18:29:38 -07001718// This functor resolves the pendinging state.
John Zulauf5c628d02021-05-04 15:46:36 -06001719class ResolvePendingBarrierFunctor : public ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>> {
1720 using Base = ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>>;
1721
John Zulauf1e331ec2020-12-04 18:29:38 -07001722 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001723 ResolvePendingBarrierFunctor(ResourceUsageTag tag) : Base(true, 0, tag) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001724};
1725
John Zulauf8e3c3e92021-01-06 11:19:36 -07001726void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001727 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001728 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001729 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001730}
1731
John Zulauf8e3c3e92021-01-06 11:19:36 -07001732void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001733 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001734 if (!SimpleBinding(buffer)) return;
1735 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001736 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001737}
John Zulauf355e49b2020-04-24 15:11:15 -06001738
John Zulauf8e3c3e92021-01-06 11:19:36 -07001739void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001740 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1741 if (!SimpleBinding(image)) return;
1742 const auto base_address = ResourceBaseAddress(image);
1743 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1744 const auto address_type = ImageAddressType(image);
1745 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1746 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1747}
1748void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001749 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001750 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001751 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001752 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001753 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1754 base_address);
1755 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001756 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001757 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001758}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001759
1760void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
John Zulauf14940722021-04-12 15:19:02 -06001761 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001762 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1763 if (!gen) return;
1764 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1765 const auto address_type = view_gen.GetAddressType();
1766 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1767 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001768}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001769
John Zulauf8e3c3e92021-01-06 11:19:36 -07001770void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001771 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001772 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001773 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1774 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001775 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001776}
1777
John Zulaufd0ec59f2021-03-13 14:25:08 -07001778template <typename Action, typename RangeGen>
1779void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1780 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1781 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001782}
1783
1784template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001785void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1786 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1787 if (!gen) return;
1788 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001789}
1790
John Zulaufd0ec59f2021-03-13 14:25:08 -07001791void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1792 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001793 const ResourceUsageTag tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001794 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001795 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001796}
1797
John Zulaufd0ec59f2021-03-13 14:25:08 -07001798void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06001799 uint32_t subpass, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001800 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001801
1802 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1803 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001804 const auto &view_gen = attachment_views[i];
1805 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001806
1807 const auto &ci = attachment_ci[i];
1808 const bool has_depth = FormatHasDepth(ci.format);
1809 const bool has_stencil = FormatHasStencil(ci.format);
1810 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001811 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001812
1813 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001814 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1815 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001816 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001817 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001818 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1819 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001820 }
John Zulauf57261402021-08-13 11:32:06 -06001821 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001822 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001823 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1824 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001825 }
1826 }
1827 }
1828 }
1829}
1830
John Zulauf540266b2020-04-06 18:54:53 -06001831template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001832void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001833 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001834 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001835 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001836 }
1837}
1838
1839void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001840 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1841 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001842 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001843 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001844 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001845 }
1846 }
1847}
1848
John Zulauf4fa68462021-04-26 21:04:22 -06001849// Caller must ensure that lifespan of this is less than from
1850void AccessContext::ImportAsyncContexts(const AccessContext &from) { async_ = from.async_; }
1851
John Zulauf355e49b2020-04-24 15:11:15 -06001852// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001853HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1854 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001855
John Zulauf355e49b2020-04-24 15:11:15 -06001856 // We should never ask for a transition from a context we don't have
John Zulaufbb890452021-12-14 11:30:18 -07001857 assert(track_back.source_subpass);
John Zulauf355e49b2020-04-24 15:11:15 -06001858
1859 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001860 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1861 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufbb890452021-12-14 11:30:18 -07001862 HazardResult hazard = track_back.source_subpass->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001863 if (!hazard.hazard) {
1864 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001865 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001866 }
John Zulaufa0a98292020-09-18 09:30:10 -06001867
John Zulauf355e49b2020-04-24 15:11:15 -06001868 return hazard;
1869}
1870
John Zulaufb02c1eb2020-10-06 16:33:36 -06001871void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001872 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001873 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001874 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001875 for (const auto &transition : transitions) {
1876 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001877 const auto &view_gen = attachment_views[transition.attachment];
1878 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001879
1880 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1881 assert(trackback);
1882
1883 // Import the attachments into the current context
John Zulaufbb890452021-12-14 11:30:18 -07001884 const auto *prev_context = trackback->source_subpass;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001885 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001886 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001887 auto &target_map = GetAccessStateMap(address_type);
1888 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001889 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1890 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001891 }
1892
John Zulauf86356ca2020-10-19 11:46:41 -06001893 // If there were no transitions skip this global map walk
1894 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001895 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001896 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001897 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001898}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001899
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001900void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulauf669dfd52021-01-27 17:15:28 -07001901 auto *events_context = GetCurrentEventsContext();
1902 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06001903 events_context->ApplyBarrier(src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001904}
1905
locke-lunarg61870c22020-06-09 14:51:50 -06001906bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1907 const char *func_name) const {
1908 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001909 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001910 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001911 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001912 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001913 return skip;
1914 }
1915
1916 using DescriptorClass = cvdescriptorset::DescriptorClass;
1917 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1918 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001919 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1920
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001921 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001922 const auto raster_state = pipe->RasterizationState();
1923 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001924 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001925 }
locke-lunarg61870c22020-06-09 14:51:50 -06001926 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001927 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001928 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001929 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001930 const auto descriptor_type = binding_it.GetType();
1931 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1932 auto array_idx = 0;
1933
1934 if (binding_it.IsVariableDescriptorCount()) {
1935 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1936 }
1937 SyncStageAccessIndex sync_index =
1938 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1939
1940 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1941 uint32_t index = i - index_range.start;
1942 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1943 switch (descriptor->GetClass()) {
1944 case DescriptorClass::ImageSampler:
1945 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001946 if (descriptor->Invalid()) {
1947 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001948 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07001949
1950 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
1951 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1952 const auto *img_view_state = image_descriptor->GetImageViewState();
1953 VkImageLayout image_layout = image_descriptor->GetImageLayout();
1954
John Zulauf361fb532020-07-22 10:45:39 -06001955 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001956 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1957 // Descriptors, so we do not have to worry about depth slicing here.
1958 // See: VUID 00343
1959 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001960 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001961 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001962
1963 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1964 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1965 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001966 // Input attachments are subject to raster ordering rules
1967 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001968 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001969 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001970 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001971 }
John Zulauf110413c2021-03-20 05:38:38 -06001972
John Zulauf33fc1d52020-07-17 11:01:10 -06001973 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001974 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001975 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001976 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1977 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001978 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001979 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1980 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1981 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001982 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1983 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
John Zulauf397e68b2022-04-19 11:44:07 -06001984 set_binding.first.binding, index, FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001985 }
1986 break;
1987 }
1988 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001989 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
1990 if (texel_descriptor->Invalid()) {
1991 continue;
1992 }
1993 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
1994 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001995 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001996 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001997 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001998 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001999 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06002000 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
2001 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002002 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
2003 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
2004 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06002005 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002006 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauf397e68b2022-04-19 11:44:07 -06002007 FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002008 }
2009 break;
2010 }
2011 case DescriptorClass::GeneralBuffer: {
2012 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07002013 if (buffer_descriptor->Invalid()) {
2014 continue;
2015 }
2016 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06002017 const ResourceAccessRange range =
2018 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06002019 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06002020 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002021 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002022 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06002023 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
2024 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002025 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2026 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
2027 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06002028 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002029 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauf397e68b2022-04-19 11:44:07 -06002030 FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002031 }
2032 break;
2033 }
2034 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2035 default:
2036 break;
2037 }
2038 }
2039 }
2040 }
2041 return skip;
2042}
2043
2044void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
John Zulauf14940722021-04-12 15:19:02 -06002045 const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002046 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06002047 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002048 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002049 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06002050 return;
2051 }
2052
2053 using DescriptorClass = cvdescriptorset::DescriptorClass;
2054 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
2055 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06002056 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
2057
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002058 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002059 const auto raster_state = pipe->RasterizationState();
2060 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06002061 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002062 }
locke-lunarg61870c22020-06-09 14:51:50 -06002063 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07002064 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002065 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002066 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06002067 const auto descriptor_type = binding_it.GetType();
2068 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2069 auto array_idx = 0;
2070
2071 if (binding_it.IsVariableDescriptorCount()) {
2072 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2073 }
2074 SyncStageAccessIndex sync_index =
2075 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2076
2077 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2078 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2079 switch (descriptor->GetClass()) {
2080 case DescriptorClass::ImageSampler:
2081 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002082 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
2083 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
2084 if (image_descriptor->Invalid()) {
2085 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002086 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07002087 const auto *img_view_state = image_descriptor->GetImageViewState();
Jeremy Gebben11a68a32021-07-29 11:59:22 -06002088 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
2089 // Descriptors, so we do not have to worry about depth slicing here.
2090 // See: VUID 00343
2091 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06002092 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002093 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06002094 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2095 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2096 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
2097 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002098 } else {
John Zulauf110413c2021-03-20 05:38:38 -06002099 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
2100 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002101 }
locke-lunarg61870c22020-06-09 14:51:50 -06002102 break;
2103 }
2104 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002105 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
2106 if (texel_descriptor->Invalid()) {
2107 continue;
2108 }
2109 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
2110 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002111 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002112 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002113 break;
2114 }
2115 case DescriptorClass::GeneralBuffer: {
2116 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07002117 if (buffer_descriptor->Invalid()) {
2118 continue;
2119 }
2120 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06002121 const ResourceAccessRange range =
2122 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002123 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002124 break;
2125 }
2126 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2127 default:
2128 break;
2129 }
2130 }
2131 }
2132 }
2133}
2134
2135bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2136 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002137 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002138 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002139 return skip;
2140 }
2141
2142 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2143 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002144 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002145
2146 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002147 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002148 if (binding_description.binding < binding_buffers_size) {
2149 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002150 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002151
locke-lunarg1ae57d62020-11-18 10:49:19 -07002152 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002153 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2154 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002155 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002156 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002157 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002158 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 -06002159 func_name, string_SyncHazard(hazard.hazard),
2160 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2161 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002162 }
2163 }
2164 }
2165 return skip;
2166}
2167
John Zulauf14940722021-04-12 15:19:02 -06002168void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002169 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002170 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002171 return;
2172 }
2173 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2174 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002175 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002176
2177 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002178 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002179 if (binding_description.binding < binding_buffers_size) {
2180 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002181 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002182
locke-lunarg1ae57d62020-11-18 10:49:19 -07002183 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002184 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2185 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002186 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2187 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002188 }
2189 }
2190}
2191
2192bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2193 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002194 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002195 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002196 }
locke-lunarg61870c22020-06-09 14:51:50 -06002197
locke-lunarg1ae57d62020-11-18 10:49:19 -07002198 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002199 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002200 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2201 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002202 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002203 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002204 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002205 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2206 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06002207 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002208 }
2209
2210 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2211 // We will detect more accurate range in the future.
2212 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2213 return skip;
2214}
2215
John Zulauf14940722021-04-12 15:19:02 -06002216void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002217 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 -06002218
locke-lunarg1ae57d62020-11-18 10:49:19 -07002219 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002220 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002221 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2222 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002223 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002224
2225 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2226 // We will detect more accurate range in the future.
2227 RecordDrawVertex(UINT32_MAX, 0, tag);
2228}
2229
2230bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002231 bool skip = false;
2232 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002233 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002234 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002235}
2236
John Zulauf14940722021-04-12 15:19:02 -06002237void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002238 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002239 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002240 }
locke-lunarg61870c22020-06-09 14:51:50 -06002241}
2242
John Zulauf41a9c7c2021-12-07 15:59:53 -07002243ResourceUsageTag CommandBufferAccessContext::RecordBeginRenderPass(CMD_TYPE cmd, const RENDER_PASS_STATE &rp_state,
2244 const VkRect2D &render_area,
2245 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
John Zulauf355e49b2020-04-24 15:11:15 -06002246 // Create an access context the current renderpass.
John Zulauf41a9c7c2021-12-07 15:59:53 -07002247 const auto barrier_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2248 const auto load_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kLoadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07002249 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002250 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002251 current_renderpass_context_->RecordBeginRenderPass(barrier_tag, load_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002252 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002253 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002254}
2255
John Zulauf41a9c7c2021-12-07 15:59:53 -07002256ResourceUsageTag CommandBufferAccessContext::RecordNextSubpass(const CMD_TYPE cmd) {
John Zulauf16adfc92020-04-08 10:28:33 -06002257 assert(current_renderpass_context_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002258 if (!current_renderpass_context_) return NextCommandTag(cmd);
2259
2260 auto store_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kStoreOp);
2261 auto barrier_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2262 auto load_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kLoadOp);
2263
2264 current_renderpass_context_->RecordNextSubpass(store_tag, barrier_tag, load_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002265 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002266 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002267}
2268
John Zulauf41a9c7c2021-12-07 15:59:53 -07002269ResourceUsageTag CommandBufferAccessContext::RecordEndRenderPass(const CMD_TYPE cmd) {
John Zulauf16adfc92020-04-08 10:28:33 -06002270 assert(current_renderpass_context_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002271 if (!current_renderpass_context_) return NextCommandTag(cmd);
John Zulauf16adfc92020-04-08 10:28:33 -06002272
John Zulauf41a9c7c2021-12-07 15:59:53 -07002273 auto store_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kStoreOp);
2274 auto barrier_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2275
2276 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, store_tag, barrier_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002277 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002278 current_renderpass_context_ = nullptr;
John Zulauf41a9c7c2021-12-07 15:59:53 -07002279 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002280}
2281
John Zulauf4a6105a2020-11-17 15:11:05 -07002282void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2283 // Erase is okay with the key not being
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002284 auto event_state = sync_state_->Get<EVENT_STATE>(event);
John Zulauf669dfd52021-01-27 17:15:28 -07002285 if (event_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002286 GetCurrentEventsContext()->Destroy(event_state.get());
John Zulaufd5115702021-01-18 12:34:33 -07002287 }
2288}
2289
John Zulaufae842002021-04-15 18:20:55 -06002290// The is the recorded cb context
John Zulaufbb890452021-12-14 11:30:18 -07002291bool CommandBufferAccessContext::ValidateFirstUse(CommandExecutionContext *proxy_context, const char *func_name,
John Zulauf4fa68462021-04-26 21:04:22 -06002292 uint32_t index) const {
2293 assert(proxy_context);
2294 auto *events_context = proxy_context->GetCurrentEventsContext();
2295 auto *access_context = proxy_context->GetCurrentAccessContext();
2296 const ResourceUsageTag base_tag = proxy_context->GetTagLimit();
John Zulaufae842002021-04-15 18:20:55 -06002297 bool skip = false;
2298 ResourceUsageRange tag_range = {0, 0};
2299 const AccessContext *recorded_context = GetCurrentAccessContext();
2300 assert(recorded_context);
2301 HazardResult hazard;
John Zulaufbb890452021-12-14 11:30:18 -07002302 auto log_msg = [this](const HazardResult &hazard, const CommandExecutionContext &exec_context, const char *func_name,
John Zulaufae842002021-04-15 18:20:55 -06002303 uint32_t index) {
John Zulaufbb890452021-12-14 11:30:18 -07002304 const auto handle = exec_context.Handle();
John Zulaufae842002021-04-15 18:20:55 -06002305 const auto recorded_handle = cb_state_->commandBuffer();
John Zulauf4fa68462021-04-26 21:04:22 -06002306 const auto *report_data = sync_state_->report_data;
John Zulaufbb890452021-12-14 11:30:18 -07002307 return sync_state_->LogError(handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf4fa68462021-04-26 21:04:22 -06002308 "%s: Hazard %s for entry %" PRIu32 ", %s, Recorded access info %s. Access info %s.", func_name,
2309 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(recorded_handle).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06002310 FormatUsage(*hazard.recorded_access).c_str(), exec_context.FormatHazard(hazard).c_str());
John Zulaufae842002021-04-15 18:20:55 -06002311 };
John Zulaufbb890452021-12-14 11:30:18 -07002312 const ReplayTrackbackBarriersAction *replay_context = nullptr;
John Zulaufae842002021-04-15 18:20:55 -06002313 for (const auto &sync_op : sync_ops_) {
John Zulauf4fa68462021-04-26 21:04:22 -06002314 // we update the range to any include layout transition first use writes,
2315 // as they are stored along with the source scope (as effective barrier) when recorded
2316 tag_range.end = sync_op.tag + 1;
John Zulauf610e28c2021-08-03 17:46:23 -06002317 skip |= sync_op.sync_op->ReplayValidate(sync_op.tag, *this, base_tag, proxy_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002318
John Zulaufbb890452021-12-14 11:30:18 -07002319 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context, replay_context);
John Zulaufae842002021-04-15 18:20:55 -06002320 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002321 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002322 }
2323 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
John Zulauf4fa68462021-04-26 21:04:22 -06002324 // Record the barrier into the proxy context.
John Zulaufbb890452021-12-14 11:30:18 -07002325 sync_op.sync_op->ReplayRecord(base_tag + sync_op.tag, access_context, events_context);
2326 replay_context = sync_op.sync_op->GetReplayTrackback();
John Zulauf4fa68462021-04-26 21:04:22 -06002327 tag_range.begin = tag_range.end;
John Zulaufae842002021-04-15 18:20:55 -06002328 }
2329
John Zulaufbb890452021-12-14 11:30:18 -07002330 // Renderpasses may not cross command buffer boundaries
2331 assert(replay_context == nullptr);
2332
John Zulaufae842002021-04-15 18:20:55 -06002333 // and anything after the last syncop
John Zulaufae842002021-04-15 18:20:55 -06002334 tag_range.end = ResourceUsageRecord::kMaxIndex;
John Zulaufbb890452021-12-14 11:30:18 -07002335 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context, replay_context);
John Zulaufae842002021-04-15 18:20:55 -06002336 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002337 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002338 }
2339
2340 return skip;
2341}
2342
John Zulauf4fa68462021-04-26 21:04:22 -06002343void CommandBufferAccessContext::RecordExecutedCommandBuffer(const CommandBufferAccessContext &recorded_cb_context, CMD_TYPE cmd) {
2344 auto *events_context = GetCurrentEventsContext();
2345 auto *access_context = GetCurrentAccessContext();
2346 const AccessContext *recorded_context = recorded_cb_context.GetCurrentAccessContext();
2347 assert(recorded_context);
2348
2349 // Just run through the barriers ignoring the usage from the recorded context, as Resolve will overwrite outdated state
2350 const ResourceUsageTag base_tag = GetTagLimit();
2351 for (const auto &sync_op : recorded_cb_context.sync_ops_) {
2352 // we update the range to any include layout transition first use writes,
2353 // as they are stored along with the source scope (as effective barrier) when recorded
John Zulaufbb890452021-12-14 11:30:18 -07002354 sync_op.sync_op->ReplayRecord(base_tag + sync_op.tag, access_context, events_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002355 }
2356
2357 ResourceUsageRange tag_range = ImportRecordedAccessLog(recorded_cb_context);
2358 assert(base_tag == tag_range.begin); // to ensure the to offset calculation agree
2359 ResolveRecordedContext(*recorded_context, tag_range.begin);
2360}
2361
John Zulauf3c788ef2022-02-22 12:12:30 -07002362void CommandExecutionContext::ResolveRecordedContext(const AccessContext &recorded_context, ResourceUsageTag offset) {
John Zulauf4fa68462021-04-26 21:04:22 -06002363 auto tag_offset = [offset](ResourceAccessState *access) { access->OffsetTag(offset); };
2364
2365 auto *access_context = GetCurrentAccessContext();
2366 for (auto address_type : kAddressTypes) {
2367 recorded_context.ResolveAccessRange(address_type, kFullRange, tag_offset, &access_context->GetAccessStateMap(address_type),
2368 nullptr, false);
2369 }
2370}
2371
John Zulauf3c788ef2022-02-22 12:12:30 -07002372ResourceUsageRange CommandExecutionContext::ImportRecordedAccessLog(const CommandBufferAccessContext &recorded_context) {
John Zulauf4fa68462021-04-26 21:04:22 -06002373 // The execution references ensure lifespan for the referenced child CB's...
2374 ResourceUsageRange tag_range(GetTagLimit(), 0);
John Zulauf3c788ef2022-02-22 12:12:30 -07002375 InsertRecordedAccessLogEntries(recorded_context);
2376 tag_range.end = GetTagLimit();
John Zulauf4fa68462021-04-26 21:04:22 -06002377 return tag_range;
2378}
2379
John Zulauf3c788ef2022-02-22 12:12:30 -07002380void CommandBufferAccessContext::InsertRecordedAccessLogEntries(const CommandBufferAccessContext &recorded_context) {
2381 cbs_referenced_.emplace(recorded_context.GetCBStateShared());
2382 access_log_.insert(access_log_.end(), recorded_context.access_log_.cbegin(), recorded_context.access_log_.end());
2383}
2384
John Zulauf41a9c7c2021-12-07 15:59:53 -07002385ResourceUsageTag CommandBufferAccessContext::NextSubcommandTag(CMD_TYPE command, ResourceUsageRecord::SubcommandType subcommand) {
2386 ResourceUsageTag next = access_log_.size();
2387 access_log_.emplace_back(command, command_number_, subcommand, ++subcommand_number_, cb_state_.get(), reset_count_);
2388 return next;
2389}
2390
2391ResourceUsageTag CommandBufferAccessContext::NextCommandTag(CMD_TYPE command, ResourceUsageRecord::SubcommandType subcommand) {
2392 command_number_++;
2393 subcommand_number_ = 0;
2394 ResourceUsageTag next = access_log_.size();
2395 access_log_.emplace_back(command, command_number_, subcommand, subcommand_number_, cb_state_.get(), reset_count_);
2396 return next;
2397}
2398
2399ResourceUsageTag CommandBufferAccessContext::NextIndexedCommandTag(CMD_TYPE command, uint32_t index) {
2400 if (index == 0) {
2401 return NextCommandTag(command, ResourceUsageRecord::SubcommandType::kIndex);
2402 }
2403 return NextSubcommandTag(command, ResourceUsageRecord::SubcommandType::kIndex);
2404}
2405
John Zulaufbb890452021-12-14 11:30:18 -07002406void CommandBufferAccessContext::RecordSyncOp(SyncOpPointer &&sync_op) {
2407 auto tag = sync_op->Record(this);
2408 // As renderpass operations can have side effects on the command buffer access context,
2409 // update the sync operation to record these if any.
2410 if (current_renderpass_context_) {
2411 const auto &rpc = *current_renderpass_context_;
2412 sync_op->SetReplayContext(rpc.GetCurrentSubpass(), rpc.GetReplayContext());
2413 }
2414 sync_ops_.emplace_back(tag, std::move(sync_op));
2415}
2416
John Zulaufae842002021-04-15 18:20:55 -06002417class HazardDetectFirstUse {
2418 public:
John Zulaufbb890452021-12-14 11:30:18 -07002419 HazardDetectFirstUse(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2420 const ReplayTrackbackBarriersAction *replay_barrier)
2421 : recorded_use_(recorded_use), tag_range_(tag_range), replay_barrier_(replay_barrier) {}
John Zulaufae842002021-04-15 18:20:55 -06002422 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulaufbb890452021-12-14 11:30:18 -07002423 if (replay_barrier_) {
2424 // Intentional copy to apply the replay barrier
2425 auto access = pos->second;
2426 (*replay_barrier_)(&access);
2427 return access.DetectHazard(recorded_use_, tag_range_);
2428 }
John Zulaufae842002021-04-15 18:20:55 -06002429 return pos->second.DetectHazard(recorded_use_, tag_range_);
2430 }
2431 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
2432 return pos->second.DetectAsyncHazard(recorded_use_, tag_range_, start_tag);
2433 }
2434
2435 private:
2436 const ResourceAccessState &recorded_use_;
2437 const ResourceUsageRange &tag_range_;
John Zulaufbb890452021-12-14 11:30:18 -07002438 const ReplayTrackbackBarriersAction *replay_barrier_;
John Zulaufae842002021-04-15 18:20:55 -06002439};
2440
2441// This is called with the *recorded* command buffers access context, with the *active* access context pass in, againsts which
2442// hazards will be detected
John Zulaufbb890452021-12-14 11:30:18 -07002443HazardResult AccessContext::DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context,
2444 const ReplayTrackbackBarriersAction *replay_barrier) const {
John Zulaufae842002021-04-15 18:20:55 -06002445 HazardResult hazard;
2446 for (const auto address_type : kAddressTypes) {
2447 const auto &recorded_access_map = GetAccessStateMap(address_type);
2448 for (const auto &recorded_access : recorded_access_map) {
2449 // Cull any entries not in the current tag range
2450 if (!recorded_access.second.FirstAccessInTagRange(tag_range)) continue;
John Zulaufbb890452021-12-14 11:30:18 -07002451 HazardDetectFirstUse detector(recorded_access.second, tag_range, replay_barrier);
John Zulaufae842002021-04-15 18:20:55 -06002452 hazard = access_context.DetectHazard(address_type, detector, recorded_access.first, DetectOptions::kDetectAll);
2453 if (hazard.hazard) break;
2454 }
2455 }
2456
2457 return hazard;
2458}
2459
John Zulaufbb890452021-12-14 11:30:18 -07002460bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &exec_context,
2461 const CMD_BUFFER_STATE &cmd, const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002462 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07002463 const auto &sync_state = exec_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002464 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002465 if (!pipe) {
2466 return skip;
2467 }
2468
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002469 const auto raster_state = pipe->RasterizationState();
2470 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002471 return skip;
2472 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002473 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002474 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002475
John Zulauf1a224292020-06-30 14:52:13 -06002476 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002477 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002478 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2479 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002480 if (location >= subpass.colorAttachmentCount ||
2481 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002482 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002483 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002484 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2485 if (!view_gen.IsValid()) continue;
2486 HazardResult hazard =
2487 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2488 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002489 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002490 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002491 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002492 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002493 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002494 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002495 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf397e68b2022-04-19 11:44:07 -06002496 location, exec_context.FormatHazard(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002497 }
2498 }
2499 }
locke-lunarg37047832020-06-12 13:44:45 -06002500
2501 // PHASE1 TODO: Add layout based read/vs. write selection.
2502 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002503 const auto ds_state = pipe->DepthStencilState();
2504 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002505
2506 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2507 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2508 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002509 bool depth_write = false, stencil_write = false;
2510
2511 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002512 if (!FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable && ds_state->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002513 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2514 depth_write = true;
2515 }
2516 // PHASE1 TODO: It needs to check if stencil is writable.
2517 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2518 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2519 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002520 if (!FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002521 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2522 stencil_write = true;
2523 }
2524
2525 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2526 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002527 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2528 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2529 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002530 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002531 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002532 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002533 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002534 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002535 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2536 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf397e68b2022-04-19 11:44:07 -06002537 exec_context.FormatHazard(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002538 }
2539 }
2540 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002541 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2542 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2543 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002544 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002545 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002546 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002547 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002548 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002549 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2550 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf397e68b2022-04-19 11:44:07 -06002551 exec_context.FormatHazard(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002552 }
locke-lunarg61870c22020-06-09 14:51:50 -06002553 }
2554 }
2555 return skip;
2556}
2557
John Zulauf14940722021-04-12 15:19:02 -06002558void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002559 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002560 if (!pipe) {
2561 return;
2562 }
2563
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002564 const auto *raster_state = pipe->RasterizationState();
2565 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002566 return;
2567 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002568 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002569 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002570
John Zulauf1a224292020-06-30 14:52:13 -06002571 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002572 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002573 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2574 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002575 if (location >= subpass.colorAttachmentCount ||
2576 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002577 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002578 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002579 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2580 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2581 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2582 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002583 }
2584 }
locke-lunarg37047832020-06-12 13:44:45 -06002585
2586 // PHASE1 TODO: Add layout based read/vs. write selection.
2587 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002588 const auto *ds_state = pipe->DepthStencilState();
2589 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002590 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2591 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2592 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002593 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002594 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2595 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002596
2597 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002598 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable &&
2599 ds_state->depthWriteEnable && IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
locke-lunarg37047832020-06-12 13:44:45 -06002600 depth_write = true;
2601 }
2602 // PHASE1 TODO: It needs to check if stencil is writable.
2603 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2604 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2605 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002606 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002607 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2608 stencil_write = true;
2609 }
2610
John Zulaufd0ec59f2021-03-13 14:25:08 -07002611 if (depth_write || stencil_write) {
2612 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2613 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2614 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2615 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002616 }
locke-lunarg61870c22020-06-09 14:51:50 -06002617 }
2618}
2619
John Zulaufbb890452021-12-14 11:30:18 -07002620bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &exec_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002621 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002622 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07002623 skip |= CurrentContext().ValidateResolveOperations(exec_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002624 current_subpass_);
John Zulaufbb890452021-12-14 11:30:18 -07002625 skip |= CurrentContext().ValidateStoreOperation(exec_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002626 func_name);
2627
John Zulauf355e49b2020-04-24 15:11:15 -06002628 const auto next_subpass = current_subpass_ + 1;
ziga-lunarg31a3e772022-03-22 11:48:46 +01002629 if (next_subpass >= subpass_contexts_.size()) {
2630 return skip;
2631 }
John Zulauf1507ee42020-05-18 11:33:09 -06002632 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002633 skip |=
John Zulaufbb890452021-12-14 11:30:18 -07002634 next_context.ValidateLayoutTransitions(exec_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002635 if (!skip) {
2636 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2637 // on a copy of the (empty) next context.
2638 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2639 AccessContext temp_context(next_context);
John Zulaufee984022022-04-13 16:39:50 -06002640 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kInvalidTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002641 skip |=
John Zulaufbb890452021-12-14 11:30:18 -07002642 temp_context.ValidateLoadOperation(exec_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002643 }
John Zulauf7635de32020-05-29 17:14:15 -06002644 return skip;
2645}
John Zulaufbb890452021-12-14 11:30:18 -07002646bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &exec_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002647 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002648 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07002649 skip |= CurrentContext().ValidateResolveOperations(exec_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002650 current_subpass_);
John Zulaufbb890452021-12-14 11:30:18 -07002651 skip |= CurrentContext().ValidateStoreOperation(exec_context, *rp_state_, render_area_, current_subpass_,
John Zulaufd0ec59f2021-03-13 14:25:08 -07002652
2653 attachment_views_, func_name);
John Zulaufbb890452021-12-14 11:30:18 -07002654 skip |= ValidateFinalSubpassLayoutTransitions(exec_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002655 return skip;
2656}
2657
John Zulauf64ffe552021-02-06 10:25:07 -07002658AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002659 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002660}
2661
John Zulaufbb890452021-12-14 11:30:18 -07002662bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &exec_context,
John Zulauf64ffe552021-02-06 10:25:07 -07002663 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002664 bool skip = false;
2665
John Zulauf7635de32020-05-29 17:14:15 -06002666 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2667 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2668 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2669 // to apply and only copy then, if this proves a hot spot.
2670 std::unique_ptr<AccessContext> proxy_for_current;
2671
John Zulauf355e49b2020-04-24 15:11:15 -06002672 // Validate the "finalLayout" transitions to external
2673 // Get them from where there we're hidding in the extra entry.
2674 const auto &final_transitions = rp_state_->subpass_transitions.back();
2675 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002676 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002677 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufbb890452021-12-14 11:30:18 -07002678 assert(trackback.source_subpass); // Transitions are given implicit transitions if the StateTracker is working correctly
2679 auto *context = trackback.source_subpass;
John Zulauf7635de32020-05-29 17:14:15 -06002680
2681 if (transition.prev_pass == current_subpass_) {
2682 if (!proxy_for_current) {
2683 // 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 -07002684 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002685 }
2686 context = proxy_for_current.get();
2687 }
2688
John Zulaufa0a98292020-09-18 09:30:10 -06002689 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2690 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002691 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002692 if (hazard.hazard) {
John Zulaufee984022022-04-13 16:39:50 -06002693 if (hazard.tag == kInvalidTag) {
2694 // Hazard vs. ILT
John Zulaufbb890452021-12-14 11:30:18 -07002695 skip |= exec_context.GetSyncState().LogError(
John Zulaufee984022022-04-13 16:39:50 -06002696 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
2697 "%s: Hazard %s vs. store/resolve operations in subpass %" PRIu32 " for attachment %" PRIu32
2698 " final image layout transition (old_layout: %s, new_layout: %s).",
2699 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2700 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout));
2701 } else {
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 with last use subpass %" PRIu32 " for attachment %" PRIu32
2705 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2706 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2707 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf397e68b2022-04-19 11:44:07 -06002708 exec_context.FormatHazard(hazard).c_str());
John Zulaufee984022022-04-13 16:39:50 -06002709 }
John Zulauf355e49b2020-04-24 15:11:15 -06002710 }
2711 }
2712 return skip;
2713}
2714
John Zulauf14940722021-04-12 15:19:02 -06002715void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002716 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002717 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002718}
2719
John Zulauf14940722021-04-12 15:19:02 -06002720void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002721 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2722 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002723
2724 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2725 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002726 const AttachmentViewGen &view_gen = attachment_views_[i];
2727 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002728
2729 const auto &ci = attachment_ci[i];
2730 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002731 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002732 const bool is_color = !(has_depth || has_stencil);
2733
2734 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002735 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2736 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2737 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2738 SyncOrdering::kColorAttachment, tag);
2739 }
John Zulauf1507ee42020-05-18 11:33:09 -06002740 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002741 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002742 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2743 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2744 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2745 SyncOrdering::kDepthStencilAttachment, tag);
2746 }
John Zulauf1507ee42020-05-18 11:33:09 -06002747 }
2748 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002749 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2750 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2751 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2752 SyncOrdering::kDepthStencilAttachment, tag);
2753 }
John Zulauf1507ee42020-05-18 11:33:09 -06002754 }
2755 }
2756 }
2757 }
2758}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002759AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2760 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2761 AttachmentViewGenVector view_gens;
2762 VkExtent3D extent = CastTo3D(render_area.extent);
2763 VkOffset3D offset = CastTo3D(render_area.offset);
2764 view_gens.reserve(attachment_views.size());
2765 for (const auto *view : attachment_views) {
2766 view_gens.emplace_back(view, offset, extent);
2767 }
2768 return view_gens;
2769}
John Zulauf64ffe552021-02-06 10:25:07 -07002770RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2771 VkQueueFlags queue_flags,
2772 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2773 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002774 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002775 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002776 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulaufbb890452021-12-14 11:30:18 -07002777 replay_context_ = std::make_shared<ReplayRenderpassContext>();
2778 auto &replay_subpass_contexts = replay_context_->subpass_contexts;
2779 replay_subpass_contexts.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002780 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002781 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulaufbb890452021-12-14 11:30:18 -07002782 replay_subpass_contexts.emplace_back(queue_flags, rp_state_->subpass_dependencies[pass], replay_subpass_contexts);
John Zulauf355e49b2020-04-24 15:11:15 -06002783 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002784 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002785}
John Zulauf41a9c7c2021-12-07 15:59:53 -07002786void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag barrier_tag, const ResourceUsageTag load_tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002787 assert(0 == current_subpass_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002788 subpass_contexts_[current_subpass_].SetStartTag(barrier_tag);
2789 RecordLayoutTransitions(barrier_tag);
2790 RecordLoadOperations(load_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002791}
John Zulauf1507ee42020-05-18 11:33:09 -06002792
John Zulauf41a9c7c2021-12-07 15:59:53 -07002793void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag store_tag, const ResourceUsageTag barrier_tag,
2794 const ResourceUsageTag load_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002795 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulauf41a9c7c2021-12-07 15:59:53 -07002796 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
2797 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002798
ziga-lunarg31a3e772022-03-22 11:48:46 +01002799 if (current_subpass_ + 1 >= subpass_contexts_.size()) {
2800 return;
2801 }
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002802 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2803 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002804 current_subpass_++;
John Zulauf41a9c7c2021-12-07 15:59:53 -07002805 subpass_contexts_[current_subpass_].SetStartTag(barrier_tag);
2806 RecordLayoutTransitions(barrier_tag);
2807 RecordLoadOperations(load_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002808}
2809
John Zulauf41a9c7c2021-12-07 15:59:53 -07002810void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag store_tag,
2811 const ResourceUsageTag barrier_tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002812 // Add the resolve and store accesses
John Zulauf41a9c7c2021-12-07 15:59:53 -07002813 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
2814 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002815
John Zulauf355e49b2020-04-24 15:11:15 -06002816 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002817 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002818
2819 // Add the "finalLayout" transitions to external
2820 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002821 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2822 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2823 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002824 const auto &final_transitions = rp_state_->subpass_transitions.back();
2825 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002826 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002827 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufbb890452021-12-14 11:30:18 -07002828 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.source_subpass);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002829 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), barrier_tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002830 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002831 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002832 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002833 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002834 }
2835}
2836
Jeremy Gebben40a22942020-12-22 14:22:06 -07002837SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002838 SyncExecScope result;
2839 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002840 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2841 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002842 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2843 return result;
2844}
2845
Jeremy Gebben40a22942020-12-22 14:22:06 -07002846SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002847 SyncExecScope result;
2848 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002849 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2850 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002851 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2852 return result;
2853}
2854
2855SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002856 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002857 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002858 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002859 dst_access_scope = 0;
2860}
2861
2862template <typename Barrier>
2863SyncBarrier::SyncBarrier(const Barrier &barrier, 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 = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002866 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002867 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2868}
2869
2870SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002871 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2872 if (barrier) {
2873 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002874 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002875 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002876
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002877 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002878 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002879 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2880
2881 } else {
2882 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002883 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002884 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2885
2886 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002887 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002888 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2889 }
2890}
2891
2892template <typename Barrier>
2893SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2894 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2895 src_exec_scope = src.exec_scope;
2896 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2897
2898 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002899 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002900 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002901}
2902
John Zulaufb02c1eb2020-10-06 16:33:36 -06002903// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2904void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2905 for (const auto &barrier : barriers) {
2906 ApplyBarrier(barrier, layout_transition);
2907 }
2908}
2909
John Zulauf89311b42020-09-29 16:28:47 -06002910// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2911// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2912// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufbb890452021-12-14 11:30:18 -07002913void ResourceAccessState::ApplyBarriersImmediate(const std::vector<SyncBarrier> &barriers) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002914 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002915 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002916 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002917 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002918 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002919 }
John Zulaufbb890452021-12-14 11:30:18 -07002920 ApplyPendingBarriers(kInvalidTag); // There can't be any need for this tag
John Zulauf3d84f1b2020-03-09 13:33:25 -06002921}
John Zulauf9cb530d2019-09-30 14:14:10 -06002922HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2923 HazardResult hazard;
2924 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002925 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002926 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002927 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002928 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002929 }
2930 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002931 // Write operation:
2932 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2933 // If reads exists -- test only against them because either:
2934 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2935 // * 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
2936 // the current write happens after the reads, so just test the write against the reades
2937 // Otherwise test against last_write
2938 //
2939 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002940 if (last_reads.size()) {
2941 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002942 if (IsReadHazard(usage_stage, read_access)) {
2943 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2944 break;
2945 }
2946 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002947 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002948 // Write-After-Write check -- if we have a previous write to test against
2949 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002950 }
2951 }
2952 return hazard;
2953}
2954
John Zulauf4fa68462021-04-26 21:04:22 -06002955HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002956 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002957 return DetectHazard(usage_index, ordering);
2958}
2959
2960HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002961 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2962 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002963 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002964 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002965 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2966 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002967 if (IsRead(usage_bit)) {
2968 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2969 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2970 if (is_raw_hazard) {
2971 // NOTE: we know last_write is non-zero
2972 // See if the ordering rules save us from the simple RAW check above
2973 // First check to see if the current usage is covered by the ordering rules
2974 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2975 const bool usage_is_ordered =
2976 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2977 if (usage_is_ordered) {
2978 // Now see of the most recent write (or a subsequent read) are ordered
2979 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2980 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002981 }
2982 }
John Zulauf4285ee92020-09-23 10:20:52 -06002983 if (is_raw_hazard) {
2984 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2985 }
John Zulauf5c628d02021-05-04 15:46:36 -06002986 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2987 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2988 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002989 } else {
2990 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002991 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002992 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002993 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002994 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002995 if (usage_write_is_ordered) {
2996 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2997 ordered_stages = GetOrderedStages(ordering);
2998 }
2999 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
3000 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003001 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06003002 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
3003 if (IsReadHazard(usage_stage, read_access)) {
3004 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3005 break;
3006 }
John Zulaufd14743a2020-07-03 09:42:39 -06003007 }
3008 }
John Zulauf2a344ca2021-09-09 17:07:19 -06003009 } else if (last_write.any() && !(last_write_is_ordered && usage_write_is_ordered)) {
3010 bool ilt_ilt_hazard = false;
3011 if ((usage_index == SYNC_IMAGE_LAYOUT_TRANSITION) && (usage_bit == last_write)) {
3012 // ILT after ILT is a special case where we check the 2nd access scope of the first ILT against the first access
3013 // scope of the second ILT, which has been passed (smuggled?) in the ordering barrier
3014 ilt_ilt_hazard = !(write_barriers & ordering.access_scope).any();
3015 }
3016 if (ilt_ilt_hazard || IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06003017 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06003018 }
John Zulauf69133422020-05-20 14:55:53 -06003019 }
3020 }
3021 return hazard;
3022}
3023
John Zulaufae842002021-04-15 18:20:55 -06003024HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
3025 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06003026 using Size = FirstAccesses::size_type;
3027 const auto &recorded_accesses = recorded_use.first_accesses_;
3028 Size count = recorded_accesses.size();
3029 if (count) {
3030 const auto &last_access = recorded_accesses.back();
3031 bool do_write_last = IsWrite(last_access.usage_index);
3032 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06003033
John Zulauf4fa68462021-04-26 21:04:22 -06003034 for (Size i = 0; i < count; ++count) {
3035 const auto &first = recorded_accesses[i];
3036 // Skip and quit logic
3037 if (first.tag < tag_range.begin) continue;
3038 if (first.tag >= tag_range.end) {
3039 do_write_last = false; // ignore last since we know it can't be in tag_range
3040 break;
3041 }
3042
3043 hazard = DetectHazard(first.usage_index, first.ordering_rule);
3044 if (hazard.hazard) {
3045 hazard.AddRecordedAccess(first);
3046 break;
3047 }
3048 }
3049
3050 if (do_write_last && tag_range.includes(last_access.tag)) {
3051 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
3052 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
3053 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
3054 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
3055 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
3056 // the barrier that applies them
3057 barrier |= recorded_use.first_write_layout_ordering_;
3058 }
3059 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
3060 // the active context
3061 if (recorded_use.first_read_stages_) {
3062 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
3063 // reads in the active context are not "most recent" as all recorded context operations are *after* them
3064 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
3065 // active context.
3066 barrier.exec_scope |= recorded_use.first_read_stages_;
3067 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
3068 barrier.access_scope |= FlagBit(last_access.usage_index);
3069 }
3070 hazard = DetectHazard(last_access.usage_index, barrier);
3071 if (hazard.hazard) {
3072 hazard.AddRecordedAccess(last_access);
3073 }
3074 }
John Zulaufae842002021-04-15 18:20:55 -06003075 }
3076 return hazard;
3077}
3078
John Zulauf2f952d22020-02-10 11:34:51 -07003079// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06003080HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07003081 HazardResult hazard;
3082 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003083 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
3084 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
3085 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07003086 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06003087 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06003088 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003089 }
3090 } else {
John Zulauf14940722021-04-12 15:19:02 -06003091 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06003092 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07003093 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003094 // 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 -07003095 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003096 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003097 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003098 break;
3099 }
3100 }
John Zulauf2f952d22020-02-10 11:34:51 -07003101 }
3102 }
3103 return hazard;
3104}
3105
John Zulaufae842002021-04-15 18:20:55 -06003106HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
3107 ResourceUsageTag start_tag) const {
3108 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06003109 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06003110 // Skip and quit logic
3111 if (first.tag < tag_range.begin) continue;
3112 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06003113
3114 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06003115 if (hazard.hazard) {
3116 hazard.AddRecordedAccess(first);
3117 break;
3118 }
John Zulaufae842002021-04-15 18:20:55 -06003119 }
3120 return hazard;
3121}
3122
Jeremy Gebben40a22942020-12-22 14:22:06 -07003123HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003124 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003125 // Only supporting image layout transitions for now
3126 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3127 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003128 // only test for WAW if there no intervening read operations.
3129 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07003130 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06003131 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07003132 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003133 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003134 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003135 break;
3136 }
3137 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003138 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3139 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3140 }
3141
3142 return hazard;
3143}
3144
Jeremy Gebben40a22942020-12-22 14:22:06 -07003145HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07003146 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06003147 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07003148 // Only supporting image layout transitions for now
3149 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3150 HazardResult hazard;
3151 // only test for WAW if there no intervening read operations.
3152 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3153
John Zulaufab7756b2020-12-29 16:10:16 -07003154 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003155 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3156 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003157 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003158 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003159 // The read is in the events first synchronization scope, so we use a barrier hazard check
3160 // If the read stage is not in the src sync scope
3161 // *AND* not execution chained with an existing sync barrier (that's the or)
3162 // then the barrier access is unsafe (R/W after R)
3163 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3164 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3165 break;
3166 }
3167 } else {
3168 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3169 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3170 }
3171 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003172 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003173 // 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 -06003174 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003175 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3176 // So do a normal barrier hazard check
3177 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3178 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3179 }
3180 } else {
3181 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003182 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3183 }
John Zulaufd14743a2020-07-03 09:42:39 -06003184 }
John Zulauf361fb532020-07-22 10:45:39 -06003185
John Zulauf0cb5be22020-01-23 12:18:22 -07003186 return hazard;
3187}
3188
John Zulauf5f13a792020-03-10 07:31:21 -06003189// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3190// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3191// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3192void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003193 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003194 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3195 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003196 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003197 } else if (other.write_tag == write_tag) {
3198 // 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 -06003199 // dependency chaining logic or any stage expansion)
3200 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003201 pending_write_barriers |= other.pending_write_barriers;
3202 pending_layout_transition |= other.pending_layout_transition;
3203 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003204 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003205
John Zulaufd14743a2020-07-03 09:42:39 -06003206 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003207 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003208 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003209 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003210 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003211 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003212 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003213 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3214 // but we should wait on profiling data for that.
3215 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003216 auto &my_read = last_reads[my_read_index];
3217 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003218 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003219 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003220 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003221 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003222 my_read.pending_dep_chain = other_read.pending_dep_chain;
3223 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3224 // May require tracking more than one access per stage.
3225 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003226 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003227 // Since I'm overwriting the fragement stage read, also update the input attachment info
3228 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003229 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003230 }
John Zulauf14940722021-04-12 15:19:02 -06003231 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003232 // The read tags match so merge the barriers
3233 my_read.barriers |= other_read.barriers;
3234 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003235 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003236
John Zulauf5f13a792020-03-10 07:31:21 -06003237 break;
3238 }
3239 }
3240 } else {
3241 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003242 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003243 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003244 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003245 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003246 }
John Zulauf5f13a792020-03-10 07:31:21 -06003247 }
3248 }
John Zulauf361fb532020-07-22 10:45:39 -06003249 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003250 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3251 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003252
3253 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3254 // of the copy and other into this using the update first logic.
3255 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3256 // of the other first_accesses... )
3257 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3258 FirstAccesses firsts(std::move(first_accesses_));
3259 first_accesses_.clear();
3260 first_read_stages_ = 0U;
3261 auto a = firsts.begin();
3262 auto a_end = firsts.end();
3263 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003264 // TODO: Determine whether some tag offset will be needed for PHASE II
3265 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003266 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3267 ++a;
3268 }
3269 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3270 }
3271 for (; a != a_end; ++a) {
3272 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3273 }
3274 }
John Zulauf5f13a792020-03-10 07:31:21 -06003275}
3276
John Zulauf14940722021-04-12 15:19:02 -06003277void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003278 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3279 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003280 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003281 // Mulitple outstanding reads may be of interest and do dependency chains independently
3282 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3283 const auto usage_stage = PipelineStageBit(usage_index);
3284 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003285 for (auto &read_access : last_reads) {
3286 if (read_access.stage == usage_stage) {
3287 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003288 break;
3289 }
3290 }
3291 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003292 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003293 last_read_stages |= usage_stage;
3294 }
John Zulauf4285ee92020-09-23 10:20:52 -06003295
3296 // 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 -07003297 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003298 // TODO Revisit re: multiple reads for a given stage
3299 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003300 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003301 } else {
3302 // Assume write
3303 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003304 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003305 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003306 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003307}
John Zulauf5f13a792020-03-10 07:31:21 -06003308
John Zulauf89311b42020-09-29 16:28:47 -06003309// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3310// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3311// We can overwrite them as *this* write is now after them.
3312//
3313// 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 -06003314void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003315 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003316 last_read_stages = 0;
3317 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003318 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003319
3320 write_barriers = 0;
3321 write_dependency_chain = 0;
3322 write_tag = tag;
3323 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003324}
3325
John Zulauf89311b42020-09-29 16:28:47 -06003326// Apply the memory barrier without updating the existing barriers. The execution barrier
3327// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3328// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3329// replace the current write barriers or add to them, so accumulate to pending as well.
3330void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3331 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3332 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003333 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3334 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3335 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3336 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003337 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003338 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003339 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003340 if (layout_transition) {
3341 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3342 }
John Zulaufa0a98292020-09-18 09:30:10 -06003343 }
John Zulauf89311b42020-09-29 16:28:47 -06003344 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3345 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003346
John Zulauf89311b42020-09-29 16:28:47 -06003347 if (!pending_layout_transition) {
3348 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3349 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003350 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003351 // 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 -07003352 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3353 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003354 }
3355 }
John Zulaufa0a98292020-09-18 09:30:10 -06003356 }
John Zulaufa0a98292020-09-18 09:30:10 -06003357}
3358
John Zulauf4a6105a2020-11-17 15:11:05 -07003359// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3360// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003361void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003362 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3363 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3364 // in order to know if it's in the excecution scope
3365 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3366 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3367 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003368 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003369 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003370 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003371 if (layout_transition) {
3372 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3373 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003374 }
3375 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3376 pending_layout_transition |= layout_transition;
3377
3378 if (!pending_layout_transition) {
3379 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3380 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003381 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003382 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3383 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3384 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3385 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3386 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3387 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3388 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003389 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 -07003390 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003391 }
3392 }
3393 }
3394}
John Zulauf14940722021-04-12 15:19:02 -06003395void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003396 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003397 // 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 -06003398 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003399 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003400 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3401 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003402 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003403 }
John Zulauf89311b42020-09-29 16:28:47 -06003404
3405 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003406 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003407 for (auto &read_access : last_reads) {
3408 read_access.barriers |= read_access.pending_dep_chain;
3409 read_execution_barriers |= read_access.barriers;
3410 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003411 }
3412
3413 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3414 write_dependency_chain |= pending_write_dep_chain;
3415 write_barriers |= pending_write_barriers;
3416 pending_write_dep_chain = 0;
3417 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003418}
3419
John Zulaufae842002021-04-15 18:20:55 -06003420bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3421 if (!first_accesses_.size()) return false;
3422 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3423 return tag_range.intersects(first_access_range);
3424}
3425
John Zulauf59e25072020-07-17 10:55:21 -06003426// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003427VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3428 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003429
John Zulaufab7756b2020-12-29 16:10:16 -07003430 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003431 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003432 barriers = read_access.barriers;
3433 break;
John Zulauf59e25072020-07-17 10:55:21 -06003434 }
3435 }
John Zulauf4285ee92020-09-23 10:20:52 -06003436
John Zulauf59e25072020-07-17 10:55:21 -06003437 return barriers;
3438}
3439
Jeremy Gebben40a22942020-12-22 14:22:06 -07003440inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003441 assert(IsRead(usage));
3442 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3443 // * the previous reads are not hazards, and thus last_write must be visible and available to
3444 // any reads that happen after.
3445 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3446 // 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 -07003447 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003448}
3449
Jeremy Gebben40a22942020-12-22 14:22:06 -07003450VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003451 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003452 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003453 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003454 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003455 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003456 // 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 -07003457 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003458 }
3459
3460 return ordered_stages;
3461}
3462
John Zulauf14940722021-04-12 15:19:02 -06003463void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003464 // Only record until we record a write.
3465 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003466 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003467 if (0 == (usage_stage & first_read_stages_)) {
3468 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003469 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003470 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003471 if (0 == (read_execution_barriers & usage_stage)) {
3472 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3473 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3474 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003475 }
3476 }
3477}
3478
John Zulauf4fa68462021-04-26 21:04:22 -06003479void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3480 // Only call this after recording an image layout transition
3481 assert(first_accesses_.size());
3482 if (first_accesses_.back().tag == tag) {
3483 // 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 +02003484 assert(first_accesses_.back().usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
John Zulauf4fa68462021-04-26 21:04:22 -06003485 first_write_layout_ordering_ = layout_ordering;
3486 }
3487}
3488
John Zulaufee984022022-04-13 16:39:50 -06003489void ResourceAccessState::ReadState::Set(VkPipelineStageFlags2KHR stage_, const SyncStageAccessFlags &access_,
3490 VkPipelineStageFlags2KHR barriers_, ResourceUsageTag tag_) {
3491 stage = stage_;
3492 access = access_;
3493 barriers = barriers_;
3494 tag = tag_;
3495 pending_dep_chain = 0; // If this is a new read, we aren't applying a barrier set.
3496}
3497
John Zulaufea943c52022-02-22 11:05:17 -07003498std::shared_ptr<CommandBufferAccessContext> SyncValidator::AccessContextFactory(VkCommandBuffer command_buffer) {
3499 // If we don't have one, make it.
3500 auto cb_state = Get<CMD_BUFFER_STATE>(command_buffer);
3501 assert(cb_state.get());
3502 auto queue_flags = cb_state->GetQueueFlags();
3503 return std::make_shared<CommandBufferAccessContext>(*this, cb_state, queue_flags);
3504}
3505
3506inline std::shared_ptr<CommandBufferAccessContext> SyncValidator::GetAccessContextShared(VkCommandBuffer command_buffer) {
3507 return GetMappedInsert(cb_access_state, command_buffer,
3508 [this, command_buffer]() { return AccessContextFactory(command_buffer); });
3509}
3510
3511std::shared_ptr<const CommandBufferAccessContext> SyncValidator::GetAccessContextShared(VkCommandBuffer command_buffer) const {
3512 return GetMapped(cb_access_state, command_buffer, []() { return std::shared_ptr<CommandBufferAccessContext>(); });
3513}
3514
3515const CommandBufferAccessContext *SyncValidator::GetAccessContext(VkCommandBuffer command_buffer) const {
3516 return GetMappedPlainFromShared(cb_access_state, command_buffer);
3517}
3518
3519CommandBufferAccessContext *SyncValidator::GetAccessContext(VkCommandBuffer command_buffer) {
3520 return GetAccessContextShared(command_buffer).get();
3521}
3522
3523CommandBufferAccessContext *SyncValidator::GetAccessContextNoInsert(VkCommandBuffer command_buffer) {
3524 return GetMappedPlainFromShared(cb_access_state, command_buffer);
3525}
3526
John Zulaufd1f85d42020-04-15 12:23:15 -06003527void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003528 auto *access_context = GetAccessContextNoInsert(command_buffer);
3529 if (access_context) {
3530 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003531 }
3532}
3533
John Zulaufd1f85d42020-04-15 12:23:15 -06003534void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3535 auto access_found = cb_access_state.find(command_buffer);
3536 if (access_found != cb_access_state.end()) {
3537 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003538 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003539 cb_access_state.erase(access_found);
3540 }
3541}
3542
John Zulauf9cb530d2019-09-30 14:14:10 -06003543bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3544 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3545 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003546 const auto *cb_context = GetAccessContext(commandBuffer);
3547 assert(cb_context);
3548 if (!cb_context) return skip;
3549 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003550
John Zulauf3d84f1b2020-03-09 13:33:25 -06003551 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003552 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3553 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003554
3555 for (uint32_t region = 0; region < regionCount; region++) {
3556 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003557 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003558 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003559 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003560 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003561 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003562 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003563 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06003564 cb_context->FormatHazard(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003565 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003566 }
John Zulauf16adfc92020-04-08 10:28:33 -06003567 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003568 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003569 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003570 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003571 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003572 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003573 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06003574 cb_context->FormatHazard(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003575 }
3576 }
3577 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003578 }
3579 return skip;
3580}
3581
3582void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3583 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003584 auto *cb_context = GetAccessContext(commandBuffer);
3585 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003586 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003587 auto *context = cb_context->GetCurrentAccessContext();
3588
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003589 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3590 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003591
3592 for (uint32_t region = 0; region < regionCount; region++) {
3593 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003594 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003595 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003596 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003597 }
John Zulauf16adfc92020-04-08 10:28:33 -06003598 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003599 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003600 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003601 }
3602 }
3603}
3604
John Zulauf4a6105a2020-11-17 15:11:05 -07003605void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3606 // Clear out events from the command buffer contexts
3607 for (auto &cb_context : cb_access_state) {
3608 cb_context.second->RecordDestroyEvent(event);
3609 }
3610}
3611
Tony-LunarGef035472021-11-02 10:23:33 -06003612bool SyncValidator::ValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos,
3613 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003614 bool skip = false;
3615 const auto *cb_context = GetAccessContext(commandBuffer);
3616 assert(cb_context);
3617 if (!cb_context) return skip;
3618 const auto *context = cb_context->GetCurrentAccessContext();
Tony-LunarGef035472021-11-02 10:23:33 -06003619 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003620
3621 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003622 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3623 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003624
3625 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3626 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3627 if (src_buffer) {
3628 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003629 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003630 if (hazard.hazard) {
3631 // TODO -- add tag information to log msg when useful.
3632 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003633 "%s(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003634 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06003635 region, cb_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003636 }
3637 }
3638 if (dst_buffer && !skip) {
3639 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003640 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003641 if (hazard.hazard) {
3642 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003643 "%s(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003644 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06003645 region, cb_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003646 }
3647 }
3648 if (skip) break;
3649 }
3650 return skip;
3651}
3652
Tony-LunarGef035472021-11-02 10:23:33 -06003653bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3654 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3655 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3656}
3657
3658bool SyncValidator::PreCallValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) const {
3659 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3660}
3661
3662void SyncValidator::RecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003663 auto *cb_context = GetAccessContext(commandBuffer);
3664 assert(cb_context);
Tony-LunarGef035472021-11-02 10:23:33 -06003665 const auto tag = cb_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003666 auto *context = cb_context->GetCurrentAccessContext();
3667
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003668 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3669 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003670
3671 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3672 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3673 if (src_buffer) {
3674 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003675 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003676 }
3677 if (dst_buffer) {
3678 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003679 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003680 }
3681 }
3682}
3683
Tony-LunarGef035472021-11-02 10:23:33 -06003684void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3685 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3686}
3687
3688void SyncValidator::PreCallRecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) {
3689 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3690}
3691
John Zulauf5c5e88d2019-12-26 11:22:02 -07003692bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3693 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3694 const VkImageCopy *pRegions) const {
3695 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003696 const auto *cb_access_context = GetAccessContext(commandBuffer);
3697 assert(cb_access_context);
3698 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003699
John Zulauf3d84f1b2020-03-09 13:33:25 -06003700 const auto *context = cb_access_context->GetCurrentAccessContext();
3701 assert(context);
3702 if (!context) return skip;
3703
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003704 auto src_image = Get<IMAGE_STATE>(srcImage);
3705 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003706 for (uint32_t region = 0; region < regionCount; region++) {
3707 const auto &copy_region = pRegions[region];
3708 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003709 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003710 copy_region.srcOffset, copy_region.extent);
3711 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003712 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003713 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003714 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06003715 cb_access_context->FormatHazard(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003716 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003717 }
3718
3719 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003720 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003721 copy_region.dstOffset, copy_region.extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003722 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003723 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003724 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003725 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06003726 cb_access_context->FormatHazard(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003727 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003728 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003729 }
3730 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003731
John Zulauf5c5e88d2019-12-26 11:22:02 -07003732 return skip;
3733}
3734
3735void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3736 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3737 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003738 auto *cb_access_context = GetAccessContext(commandBuffer);
3739 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003740 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003741 auto *context = cb_access_context->GetCurrentAccessContext();
3742 assert(context);
3743
Jeremy Gebben9f537102021-10-05 16:37:12 -06003744 auto src_image = Get<IMAGE_STATE>(srcImage);
3745 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003746
3747 for (uint32_t region = 0; region < regionCount; region++) {
3748 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003749 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003750 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003751 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003752 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003753 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003754 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003755 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003756 }
3757 }
3758}
3759
Tony-LunarGb61514a2021-11-02 12:36:51 -06003760bool SyncValidator::ValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo,
3761 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003762 bool skip = false;
3763 const auto *cb_access_context = GetAccessContext(commandBuffer);
3764 assert(cb_access_context);
3765 if (!cb_access_context) return skip;
3766
3767 const auto *context = cb_access_context->GetCurrentAccessContext();
3768 assert(context);
3769 if (!context) return skip;
3770
Tony-LunarGb61514a2021-11-02 12:36:51 -06003771 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003772 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3773 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003774
Jeff Leger178b1e52020-10-05 12:22:23 -04003775 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3776 const auto &copy_region = pCopyImageInfo->pRegions[region];
3777 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003778 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003779 copy_region.srcOffset, copy_region.extent);
3780 if (hazard.hazard) {
3781 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003782 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003783 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06003784 region, cb_access_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003785 }
3786 }
3787
3788 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003789 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003790 copy_region.dstOffset, copy_region.extent);
Jeff Leger178b1e52020-10-05 12:22:23 -04003791 if (hazard.hazard) {
3792 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003793 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003794 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06003795 region, cb_access_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003796 }
3797 if (skip) break;
3798 }
3799 }
3800
3801 return skip;
3802}
3803
Tony-LunarGb61514a2021-11-02 12:36:51 -06003804bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3805 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3806 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3807}
3808
3809bool SyncValidator::PreCallValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) const {
3810 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3811}
3812
3813void SyncValidator::RecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003814 auto *cb_access_context = GetAccessContext(commandBuffer);
3815 assert(cb_access_context);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003816 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003817 auto *context = cb_access_context->GetCurrentAccessContext();
3818 assert(context);
3819
Jeremy Gebben9f537102021-10-05 16:37:12 -06003820 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3821 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04003822
3823 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3824 const auto &copy_region = pCopyImageInfo->pRegions[region];
3825 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003826 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003827 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003828 }
3829 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003830 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003831 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003832 }
3833 }
3834}
3835
Tony-LunarGb61514a2021-11-02 12:36:51 -06003836void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3837 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3838}
3839
3840void SyncValidator::PreCallRecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) {
3841 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3842}
3843
John Zulauf9cb530d2019-09-30 14:14:10 -06003844bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3845 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3846 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3847 uint32_t bufferMemoryBarrierCount,
3848 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3849 uint32_t imageMemoryBarrierCount,
3850 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3851 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003852 const auto *cb_access_context = GetAccessContext(commandBuffer);
3853 assert(cb_access_context);
3854 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003855
John Zulauf36ef9282021-02-02 11:47:24 -07003856 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3857 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3858 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3859 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003860 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003861 return skip;
3862}
3863
3864void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3865 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3866 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3867 uint32_t bufferMemoryBarrierCount,
3868 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3869 uint32_t imageMemoryBarrierCount,
3870 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003871 auto *cb_access_context = GetAccessContext(commandBuffer);
3872 assert(cb_access_context);
3873 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003874
John Zulauf1bf30522021-09-03 15:39:06 -06003875 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(),
3876 srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount,
3877 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3878 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06003879}
3880
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003881bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3882 const VkDependencyInfoKHR *pDependencyInfo) const {
3883 bool skip = false;
3884 const auto *cb_access_context = GetAccessContext(commandBuffer);
3885 assert(cb_access_context);
3886 if (!cb_access_context) return skip;
3887
3888 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3889 skip = pipeline_barrier.Validate(*cb_access_context);
3890 return skip;
3891}
3892
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003893bool SyncValidator::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
3894 const VkDependencyInfo *pDependencyInfo) const {
3895 bool skip = false;
3896 const auto *cb_access_context = GetAccessContext(commandBuffer);
3897 assert(cb_access_context);
3898 if (!cb_access_context) return skip;
3899
3900 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3901 skip = pipeline_barrier.Validate(*cb_access_context);
3902 return skip;
3903}
3904
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003905void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3906 auto *cb_access_context = GetAccessContext(commandBuffer);
3907 assert(cb_access_context);
3908 if (!cb_access_context) return;
3909
John Zulauf1bf30522021-09-03 15:39:06 -06003910 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(),
3911 *pDependencyInfo);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003912}
3913
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003914void SyncValidator::PreCallRecordCmdPipelineBarrier2(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) {
3915 auto *cb_access_context = GetAccessContext(commandBuffer);
3916 assert(cb_access_context);
3917 if (!cb_access_context) return;
3918
3919 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(),
3920 *pDependencyInfo);
3921}
3922
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003923void SyncValidator::CreateDevice(const VkDeviceCreateInfo *pCreateInfo) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003924 // The state tracker sets up the device state
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003925 StateTracker::CreateDevice(pCreateInfo);
John Zulauf9cb530d2019-09-30 14:14:10 -06003926
John Zulauf5f13a792020-03-10 07:31:21 -06003927 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3928 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003929 // TODO: Find a good way to do this hooklessly.
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003930 SetCommandBufferResetCallback([this](VkCommandBuffer command_buffer) -> void { ResetCommandBufferCallback(command_buffer); });
3931 SetCommandBufferFreeCallback([this](VkCommandBuffer command_buffer) -> void { FreeCommandBufferCallback(command_buffer); });
John Zulauf9cb530d2019-09-30 14:14:10 -06003932}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003933
John Zulauf355e49b2020-04-24 15:11:15 -06003934bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003935 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003936 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003937 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003938 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003939 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003940 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003941 }
John Zulauf355e49b2020-04-24 15:11:15 -06003942 return skip;
3943}
3944
3945bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3946 VkSubpassContents contents) const {
3947 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003948 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003949 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003950 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003951 return skip;
3952}
3953
3954bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003955 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003956 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003957 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003958 return skip;
3959}
3960
3961bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3962 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::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003965 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003966 return skip;
3967}
3968
John Zulauf3d84f1b2020-03-09 13:33:25 -06003969void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3970 VkResult result) {
3971 // The state tracker sets up the command buffer state
3972 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3973
3974 // Create/initialize the structure that trackers accesses at the command buffer scope.
3975 auto cb_access_context = GetAccessContext(commandBuffer);
3976 assert(cb_access_context);
3977 cb_access_context->Reset();
3978}
3979
3980void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003981 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003982 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003983 if (cb_context) {
John Zulaufbb890452021-12-14 11:30:18 -07003984 cb_context->RecordSyncOp<SyncOpBeginRenderPass>(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003985 }
3986}
3987
3988void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3989 VkSubpassContents contents) {
3990 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003991 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003992 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003993 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003994}
3995
3996void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3997 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3998 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003999 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004000}
4001
4002void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
4003 const VkRenderPassBeginInfo *pRenderPassBegin,
4004 const VkSubpassBeginInfo *pSubpassBeginInfo) {
4005 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07004006 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06004007}
4008
Mike Schuchardt2df08912020-12-15 16:28:09 -08004009bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07004010 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004011 bool skip = false;
4012
4013 auto cb_context = GetAccessContext(commandBuffer);
4014 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07004015 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07004016 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07004017 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06004018}
4019
4020bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
4021 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07004022 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07004023 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06004024 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07004025 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
4026 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06004027 return skip;
4028}
4029
Mike Schuchardt2df08912020-12-15 16:28:09 -08004030bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
4031 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004032 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07004033 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06004034 return skip;
4035}
4036
4037bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
4038 const VkSubpassEndInfo *pSubpassEndInfo) const {
4039 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07004040 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06004041 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06004042}
4043
4044void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07004045 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06004046 auto cb_context = GetAccessContext(commandBuffer);
4047 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07004048 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06004049
John Zulaufbb890452021-12-14 11:30:18 -07004050 cb_context->RecordSyncOp<SyncOpNextSubpass>(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004051}
4052
4053void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
4054 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07004055 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06004056 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06004057 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004058}
4059
4060void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
4061 const VkSubpassEndInfo *pSubpassEndInfo) {
4062 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06004063 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004064}
4065
4066void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
4067 const VkSubpassEndInfo *pSubpassEndInfo) {
4068 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07004069 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004070}
4071
sfricke-samsung85584a72021-09-30 21:43:38 -07004072bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
4073 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004074 bool skip = false;
4075
4076 auto cb_context = GetAccessContext(commandBuffer);
4077 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07004078 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06004079
sfricke-samsung85584a72021-09-30 21:43:38 -07004080 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07004081 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06004082 return skip;
4083}
4084
4085bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
4086 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07004087 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06004088 return skip;
4089}
4090
Mike Schuchardt2df08912020-12-15 16:28:09 -08004091bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004092 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07004093 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06004094 return skip;
4095}
4096
4097bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08004098 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004099 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07004100 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06004101 return skip;
4102}
4103
sfricke-samsung85584a72021-09-30 21:43:38 -07004104void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06004105 // Resolve the all subpass contexts to the command buffer contexts
4106 auto cb_context = GetAccessContext(commandBuffer);
4107 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07004108 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06004109
John Zulaufbb890452021-12-14 11:30:18 -07004110 cb_context->RecordSyncOp<SyncOpEndRenderPass>(cmd, *this, pSubpassEndInfo);
John Zulaufe5da6e52020-03-18 15:32:18 -06004111}
John Zulauf3d84f1b2020-03-09 13:33:25 -06004112
John Zulauf33fc1d52020-07-17 11:01:10 -06004113// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
4114// updates to a resource which do not conflict at the byte level.
4115// TODO: Revisit this rule to see if it needs to be tighter or looser
4116// TODO: Add programatic control over suppression heuristics
4117bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
4118 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
4119}
4120
John Zulauf3d84f1b2020-03-09 13:33:25 -06004121void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004122 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06004123 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004124}
4125
4126void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004127 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004128 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004129}
4130
4131void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07004132 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06004133 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004134}
locke-lunarga19c71d2020-03-02 18:17:04 -07004135
sfricke-samsung71f04e32022-03-16 01:21:21 -05004136template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004137bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004138 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4139 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004140 bool skip = false;
4141 const auto *cb_access_context = GetAccessContext(commandBuffer);
4142 assert(cb_access_context);
4143 if (!cb_access_context) return skip;
4144
Tony Barbour845d29b2021-11-09 11:43:14 -07004145 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004146
locke-lunarga19c71d2020-03-02 18:17:04 -07004147 const auto *context = cb_access_context->GetCurrentAccessContext();
4148 assert(context);
4149 if (!context) return skip;
4150
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004151 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4152 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004153
4154 for (uint32_t region = 0; region < regionCount; region++) {
4155 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07004156 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07004157 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004158 if (src_buffer) {
4159 ResourceAccessRange src_range =
4160 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004161 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07004162 if (hazard.hazard) {
4163 // PHASE1 TODO -- add tag information to log msg when useful.
4164 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
4165 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4166 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004167 cb_access_context->FormatHazard(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004168 }
4169 }
4170
Jeremy Gebben40a22942020-12-22 14:22:06 -07004171 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07004172 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004173 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004174 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004175 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004176 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004177 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004178 }
4179 if (skip) break;
4180 }
4181 if (skip) break;
4182 }
4183 return skip;
4184}
4185
Jeff Leger178b1e52020-10-05 12:22:23 -04004186bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4187 VkImageLayout dstImageLayout, uint32_t regionCount,
4188 const VkBufferImageCopy *pRegions) const {
4189 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
Tony Barbour845d29b2021-11-09 11:43:14 -07004190 CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004191}
4192
4193bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4194 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4195 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4196 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004197 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4198}
4199
4200bool SyncValidator::PreCallValidateCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4201 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) const {
4202 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4203 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4204 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004205}
4206
sfricke-samsung71f04e32022-03-16 01:21:21 -05004207template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004208void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004209 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4210 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004211 auto *cb_access_context = GetAccessContext(commandBuffer);
4212 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004213
Jeff Leger178b1e52020-10-05 12:22:23 -04004214 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004215 auto *context = cb_access_context->GetCurrentAccessContext();
4216 assert(context);
4217
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004218 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4219 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004220
4221 for (uint32_t region = 0; region < regionCount; region++) {
4222 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07004223 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004224 if (src_buffer) {
4225 ResourceAccessRange src_range =
4226 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004227 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004228 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07004229 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004230 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004231 }
4232 }
4233}
4234
Jeff Leger178b1e52020-10-05 12:22:23 -04004235void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4236 VkImageLayout dstImageLayout, uint32_t regionCount,
4237 const VkBufferImageCopy *pRegions) {
4238 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
Tony Barbour845d29b2021-11-09 11:43:14 -07004239 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004240}
4241
4242void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4243 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4244 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4245 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4246 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004247 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4248}
4249
4250void SyncValidator::PreCallRecordCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4251 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) {
4252 StateTracker::PreCallRecordCmdCopyBufferToImage2(commandBuffer, pCopyBufferToImageInfo);
4253 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4254 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4255 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004256}
4257
sfricke-samsung71f04e32022-03-16 01:21:21 -05004258template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004259bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004260 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
4261 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004262 bool skip = false;
4263 const auto *cb_access_context = GetAccessContext(commandBuffer);
4264 assert(cb_access_context);
4265 if (!cb_access_context) return skip;
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004266 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004267
locke-lunarga19c71d2020-03-02 18:17:04 -07004268 const auto *context = cb_access_context->GetCurrentAccessContext();
4269 assert(context);
4270 if (!context) return skip;
4271
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004272 auto src_image = Get<IMAGE_STATE>(srcImage);
4273 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004274 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004275 for (uint32_t region = 0; region < regionCount; region++) {
4276 const auto &copy_region = pRegions[region];
4277 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004278 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004279 copy_region.imageOffset, copy_region.imageExtent);
4280 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004281 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004282 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004283 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004284 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004285 }
John Zulauf477700e2021-01-06 11:41:49 -07004286 if (dst_mem) {
4287 ResourceAccessRange dst_range =
4288 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004289 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004290 if (hazard.hazard) {
4291 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4292 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4293 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004294 cb_access_context->FormatHazard(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004295 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004296 }
4297 }
4298 if (skip) break;
4299 }
4300 return skip;
4301}
4302
Jeff Leger178b1e52020-10-05 12:22:23 -04004303bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4304 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4305 const VkBufferImageCopy *pRegions) const {
4306 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004307 CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004308}
4309
4310bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4311 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4312 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4313 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004314 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4315}
4316
4317bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4318 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) const {
4319 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4320 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4321 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004322}
4323
sfricke-samsung71f04e32022-03-16 01:21:21 -05004324template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004325void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004326 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004327 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004328 auto *cb_access_context = GetAccessContext(commandBuffer);
4329 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004330
Jeff Leger178b1e52020-10-05 12:22:23 -04004331 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004332 auto *context = cb_access_context->GetCurrentAccessContext();
4333 assert(context);
4334
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004335 auto src_image = Get<IMAGE_STATE>(srcImage);
Jeremy Gebben9f537102021-10-05 16:37:12 -06004336 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004337 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004338 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004339
4340 for (uint32_t region = 0; region < regionCount; region++) {
4341 const auto &copy_region = pRegions[region];
4342 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004343 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004344 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004345 if (dst_buffer) {
4346 ResourceAccessRange dst_range =
4347 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004348 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004349 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004350 }
4351 }
4352}
4353
Jeff Leger178b1e52020-10-05 12:22:23 -04004354void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4355 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4356 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004357 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004358}
4359
4360void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4361 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4362 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4363 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4364 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004365 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4366}
4367
4368void SyncValidator::PreCallRecordCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4369 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) {
4370 StateTracker::PreCallRecordCmdCopyImageToBuffer2(commandBuffer, pCopyImageToBufferInfo);
4371 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4372 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4373 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004374}
4375
4376template <typename RegionType>
4377bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4378 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4379 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004380 bool skip = false;
4381 const auto *cb_access_context = GetAccessContext(commandBuffer);
4382 assert(cb_access_context);
4383 if (!cb_access_context) return skip;
4384
4385 const auto *context = cb_access_context->GetCurrentAccessContext();
4386 assert(context);
4387 if (!context) return skip;
4388
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004389 auto src_image = Get<IMAGE_STATE>(srcImage);
4390 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004391
4392 for (uint32_t region = 0; region < regionCount; region++) {
4393 const auto &blit_region = pRegions[region];
4394 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004395 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4396 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4397 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4398 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4399 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4400 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004401 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004402 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004403 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004404 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004405 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004406 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004407 }
4408 }
4409
4410 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004411 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4412 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4413 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4414 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4415 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4416 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004417 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004418 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004419 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004420 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004421 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06004422 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004423 }
4424 if (skip) break;
4425 }
4426 }
4427
4428 return skip;
4429}
4430
Jeff Leger178b1e52020-10-05 12:22:23 -04004431bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4432 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4433 const VkImageBlit *pRegions, VkFilter filter) const {
4434 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4435 "vkCmdBlitImage");
4436}
4437
4438bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4439 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4440 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4441 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4442 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4443}
4444
Tony-LunarG542ae912021-11-04 16:06:44 -06004445bool SyncValidator::PreCallValidateCmdBlitImage2(VkCommandBuffer commandBuffer,
4446 const VkBlitImageInfo2 *pBlitImageInfo) const {
4447 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4448 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4449 pBlitImageInfo->filter, "vkCmdBlitImage2");
4450}
4451
Jeff Leger178b1e52020-10-05 12:22:23 -04004452template <typename RegionType>
4453void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4454 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4455 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004456 auto *cb_access_context = GetAccessContext(commandBuffer);
4457 assert(cb_access_context);
4458 auto *context = cb_access_context->GetCurrentAccessContext();
4459 assert(context);
4460
Jeremy Gebben9f537102021-10-05 16:37:12 -06004461 auto src_image = Get<IMAGE_STATE>(srcImage);
4462 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004463
4464 for (uint32_t region = 0; region < regionCount; region++) {
4465 const auto &blit_region = pRegions[region];
4466 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004467 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4468 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4469 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4470 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4471 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4472 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004473 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004474 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004475 }
4476 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004477 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4478 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4479 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4480 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4481 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4482 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004483 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004484 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004485 }
4486 }
4487}
locke-lunarg36ba2592020-04-03 09:42:04 -06004488
Jeff Leger178b1e52020-10-05 12:22:23 -04004489void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4490 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4491 const VkImageBlit *pRegions, VkFilter filter) {
4492 auto *cb_access_context = GetAccessContext(commandBuffer);
4493 assert(cb_access_context);
4494 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4495 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4496 pRegions, filter);
4497 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4498}
4499
4500void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4501 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4502 auto *cb_access_context = GetAccessContext(commandBuffer);
4503 assert(cb_access_context);
4504 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4505 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4506 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4507 pBlitImageInfo->filter, tag);
4508}
4509
Tony-LunarG542ae912021-11-04 16:06:44 -06004510void SyncValidator::PreCallRecordCmdBlitImage2(VkCommandBuffer commandBuffer, const VkBlitImageInfo2 *pBlitImageInfo) {
4511 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4512 auto *cb_access_context = GetAccessContext(commandBuffer);
4513 assert(cb_access_context);
4514 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2);
4515 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4516 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4517 pBlitImageInfo->filter, tag);
4518}
4519
John Zulauffaea0ee2021-01-14 14:01:32 -07004520bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4521 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4522 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4523 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004524 bool skip = false;
4525 if (drawCount == 0) return skip;
4526
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004527 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004528 VkDeviceSize size = struct_size;
4529 if (drawCount == 1 || stride == size) {
4530 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004531 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004532 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4533 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004534 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004535 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004536 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06004537 cb_context.FormatHazard(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004538 }
4539 } else {
4540 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004541 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004542 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4543 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004544 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004545 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4546 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06004547 cb_context.FormatHazard(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004548 break;
4549 }
4550 }
4551 }
4552 return skip;
4553}
4554
John Zulauf14940722021-04-12 15:19:02 -06004555void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004556 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4557 uint32_t stride) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004558 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004559 VkDeviceSize size = struct_size;
4560 if (drawCount == 1 || stride == size) {
4561 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004562 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004563 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004564 } else {
4565 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004566 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004567 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4568 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004569 }
4570 }
4571}
4572
John Zulauffaea0ee2021-01-14 14:01:32 -07004573bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4574 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4575 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004576 bool skip = false;
4577
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004578 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004579 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004580 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4581 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004582 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004583 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004584 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06004585 cb_context.FormatHazard(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004586 }
4587 return skip;
4588}
4589
John Zulauf14940722021-04-12 15:19:02 -06004590void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004591 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004592 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004593 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004594}
4595
locke-lunarg36ba2592020-04-03 09:42:04 -06004596bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004597 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004598 const auto *cb_access_context = GetAccessContext(commandBuffer);
4599 assert(cb_access_context);
4600 if (!cb_access_context) return skip;
4601
locke-lunarg61870c22020-06-09 14:51:50 -06004602 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004603 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004604}
4605
4606void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004607 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004608 auto *cb_access_context = GetAccessContext(commandBuffer);
4609 assert(cb_access_context);
4610 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004611
locke-lunarg61870c22020-06-09 14:51:50 -06004612 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004613}
locke-lunarge1a67022020-04-29 00:15:36 -06004614
4615bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004616 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004617 const auto *cb_access_context = GetAccessContext(commandBuffer);
4618 assert(cb_access_context);
4619 if (!cb_access_context) return skip;
4620
4621 const auto *context = cb_access_context->GetCurrentAccessContext();
4622 assert(context);
4623 if (!context) return skip;
4624
locke-lunarg61870c22020-06-09 14:51:50 -06004625 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004626 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4627 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004628 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004629}
4630
4631void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004632 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004633 auto *cb_access_context = GetAccessContext(commandBuffer);
4634 assert(cb_access_context);
4635 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4636 auto *context = cb_access_context->GetCurrentAccessContext();
4637 assert(context);
4638
locke-lunarg61870c22020-06-09 14:51:50 -06004639 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4640 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004641}
4642
4643bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4644 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004645 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004646 const auto *cb_access_context = GetAccessContext(commandBuffer);
4647 assert(cb_access_context);
4648 if (!cb_access_context) return skip;
4649
locke-lunarg61870c22020-06-09 14:51:50 -06004650 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4651 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4652 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004653 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004654}
4655
4656void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4657 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004658 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004659 auto *cb_access_context = GetAccessContext(commandBuffer);
4660 assert(cb_access_context);
4661 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004662
locke-lunarg61870c22020-06-09 14:51:50 -06004663 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4664 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4665 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004666}
4667
4668bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4669 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004670 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004671 const auto *cb_access_context = GetAccessContext(commandBuffer);
4672 assert(cb_access_context);
4673 if (!cb_access_context) return skip;
4674
locke-lunarg61870c22020-06-09 14:51:50 -06004675 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4676 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4677 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004678 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004679}
4680
4681void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4682 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004683 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004684 auto *cb_access_context = GetAccessContext(commandBuffer);
4685 assert(cb_access_context);
4686 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004687
locke-lunarg61870c22020-06-09 14:51:50 -06004688 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4689 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4690 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004691}
4692
4693bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4694 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004695 bool skip = false;
4696 if (drawCount == 0) return skip;
4697
locke-lunargff255f92020-05-13 18:53:52 -06004698 const auto *cb_access_context = GetAccessContext(commandBuffer);
4699 assert(cb_access_context);
4700 if (!cb_access_context) return skip;
4701
4702 const auto *context = cb_access_context->GetCurrentAccessContext();
4703 assert(context);
4704 if (!context) return skip;
4705
locke-lunarg61870c22020-06-09 14:51:50 -06004706 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4707 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004708 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4709 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004710
4711 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4712 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4713 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004714 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004715 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004716}
4717
4718void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4719 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004720 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004721 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004722 auto *cb_access_context = GetAccessContext(commandBuffer);
4723 assert(cb_access_context);
4724 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4725 auto *context = cb_access_context->GetCurrentAccessContext();
4726 assert(context);
4727
locke-lunarg61870c22020-06-09 14:51:50 -06004728 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4729 cb_access_context->RecordDrawSubpassAttachment(tag);
4730 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004731
4732 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4733 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4734 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004735 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004736}
4737
4738bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4739 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004740 bool skip = false;
4741 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004742 const auto *cb_access_context = GetAccessContext(commandBuffer);
4743 assert(cb_access_context);
4744 if (!cb_access_context) return skip;
4745
4746 const auto *context = cb_access_context->GetCurrentAccessContext();
4747 assert(context);
4748 if (!context) return skip;
4749
locke-lunarg61870c22020-06-09 14:51:50 -06004750 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4751 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004752 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4753 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004754
4755 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4756 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4757 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004758 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004759 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004760}
4761
4762void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4763 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004764 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004765 auto *cb_access_context = GetAccessContext(commandBuffer);
4766 assert(cb_access_context);
4767 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4768 auto *context = cb_access_context->GetCurrentAccessContext();
4769 assert(context);
4770
locke-lunarg61870c22020-06-09 14:51:50 -06004771 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4772 cb_access_context->RecordDrawSubpassAttachment(tag);
4773 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004774
4775 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4776 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4777 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004778 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004779}
4780
4781bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4782 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4783 uint32_t stride, const char *function) const {
4784 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004785 const auto *cb_access_context = GetAccessContext(commandBuffer);
4786 assert(cb_access_context);
4787 if (!cb_access_context) return skip;
4788
4789 const auto *context = cb_access_context->GetCurrentAccessContext();
4790 assert(context);
4791 if (!context) return skip;
4792
locke-lunarg61870c22020-06-09 14:51:50 -06004793 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4794 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004795 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4796 maxDrawCount, stride, function);
4797 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004798
4799 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4800 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4801 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004802 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004803 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004804}
4805
4806bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4807 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4808 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004809 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4810 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004811}
4812
sfricke-samsung85584a72021-09-30 21:43:38 -07004813void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4814 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4815 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004816 auto *cb_access_context = GetAccessContext(commandBuffer);
4817 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004818 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004819 auto *context = cb_access_context->GetCurrentAccessContext();
4820 assert(context);
4821
locke-lunarg61870c22020-06-09 14:51:50 -06004822 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4823 cb_access_context->RecordDrawSubpassAttachment(tag);
4824 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4825 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004826
4827 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4828 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4829 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004830 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004831}
4832
sfricke-samsung85584a72021-09-30 21:43:38 -07004833void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4834 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4835 uint32_t stride) {
4836 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4837 stride);
4838 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4839 CMD_DRAWINDIRECTCOUNT);
4840}
locke-lunarge1a67022020-04-29 00:15:36 -06004841bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4842 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4843 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004844 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4845 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004846}
4847
4848void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4849 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4850 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004851 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4852 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004853 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4854 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004855}
4856
4857bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4858 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4859 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004860 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4861 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004862}
4863
4864void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4865 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4866 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004867 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4868 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004869 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4870 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004871}
4872
4873bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4874 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4875 uint32_t stride, const char *function) const {
4876 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004877 const auto *cb_access_context = GetAccessContext(commandBuffer);
4878 assert(cb_access_context);
4879 if (!cb_access_context) return skip;
4880
4881 const auto *context = cb_access_context->GetCurrentAccessContext();
4882 assert(context);
4883 if (!context) return skip;
4884
locke-lunarg61870c22020-06-09 14:51:50 -06004885 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4886 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004887 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4888 offset, maxDrawCount, stride, function);
4889 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004890
4891 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4892 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4893 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004894 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004895 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004896}
4897
4898bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4899 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4900 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004901 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4902 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004903}
4904
sfricke-samsung85584a72021-09-30 21:43:38 -07004905void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4906 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4907 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004908 auto *cb_access_context = GetAccessContext(commandBuffer);
4909 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004910 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004911 auto *context = cb_access_context->GetCurrentAccessContext();
4912 assert(context);
4913
locke-lunarg61870c22020-06-09 14:51:50 -06004914 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4915 cb_access_context->RecordDrawSubpassAttachment(tag);
4916 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4917 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004918
4919 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4920 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004921 // We will update the index and vertex buffer in SubmitQueue in the future.
4922 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004923}
4924
sfricke-samsung85584a72021-09-30 21:43:38 -07004925void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4926 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4927 uint32_t maxDrawCount, uint32_t stride) {
4928 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4929 maxDrawCount, stride);
4930 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4931 CMD_DRAWINDEXEDINDIRECTCOUNT);
4932}
4933
locke-lunarge1a67022020-04-29 00:15:36 -06004934bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4935 VkDeviceSize offset, VkBuffer countBuffer,
4936 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4937 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004938 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4939 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004940}
4941
4942void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4943 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4944 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004945 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4946 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004947 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4948 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004949}
4950
4951bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4952 VkDeviceSize offset, VkBuffer countBuffer,
4953 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4954 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004955 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4956 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004957}
4958
4959void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4960 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4961 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004962 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4963 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004964 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4965 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004966}
4967
4968bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4969 const VkClearColorValue *pColor, uint32_t rangeCount,
4970 const VkImageSubresourceRange *pRanges) const {
4971 bool skip = false;
4972 const auto *cb_access_context = GetAccessContext(commandBuffer);
4973 assert(cb_access_context);
4974 if (!cb_access_context) return skip;
4975
4976 const auto *context = cb_access_context->GetCurrentAccessContext();
4977 assert(context);
4978 if (!context) return skip;
4979
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004980 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004981
4982 for (uint32_t index = 0; index < rangeCount; index++) {
4983 const auto &range = pRanges[index];
4984 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004985 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004986 if (hazard.hazard) {
4987 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004988 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004989 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf397e68b2022-04-19 11:44:07 -06004990 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004991 }
4992 }
4993 }
4994 return skip;
4995}
4996
4997void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4998 const VkClearColorValue *pColor, uint32_t rangeCount,
4999 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005000 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06005001 auto *cb_access_context = GetAccessContext(commandBuffer);
5002 assert(cb_access_context);
5003 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
5004 auto *context = cb_access_context->GetCurrentAccessContext();
5005 assert(context);
5006
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005007 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06005008
5009 for (uint32_t index = 0; index < rangeCount; index++) {
5010 const auto &range = pRanges[index];
5011 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06005012 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005013 }
5014 }
5015}
5016
5017bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
5018 VkImageLayout imageLayout,
5019 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
5020 const VkImageSubresourceRange *pRanges) const {
5021 bool skip = false;
5022 const auto *cb_access_context = GetAccessContext(commandBuffer);
5023 assert(cb_access_context);
5024 if (!cb_access_context) return skip;
5025
5026 const auto *context = cb_access_context->GetCurrentAccessContext();
5027 assert(context);
5028 if (!context) return skip;
5029
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005030 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06005031
5032 for (uint32_t index = 0; index < rangeCount; index++) {
5033 const auto &range = pRanges[index];
5034 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06005035 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005036 if (hazard.hazard) {
5037 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005038 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005039 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauf397e68b2022-04-19 11:44:07 -06005040 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005041 }
5042 }
5043 }
5044 return skip;
5045}
5046
5047void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
5048 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
5049 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005050 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06005051 auto *cb_access_context = GetAccessContext(commandBuffer);
5052 assert(cb_access_context);
5053 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
5054 auto *context = cb_access_context->GetCurrentAccessContext();
5055 assert(context);
5056
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005057 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06005058
5059 for (uint32_t index = 0; index < rangeCount; index++) {
5060 const auto &range = pRanges[index];
5061 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06005062 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005063 }
5064 }
5065}
5066
5067bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
5068 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
5069 VkDeviceSize dstOffset, VkDeviceSize stride,
5070 VkQueryResultFlags flags) const {
5071 bool skip = false;
5072 const auto *cb_access_context = GetAccessContext(commandBuffer);
5073 assert(cb_access_context);
5074 if (!cb_access_context) return skip;
5075
5076 const auto *context = cb_access_context->GetCurrentAccessContext();
5077 assert(context);
5078 if (!context) return skip;
5079
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005080 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005081
5082 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005083 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005084 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005085 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005086 skip |=
5087 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5088 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf397e68b2022-04-19 11:44:07 -06005089 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005090 }
5091 }
locke-lunargff255f92020-05-13 18:53:52 -06005092
5093 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06005094 return skip;
5095}
5096
5097void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
5098 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5099 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005100 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
5101 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06005102 auto *cb_access_context = GetAccessContext(commandBuffer);
5103 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06005104 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06005105 auto *context = cb_access_context->GetCurrentAccessContext();
5106 assert(context);
5107
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005108 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005109
5110 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005111 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005112 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005113 }
locke-lunargff255f92020-05-13 18:53:52 -06005114
5115 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06005116}
5117
5118bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5119 VkDeviceSize size, uint32_t data) const {
5120 bool skip = false;
5121 const auto *cb_access_context = GetAccessContext(commandBuffer);
5122 assert(cb_access_context);
5123 if (!cb_access_context) return skip;
5124
5125 const auto *context = cb_access_context->GetCurrentAccessContext();
5126 assert(context);
5127 if (!context) return skip;
5128
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005129 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005130
5131 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005132 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005133 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005134 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005135 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005136 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf397e68b2022-04-19 11:44:07 -06005137 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005138 }
5139 }
5140 return skip;
5141}
5142
5143void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5144 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005145 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06005146 auto *cb_access_context = GetAccessContext(commandBuffer);
5147 assert(cb_access_context);
5148 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
5149 auto *context = cb_access_context->GetCurrentAccessContext();
5150 assert(context);
5151
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005152 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005153
5154 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005155 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005156 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005157 }
5158}
5159
5160bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5161 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5162 const VkImageResolve *pRegions) const {
5163 bool skip = false;
5164 const auto *cb_access_context = GetAccessContext(commandBuffer);
5165 assert(cb_access_context);
5166 if (!cb_access_context) return skip;
5167
5168 const auto *context = cb_access_context->GetCurrentAccessContext();
5169 assert(context);
5170 if (!context) return skip;
5171
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005172 auto src_image = Get<IMAGE_STATE>(srcImage);
5173 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005174
5175 for (uint32_t region = 0; region < regionCount; region++) {
5176 const auto &resolve_region = pRegions[region];
5177 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005178 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005179 resolve_region.srcOffset, resolve_region.extent);
5180 if (hazard.hazard) {
5181 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005182 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005183 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06005184 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005185 }
5186 }
5187
5188 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005189 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005190 resolve_region.dstOffset, resolve_region.extent);
5191 if (hazard.hazard) {
5192 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005193 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005194 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauf397e68b2022-04-19 11:44:07 -06005195 cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005196 }
5197 if (skip) break;
5198 }
5199 }
5200
5201 return skip;
5202}
5203
5204void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5205 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5206 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005207 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5208 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005209 auto *cb_access_context = GetAccessContext(commandBuffer);
5210 assert(cb_access_context);
5211 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5212 auto *context = cb_access_context->GetCurrentAccessContext();
5213 assert(context);
5214
Jeremy Gebben9f537102021-10-05 16:37:12 -06005215 auto src_image = Get<IMAGE_STATE>(srcImage);
5216 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005217
5218 for (uint32_t region = 0; region < regionCount; region++) {
5219 const auto &resolve_region = pRegions[region];
5220 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005221 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005222 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005223 }
5224 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005225 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005226 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005227 }
5228 }
5229}
5230
Tony-LunarG562fc102021-11-12 13:58:35 -07005231bool SyncValidator::ValidateCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5232 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04005233 bool skip = false;
5234 const auto *cb_access_context = GetAccessContext(commandBuffer);
5235 assert(cb_access_context);
5236 if (!cb_access_context) return skip;
5237
5238 const auto *context = cb_access_context->GetCurrentAccessContext();
5239 assert(context);
5240 if (!context) return skip;
5241
Tony-LunarG562fc102021-11-12 13:58:35 -07005242 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005243 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5244 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005245
5246 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5247 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5248 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005249 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005250 resolve_region.srcOffset, resolve_region.extent);
5251 if (hazard.hazard) {
5252 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005253 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005254 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06005255 region, cb_access_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005256 }
5257 }
5258
5259 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005260 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005261 resolve_region.dstOffset, resolve_region.extent);
5262 if (hazard.hazard) {
5263 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005264 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005265 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06005266 region, cb_access_context->FormatHazard(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005267 }
5268 if (skip) break;
5269 }
5270 }
5271
5272 return skip;
5273}
5274
Tony-LunarG562fc102021-11-12 13:58:35 -07005275bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5276 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5277 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5278}
5279
5280bool SyncValidator::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
5281 const VkResolveImageInfo2 *pResolveImageInfo) const {
5282 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5283}
5284
5285void SyncValidator::RecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5286 CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04005287 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5288 auto *cb_access_context = GetAccessContext(commandBuffer);
5289 assert(cb_access_context);
Tony-LunarG562fc102021-11-12 13:58:35 -07005290 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04005291 auto *context = cb_access_context->GetCurrentAccessContext();
5292 assert(context);
5293
Jeremy Gebben9f537102021-10-05 16:37:12 -06005294 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5295 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005296
5297 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5298 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5299 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005300 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005301 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005302 }
5303 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005304 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005305 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005306 }
5307 }
5308}
5309
Tony-LunarG562fc102021-11-12 13:58:35 -07005310void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5311 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5312 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5313}
5314
5315void SyncValidator::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2 *pResolveImageInfo) {
5316 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5317}
5318
locke-lunarge1a67022020-04-29 00:15:36 -06005319bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5320 VkDeviceSize dataSize, const void *pData) const {
5321 bool skip = false;
5322 const auto *cb_access_context = GetAccessContext(commandBuffer);
5323 assert(cb_access_context);
5324 if (!cb_access_context) return skip;
5325
5326 const auto *context = cb_access_context->GetCurrentAccessContext();
5327 assert(context);
5328 if (!context) return skip;
5329
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005330 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005331
5332 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005333 // VK_WHOLE_SIZE not allowed
5334 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005335 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005336 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005337 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005338 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf397e68b2022-04-19 11:44:07 -06005339 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatHazard(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005340 }
5341 }
5342 return skip;
5343}
5344
5345void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5346 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005347 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005348 auto *cb_access_context = GetAccessContext(commandBuffer);
5349 assert(cb_access_context);
5350 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5351 auto *context = cb_access_context->GetCurrentAccessContext();
5352 assert(context);
5353
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005354 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005355
5356 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005357 // VK_WHOLE_SIZE not allowed
5358 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005359 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005360 }
5361}
locke-lunargff255f92020-05-13 18:53:52 -06005362
5363bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5364 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5365 bool skip = false;
5366 const auto *cb_access_context = GetAccessContext(commandBuffer);
5367 assert(cb_access_context);
5368 if (!cb_access_context) return skip;
5369
5370 const auto *context = cb_access_context->GetCurrentAccessContext();
5371 assert(context);
5372 if (!context) return skip;
5373
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005374 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005375
5376 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005377 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005378 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005379 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005380 skip |=
5381 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5382 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauf397e68b2022-04-19 11:44:07 -06005383 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatHazard(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005384 }
5385 }
5386 return skip;
5387}
5388
5389void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5390 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005391 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005392 auto *cb_access_context = GetAccessContext(commandBuffer);
5393 assert(cb_access_context);
5394 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5395 auto *context = cb_access_context->GetCurrentAccessContext();
5396 assert(context);
5397
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005398 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005399
5400 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005401 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005402 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005403 }
5404}
John Zulauf49beb112020-11-04 16:06:31 -07005405
5406bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5407 bool skip = false;
5408 const auto *cb_context = GetAccessContext(commandBuffer);
5409 assert(cb_context);
5410 if (!cb_context) return skip;
5411
John Zulauf36ef9282021-02-02 11:47:24 -07005412 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005413 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005414}
5415
5416void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5417 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5418 auto *cb_context = GetAccessContext(commandBuffer);
5419 assert(cb_context);
5420 if (!cb_context) return;
John Zulauf1bf30522021-09-03 15:39:06 -06005421 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005422}
5423
John Zulauf4edde622021-02-15 08:54:50 -07005424bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5425 const VkDependencyInfoKHR *pDependencyInfo) const {
5426 bool skip = false;
5427 const auto *cb_context = GetAccessContext(commandBuffer);
5428 assert(cb_context);
5429 if (!cb_context || !pDependencyInfo) return skip;
5430
5431 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5432 return set_event_op.Validate(*cb_context);
5433}
5434
Tony-LunarGc43525f2021-11-15 16:12:38 -07005435bool SyncValidator::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5436 const VkDependencyInfo *pDependencyInfo) const {
5437 bool skip = false;
5438 const auto *cb_context = GetAccessContext(commandBuffer);
5439 assert(cb_context);
5440 if (!cb_context || !pDependencyInfo) return skip;
5441
5442 SyncOpSetEvent set_event_op(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5443 return set_event_op.Validate(*cb_context);
5444}
5445
John Zulauf4edde622021-02-15 08:54:50 -07005446void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5447 const VkDependencyInfoKHR *pDependencyInfo) {
5448 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5449 auto *cb_context = GetAccessContext(commandBuffer);
5450 assert(cb_context);
5451 if (!cb_context || !pDependencyInfo) return;
5452
John Zulauf1bf30522021-09-03 15:39:06 -06005453 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
John Zulauf4edde622021-02-15 08:54:50 -07005454}
5455
Tony-LunarGc43525f2021-11-15 16:12:38 -07005456void SyncValidator::PostCallRecordCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5457 const VkDependencyInfo *pDependencyInfo) {
5458 StateTracker::PostCallRecordCmdSetEvent2(commandBuffer, event, pDependencyInfo);
5459 auto *cb_context = GetAccessContext(commandBuffer);
5460 assert(cb_context);
5461 if (!cb_context || !pDependencyInfo) return;
5462
5463 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5464}
5465
John Zulauf49beb112020-11-04 16:06:31 -07005466bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5467 VkPipelineStageFlags stageMask) const {
5468 bool skip = false;
5469 const auto *cb_context = GetAccessContext(commandBuffer);
5470 assert(cb_context);
5471 if (!cb_context) return skip;
5472
John Zulauf36ef9282021-02-02 11:47:24 -07005473 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005474 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005475}
5476
5477void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5478 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5479 auto *cb_context = GetAccessContext(commandBuffer);
5480 assert(cb_context);
5481 if (!cb_context) return;
5482
John Zulauf1bf30522021-09-03 15:39:06 -06005483 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005484}
5485
John Zulauf4edde622021-02-15 08:54:50 -07005486bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5487 VkPipelineStageFlags2KHR stageMask) const {
5488 bool skip = false;
5489 const auto *cb_context = GetAccessContext(commandBuffer);
5490 assert(cb_context);
5491 if (!cb_context) return skip;
5492
5493 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5494 return reset_event_op.Validate(*cb_context);
5495}
5496
Tony-LunarGa2662db2021-11-16 07:26:24 -07005497bool SyncValidator::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5498 VkPipelineStageFlags2 stageMask) const {
5499 bool skip = false;
5500 const auto *cb_context = GetAccessContext(commandBuffer);
5501 assert(cb_context);
5502 if (!cb_context) return skip;
5503
5504 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5505 return reset_event_op.Validate(*cb_context);
5506}
5507
John Zulauf4edde622021-02-15 08:54:50 -07005508void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5509 VkPipelineStageFlags2KHR stageMask) {
5510 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5511 auto *cb_context = GetAccessContext(commandBuffer);
5512 assert(cb_context);
5513 if (!cb_context) return;
5514
John Zulauf1bf30522021-09-03 15:39:06 -06005515 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4edde622021-02-15 08:54:50 -07005516}
5517
Tony-LunarGa2662db2021-11-16 07:26:24 -07005518void SyncValidator::PostCallRecordCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags2 stageMask) {
5519 StateTracker::PostCallRecordCmdResetEvent2(commandBuffer, event, stageMask);
5520 auto *cb_context = GetAccessContext(commandBuffer);
5521 assert(cb_context);
5522 if (!cb_context) return;
5523
5524 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5525}
5526
John Zulauf49beb112020-11-04 16:06:31 -07005527bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5528 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5529 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5530 uint32_t bufferMemoryBarrierCount,
5531 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5532 uint32_t imageMemoryBarrierCount,
5533 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5534 bool skip = false;
5535 const auto *cb_context = GetAccessContext(commandBuffer);
5536 assert(cb_context);
5537 if (!cb_context) return skip;
5538
John Zulauf36ef9282021-02-02 11:47:24 -07005539 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5540 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5541 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005542 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005543}
5544
5545void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5546 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5547 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5548 uint32_t bufferMemoryBarrierCount,
5549 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5550 uint32_t imageMemoryBarrierCount,
5551 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5552 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5553 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5554 imageMemoryBarrierCount, pImageMemoryBarriers);
5555
5556 auto *cb_context = GetAccessContext(commandBuffer);
5557 assert(cb_context);
5558 if (!cb_context) return;
5559
John Zulauf1bf30522021-09-03 15:39:06 -06005560 cb_context->RecordSyncOp<SyncOpWaitEvents>(
John Zulauf610e28c2021-08-03 17:46:23 -06005561 CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
John Zulauf1bf30522021-09-03 15:39:06 -06005562 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf4a6105a2020-11-17 15:11:05 -07005563}
5564
John Zulauf4edde622021-02-15 08:54:50 -07005565bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5566 const VkDependencyInfoKHR *pDependencyInfos) const {
5567 bool skip = false;
5568 const auto *cb_context = GetAccessContext(commandBuffer);
5569 assert(cb_context);
5570 if (!cb_context) return skip;
5571
5572 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5573 skip |= wait_events_op.Validate(*cb_context);
5574 return skip;
5575}
5576
5577void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5578 const VkDependencyInfoKHR *pDependencyInfos) {
5579 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5580
5581 auto *cb_context = GetAccessContext(commandBuffer);
5582 assert(cb_context);
5583 if (!cb_context) return;
5584
John Zulauf1bf30522021-09-03 15:39:06 -06005585 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5586 pDependencyInfos);
John Zulauf4edde622021-02-15 08:54:50 -07005587}
5588
Tony-LunarG1364cf52021-11-17 16:10:11 -07005589bool SyncValidator::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5590 const VkDependencyInfo *pDependencyInfos) const {
5591 bool skip = false;
5592 const auto *cb_context = GetAccessContext(commandBuffer);
5593 assert(cb_context);
5594 if (!cb_context) return skip;
5595
5596 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5597 skip |= wait_events_op.Validate(*cb_context);
5598 return skip;
5599}
5600
5601void SyncValidator::PostCallRecordCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5602 const VkDependencyInfo *pDependencyInfos) {
5603 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5604
5605 auto *cb_context = GetAccessContext(commandBuffer);
5606 assert(cb_context);
5607 if (!cb_context) return;
5608
5609 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5610 pDependencyInfos);
5611}
5612
John Zulauf4a6105a2020-11-17 15:11:05 -07005613void SyncEventState::ResetFirstScope() {
5614 for (const auto address_type : kAddressTypes) {
5615 first_scope[static_cast<size_t>(address_type)].clear();
5616 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005617 scope = SyncExecScope();
John Zulauf78b1f892021-09-20 15:02:09 -06005618 first_scope_set = false;
5619 first_scope_tag = 0;
John Zulauf4a6105a2020-11-17 15:11:05 -07005620}
5621
5622// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005623SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005624 IgnoreReason reason = NotIgnored;
5625
Tony-LunarG1364cf52021-11-17 16:10:11 -07005626 if ((CMD_WAITEVENTS2KHR == cmd || CMD_WAITEVENTS2 == cmd) && (CMD_SETEVENT == last_command)) {
John Zulauf4edde622021-02-15 08:54:50 -07005627 reason = SetVsWait2;
5628 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5629 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005630 } else if (unsynchronized_set) {
5631 reason = SetRace;
John Zulauf78b1f892021-09-20 15:02:09 -06005632 } else if (first_scope_set) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005633 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005634 if (missing_bits) reason = MissingStageBits;
5635 }
5636
5637 return reason;
5638}
5639
Jeremy Gebben40a22942020-12-22 14:22:06 -07005640bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005641 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5642 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5643 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005644}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005645
John Zulaufbb890452021-12-14 11:30:18 -07005646void SyncOpBase::SetReplayContext(uint32_t subpass, ReplayContextPtr &&replay) {
5647 subpass_ = subpass;
5648 replay_context_ = std::move(replay);
5649}
5650
5651const ReplayTrackbackBarriersAction *SyncOpBase::GetReplayTrackback() const {
5652 if (replay_context_) {
5653 assert(subpass_ < replay_context_->subpass_contexts.size());
5654 return &replay_context_->subpass_contexts[subpass_];
5655 }
5656 return nullptr;
5657}
5658
John Zulauf36ef9282021-02-02 11:47:24 -07005659SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5660 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5661 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005662 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5663 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5664 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005665 : SyncOpBase(cmd), barriers_(1) {
5666 auto &barrier_set = barriers_[0];
5667 barrier_set.dependency_flags = dependencyFlags;
5668 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5669 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005670 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005671 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5672 pMemoryBarriers);
5673 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5674 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5675 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5676 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005677}
5678
John Zulauf4edde622021-02-15 08:54:50 -07005679SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5680 const VkDependencyInfoKHR *dep_infos)
5681 : SyncOpBase(cmd), barriers_(event_count) {
5682 for (uint32_t i = 0; i < event_count; i++) {
5683 const auto &dep_info = dep_infos[i];
5684 auto &barrier_set = barriers_[i];
5685 barrier_set.dependency_flags = dep_info.dependencyFlags;
5686 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5687 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5688 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5689 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5690 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5691 dep_info.pMemoryBarriers);
5692 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5693 dep_info.pBufferMemoryBarriers);
5694 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5695 dep_info.pImageMemoryBarriers);
5696 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005697}
5698
John Zulauf36ef9282021-02-02 11:47:24 -07005699SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005700 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5701 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5702 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5703 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5704 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005705 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005706 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5707
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005708SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5709 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005710 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005711
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005712bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5713 bool skip = false;
5714 const auto *context = cb_context.GetCurrentAccessContext();
5715 assert(context);
5716 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005717 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5718
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005719 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005720 const auto &barrier_set = barriers_[0];
5721 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5722 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5723 const auto *image_state = image_barrier.image.get();
5724 if (!image_state) continue;
5725 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5726 if (hazard.hazard) {
5727 // PHASE1 TODO -- add tag information to log msg when useful.
5728 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005729 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005730 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5731 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5732 string_SyncHazard(hazard.hazard), image_barrier.index,
5733 sync_state.report_data->FormatHandle(image_handle).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06005734 cb_context.FormatHazard(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005735 }
5736 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005737 return skip;
5738}
5739
John Zulaufd5115702021-01-18 12:34:33 -07005740struct SyncOpPipelineBarrierFunctorFactory {
5741 using BarrierOpFunctor = PipelineBarrierOp;
5742 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5743 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5744 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5745 using BufferRange = ResourceAccessRange;
5746 using ImageRange = subresource_adapter::ImageRangeGenerator;
5747 using GlobalRange = ResourceAccessRange;
5748
5749 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5750 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5751 }
John Zulauf14940722021-04-12 15:19:02 -06005752 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005753 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5754 }
5755 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5756 return GlobalBarrierOpFunctor(barrier, false);
5757 }
5758
5759 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5760 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5761 const auto base_address = ResourceBaseAddress(buffer);
5762 return (range + base_address);
5763 }
John Zulauf110413c2021-03-20 05:38:38 -06005764 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005765 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005766
5767 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005768 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005769 return range_gen;
5770 }
5771 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5772};
5773
5774template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005775void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005776 AccessContext *context) {
5777 for (const auto &barrier : barriers) {
5778 const auto *state = barrier.GetState();
5779 if (state) {
5780 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5781 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5782 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5783 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5784 }
5785 }
5786}
5787
5788template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005789void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005790 AccessContext *access_context) {
5791 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5792 for (const auto &barrier : barriers) {
5793 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5794 }
5795 for (const auto address_type : kAddressTypes) {
5796 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5797 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5798 }
5799}
5800
John Zulauf8eda1562021-04-13 17:06:41 -06005801ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005802 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005803 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005804 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufbb890452021-12-14 11:30:18 -07005805 ReplayRecord(tag, access_context, events_context);
John Zulauf4fa68462021-04-26 21:04:22 -06005806 return tag;
5807}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005808
John Zulaufbb890452021-12-14 11:30:18 -07005809void SyncOpPipelineBarrier::ReplayRecord(const ResourceUsageTag tag, AccessContext *access_context,
5810 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005811 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005812 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5813 assert(barriers_.size() == 1);
5814 const auto &barrier_set = barriers_[0];
5815 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5816 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5817 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005818 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005819 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005820 } else {
5821 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005822 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005823 }
5824 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005825}
5826
John Zulauf8eda1562021-04-13 17:06:41 -06005827bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07005828 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005829 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5830 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005831 return false;
5832}
5833
John Zulauf4edde622021-02-15 08:54:50 -07005834void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5835 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5836 const VkMemoryBarrier *barriers) {
5837 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005838 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005839 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005840 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005841 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005842 }
5843 if (0 == memory_barrier_count) {
5844 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005845 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005846 }
John Zulauf4edde622021-02-15 08:54:50 -07005847 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005848}
5849
John Zulauf4edde622021-02-15 08:54:50 -07005850void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5851 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5852 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5853 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005854 for (uint32_t index = 0; index < barrier_count; index++) {
5855 const auto &barrier = barriers[index];
Jeremy Gebben9f537102021-10-05 16:37:12 -06005856 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005857 if (buffer) {
5858 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5859 const auto range = MakeRange(barrier.offset, barrier_size);
5860 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005861 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005862 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005863 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005864 }
5865 }
5866}
5867
John Zulauf4edde622021-02-15 08:54:50 -07005868void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005869 uint32_t memory_barrier_count, const VkMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005870 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005871 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005872 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005873 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5874 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5875 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005876 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005877 }
John Zulauf4edde622021-02-15 08:54:50 -07005878 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005879}
5880
John Zulauf4edde622021-02-15 08:54:50 -07005881void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5882 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005883 const VkBufferMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005884 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005885 for (uint32_t index = 0; index < barrier_count; index++) {
5886 const auto &barrier = barriers[index];
5887 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5888 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9f537102021-10-05 16:37:12 -06005889 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005890 if (buffer) {
5891 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5892 const auto range = MakeRange(barrier.offset, barrier_size);
5893 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005894 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005895 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005896 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005897 }
5898 }
5899}
5900
John Zulauf4edde622021-02-15 08:54:50 -07005901void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5902 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5903 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5904 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005905 for (uint32_t index = 0; index < barrier_count; index++) {
5906 const auto &barrier = barriers[index];
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005907 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005908 if (image) {
5909 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5910 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005911 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005912 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005913 image_memory_barriers.emplace_back();
5914 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005915 }
5916 }
5917}
John Zulaufd5115702021-01-18 12:34:33 -07005918
John Zulauf4edde622021-02-15 08:54:50 -07005919void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5920 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005921 const VkImageMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005922 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005923 for (uint32_t index = 0; index < barrier_count; index++) {
5924 const auto &barrier = barriers[index];
5925 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5926 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005927 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005928 if (image) {
5929 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5930 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005931 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005932 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005933 image_memory_barriers.emplace_back();
5934 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005935 }
5936 }
5937}
5938
John Zulauf36ef9282021-02-02 11:47:24 -07005939SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005940 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5941 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5942 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5943 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005944 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005945 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5946 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005947 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005948}
5949
John Zulauf4edde622021-02-15 08:54:50 -07005950SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5951 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5952 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5953 MakeEventsList(sync_state, eventCount, pEvents);
5954 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5955}
5956
John Zulauf610e28c2021-08-03 17:46:23 -06005957const char *const SyncOpWaitEvents::kIgnored = "Wait operation is ignored for this event.";
5958
John Zulaufd5115702021-01-18 12:34:33 -07005959bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005960 bool skip = false;
5961 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005962 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005963
John Zulauf610e28c2021-08-03 17:46:23 -06005964 // This is only interesting at record and not replay (Execute/Submit) time.
John Zulauf4edde622021-02-15 08:54:50 -07005965 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5966 const auto &barrier_set = barriers_[barrier_set_index];
5967 if (barrier_set.single_exec_scope) {
5968 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5969 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5970 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5971 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5972 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5973 } else {
5974 const auto &barriers = barrier_set.memory_barriers;
5975 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5976 const auto &barrier = barriers[barrier_index];
5977 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5978 const std::string vuid =
5979 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5980 skip =
5981 sync_state.LogInfo(command_buffer_handle, vuid,
5982 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5983 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5984 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5985 }
5986 }
5987 }
5988 }
John Zulaufd5115702021-01-18 12:34:33 -07005989 }
5990
John Zulauf610e28c2021-08-03 17:46:23 -06005991 // The rest is common to record time and replay time.
5992 skip |= DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5993 return skip;
5994}
5995
John Zulaufbb890452021-12-14 11:30:18 -07005996bool SyncOpWaitEvents::DoValidate(const CommandExecutionContext &exec_context, const ResourceUsageTag base_tag) const {
John Zulauf610e28c2021-08-03 17:46:23 -06005997 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07005998 const auto &sync_state = exec_context.GetSyncState();
John Zulauf610e28c2021-08-03 17:46:23 -06005999
Jeremy Gebben40a22942020-12-22 14:22:06 -07006000 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07006001 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07006002 bool events_not_found = false;
John Zulaufbb890452021-12-14 11:30:18 -07006003 const auto *events_context = exec_context.GetCurrentEventsContext();
John Zulauf669dfd52021-01-27 17:15:28 -07006004 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07006005 size_t barrier_set_index = 0;
6006 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06006007 for (const auto &event : events_) {
6008 const auto *sync_event = events_context->Get(event.get());
6009 const auto &barrier_set = barriers_[barrier_set_index];
6010 if (!sync_event) {
6011 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
6012 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
6013 // new validation error... wait without previously submitted set event...
6014 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 -07006015 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06006016 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07006017 }
John Zulauf610e28c2021-08-03 17:46:23 -06006018
6019 // For replay calls, don't revalidate "same command buffer" events
6020 if (sync_event->last_command_tag > base_tag) continue;
6021
John Zulauf78394fc2021-07-12 15:41:40 -06006022 const auto event_handle = sync_event->event->event();
6023 // TODO add "destroyed" checks
6024
John Zulauf78b1f892021-09-20 15:02:09 -06006025 if (sync_event->first_scope_set) {
6026 // Only accumulate barrier and event stages if there is a pending set in the current context
6027 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
6028 event_stage_masks |= sync_event->scope.mask_param;
6029 }
6030
John Zulauf78394fc2021-07-12 15:41:40 -06006031 const auto &src_exec_scope = barrier_set.src_exec_scope;
John Zulauf78b1f892021-09-20 15:02:09 -06006032
John Zulauf78394fc2021-07-12 15:41:40 -06006033 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
6034 if (ignore_reason) {
6035 switch (ignore_reason) {
6036 case SyncEventState::ResetWaitRace:
6037 case SyncEventState::Reset2WaitRace: {
6038 // Four permuations of Reset and Wait calls...
6039 const char *vuid =
6040 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
6041 if (ignore_reason == SyncEventState::Reset2WaitRace) {
Tony-LunarG279601c2021-11-16 10:50:51 -07006042 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2-event-03831"
6043 : "VUID-vkCmdResetEvent2-event-03832";
John Zulauf78394fc2021-07-12 15:41:40 -06006044 }
6045 const char *const message =
6046 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
6047 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
6048 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
John Zulauf610e28c2021-08-03 17:46:23 -06006049 CommandTypeString(sync_event->last_command), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06006050 break;
6051 }
6052 case SyncEventState::SetRace: {
6053 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
6054 // this event
6055 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
6056 const char *const message =
6057 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
6058 const char *const reason = "First synchronization scope is undefined.";
6059 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
6060 sync_state.report_data->FormatHandle(event_handle).c_str(),
John Zulauf610e28c2021-08-03 17:46:23 -06006061 CommandTypeString(sync_event->last_command), reason, kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06006062 break;
6063 }
6064 case SyncEventState::MissingStageBits: {
6065 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
6066 // Issue error message that event waited for is not in wait events scope
6067 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
6068 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
6069 ". Bits missing from srcStageMask %s. %s";
6070 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
6071 sync_state.report_data->FormatHandle(event_handle).c_str(),
6072 sync_event->scope.mask_param, src_exec_scope.mask_param,
John Zulauf610e28c2021-08-03 17:46:23 -06006073 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06006074 break;
6075 }
6076 case SyncEventState::SetVsWait2: {
Tony-LunarG279601c2021-11-16 10:50:51 -07006077 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2-pEvents-03837",
John Zulauf78394fc2021-07-12 15:41:40 -06006078 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
6079 sync_state.report_data->FormatHandle(event_handle).c_str(),
6080 CommandTypeString(sync_event->last_command));
6081 break;
6082 }
6083 default:
6084 assert(ignore_reason == SyncEventState::NotIgnored);
6085 }
6086 } else if (barrier_set.image_memory_barriers.size()) {
6087 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
John Zulaufbb890452021-12-14 11:30:18 -07006088 const auto *context = exec_context.GetCurrentAccessContext();
John Zulauf78394fc2021-07-12 15:41:40 -06006089 assert(context);
6090 for (const auto &image_memory_barrier : image_memory_barriers) {
6091 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
6092 const auto *image_state = image_memory_barrier.image.get();
6093 if (!image_state) continue;
6094 const auto &subresource_range = image_memory_barrier.range;
6095 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
6096 const auto hazard =
6097 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
6098 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
6099 if (hazard.hazard) {
6100 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
6101 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
6102 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
6103 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06006104 exec_context.FormatHazard(hazard).c_str());
John Zulauf78394fc2021-07-12 15:41:40 -06006105 break;
6106 }
6107 }
6108 }
6109 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
6110 // 03839
6111 barrier_set_index += barrier_set_incr;
6112 }
John Zulaufd5115702021-01-18 12:34:33 -07006113
6114 // 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 -07006115 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 -07006116 if (extra_stage_bits) {
6117 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07006118 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
6119 const char *const vuid =
Tony-LunarG279601c2021-11-16 10:50:51 -07006120 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07006121 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07006122 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufbb890452021-12-14 11:30:18 -07006123 const auto handle = exec_context.Handle();
John Zulaufd5115702021-01-18 12:34:33 -07006124 if (events_not_found) {
John Zulaufbb890452021-12-14 11:30:18 -07006125 skip |= sync_state.LogInfo(handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07006126 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07006127 " vkCmdSetEvent may be in previously submitted command buffer.");
6128 } else {
John Zulaufbb890452021-12-14 11:30:18 -07006129 skip |= sync_state.LogError(handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07006130 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07006131 }
6132 }
6133 return skip;
6134}
6135
6136struct SyncOpWaitEventsFunctorFactory {
6137 using BarrierOpFunctor = WaitEventBarrierOp;
6138 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
6139 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
6140 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
6141 using BufferRange = EventSimpleRangeGenerator;
6142 using ImageRange = EventImageRangeGenerator;
6143 using GlobalRange = EventSimpleRangeGenerator;
6144
6145 // Need to restrict to only valid exec and access scope for this event
6146 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
6147 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07006148 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07006149 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
6150 return barrier;
6151 }
6152 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
6153 auto barrier = RestrictToEvent(barrier_arg);
6154 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
6155 }
John Zulauf14940722021-04-12 15:19:02 -06006156 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07006157 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
6158 }
6159 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
6160 auto barrier = RestrictToEvent(barrier_arg);
6161 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
6162 }
6163
6164 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
6165 const AccessAddressType address_type = GetAccessAddressType(buffer);
6166 const auto base_address = ResourceBaseAddress(buffer);
6167 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
6168 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
6169 return filtered_range_gen;
6170 }
John Zulauf110413c2021-03-20 05:38:38 -06006171 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07006172 if (!SimpleBinding(image)) return ImageRange();
6173 const auto address_type = GetAccessAddressType(image);
6174 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06006175 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07006176 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
6177
6178 return filtered_range_gen;
6179 }
6180 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
6181 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
6182 }
6183 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
6184 SyncEventState *sync_event;
6185};
6186
John Zulauf8eda1562021-04-13 17:06:41 -06006187ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006188 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07006189 auto *access_context = cb_context->GetCurrentAccessContext();
6190 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006191 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07006192 auto *events_context = cb_context->GetCurrentEventsContext();
6193 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006194 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07006195
John Zulaufbb890452021-12-14 11:30:18 -07006196 ReplayRecord(tag, access_context, events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006197 return tag;
6198}
6199
John Zulaufbb890452021-12-14 11:30:18 -07006200void SyncOpWaitEvents::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07006201 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
6202 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
6203 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
6204 access_context->ResolvePreviousAccesses();
6205
John Zulauf4edde622021-02-15 08:54:50 -07006206 size_t barrier_set_index = 0;
6207 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
6208 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07006209 for (auto &event_shared : events_) {
6210 if (!event_shared.get()) continue;
6211 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07006212
John Zulauf4edde622021-02-15 08:54:50 -07006213 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006214 sync_event->last_command_tag = tag;
John Zulaufd5115702021-01-18 12:34:33 -07006215
John Zulauf4edde622021-02-15 08:54:50 -07006216 const auto &barrier_set = barriers_[barrier_set_index];
6217 const auto &dst = barrier_set.dst_exec_scope;
6218 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07006219 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
6220 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
6221 // of the barriers is maintained.
6222 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07006223 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
6224 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
6225 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07006226
6227 // Apply the global barrier to the event itself (for race condition tracking)
6228 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
6229 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6230 sync_event->barriers |= dst.exec_scope;
6231 } else {
6232 // We ignored this wait, so we don't have any effective synchronization barriers for it.
6233 sync_event->barriers = 0U;
6234 }
John Zulauf4edde622021-02-15 08:54:50 -07006235 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07006236 }
6237
6238 // Apply the pending barriers
6239 ResolvePendingBarrierFunctor apply_pending_action(tag);
6240 access_context->ApplyToContext(apply_pending_action);
6241}
6242
John Zulauf8eda1562021-04-13 17:06:41 -06006243bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006244 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
6245 return DoValidate(*exec_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006246}
6247
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006248bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6249 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
6250 bool skip = false;
6251 const auto *cb_access_context = GetAccessContext(commandBuffer);
6252 assert(cb_access_context);
6253 if (!cb_access_context) return skip;
6254
6255 const auto *context = cb_access_context->GetCurrentAccessContext();
6256 assert(context);
6257 if (!context) return skip;
6258
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006259 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006260
6261 if (dst_buffer) {
6262 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6263 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
6264 if (hazard.hazard) {
6265 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
6266 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
6267 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf397e68b2022-04-19 11:44:07 -06006268 cb_access_context->FormatHazard(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006269 }
6270 }
6271 return skip;
6272}
6273
John Zulauf669dfd52021-01-27 17:15:28 -07006274void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07006275 events_.reserve(event_count);
6276 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006277 events_.emplace_back(sync_state.Get<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07006278 }
6279}
John Zulauf6ce24372021-01-30 05:56:25 -07006280
John Zulauf36ef9282021-02-02 11:47:24 -07006281SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006282 VkPipelineStageFlags2KHR stageMask)
Jeremy Gebben9f537102021-10-05 16:37:12 -06006283 : SyncOpBase(cmd), event_(sync_state.Get<EVENT_STATE>(event)), exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006284
John Zulauf1bf30522021-09-03 15:39:06 -06006285bool SyncOpResetEvent::Validate(const CommandBufferAccessContext& cb_context) const {
6286 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6287}
6288
John Zulaufbb890452021-12-14 11:30:18 -07006289bool SyncOpResetEvent::DoValidate(const CommandExecutionContext &exec_context, const ResourceUsageTag base_tag) const {
6290 auto *events_context = exec_context.GetCurrentEventsContext();
John Zulauf6ce24372021-01-30 05:56:25 -07006291 assert(events_context);
6292 bool skip = false;
6293 if (!events_context) return skip;
6294
John Zulaufbb890452021-12-14 11:30:18 -07006295 const auto &sync_state = exec_context.GetSyncState();
John Zulauf6ce24372021-01-30 05:56:25 -07006296 const auto *sync_event = events_context->Get(event_);
6297 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6298
John Zulauf1bf30522021-09-03 15:39:06 -06006299 if (sync_event->last_command_tag > base_tag) return skip; // if we validated this in recording of the secondary, don't repeat
6300
John Zulauf6ce24372021-01-30 05:56:25 -07006301 const char *const set_wait =
6302 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6303 "hazards.";
6304 const char *message = set_wait; // Only one message this call.
6305 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
6306 const char *vuid = nullptr;
6307 switch (sync_event->last_command) {
6308 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006309 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006310 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006311 // Needs a barrier between set and reset
6312 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
6313 break;
John Zulauf4edde622021-02-15 08:54:50 -07006314 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006315 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006316 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07006317 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
6318 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
6319 break;
6320 }
6321 default:
6322 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07006323 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
6324 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07006325 break;
6326 }
6327 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006328 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
6329 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006330 CommandTypeString(sync_event->last_command));
6331 }
6332 }
6333 return skip;
6334}
6335
John Zulauf8eda1562021-04-13 17:06:41 -06006336ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
6337 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006338 auto *events_context = cb_context->GetCurrentEventsContext();
6339 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006340 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006341
6342 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06006343 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006344
6345 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07006346 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006347 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006348 sync_event->unsynchronized_set = CMD_NONE;
6349 sync_event->ResetFirstScope();
6350 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06006351
6352 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006353}
6354
John Zulauf8eda1562021-04-13 17:06:41 -06006355bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006356 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
6357 return DoValidate(*exec_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006358}
6359
John Zulaufbb890452021-12-14 11:30:18 -07006360void SyncOpResetEvent::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006361
John Zulauf36ef9282021-02-02 11:47:24 -07006362SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006363 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07006364 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006365 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006366 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
6367 dep_info_() {}
6368
6369SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
6370 const VkDependencyInfoKHR &dep_info)
6371 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006372 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006373 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
Tony-LunarG273f32f2021-09-28 08:56:30 -06006374 dep_info_(new safe_VkDependencyInfo(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006375
6376bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf610e28c2021-08-03 17:46:23 -06006377 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6378}
6379bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006380 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
6381 assert(exec_context);
6382 return DoValidate(*exec_context, base_tag);
John Zulauf610e28c2021-08-03 17:46:23 -06006383}
6384
John Zulaufbb890452021-12-14 11:30:18 -07006385bool SyncOpSetEvent::DoValidate(const CommandExecutionContext &exec_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006386 bool skip = false;
6387
John Zulaufbb890452021-12-14 11:30:18 -07006388 const auto &sync_state = exec_context.GetSyncState();
6389 auto *events_context = exec_context.GetCurrentEventsContext();
John Zulauf6ce24372021-01-30 05:56:25 -07006390 assert(events_context);
6391 if (!events_context) return skip;
6392
6393 const auto *sync_event = events_context->Get(event_);
6394 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6395
John Zulauf610e28c2021-08-03 17:46:23 -06006396 if (sync_event->last_command_tag >= base_tag) return skip; // for replay we don't want to revalidate internal "last commmand"
6397
John Zulauf6ce24372021-01-30 05:56:25 -07006398 const char *const reset_set =
6399 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6400 "hazards.";
6401 const char *const wait =
6402 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6403
6404 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006405 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006406 const char *message = nullptr;
6407 switch (sync_event->last_command) {
6408 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006409 case CMD_RESETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006410 case CMD_RESETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006411 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006412 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006413 message = reset_set;
6414 break;
6415 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006416 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006417 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006418 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006419 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006420 message = reset_set;
6421 break;
6422 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006423 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006424 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006425 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006426 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006427 message = wait;
6428 break;
6429 default:
6430 // The only other valid last command that wasn't one.
6431 assert(sync_event->last_command == CMD_NONE);
6432 break;
6433 }
John Zulauf4edde622021-02-15 08:54:50 -07006434 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006435 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006436 std::string vuid("SYNC-");
6437 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006438 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6439 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006440 CommandTypeString(sync_event->last_command));
6441 }
6442 }
6443
6444 return skip;
6445}
6446
John Zulauf8eda1562021-04-13 17:06:41 -06006447ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006448 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006449 auto *events_context = cb_context->GetCurrentEventsContext();
6450 auto *access_context = cb_context->GetCurrentAccessContext();
6451 assert(events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006452 if (access_context && events_context) {
John Zulaufbb890452021-12-14 11:30:18 -07006453 ReplayRecord(tag, access_context, events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006454 }
6455 return tag;
6456}
John Zulauf6ce24372021-01-30 05:56:25 -07006457
John Zulaufbb890452021-12-14 11:30:18 -07006458void SyncOpSetEvent::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006459 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf610e28c2021-08-03 17:46:23 -06006460 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006461
6462 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6463 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6464 // any issues caused by naive scope setting here.
6465
6466 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6467 // Given:
6468 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6469 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6470
6471 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6472 sync_event->unsynchronized_set = sync_event->last_command;
6473 sync_event->ResetFirstScope();
John Zulauf78b1f892021-09-20 15:02:09 -06006474 } else if (!sync_event->first_scope_set) {
John Zulauf6ce24372021-01-30 05:56:25 -07006475 // We only set the scope if there isn't one
6476 sync_event->scope = src_exec_scope_;
6477
6478 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6479 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6480 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6481 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6482 }
6483 };
6484 access_context->ForAll(set_scope);
6485 sync_event->unsynchronized_set = CMD_NONE;
John Zulauf78b1f892021-09-20 15:02:09 -06006486 sync_event->first_scope_set = true;
John Zulauf6ce24372021-01-30 05:56:25 -07006487 sync_event->first_scope_tag = tag;
6488 }
John Zulauf4edde622021-02-15 08:54:50 -07006489 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6490 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006491 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006492 sync_event->barriers = 0U;
6493}
John Zulauf64ffe552021-02-06 10:25:07 -07006494
6495SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6496 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006497 const VkSubpassBeginInfo *pSubpassBeginInfo)
6498 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006499 if (pRenderPassBegin) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006500 rp_state_ = sync_state.Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
John Zulauf64ffe552021-02-06 10:25:07 -07006501 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006502 auto fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07006503 if (fb_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006504 shared_attachments_ = sync_state.GetAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
John Zulauf64ffe552021-02-06 10:25:07 -07006505 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6506 // Note that this a safe to presist as long as shared_attachments is not cleared
6507 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006508 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006509 attachments_.emplace_back(attachment.get());
6510 }
6511 }
6512 if (pSubpassBeginInfo) {
6513 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6514 }
6515 }
6516}
6517
6518bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6519 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6520 bool skip = false;
6521
6522 assert(rp_state_.get());
6523 if (nullptr == rp_state_.get()) return skip;
6524 auto &rp_state = *rp_state_.get();
6525
6526 const uint32_t subpass = 0;
6527
6528 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6529 // hasn't happened yet)
6530 const std::vector<AccessContext> empty_context_vector;
6531 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6532 cb_context.GetCurrentAccessContext());
6533
6534 // Validate attachment operations
6535 if (attachments_.size() == 0) return skip;
6536 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006537
6538 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6539 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6540 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6541 // operations (though it's currently a messy approach)
6542 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6543 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006544
6545 // Validate load operations if there were no layout transition hazards
6546 if (!skip) {
John Zulaufee984022022-04-13 16:39:50 -06006547 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kInvalidTag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07006548 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006549 }
6550
6551 return skip;
6552}
6553
John Zulauf8eda1562021-04-13 17:06:41 -06006554ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006555 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6556 assert(rp_state_.get());
John Zulauf41a9c7c2021-12-07 15:59:53 -07006557 if (nullptr == rp_state_.get()) return cb_context->NextCommandTag(cmd_);
6558 return cb_context->RecordBeginRenderPass(cmd_, *rp_state_.get(), renderpass_begin_info_.renderArea, attachments_);
John Zulauf64ffe552021-02-06 10:25:07 -07006559}
6560
John Zulauf8eda1562021-04-13 17:06:41 -06006561bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006562 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006563 return false;
6564}
6565
John Zulaufbb890452021-12-14 11:30:18 -07006566void SyncOpBeginRenderPass::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context,
6567 SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006568
John Zulauf64ffe552021-02-06 10:25:07 -07006569SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006570 const VkSubpassEndInfo *pSubpassEndInfo)
6571 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006572 if (pSubpassBeginInfo) {
6573 subpass_begin_info_.initialize(pSubpassBeginInfo);
6574 }
6575 if (pSubpassEndInfo) {
6576 subpass_end_info_.initialize(pSubpassEndInfo);
6577 }
6578}
6579
6580bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6581 bool skip = false;
6582 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6583 if (!renderpass_context) return skip;
6584
6585 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6586 return skip;
6587}
6588
John Zulauf8eda1562021-04-13 17:06:41 -06006589ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006590 return cb_context->RecordNextSubpass(cmd_);
John Zulauf8eda1562021-04-13 17:06:41 -06006591}
6592
6593bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006594 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006595 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006596}
6597
sfricke-samsung85584a72021-09-30 21:43:38 -07006598SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6599 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006600 if (pSubpassEndInfo) {
6601 subpass_end_info_.initialize(pSubpassEndInfo);
6602 }
6603}
6604
John Zulaufbb890452021-12-14 11:30:18 -07006605void SyncOpNextSubpass::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6606}
John Zulauf8eda1562021-04-13 17:06:41 -06006607
John Zulauf64ffe552021-02-06 10:25:07 -07006608bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6609 bool skip = false;
6610 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6611
6612 if (!renderpass_context) return skip;
6613 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6614 return skip;
6615}
6616
John Zulauf8eda1562021-04-13 17:06:41 -06006617ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006618 return cb_context->RecordEndRenderPass(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006619}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006620
John Zulauf8eda1562021-04-13 17:06:41 -06006621bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006622 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006623 return false;
6624}
6625
John Zulaufbb890452021-12-14 11:30:18 -07006626void SyncOpEndRenderPass::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context,
6627 SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006628
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006629void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6630 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6631 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6632 auto *cb_access_context = GetAccessContext(commandBuffer);
6633 assert(cb_access_context);
6634 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6635 auto *context = cb_access_context->GetCurrentAccessContext();
6636 assert(context);
6637
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006638 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006639
6640 if (dst_buffer) {
6641 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6642 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6643 }
6644}
John Zulaufd05c5842021-03-26 11:32:16 -06006645
John Zulaufae842002021-04-15 18:20:55 -06006646bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6647 const VkCommandBuffer *pCommandBuffers) const {
6648 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6649 const char *func_name = "vkCmdExecuteCommands";
6650 const auto *cb_context = GetAccessContext(commandBuffer);
6651 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006652
6653 // Heavyweight, but we need a proxy copy of the active command buffer access context
6654 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006655
6656 // Make working copies of the access and events contexts
John Zulaufae842002021-04-15 18:20:55 -06006657 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006658 proxy_cb_context.NextIndexedCommandTag(CMD_EXECUTECOMMANDS, cb_index);
6659
John Zulaufae842002021-04-15 18:20:55 -06006660 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6661 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006662
6663 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6664 assert(recorded_context);
6665 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6666
6667 // The barriers have already been applied in ValidatFirstUse
6668 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6669 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006670 }
6671
John Zulaufae842002021-04-15 18:20:55 -06006672 return skip;
6673}
6674
6675void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6676 const VkCommandBuffer *pCommandBuffers) {
6677 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006678 auto *cb_context = GetAccessContext(commandBuffer);
6679 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006680 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006681 cb_context->NextIndexedCommandTag(CMD_EXECUTECOMMANDS, cb_index);
John Zulauf4fa68462021-04-26 21:04:22 -06006682 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6683 if (!recorded_cb_context) continue;
6684 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6685 }
John Zulaufae842002021-04-15 18:20:55 -06006686}
6687
John Zulaufd0ec59f2021-03-13 14:25:08 -07006688AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6689 : view_(view), view_mask_(), gen_store_() {
6690 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6691 const IMAGE_STATE &image_state = *view_->image_state.get();
6692 const auto base_address = ResourceBaseAddress(image_state);
6693 const auto *encoder = image_state.fragment_encoder.get();
6694 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006695 // Get offset and extent for the view, accounting for possible depth slicing
6696 const VkOffset3D zero_offset = view->GetOffset();
6697 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006698 // Intentional copy
6699 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6700 view_mask_ = subres_range.aspectMask;
6701 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6702 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6703
6704 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6705 if (depth && (depth != view_mask_)) {
6706 subres_range.aspectMask = depth;
6707 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6708 }
6709 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6710 if (stencil && (stencil != view_mask_)) {
6711 subres_range.aspectMask = stencil;
6712 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6713 }
6714}
6715
6716const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6717 const ImageRangeGen *got = nullptr;
6718 switch (gen_type) {
6719 case kViewSubresource:
6720 got = &gen_store_[kViewSubresource];
6721 break;
6722 case kRenderArea:
6723 got = &gen_store_[kRenderArea];
6724 break;
6725 case kDepthOnlyRenderArea:
6726 got =
6727 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6728 break;
6729 case kStencilOnlyRenderArea:
6730 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6731 : &gen_store_[Gen::kStencilOnlyRenderArea];
6732 break;
6733 default:
6734 assert(got);
6735 }
6736 return got;
6737}
6738
6739AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6740 assert(IsValid());
6741 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6742 if (depth_op) {
6743 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6744 if (stencil_op) {
6745 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6746 return kRenderArea;
6747 }
6748 return kDepthOnlyRenderArea;
6749 }
6750 if (stencil_op) {
6751 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6752 return kStencilOnlyRenderArea;
6753 }
6754
6755 assert(depth_op || stencil_op);
6756 return kRenderArea;
6757}
6758
6759AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006760
6761void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6762 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6763 for (auto &event_pair : map_) {
6764 assert(event_pair.second); // Shouldn't be storing empty
6765 auto &sync_event = *event_pair.second;
6766 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6767 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6768 sync_event.barriers |= dst.exec_scope;
6769 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6770 }
6771 }
6772}
John Zulaufbb890452021-12-14 11:30:18 -07006773
6774ReplayTrackbackBarriersAction::ReplayTrackbackBarriersAction(VkQueueFlags queue_flags,
6775 const SubpassDependencyGraphNode &subpass_dep,
6776 const std::vector<ReplayTrackbackBarriersAction> &replay_contexts) {
6777 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
6778 trackback_barriers.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
6779 for (const auto &prev_dep : subpass_dep.prev) {
6780 const auto prev_pass = prev_dep.first->pass;
6781 const auto &prev_barriers = prev_dep.second;
6782 trackback_barriers.emplace_back(&replay_contexts[prev_pass], queue_flags, prev_barriers);
6783 }
6784 if (has_barrier_from_external) {
6785 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
6786 trackback_barriers.emplace_back(nullptr, queue_flags, subpass_dep.barrier_from_external);
6787 }
6788}
6789
6790void ReplayTrackbackBarriersAction::operator()(ResourceAccessState *access) const {
6791 if (trackback_barriers.size() == 1) {
6792 trackback_barriers[0](access);
6793 } else {
6794 ResourceAccessState resolved;
6795 for (const auto &trackback : trackback_barriers) {
6796 ResourceAccessState access_copy = *access;
6797 trackback(&access_copy);
6798 resolved.Resolve(access_copy);
6799 }
6800 *access = resolved;
6801 }
6802}
6803
6804ReplayTrackbackBarriersAction::TrackbackBarriers::TrackbackBarriers(
6805 const ReplayTrackbackBarriersAction *source_subpass_, VkQueueFlags queue_flags_,
6806 const std::vector<const VkSubpassDependency2 *> &subpass_dependencies_)
6807 : Base(source_subpass_, queue_flags_, subpass_dependencies_) {}
6808
6809void ReplayTrackbackBarriersAction::TrackbackBarriers::operator()(ResourceAccessState *access) const {
6810 if (source_subpass) {
6811 (*source_subpass)(access);
6812 }
6813 access->ApplyBarriersImmediate(barriers);
6814}