blob: 8e28a11b84a2b6a3f46c6c084c5897dfdf2d3bd1 [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
Jeremy Gebben6fbf8242021-06-21 09:14:46 -060029static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.Binding(); }
John Zulauf264cce02021-02-05 14:40:47 -070030
John Zulauf29d00532021-03-04 13:28:54 -070031static bool SimpleBinding(const IMAGE_STATE &image_state) {
Jeremy Gebben62c3bf42021-07-21 15:38:24 -060032 bool simple =
Jeremy Gebben82e11d52021-07-26 09:19:37 -060033 SimpleBinding(static_cast<const BINDABLE &>(image_state)) || image_state.IsSwapchainImage() || image_state.bind_swapchain;
John Zulauf29d00532021-03-04 13:28:54 -070034
35 // If it's not simple we must have an encoder.
36 assert(!simple || image_state.fragment_encoder.get());
37 return simple;
38}
39
John Zulauf4fa68462021-04-26 21:04:22 -060040static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
41static const std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
John Zulauf43cc7462020-12-03 12:33:12 -070042 AccessAddressType::kLinear, AccessAddressType::kIdealized};
43
John Zulaufd5115702021-01-18 12:34:33 -070044static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070045static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
46 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
47}
John Zulaufd5115702021-01-18 12:34:33 -070048
John Zulauf9cb530d2019-09-30 14:14:10 -060049static const char *string_SyncHazardVUID(SyncHazard hazard) {
50 switch (hazard) {
51 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070052 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060053 break;
54 case SyncHazard::READ_AFTER_WRITE:
55 return "SYNC-HAZARD-READ_AFTER_WRITE";
56 break;
57 case SyncHazard::WRITE_AFTER_READ:
58 return "SYNC-HAZARD-WRITE_AFTER_READ";
59 break;
60 case SyncHazard::WRITE_AFTER_WRITE:
61 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
62 break;
John Zulauf2f952d22020-02-10 11:34:51 -070063 case SyncHazard::READ_RACING_WRITE:
64 return "SYNC-HAZARD-READ-RACING-WRITE";
65 break;
66 case SyncHazard::WRITE_RACING_WRITE:
67 return "SYNC-HAZARD-WRITE-RACING-WRITE";
68 break;
69 case SyncHazard::WRITE_RACING_READ:
70 return "SYNC-HAZARD-WRITE-RACING-READ";
71 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060072 default:
73 assert(0);
74 }
75 return "SYNC-HAZARD-INVALID";
76}
77
John Zulauf59e25072020-07-17 10:55:21 -060078static bool IsHazardVsRead(SyncHazard hazard) {
79 switch (hazard) {
80 case SyncHazard::NONE:
81 return false;
82 break;
83 case SyncHazard::READ_AFTER_WRITE:
84 return false;
85 break;
86 case SyncHazard::WRITE_AFTER_READ:
87 return true;
88 break;
89 case SyncHazard::WRITE_AFTER_WRITE:
90 return false;
91 break;
92 case SyncHazard::READ_RACING_WRITE:
93 return false;
94 break;
95 case SyncHazard::WRITE_RACING_WRITE:
96 return false;
97 break;
98 case SyncHazard::WRITE_RACING_READ:
99 return true;
100 break;
101 default:
102 assert(0);
103 }
104 return false;
105}
106
John Zulauf9cb530d2019-09-30 14:14:10 -0600107static const char *string_SyncHazard(SyncHazard hazard) {
108 switch (hazard) {
109 case SyncHazard::NONE:
110 return "NONR";
111 break;
112 case SyncHazard::READ_AFTER_WRITE:
113 return "READ_AFTER_WRITE";
114 break;
115 case SyncHazard::WRITE_AFTER_READ:
116 return "WRITE_AFTER_READ";
117 break;
118 case SyncHazard::WRITE_AFTER_WRITE:
119 return "WRITE_AFTER_WRITE";
120 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700121 case SyncHazard::READ_RACING_WRITE:
122 return "READ_RACING_WRITE";
123 break;
124 case SyncHazard::WRITE_RACING_WRITE:
125 return "WRITE_RACING_WRITE";
126 break;
127 case SyncHazard::WRITE_RACING_READ:
128 return "WRITE_RACING_READ";
129 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600130 default:
131 assert(0);
132 }
133 return "INVALID HAZARD";
134}
135
John Zulauf37ceaed2020-07-03 16:18:15 -0600136static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
137 // Return the info for the first bit found
138 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700139 for (size_t i = 0; i < flags.size(); i++) {
140 if (flags.test(i)) {
141 info = &syncStageAccessInfoByStageAccessIndex[i];
142 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600143 }
144 }
145 return info;
146}
147
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700148static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600149 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700150 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600151 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700152 } else {
153 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
154 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
155 if ((flags & info.stage_access_bit).any()) {
156 if (!out_str.empty()) {
157 out_str.append(sep);
158 }
159 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600160 }
John Zulauf59e25072020-07-17 10:55:21 -0600161 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700162 if (out_str.length() == 0) {
163 out_str.append("Unhandled SyncStageAccess");
164 }
John Zulauf59e25072020-07-17 10:55:21 -0600165 }
166 return out_str;
167}
168
John Zulauf14940722021-04-12 15:19:02 -0600169static std::string string_UsageTag(const ResourceUsageRecord &tag) {
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700170 std::stringstream out;
171
John Zulauffaea0ee2021-01-14 14:01:32 -0700172 out << "command: " << CommandTypeString(tag.command);
173 out << ", seq_no: " << tag.seq_num;
174 if (tag.sub_command != 0) {
175 out << ", subcmd: " << tag.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700176 }
177 return out.str();
178}
John Zulauf4fa68462021-04-26 21:04:22 -0600179static std::string string_UsageIndex(SyncStageAccessIndex usage_index) {
180 const char *stage_access_name = "INVALID_STAGE_ACCESS";
181 if (usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size())) {
182 stage_access_name = syncStageAccessInfoByStageAccessIndex[usage_index].name;
183 }
184 return std::string(stage_access_name);
185}
186
187struct NoopBarrierAction {
188 explicit NoopBarrierAction() {}
189 void operator()(ResourceAccessState *access) const {}
John Zulauf5c628d02021-05-04 15:46:36 -0600190 const bool layout_transition = false;
John Zulauf4fa68462021-04-26 21:04:22 -0600191};
192
193// NOTE: Make sure the proxy doesn't outlive from, as the proxy is pointing directly to access contexts owned by from.
194CommandBufferAccessContext::CommandBufferAccessContext(const CommandBufferAccessContext &from, AsProxyContext dummy)
195 : CommandBufferAccessContext(from.sync_state_) {
196 // Copy only the needed fields out of from for a temporary, proxy command buffer context
197 cb_state_ = from.cb_state_;
198 queue_flags_ = from.queue_flags_;
199 destroyed_ = from.destroyed_;
200 access_log_ = from.access_log_; // potentially large, but no choice given tagging lookup.
201 command_number_ = from.command_number_;
202 subcommand_number_ = from.subcommand_number_;
203 reset_count_ = from.reset_count_;
204
205 const auto *from_context = from.GetCurrentAccessContext();
206 assert(from_context);
207
208 // Construct a fully resolved single access context out of from
209 const NoopBarrierAction noop_barrier;
210 for (AccessAddressType address_type : kAddressTypes) {
211 from_context->ResolveAccessRange(address_type, kFullRange, noop_barrier,
212 &cb_access_context_.GetAccessStateMap(address_type), nullptr);
213 }
214 // The proxy has flatten the current render pass context (if any), but the async contexts are needed for hazard detection
215 cb_access_context_.ImportAsyncContexts(*from_context);
216
217 events_context_ = from.events_context_;
218
219 // We don't want to copy the full render_pass_context_ history just for the proxy.
220}
221
222std::string CommandBufferAccessContext::FormatUsage(const ResourceUsageTag tag) const {
223 std::stringstream out;
224 assert(tag < access_log_.size());
225 const auto &record = access_log_[tag];
226 out << string_UsageTag(record);
227 if (record.cb_state != cb_state_.get()) {
228 out << ", command_buffer: " << sync_state_->report_data->FormatHandle(record.cb_state->commandBuffer()).c_str();
229 if (record.cb_state->Destroyed()) {
230 out << " (destroyed)";
231 }
232
John Zulauf4fa68462021-04-26 21:04:22 -0600233 }
John Zulaufd142c9a2022-04-12 14:22:44 -0600234 out << ", reset_no: " << std::to_string(record.reset_count);
John Zulauf4fa68462021-04-26 21:04:22 -0600235 return out.str();
236}
237std::string CommandBufferAccessContext::FormatUsage(const ResourceFirstAccess &access) const {
238 std::stringstream out;
239 out << "(recorded_usage: " << string_UsageIndex(access.usage_index);
240 out << ", " << FormatUsage(access.tag) << ")";
241 return out.str();
242}
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700243
John Zulauffaea0ee2021-01-14 14:01:32 -0700244std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600245 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600246 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
247 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600248 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600249 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
250 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf4fa68462021-04-26 21:04:22 -0600251 out << "(";
252 if (!hazard.recorded_access.get()) {
253 // if we have a recorded usage the usage is reported from the recorded contexts point of view
254 out << "usage: " << usage_info.name << ", ";
255 }
256 out << "prior_usage: " << stage_access_name;
John Zulauf59e25072020-07-17 10:55:21 -0600257 if (IsHazardVsRead(hazard.hazard)) {
258 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700259 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600260 } else {
261 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
262 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
263 }
264
ziga-lunarg0f248902022-03-24 16:42:45 +0100265 if (tag < access_log_.size()) {
266 out << ", " << FormatUsage(tag) << ")";
267 }
John Zulauf1dae9192020-06-16 15:46:44 -0600268 return out.str();
269}
270
John Zulaufd14743a2020-07-03 09:42:39 -0600271// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
272// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
273// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700274static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700275static const SyncStageAccessFlags kColorAttachmentAccessScope =
276 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
277 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
278 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
279 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700280static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
281 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 -0700282static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
283 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
284 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
285 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700286static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700287static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600288
John Zulauf8e3c3e92021-01-06 11:19:36 -0700289ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700290 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700291 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
292 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
293 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
294
John Zulaufee984022022-04-13 16:39:50 -0600295// Sometimes we have an internal access conflict, and we using the kInvalidTag to set and detect in temporary/proxy contexts
296static const ResourceUsageTag kInvalidTag(ResourceUsageRecord::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600297
Jeremy Gebben62c3bf42021-07-21 15:38:24 -0600298static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) { return bindable.GetFakeBaseAddress(); }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600299
locke-lunarg3c038002020-04-30 23:08:08 -0600300inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
301 if (size == VK_WHOLE_SIZE) {
302 return (whole_size - offset);
303 }
304 return size;
305}
306
John Zulauf3e86bf02020-09-12 10:47:57 -0600307static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
308 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
309}
310
John Zulauf16adfc92020-04-08 10:28:33 -0600311template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600312static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600313 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
314}
315
John Zulauf355e49b2020-04-24 15:11:15 -0600316static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600317
John Zulauf3e86bf02020-09-12 10:47:57 -0600318static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
319 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
320}
321
322static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
323 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
324}
325
John Zulauf4a6105a2020-11-17 15:11:05 -0700326// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
327//
John Zulauf10f1f522020-12-18 12:00:35 -0700328// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
329//
John Zulauf4a6105a2020-11-17 15:11:05 -0700330// Usage:
331// Constructor() -- initializes the generator to point to the begin of the space declared.
332// * -- the current range of the generator empty signfies end
333// ++ -- advance to the next non-empty range (or end)
334
335// A wrapper for a single range with the same semantics as the actual generators below
336template <typename KeyType>
337class SingleRangeGenerator {
338 public:
339 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700340 const KeyType &operator*() const { return current_; }
341 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700342 SingleRangeGenerator &operator++() {
343 current_ = KeyType(); // just one real range
344 return *this;
345 }
346
347 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
348
349 private:
350 SingleRangeGenerator() = default;
351 const KeyType range_;
352 KeyType current_;
353};
354
John Zulaufae842002021-04-15 18:20:55 -0600355// Generate the ranges that are the intersection of range and the entries in the RangeMap
356template <typename RangeMap, typename KeyType = typename RangeMap::key_type>
357class MapRangesRangeGenerator {
John Zulauf4a6105a2020-11-17 15:11:05 -0700358 public:
John Zulaufd5115702021-01-18 12:34:33 -0700359 // Default constructed is safe to dereference for "empty" test, but for no other operation.
John Zulaufae842002021-04-15 18:20:55 -0600360 MapRangesRangeGenerator() : range_(), map_(nullptr), map_pos_(), current_() {
John Zulaufd5115702021-01-18 12:34:33 -0700361 // Default construction for KeyType *must* be empty range
362 assert(current_.empty());
363 }
John Zulaufae842002021-04-15 18:20:55 -0600364 MapRangesRangeGenerator(const RangeMap &filter, const KeyType &range) : range_(range), map_(&filter), map_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700365 SeekBegin();
366 }
John Zulaufae842002021-04-15 18:20:55 -0600367 MapRangesRangeGenerator(const MapRangesRangeGenerator &from) = default;
John Zulaufd5115702021-01-18 12:34:33 -0700368
John Zulauf4a6105a2020-11-17 15:11:05 -0700369 const KeyType &operator*() const { return current_; }
370 const KeyType *operator->() const { return &current_; }
John Zulaufae842002021-04-15 18:20:55 -0600371 MapRangesRangeGenerator &operator++() {
372 ++map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700373 UpdateCurrent();
374 return *this;
375 }
376
John Zulaufae842002021-04-15 18:20:55 -0600377 bool operator==(const MapRangesRangeGenerator &other) const { return current_ == other.current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700378
John Zulaufae842002021-04-15 18:20:55 -0600379 protected:
John Zulauf4a6105a2020-11-17 15:11:05 -0700380 void UpdateCurrent() {
John Zulaufae842002021-04-15 18:20:55 -0600381 if (map_pos_ != map_->cend()) {
382 current_ = range_ & map_pos_->first;
John Zulauf4a6105a2020-11-17 15:11:05 -0700383 } else {
384 current_ = KeyType();
385 }
386 }
387 void SeekBegin() {
John Zulaufae842002021-04-15 18:20:55 -0600388 map_pos_ = map_->lower_bound(range_);
John Zulauf4a6105a2020-11-17 15:11:05 -0700389 UpdateCurrent();
390 }
John Zulaufae842002021-04-15 18:20:55 -0600391
392 // Adding this functionality here, to avoid gratuitous Base:: qualifiers in the derived class
393 // Note: Not exposed in this classes public interface to encourage using a consistent ++/empty generator semantic
394 template <typename Pred>
395 MapRangesRangeGenerator &PredicatedIncrement(Pred &pred) {
396 do {
397 ++map_pos_;
398 } while (map_pos_ != map_->cend() && map_pos_->first.intersects(range_) && !pred(map_pos_));
399 UpdateCurrent();
400 return *this;
401 }
402
John Zulauf4a6105a2020-11-17 15:11:05 -0700403 const KeyType range_;
John Zulaufae842002021-04-15 18:20:55 -0600404 const RangeMap *map_;
405 typename RangeMap::const_iterator map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700406 KeyType current_;
407};
John Zulaufd5115702021-01-18 12:34:33 -0700408using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulaufae842002021-04-15 18:20:55 -0600409using EventSimpleRangeGenerator = MapRangesRangeGenerator<SyncEventState::ScopeMap>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700410
John Zulaufae842002021-04-15 18:20:55 -0600411// Generate the ranges for entries meeting the predicate that are the intersection of range and the entries in the RangeMap
412template <typename RangeMap, typename Predicate, typename KeyType = typename RangeMap::key_type>
413class PredicatedMapRangesRangeGenerator : public MapRangesRangeGenerator<RangeMap, KeyType> {
414 public:
415 using Base = MapRangesRangeGenerator<RangeMap, KeyType>;
416 // Default constructed is safe to dereference for "empty" test, but for no other operation.
417 PredicatedMapRangesRangeGenerator() : Base(), pred_() {}
418 PredicatedMapRangesRangeGenerator(const RangeMap &filter, const KeyType &range, Predicate pred)
419 : Base(filter, range), pred_(pred) {}
420 PredicatedMapRangesRangeGenerator(const PredicatedMapRangesRangeGenerator &from) = default;
421
422 PredicatedMapRangesRangeGenerator &operator++() {
423 Base::PredicatedIncrement(pred_);
424 return *this;
425 }
426
427 protected:
428 Predicate pred_;
429};
John Zulauf4a6105a2020-11-17 15:11:05 -0700430
431// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulaufae842002021-04-15 18:20:55 -0600432// Templated to allow for different Range generators or map sources...
433template <typename RangeMap, typename RangeGen, typename KeyType = typename RangeMap::key_type>
John Zulauf4a6105a2020-11-17 15:11:05 -0700434class FilteredGeneratorGenerator {
435 public:
John Zulaufd5115702021-01-18 12:34:33 -0700436 // Default constructed is safe to dereference for "empty" test, but for no other operation.
437 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
438 // Default construction for KeyType *must* be empty range
439 assert(current_.empty());
440 }
John Zulaufae842002021-04-15 18:20:55 -0600441 FilteredGeneratorGenerator(const RangeMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700442 SeekBegin();
443 }
John Zulaufd5115702021-01-18 12:34:33 -0700444 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700445 const KeyType &operator*() const { return current_; }
446 const KeyType *operator->() const { return &current_; }
447 FilteredGeneratorGenerator &operator++() {
448 KeyType gen_range = GenRange();
449 KeyType filter_range = FilterRange();
450 current_ = KeyType();
451 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
452 if (gen_range.end > filter_range.end) {
453 // if the generated range is beyond the filter_range, advance the filter range
454 filter_range = AdvanceFilter();
455 } else {
456 gen_range = AdvanceGen();
457 }
458 current_ = gen_range & filter_range;
459 }
460 return *this;
461 }
462
463 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
464
465 private:
466 KeyType AdvanceFilter() {
467 ++filter_pos_;
468 auto filter_range = FilterRange();
469 if (filter_range.valid()) {
470 FastForwardGen(filter_range);
471 }
472 return filter_range;
473 }
474 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700475 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700476 auto gen_range = GenRange();
477 if (gen_range.valid()) {
478 FastForwardFilter(gen_range);
479 }
480 return gen_range;
481 }
482
483 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700484 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700485
486 KeyType FastForwardFilter(const KeyType &range) {
487 auto filter_range = FilterRange();
488 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700489 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700490 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
491 if (retry_count < kRetryLimit) {
492 ++filter_pos_;
493 filter_range = FilterRange();
494 retry_count++;
495 } else {
496 // Okay we've tried walking, do a seek.
497 filter_pos_ = filter_->lower_bound(range);
498 break;
499 }
500 }
501 return FilterRange();
502 }
503
504 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
505 // faster.
506 KeyType FastForwardGen(const KeyType &range) {
507 auto gen_range = GenRange();
508 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700509 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700510 gen_range = GenRange();
511 }
512 return gen_range;
513 }
514
515 void SeekBegin() {
516 auto gen_range = GenRange();
517 if (gen_range.empty()) {
518 current_ = KeyType();
519 filter_pos_ = filter_->cend();
520 } else {
521 filter_pos_ = filter_->lower_bound(gen_range);
522 current_ = gen_range & FilterRange();
523 }
524 }
525
John Zulaufae842002021-04-15 18:20:55 -0600526 const RangeMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700527 RangeGen gen_;
John Zulaufae842002021-04-15 18:20:55 -0600528 typename RangeMap::const_iterator filter_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700529 KeyType current_;
530};
531
532using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
533
John Zulauf5c5e88d2019-12-26 11:22:02 -0700534
John Zulauf3e86bf02020-09-12 10:47:57 -0600535ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
536 VkDeviceSize stride) {
537 VkDeviceSize range_start = offset + first_index * stride;
538 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600539 if (count == UINT32_MAX) {
540 range_size = buf_whole_size - range_start;
541 } else {
542 range_size = count * stride;
543 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600544 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600545}
546
locke-lunarg654e3692020-06-04 17:19:15 -0600547SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
548 VkShaderStageFlagBits stage_flag) {
549 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
550 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
551 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
552 }
553 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
554 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
555 assert(0);
556 }
557 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
558 return stage_access->second.uniform_read;
559 }
560
561 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
562 // Because if write hazard happens, read hazard might or might not happen.
563 // But if write hazard doesn't happen, read hazard is impossible to happen.
564 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700565 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600566 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700567 // TODO: sampled_read
568 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600569}
570
locke-lunarg37047832020-06-12 13:44:45 -0600571bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
572 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
573 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
574 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
575 ? true
576 : false;
577}
578
579bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
580 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
581 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
582 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
583 ? true
584 : false;
585}
586
John Zulauf355e49b2020-04-24 15:11:15 -0600587// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600588template <typename Action>
589static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
590 Action &action) {
591 // At this point the "apply over range" logic only supports a single memory binding
592 if (!SimpleBinding(image_state)) return;
593 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600594 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700595 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
596 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600597 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700598 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600599 }
600}
601
John Zulauf7635de32020-05-29 17:14:15 -0600602// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
603// Used by both validation and record operations
604//
605// The signature for Action() reflect the needs of both uses.
606template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700607void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
608 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600609 const auto &rp_ci = rp_state.createInfo;
610 const auto *attachment_ci = rp_ci.pAttachments;
611 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
612
613 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
614 const auto *color_attachments = subpass_ci.pColorAttachments;
615 const auto *color_resolve = subpass_ci.pResolveAttachments;
616 if (color_resolve && color_attachments) {
617 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
618 const auto &color_attach = color_attachments[i].attachment;
619 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
620 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
621 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700622 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
623 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600624 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700625 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
626 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600627 }
628 }
629 }
630
631 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700632 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600633 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
634 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
635 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
636 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
637 const auto src_ci = attachment_ci[src_at];
638 // The formats are required to match so we can pick either
639 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
640 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
641 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600642
643 // Figure out which aspects are actually touched during resolve operations
644 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700645 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600646 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600647 aspect_string = "depth/stencil";
648 } else if (resolve_depth) {
649 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700650 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600651 aspect_string = "depth";
652 } else if (resolve_stencil) {
653 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700654 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600655 aspect_string = "stencil";
656 }
657
John Zulaufd0ec59f2021-03-13 14:25:08 -0700658 if (aspect_string) {
659 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
660 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
661 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
662 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600663 }
664 }
665}
666
667// Action for validating resolve operations
668class ValidateResolveAction {
669 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700670 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700671 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600672 : render_pass_(render_pass),
673 subpass_(subpass),
674 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700675 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600676 func_name_(func_name),
677 skip_(false) {}
678 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 -0700679 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
680 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600681 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700682 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600683 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700684 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700685 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700686 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
687 " to resolve attachment %" PRIu32 ". Access info %s.",
688 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700689 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600690 }
691 }
692 // Providing a mechanism for the constructing caller to get the result of the validation
693 bool GetSkip() const { return skip_; }
694
695 private:
696 VkRenderPass render_pass_;
697 const uint32_t subpass_;
698 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700699 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600700 const char *func_name_;
701 bool skip_;
702};
703
704// Update action for resolve operations
705class UpdateStateResolveAction {
706 public:
John Zulauf14940722021-04-12 15:19:02 -0600707 UpdateStateResolveAction(AccessContext &context, ResourceUsageTag tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700708 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
709 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600710 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700711 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600712 }
713
714 private:
715 AccessContext &context_;
John Zulauf14940722021-04-12 15:19:02 -0600716 const ResourceUsageTag tag_;
John Zulauf7635de32020-05-29 17:14:15 -0600717};
718
John Zulauf59e25072020-07-17 10:55:21 -0600719void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
John Zulauf14940722021-04-12 15:19:02 -0600720 const SyncStageAccessFlags &prior_, const ResourceUsageTag tag_) {
John Zulauf4fa68462021-04-26 21:04:22 -0600721 access_state = layer_data::make_unique<const ResourceAccessState>(*access_state_);
John Zulauf59e25072020-07-17 10:55:21 -0600722 usage_index = usage_index_;
723 hazard = hazard_;
724 prior_access = prior_;
725 tag = tag_;
726}
727
John Zulauf4fa68462021-04-26 21:04:22 -0600728void HazardResult::AddRecordedAccess(const ResourceFirstAccess &first_access) {
729 recorded_access = layer_data::make_unique<const ResourceFirstAccess>(first_access);
730}
731
John Zulauf540266b2020-04-06 18:54:53 -0600732AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
733 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600734 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600735 Reset();
736 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700737 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
738 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600739 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600740 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600741 const auto prev_pass = prev_dep.first->pass;
742 const auto &prev_barriers = prev_dep.second;
743 assert(prev_dep.second.size());
744 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
745 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700746 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600747
748 async_.reserve(subpass_dep.async.size());
749 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700750 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600751 }
John Zulauf22aefed2021-03-11 18:14:35 -0700752 if (has_barrier_from_external) {
753 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
754 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
755 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600756 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600757 if (subpass_dep.barrier_to_external.size()) {
758 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600759 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700760}
761
John Zulauf5f13a792020-03-10 07:31:21 -0600762template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700763HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600764 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600765 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600766 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600767
768 HazardResult hazard;
769 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
770 hazard = detector.Detect(prev);
771 }
772 return hazard;
773}
774
John Zulauf4a6105a2020-11-17 15:11:05 -0700775template <typename Action>
776void AccessContext::ForAll(Action &&action) {
777 for (const auto address_type : kAddressTypes) {
778 auto &accesses = GetAccessStateMap(address_type);
779 for (const auto &access : accesses) {
780 action(address_type, access);
781 }
782 }
783}
784
John Zulauf3d84f1b2020-03-09 13:33:25 -0600785// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
786// the DAG of the contexts (for example subpasses)
787template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700788HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600789 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600790 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600791
John Zulauf1a224292020-06-30 14:52:13 -0600792 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600793 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
794 // so we'll check these first
795 for (const auto &async_context : async_) {
796 hazard = async_context->DetectAsyncHazard(type, detector, range);
797 if (hazard.hazard) return hazard;
798 }
John Zulauf5f13a792020-03-10 07:31:21 -0600799 }
800
John Zulauf1a224292020-06-30 14:52:13 -0600801 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600802
John Zulauf69133422020-05-20 14:55:53 -0600803 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600804 const auto the_end = accesses.cend(); // End is not invalidated
805 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600806 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600807
John Zulauf3cafbf72021-03-26 16:55:19 -0600808 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600809 // Cover any leading gap, or gap between entries
810 if (detect_prev) {
811 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
812 // Cover any leading gap, or gap between entries
813 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600814 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600815 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600816 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600817 if (hazard.hazard) return hazard;
818 }
John Zulauf69133422020-05-20 14:55:53 -0600819 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
820 gap.begin = pos->first.end;
821 }
822
823 hazard = detector.Detect(pos);
824 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600825 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600826 }
827
828 if (detect_prev) {
829 // Detect in the trailing empty as needed
830 gap.end = range.end;
831 if (gap.non_empty()) {
832 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600833 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600834 }
835
836 return hazard;
837}
838
839// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
840template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700841HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
842 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600843 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600844 auto pos = accesses.lower_bound(range);
845 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600846
John Zulauf3d84f1b2020-03-09 13:33:25 -0600847 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600848 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700849 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600850 if (hazard.hazard) break;
851 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600852 }
John Zulauf16adfc92020-04-08 10:28:33 -0600853
John Zulauf3d84f1b2020-03-09 13:33:25 -0600854 return hazard;
855}
856
John Zulaufb02c1eb2020-10-06 16:33:36 -0600857struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700858 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600859 void operator()(ResourceAccessState *access) const {
860 assert(access);
861 access->ApplyBarriers(barriers, true);
862 }
863 const std::vector<SyncBarrier> &barriers;
864};
865
John Zulauf22aefed2021-03-11 18:14:35 -0700866struct ApplyTrackbackStackAction {
867 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
868 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
869 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600870 void operator()(ResourceAccessState *access) const {
871 assert(access);
872 assert(!access->HasPendingState());
873 access->ApplyBarriers(barriers, false);
John Zulaufee984022022-04-13 16:39:50 -0600874 // NOTE: We can use invalid tag, as these barriers do no include layout transitions (would assert in SetWrite)
875 access->ApplyPendingBarriers(kInvalidTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700876 if (previous_barrier) {
877 assert(bool(*previous_barrier));
878 (*previous_barrier)(access);
879 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600880 }
881 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700882 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600883};
884
885// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
886// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
887// *different* map from dest.
888// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
889// range [first, last)
890template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600891static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
892 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600893 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600894 auto at = entry;
895 for (auto pos = first; pos != last; ++pos) {
896 // Every member of the input iterator range must fit within the remaining portion of entry
897 assert(at->first.includes(pos->first));
898 assert(at != dest->end());
899 // Trim up at to the same size as the entry to resolve
900 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600901 auto access = pos->second; // intentional copy
902 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600903 at->second.Resolve(access);
904 ++at; // Go to the remaining unused section of entry
905 }
906}
907
John Zulaufa0a98292020-09-18 09:30:10 -0600908static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
909 SyncBarrier merged = {};
910 for (const auto &barrier : barriers) {
911 merged.Merge(barrier);
912 }
913 return merged;
914}
915
John Zulaufb02c1eb2020-10-06 16:33:36 -0600916template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700917void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600918 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
919 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600920 if (!range.non_empty()) return;
921
John Zulauf355e49b2020-04-24 15:11:15 -0600922 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
923 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600924 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600925 if (current->pos_B->valid) {
926 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600927 auto access = src_pos->second; // intentional copy
928 barrier_action(&access);
929
John Zulauf16adfc92020-04-08 10:28:33 -0600930 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600931 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
932 trimmed->second.Resolve(access);
933 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600934 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600935 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600936 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600937 }
John Zulauf16adfc92020-04-08 10:28:33 -0600938 } else {
939 // we have to descend to fill this gap
940 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700941 ResourceAccessRange recurrence_range = current_range;
942 // The current context is empty for the current range, so recur to fill the gap.
943 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
944 // is not valid, to minimize that recurrence
945 if (current->pos_B.at_end()) {
946 // Do the remainder here....
947 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600948 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700949 // Recur only over the range until B becomes valid (within the limits of range).
950 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600951 }
John Zulauf22aefed2021-03-11 18:14:35 -0700952 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
953
John Zulauf355e49b2020-04-24 15:11:15 -0600954 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
955 // iterator of the outer while.
956
957 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
958 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
959 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700960 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 -0600961 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600962 current.seek(seek_to);
963 } else if (!current->pos_A->valid && infill_state) {
964 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
965 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
966 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600967 }
John Zulauf5f13a792020-03-10 07:31:21 -0600968 }
ziga-lunargf0e27ad2022-03-28 00:44:12 +0200969 if (current->range.non_empty()) {
970 ++current;
971 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600972 }
John Zulauf1a224292020-06-30 14:52:13 -0600973
974 // Infill if range goes passed both the current and resolve map prior contents
975 if (recur_to_infill && (current->range.end < range.end)) {
976 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700977 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600978 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600979}
980
John Zulauf22aefed2021-03-11 18:14:35 -0700981template <typename BarrierAction>
982void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
983 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
984 const BarrierAction &previous_barrier) const {
985 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
986 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
987}
988
John Zulauf43cc7462020-12-03 12:33:12 -0700989void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700990 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
991 const ResourceAccessStateFunction *previous_barrier) const {
992 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600993 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700994 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
995 ResourceAccessState state_copy;
996 if (previous_barrier) {
997 assert(bool(*previous_barrier));
998 state_copy = *infill_state;
999 (*previous_barrier)(&state_copy);
1000 infill_state = &state_copy;
1001 }
1002 sparse_container::update_range_value(*descent_map, range, *infill_state,
1003 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -06001004 }
1005 } else {
1006 // Look for something to fill the gap further along.
1007 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -07001008 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001009 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001010 }
John Zulauf5f13a792020-03-10 07:31:21 -06001011 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001012}
1013
John Zulauf4a6105a2020-11-17 15:11:05 -07001014// Non-lazy import of all accesses, WaitEvents needs this.
1015void AccessContext::ResolvePreviousAccesses() {
1016 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -07001017 if (!prev_.size()) return; // If no previous contexts, nothing to do
1018
John Zulauf4a6105a2020-11-17 15:11:05 -07001019 for (const auto address_type : kAddressTypes) {
1020 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
1021 }
1022}
1023
John Zulauf43cc7462020-12-03 12:33:12 -07001024AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
1025 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -06001026}
1027
John Zulauf1507ee42020-05-18 11:33:09 -06001028static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001029 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1030 ? SYNC_ACCESS_INDEX_NONE
1031 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
1032 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001033 return stage_access;
1034}
1035static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001036 const auto stage_access =
1037 (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1038 ? SYNC_ACCESS_INDEX_NONE
1039 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
1040 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001041 return stage_access;
1042}
1043
John Zulauf7635de32020-05-29 17:14:15 -06001044// Caller must manage returned pointer
1045static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001046 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -06001047 auto *proxy = new AccessContext(context);
John Zulaufee984022022-04-13 16:39:50 -06001048 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kInvalidTag);
1049 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kInvalidTag);
John Zulauf7635de32020-05-29 17:14:15 -06001050 return proxy;
1051}
1052
John Zulaufb02c1eb2020-10-06 16:33:36 -06001053template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001054void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1055 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
1056 const ResourceAccessState *infill_state) const {
1057 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1058 if (!attachment_gen) return;
1059
1060 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1061 const AccessAddressType address_type = view_gen.GetAddressType();
1062 for (; range_gen->non_empty(); ++range_gen) {
1063 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001064 }
John Zulauf62f10592020-04-03 12:20:02 -06001065}
1066
John Zulauf7635de32020-05-29 17:14:15 -06001067// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -07001068bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001069 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001070 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001071 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001072 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
1073 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
1074 // those affects have not been recorded yet.
1075 //
1076 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1077 // to apply and only copy then, if this proves a hot spot.
1078 std::unique_ptr<AccessContext> proxy_for_prev;
1079 TrackBack proxy_track_back;
1080
John Zulauf355e49b2020-04-24 15:11:15 -06001081 const auto &transitions = rp_state.subpass_transitions[subpass];
1082 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001083 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1084
1085 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001086 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001087 if (prev_needs_proxy) {
1088 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001089 proxy_for_prev.reset(
1090 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -06001091 proxy_track_back = *track_back;
1092 proxy_track_back.context = proxy_for_prev.get();
1093 }
1094 track_back = &proxy_track_back;
1095 }
1096 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001097 if (hazard.hazard) {
John Zulaufee984022022-04-13 16:39:50 -06001098 if (hazard.tag == kInvalidTag) {
1099 skip |= ex_context.GetSyncState().LogError(
1100 rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
1101 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1102 " image layout transition (old_layout: %s, new_layout: %s) after store/resolve operation in subpass %" PRIu32,
1103 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1104 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout), transition.prev_pass);
1105 } else {
1106 skip |= ex_context.GetSyncState().LogError(
1107 rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
1108 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1109 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1110 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1111 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
1112 ex_context.FormatUsage(hazard).c_str());
1113 }
John Zulauf355e49b2020-04-24 15:11:15 -06001114 }
1115 }
1116 return skip;
1117}
1118
John Zulauf64ffe552021-02-06 10:25:07 -07001119bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001120 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001121 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001122 bool skip = false;
1123 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001124
John Zulauf1507ee42020-05-18 11:33:09 -06001125 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1126 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001127 const auto &view_gen = attachment_views[i];
1128 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001129 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001130
1131 // Need check in the following way
1132 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1133 // vs. transition
1134 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1135 // for each aspect loaded.
1136
1137 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001138 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001139 const bool is_color = !(has_depth || has_stencil);
1140
1141 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001142 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001143
John Zulaufaff20662020-06-01 14:07:58 -06001144 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001145 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001146
John Zulaufb02c1eb2020-10-06 16:33:36 -06001147 bool checked_stencil = false;
John Zulauf57261402021-08-13 11:32:06 -06001148 if (is_color && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001149 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001150 aspect = "color";
1151 } else {
John Zulauf57261402021-08-13 11:32:06 -06001152 if (has_depth && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001153 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1154 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001155 aspect = "depth";
1156 }
John Zulauf57261402021-08-13 11:32:06 -06001157 if (!hazard.hazard && has_stencil && (stencil_load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001158 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1159 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001160 aspect = "stencil";
1161 checked_stencil = true;
1162 }
1163 }
1164
1165 if (hazard.hazard) {
1166 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001167 const auto &sync_state = ex_context.GetSyncState();
John Zulaufee984022022-04-13 16:39:50 -06001168 if (hazard.tag == kInvalidTag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001169 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001170 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001171 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1172 " aspect %s during load with loadOp %s.",
1173 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1174 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001175 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001176 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001177 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001178 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001179 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001180 }
1181 }
1182 }
1183 }
1184 return skip;
1185}
1186
John Zulaufaff20662020-06-01 14:07:58 -06001187// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1188// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1189// store is part of the same Next/End operation.
1190// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001191bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001192 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001193 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001194 bool skip = false;
1195 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001196
1197 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1198 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001199 const AttachmentViewGen &view_gen = attachment_views[i];
1200 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001201 const auto &ci = attachment_ci[i];
1202
1203 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1204 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1205 // sake, we treat DONT_CARE as writing.
1206 const bool has_depth = FormatHasDepth(ci.format);
1207 const bool has_stencil = FormatHasStencil(ci.format);
1208 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001209 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001210 if (!has_stencil && !store_op_stores) continue;
1211
1212 HazardResult hazard;
1213 const char *aspect = nullptr;
1214 bool checked_stencil = false;
1215 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001216 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1217 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001218 aspect = "color";
1219 } else {
John Zulauf57261402021-08-13 11:32:06 -06001220 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001221 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001222 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1223 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001224 aspect = "depth";
1225 }
1226 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001227 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1228 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001229 aspect = "stencil";
1230 checked_stencil = true;
1231 }
1232 }
1233
1234 if (hazard.hazard) {
1235 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1236 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001237 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001238 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1239 " %s aspect during store with %s %s. Access info %s",
1240 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001241 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001242 }
1243 }
1244 }
1245 return skip;
1246}
1247
John Zulauf64ffe552021-02-06 10:25:07 -07001248bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001249 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1250 const char *func_name, uint32_t subpass) const {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001251 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001252 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001253 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001254}
1255
John Zulauf3d84f1b2020-03-09 13:33:25 -06001256class HazardDetector {
1257 SyncStageAccessIndex usage_index_;
1258
1259 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001260 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf14940722021-04-12 15:19:02 -06001261 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001262 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001263 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001264 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001265};
1266
John Zulauf69133422020-05-20 14:55:53 -06001267class HazardDetectorWithOrdering {
1268 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001269 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001270
1271 public:
1272 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001273 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001274 }
John Zulauf14940722021-04-12 15:19:02 -06001275 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001276 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001277 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001278 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001279};
1280
John Zulauf16adfc92020-04-08 10:28:33 -06001281HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001282 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001283 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001284 const auto base_address = ResourceBaseAddress(buffer);
1285 HazardDetector detector(usage_index);
1286 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001287}
1288
John Zulauf69133422020-05-20 14:55:53 -06001289template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001290HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1291 DetectOptions options) const {
1292 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1293 if (!attachment_gen) return HazardResult();
1294
1295 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1296 const auto address_type = view_gen.GetAddressType();
1297 for (; range_gen->non_empty(); ++range_gen) {
1298 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1299 if (hazard.hazard) return hazard;
1300 }
1301
1302 return HazardResult();
1303}
1304
1305template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001306HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1307 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1308 const VkExtent3D &extent, DetectOptions options) const {
1309 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001310 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001311 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1312 base_address);
1313 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001314 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001315 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001316 if (hazard.hazard) return hazard;
1317 }
1318 return HazardResult();
1319}
John Zulauf110413c2021-03-20 05:38:38 -06001320template <typename Detector>
1321HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1322 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1323 if (!SimpleBinding(image)) return HazardResult();
1324 const auto base_address = ResourceBaseAddress(image);
1325 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1326 const auto address_type = ImageAddressType(image);
1327 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001328 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1329 if (hazard.hazard) return hazard;
1330 }
1331 return HazardResult();
1332}
John Zulauf69133422020-05-20 14:55:53 -06001333
John Zulauf540266b2020-04-06 18:54:53 -06001334HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1335 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1336 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001337 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1338 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001339 HazardDetector detector(current_usage);
1340 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001341}
1342
1343HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001344 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001345 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001346 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001347}
1348
John Zulaufd0ec59f2021-03-13 14:25:08 -07001349HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1350 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1351 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1352 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1353}
1354
John Zulauf69133422020-05-20 14:55:53 -06001355HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001356 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001357 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001358 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001359 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001360}
1361
John Zulauf3d84f1b2020-03-09 13:33:25 -06001362class BarrierHazardDetector {
1363 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001364 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001365 SyncStageAccessFlags src_access_scope)
1366 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1367
John Zulauf5f13a792020-03-10 07:31:21 -06001368 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1369 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001370 }
John Zulauf14940722021-04-12 15:19:02 -06001371 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001372 // 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 -07001373 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001374 }
1375
1376 private:
1377 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001378 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001379 SyncStageAccessFlags src_access_scope_;
1380};
1381
John Zulauf4a6105a2020-11-17 15:11:05 -07001382class EventBarrierHazardDetector {
1383 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001384 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001385 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
John Zulauf14940722021-04-12 15:19:02 -06001386 ResourceUsageTag scope_tag)
John Zulauf4a6105a2020-11-17 15:11:05 -07001387 : usage_index_(usage_index),
1388 src_exec_scope_(src_exec_scope),
1389 src_access_scope_(src_access_scope),
1390 event_scope_(event_scope),
1391 scope_pos_(event_scope.cbegin()),
1392 scope_end_(event_scope.cend()),
1393 scope_tag_(scope_tag) {}
1394
1395 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1396 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1397 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1398 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1399 if (scope_pos_ == scope_end_) return HazardResult();
1400 if (!scope_pos_->first.intersects(pos->first)) {
1401 event_scope_.lower_bound(pos->first);
1402 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1403 }
1404
1405 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1406 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1407 }
John Zulauf14940722021-04-12 15:19:02 -06001408 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07001409 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1410 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1411 }
1412
1413 private:
1414 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001415 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001416 SyncStageAccessFlags src_access_scope_;
1417 const SyncEventState::ScopeMap &event_scope_;
1418 SyncEventState::ScopeMap::const_iterator scope_pos_;
1419 SyncEventState::ScopeMap::const_iterator scope_end_;
John Zulauf14940722021-04-12 15:19:02 -06001420 const ResourceUsageTag scope_tag_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001421};
1422
Jeremy Gebben40a22942020-12-22 14:22:06 -07001423HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001424 const SyncStageAccessFlags &src_access_scope,
1425 const VkImageSubresourceRange &subresource_range,
1426 const SyncEventState &sync_event, DetectOptions options) const {
1427 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1428 // first access scope map to use, and there's no easy way to plumb it in below.
1429 const auto address_type = ImageAddressType(image);
1430 const auto &event_scope = sync_event.FirstScope(address_type);
1431
1432 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1433 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001434 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001435}
1436
John Zulaufd0ec59f2021-03-13 14:25:08 -07001437HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1438 DetectOptions options) const {
1439 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1440 barrier.src_access_scope);
1441 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1442}
1443
Jeremy Gebben40a22942020-12-22 14:22:06 -07001444HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001445 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001446 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001447 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001448 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001449 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001450}
1451
Jeremy Gebben40a22942020-12-22 14:22:06 -07001452HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001453 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001454 const VkImageMemoryBarrier &barrier) const {
1455 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1456 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1457 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1458}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001459HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001460 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001461 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001462}
John Zulauf355e49b2020-04-24 15:11:15 -06001463
John Zulauf9cb530d2019-09-30 14:14:10 -06001464template <typename Flags, typename Map>
1465SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1466 SyncStageAccessFlags scope = 0;
1467 for (const auto &bit_scope : map) {
1468 if (flag_mask < bit_scope.first) break;
1469
1470 if (flag_mask & bit_scope.first) {
1471 scope |= bit_scope.second;
1472 }
1473 }
1474 return scope;
1475}
1476
Jeremy Gebben40a22942020-12-22 14:22:06 -07001477SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001478 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1479}
1480
Jeremy Gebben40a22942020-12-22 14:22:06 -07001481SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1482 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001483}
1484
Jeremy Gebben40a22942020-12-22 14:22:06 -07001485// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1486SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001487 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1488 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1489 // 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 -06001490 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1491}
1492
1493template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001494void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001495 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1496 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001497 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001498 auto pos = accesses->lower_bound(range);
1499 if (pos == accesses->end() || !pos->first.intersects(range)) {
1500 // The range is empty, fill it with a default value.
1501 pos = action.Infill(accesses, pos, range);
1502 } else if (range.begin < pos->first.begin) {
1503 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001504 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001505 } else if (pos->first.begin < range.begin) {
1506 // Trim the beginning if needed
1507 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1508 ++pos;
1509 }
1510
1511 const auto the_end = accesses->end();
1512 while ((pos != the_end) && pos->first.intersects(range)) {
1513 if (pos->first.end > range.end) {
1514 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1515 }
1516
1517 pos = action(accesses, pos);
1518 if (pos == the_end) break;
1519
1520 auto next = pos;
1521 ++next;
1522 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1523 // Need to infill if next is disjoint
1524 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001525 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001526 next = action.Infill(accesses, next, new_range);
1527 }
1528 pos = next;
1529 }
1530}
John Zulaufd5115702021-01-18 12:34:33 -07001531
1532// Give a comparable interface for range generators and ranges
1533template <typename Action>
1534inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1535 assert(range);
1536 UpdateMemoryAccessState(accesses, *range, action);
1537}
1538
John Zulauf4a6105a2020-11-17 15:11:05 -07001539template <typename Action, typename RangeGen>
1540void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1541 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001542 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 -07001543 for (; range_gen->non_empty(); ++range_gen) {
1544 UpdateMemoryAccessState(accesses, *range_gen, action);
1545 }
1546}
John Zulauf9cb530d2019-09-30 14:14:10 -06001547
John Zulaufd0ec59f2021-03-13 14:25:08 -07001548template <typename Action, typename RangeGen>
1549void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1550 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1551 for (; range_gen->non_empty(); ++range_gen) {
1552 UpdateMemoryAccessState(accesses, *range_gen, action);
1553 }
1554}
John Zulauf9cb530d2019-09-30 14:14:10 -06001555struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001556 using Iterator = ResourceAccessRangeMap::iterator;
1557 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001558 // this is only called on gaps, and never returns a gap.
1559 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001560 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001561 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001562 }
John Zulauf5f13a792020-03-10 07:31:21 -06001563
John Zulauf5c5e88d2019-12-26 11:22:02 -07001564 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001565 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001566 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001567 return pos;
1568 }
1569
John Zulauf43cc7462020-12-03 12:33:12 -07001570 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf14940722021-04-12 15:19:02 -06001571 SyncOrdering ordering_rule_, ResourceUsageTag tag_)
John Zulauf8e3c3e92021-01-06 11:19:36 -07001572 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001573 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001574 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001575 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001576 const SyncOrdering ordering_rule;
John Zulauf14940722021-04-12 15:19:02 -06001577 const ResourceUsageTag tag;
John Zulauf9cb530d2019-09-30 14:14:10 -06001578};
1579
John Zulauf4a6105a2020-11-17 15:11:05 -07001580// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001581struct PipelineBarrierOp {
1582 SyncBarrier barrier;
1583 bool layout_transition;
1584 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1585 : barrier(barrier_), layout_transition(layout_transition_) {}
1586 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001587 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001588 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1589};
John Zulauf4a6105a2020-11-17 15:11:05 -07001590// The barrier operation for wait events
1591struct WaitEventBarrierOp {
John Zulauf14940722021-04-12 15:19:02 -06001592 ResourceUsageTag scope_tag;
John Zulauf4a6105a2020-11-17 15:11:05 -07001593 SyncBarrier barrier;
1594 bool layout_transition;
John Zulauf14940722021-04-12 15:19:02 -06001595 WaitEventBarrierOp(const ResourceUsageTag scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1596 : scope_tag(scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
John Zulauf4a6105a2020-11-17 15:11:05 -07001597 WaitEventBarrierOp() = default;
John Zulauf14940722021-04-12 15:19:02 -06001598 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(scope_tag, barrier, layout_transition); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001599};
John Zulauf1e331ec2020-12-04 18:29:38 -07001600
John Zulauf4a6105a2020-11-17 15:11:05 -07001601// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1602// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1603// of a collection is known/present.
John Zulauf5c628d02021-05-04 15:46:36 -06001604template <typename BarrierOp, typename OpVector = std::vector<BarrierOp>>
John Zulauf89311b42020-09-29 16:28:47 -06001605class ApplyBarrierOpsFunctor {
1606 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001607 using Iterator = ResourceAccessRangeMap::iterator;
John Zulauf5c628d02021-05-04 15:46:36 -06001608 // Only called with a gap, and pos at the lower_bound(range)
1609 inline Iterator Infill(ResourceAccessRangeMap *accesses, const Iterator &pos, const ResourceAccessRange &range) const {
1610 if (!infill_default_) {
1611 return pos;
1612 }
1613 ResourceAccessState default_state;
1614 auto inserted = accesses->insert(pos, std::make_pair(range, default_state));
1615 return inserted;
1616 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001617
John Zulauf5c628d02021-05-04 15:46:36 -06001618 Iterator operator()(ResourceAccessRangeMap *accesses, const Iterator &pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001619 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001620 for (const auto &op : barrier_ops_) {
1621 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001622 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001623
John Zulauf89311b42020-09-29 16:28:47 -06001624 if (resolve_) {
1625 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1626 // another walk
1627 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001628 }
1629 return pos;
1630 }
1631
John Zulauf89311b42020-09-29 16:28:47 -06001632 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf5c628d02021-05-04 15:46:36 -06001633 ApplyBarrierOpsFunctor(bool resolve, typename OpVector::size_type size_hint, ResourceUsageTag tag)
1634 : resolve_(resolve), infill_default_(false), barrier_ops_(), tag_(tag) {
John Zulaufd5115702021-01-18 12:34:33 -07001635 barrier_ops_.reserve(size_hint);
1636 }
John Zulauf5c628d02021-05-04 15:46:36 -06001637 void EmplaceBack(const BarrierOp &op) {
1638 barrier_ops_.emplace_back(op);
1639 infill_default_ |= op.layout_transition;
1640 }
John Zulauf89311b42020-09-29 16:28:47 -06001641
1642 private:
1643 bool resolve_;
John Zulauf5c628d02021-05-04 15:46:36 -06001644 bool infill_default_;
1645 OpVector barrier_ops_;
John Zulauf14940722021-04-12 15:19:02 -06001646 const ResourceUsageTag tag_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001647};
1648
John Zulauf4a6105a2020-11-17 15:11:05 -07001649// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1650// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1651template <typename BarrierOp>
John Zulauf5c628d02021-05-04 15:46:36 -06001652class ApplyBarrierFunctor : public ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>> {
1653 using Base = ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>>;
1654
John Zulauf4a6105a2020-11-17 15:11:05 -07001655 public:
John Zulaufee984022022-04-13 16:39:50 -06001656 ApplyBarrierFunctor(const BarrierOp &barrier_op) : Base(false, 1, kInvalidTag) { Base::EmplaceBack(barrier_op); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001657};
1658
John Zulauf1e331ec2020-12-04 18:29:38 -07001659// This functor resolves the pendinging state.
John Zulauf5c628d02021-05-04 15:46:36 -06001660class ResolvePendingBarrierFunctor : public ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>> {
1661 using Base = ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>>;
1662
John Zulauf1e331ec2020-12-04 18:29:38 -07001663 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001664 ResolvePendingBarrierFunctor(ResourceUsageTag tag) : Base(true, 0, tag) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001665};
1666
John Zulauf8e3c3e92021-01-06 11:19:36 -07001667void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001668 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001669 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001670 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001671}
1672
John Zulauf8e3c3e92021-01-06 11:19:36 -07001673void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001674 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001675 if (!SimpleBinding(buffer)) return;
1676 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001677 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001678}
John Zulauf355e49b2020-04-24 15:11:15 -06001679
John Zulauf8e3c3e92021-01-06 11:19:36 -07001680void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001681 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1682 if (!SimpleBinding(image)) return;
1683 const auto base_address = ResourceBaseAddress(image);
1684 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1685 const auto address_type = ImageAddressType(image);
1686 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1687 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1688}
1689void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001690 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001691 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001692 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001693 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001694 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1695 base_address);
1696 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001697 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001698 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001699}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001700
1701void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
John Zulauf14940722021-04-12 15:19:02 -06001702 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001703 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1704 if (!gen) return;
1705 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1706 const auto address_type = view_gen.GetAddressType();
1707 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1708 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001709}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001710
John Zulauf8e3c3e92021-01-06 11:19:36 -07001711void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001712 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001713 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001714 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1715 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001716 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001717}
1718
John Zulaufd0ec59f2021-03-13 14:25:08 -07001719template <typename Action, typename RangeGen>
1720void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1721 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1722 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001723}
1724
1725template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001726void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1727 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1728 if (!gen) return;
1729 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001730}
1731
John Zulaufd0ec59f2021-03-13 14:25:08 -07001732void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1733 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001734 const ResourceUsageTag tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001735 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001736 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001737}
1738
John Zulaufd0ec59f2021-03-13 14:25:08 -07001739void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06001740 uint32_t subpass, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001741 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001742
1743 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1744 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001745 const auto &view_gen = attachment_views[i];
1746 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001747
1748 const auto &ci = attachment_ci[i];
1749 const bool has_depth = FormatHasDepth(ci.format);
1750 const bool has_stencil = FormatHasStencil(ci.format);
1751 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001752 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001753
1754 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001755 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1756 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001757 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001758 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001759 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1760 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001761 }
John Zulauf57261402021-08-13 11:32:06 -06001762 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001763 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001764 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1765 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001766 }
1767 }
1768 }
1769 }
1770}
1771
John Zulauf540266b2020-04-06 18:54:53 -06001772template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001773void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001774 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001775 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001776 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001777 }
1778}
1779
1780void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001781 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1782 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001783 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001784 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001785 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001786 }
1787 }
1788}
1789
John Zulauf4fa68462021-04-26 21:04:22 -06001790// Caller must ensure that lifespan of this is less than from
1791void AccessContext::ImportAsyncContexts(const AccessContext &from) { async_ = from.async_; }
1792
John Zulauf355e49b2020-04-24 15:11:15 -06001793// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001794HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1795 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001796
John Zulauf355e49b2020-04-24 15:11:15 -06001797 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001798 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001799
1800 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001801 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1802 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001803 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001804 if (!hazard.hazard) {
1805 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001806 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001807 }
John Zulaufa0a98292020-09-18 09:30:10 -06001808
John Zulauf355e49b2020-04-24 15:11:15 -06001809 return hazard;
1810}
1811
John Zulaufb02c1eb2020-10-06 16:33:36 -06001812void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001813 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001814 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001815 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001816 for (const auto &transition : transitions) {
1817 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001818 const auto &view_gen = attachment_views[transition.attachment];
1819 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001820
1821 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1822 assert(trackback);
1823
1824 // Import the attachments into the current context
1825 const auto *prev_context = trackback->context;
1826 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001827 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001828 auto &target_map = GetAccessStateMap(address_type);
1829 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001830 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1831 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001832 }
1833
John Zulauf86356ca2020-10-19 11:46:41 -06001834 // If there were no transitions skip this global map walk
1835 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001836 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001837 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001838 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001839}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001840
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001841void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulauf669dfd52021-01-27 17:15:28 -07001842 auto *events_context = GetCurrentEventsContext();
1843 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06001844 events_context->ApplyBarrier(src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001845}
1846
locke-lunarg61870c22020-06-09 14:51:50 -06001847bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1848 const char *func_name) const {
1849 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001850 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001851 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001852 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001853 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001854 return skip;
1855 }
1856
1857 using DescriptorClass = cvdescriptorset::DescriptorClass;
1858 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1859 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001860 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1861
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001862 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001863 const auto raster_state = pipe->RasterizationState();
1864 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001865 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001866 }
locke-lunarg61870c22020-06-09 14:51:50 -06001867 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001868 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001869 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001870 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001871 const auto descriptor_type = binding_it.GetType();
1872 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1873 auto array_idx = 0;
1874
1875 if (binding_it.IsVariableDescriptorCount()) {
1876 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1877 }
1878 SyncStageAccessIndex sync_index =
1879 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1880
1881 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1882 uint32_t index = i - index_range.start;
1883 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1884 switch (descriptor->GetClass()) {
1885 case DescriptorClass::ImageSampler:
1886 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001887 if (descriptor->Invalid()) {
1888 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001889 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07001890
1891 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
1892 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1893 const auto *img_view_state = image_descriptor->GetImageViewState();
1894 VkImageLayout image_layout = image_descriptor->GetImageLayout();
1895
John Zulauf361fb532020-07-22 10:45:39 -06001896 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001897 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1898 // Descriptors, so we do not have to worry about depth slicing here.
1899 // See: VUID 00343
1900 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001901 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001902 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001903
1904 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1905 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1906 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001907 // Input attachments are subject to raster ordering rules
1908 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001909 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001910 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001911 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001912 }
John Zulauf110413c2021-03-20 05:38:38 -06001913
John Zulauf33fc1d52020-07-17 11:01:10 -06001914 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001915 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001916 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001917 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1918 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001919 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001920 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1921 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1922 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001923 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1924 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001925 set_binding.first.binding, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001926 }
1927 break;
1928 }
1929 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001930 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
1931 if (texel_descriptor->Invalid()) {
1932 continue;
1933 }
1934 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
1935 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001936 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001937 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001938 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001939 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001940 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001941 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1942 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001943 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1944 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1945 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001946 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001947 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001948 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001949 }
1950 break;
1951 }
1952 case DescriptorClass::GeneralBuffer: {
1953 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07001954 if (buffer_descriptor->Invalid()) {
1955 continue;
1956 }
1957 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06001958 const ResourceAccessRange range =
1959 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001960 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001961 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001962 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001963 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001964 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1965 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001966 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1967 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1968 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001969 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001970 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001971 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001972 }
1973 break;
1974 }
1975 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1976 default:
1977 break;
1978 }
1979 }
1980 }
1981 }
1982 return skip;
1983}
1984
1985void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
John Zulauf14940722021-04-12 15:19:02 -06001986 const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001987 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001988 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001989 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001990 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001991 return;
1992 }
1993
1994 using DescriptorClass = cvdescriptorset::DescriptorClass;
1995 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1996 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001997 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1998
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001999 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002000 const auto raster_state = pipe->RasterizationState();
2001 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06002002 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002003 }
locke-lunarg61870c22020-06-09 14:51:50 -06002004 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07002005 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002006 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002007 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06002008 const auto descriptor_type = binding_it.GetType();
2009 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2010 auto array_idx = 0;
2011
2012 if (binding_it.IsVariableDescriptorCount()) {
2013 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2014 }
2015 SyncStageAccessIndex sync_index =
2016 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2017
2018 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2019 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2020 switch (descriptor->GetClass()) {
2021 case DescriptorClass::ImageSampler:
2022 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002023 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
2024 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
2025 if (image_descriptor->Invalid()) {
2026 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002027 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07002028 const auto *img_view_state = image_descriptor->GetImageViewState();
Jeremy Gebben11a68a32021-07-29 11:59:22 -06002029 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
2030 // Descriptors, so we do not have to worry about depth slicing here.
2031 // See: VUID 00343
2032 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06002033 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002034 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06002035 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2036 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2037 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
2038 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002039 } else {
John Zulauf110413c2021-03-20 05:38:38 -06002040 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
2041 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002042 }
locke-lunarg61870c22020-06-09 14:51:50 -06002043 break;
2044 }
2045 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002046 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
2047 if (texel_descriptor->Invalid()) {
2048 continue;
2049 }
2050 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
2051 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002052 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002053 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002054 break;
2055 }
2056 case DescriptorClass::GeneralBuffer: {
2057 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07002058 if (buffer_descriptor->Invalid()) {
2059 continue;
2060 }
2061 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06002062 const ResourceAccessRange range =
2063 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002064 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002065 break;
2066 }
2067 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2068 default:
2069 break;
2070 }
2071 }
2072 }
2073 }
2074}
2075
2076bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2077 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002078 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002079 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002080 return skip;
2081 }
2082
2083 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2084 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002085 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002086
2087 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002088 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002089 if (binding_description.binding < binding_buffers_size) {
2090 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002091 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002092
locke-lunarg1ae57d62020-11-18 10:49:19 -07002093 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002094 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2095 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002096 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002097 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002098 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002099 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
2100 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2101 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002102 }
2103 }
2104 }
2105 return skip;
2106}
2107
John Zulauf14940722021-04-12 15:19:02 -06002108void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002109 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002110 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002111 return;
2112 }
2113 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2114 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002115 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002116
2117 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002118 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002119 if (binding_description.binding < binding_buffers_size) {
2120 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002121 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002122
locke-lunarg1ae57d62020-11-18 10:49:19 -07002123 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002124 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2125 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002126 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2127 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002128 }
2129 }
2130}
2131
2132bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2133 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002134 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002135 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002136 }
locke-lunarg61870c22020-06-09 14:51:50 -06002137
locke-lunarg1ae57d62020-11-18 10:49:19 -07002138 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002139 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002140 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2141 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002142 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002143 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002144 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002145 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2146 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2147 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002148 }
2149
2150 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2151 // We will detect more accurate range in the future.
2152 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2153 return skip;
2154}
2155
John Zulauf14940722021-04-12 15:19:02 -06002156void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002157 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 -06002158
locke-lunarg1ae57d62020-11-18 10:49:19 -07002159 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002160 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002161 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2162 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002163 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002164
2165 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2166 // We will detect more accurate range in the future.
2167 RecordDrawVertex(UINT32_MAX, 0, tag);
2168}
2169
2170bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002171 bool skip = false;
2172 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002173 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002174 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002175}
2176
John Zulauf14940722021-04-12 15:19:02 -06002177void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002178 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002179 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002180 }
locke-lunarg61870c22020-06-09 14:51:50 -06002181}
2182
John Zulauf41a9c7c2021-12-07 15:59:53 -07002183ResourceUsageTag CommandBufferAccessContext::RecordBeginRenderPass(CMD_TYPE cmd, const RENDER_PASS_STATE &rp_state,
2184 const VkRect2D &render_area,
2185 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
John Zulauf355e49b2020-04-24 15:11:15 -06002186 // Create an access context the current renderpass.
John Zulauf41a9c7c2021-12-07 15:59:53 -07002187 const auto barrier_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2188 const auto load_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kLoadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07002189 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002190 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002191 current_renderpass_context_->RecordBeginRenderPass(barrier_tag, load_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002192 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002193 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002194}
2195
John Zulauf41a9c7c2021-12-07 15:59:53 -07002196ResourceUsageTag CommandBufferAccessContext::RecordNextSubpass(const CMD_TYPE cmd) {
John Zulauf16adfc92020-04-08 10:28:33 -06002197 assert(current_renderpass_context_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002198 if (!current_renderpass_context_) return NextCommandTag(cmd);
2199
2200 auto store_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kStoreOp);
2201 auto barrier_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2202 auto load_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kLoadOp);
2203
2204 current_renderpass_context_->RecordNextSubpass(store_tag, barrier_tag, load_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002205 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002206 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002207}
2208
John Zulauf41a9c7c2021-12-07 15:59:53 -07002209ResourceUsageTag CommandBufferAccessContext::RecordEndRenderPass(const CMD_TYPE cmd) {
John Zulauf16adfc92020-04-08 10:28:33 -06002210 assert(current_renderpass_context_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002211 if (!current_renderpass_context_) return NextCommandTag(cmd);
John Zulauf16adfc92020-04-08 10:28:33 -06002212
John Zulauf41a9c7c2021-12-07 15:59:53 -07002213 auto store_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kStoreOp);
2214 auto barrier_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2215
2216 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, store_tag, barrier_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002217 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002218 current_renderpass_context_ = nullptr;
John Zulauf41a9c7c2021-12-07 15:59:53 -07002219 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002220}
2221
John Zulauf4a6105a2020-11-17 15:11:05 -07002222void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2223 // Erase is okay with the key not being
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002224 auto event_state = sync_state_->Get<EVENT_STATE>(event);
John Zulauf669dfd52021-01-27 17:15:28 -07002225 if (event_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002226 GetCurrentEventsContext()->Destroy(event_state.get());
John Zulaufd5115702021-01-18 12:34:33 -07002227 }
2228}
2229
John Zulaufae842002021-04-15 18:20:55 -06002230// The is the recorded cb context
John Zulauf4fa68462021-04-26 21:04:22 -06002231bool CommandBufferAccessContext::ValidateFirstUse(CommandBufferAccessContext *proxy_context, const char *func_name,
2232 uint32_t index) const {
2233 assert(proxy_context);
2234 auto *events_context = proxy_context->GetCurrentEventsContext();
2235 auto *access_context = proxy_context->GetCurrentAccessContext();
2236 const ResourceUsageTag base_tag = proxy_context->GetTagLimit();
John Zulaufae842002021-04-15 18:20:55 -06002237 bool skip = false;
2238 ResourceUsageRange tag_range = {0, 0};
2239 const AccessContext *recorded_context = GetCurrentAccessContext();
2240 assert(recorded_context);
2241 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002242 auto log_msg = [this](const HazardResult &hazard, const CommandBufferAccessContext &active_context, const char *func_name,
John Zulaufae842002021-04-15 18:20:55 -06002243 uint32_t index) {
2244 const auto cb_handle = active_context.cb_state_->commandBuffer();
2245 const auto recorded_handle = cb_state_->commandBuffer();
John Zulauf4fa68462021-04-26 21:04:22 -06002246 const auto *report_data = sync_state_->report_data;
John Zulaufae842002021-04-15 18:20:55 -06002247 return sync_state_->LogError(cb_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf4fa68462021-04-26 21:04:22 -06002248 "%s: Hazard %s for entry %" PRIu32 ", %s, Recorded access info %s. Access info %s.", func_name,
2249 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(recorded_handle).c_str(),
2250 FormatUsage(*hazard.recorded_access).c_str(), active_context.FormatUsage(hazard).c_str());
John Zulaufae842002021-04-15 18:20:55 -06002251 };
2252 for (const auto &sync_op : sync_ops_) {
John Zulauf4fa68462021-04-26 21:04:22 -06002253 // we update the range to any include layout transition first use writes,
2254 // as they are stored along with the source scope (as effective barrier) when recorded
2255 tag_range.end = sync_op.tag + 1;
John Zulauf610e28c2021-08-03 17:46:23 -06002256 skip |= sync_op.sync_op->ReplayValidate(sync_op.tag, *this, base_tag, proxy_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002257
John Zulaufae842002021-04-15 18:20:55 -06002258 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2259 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002260 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002261 }
2262 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
John Zulauf4fa68462021-04-26 21:04:22 -06002263 // Record the barrier into the proxy context.
2264 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2265 tag_range.begin = tag_range.end;
John Zulaufae842002021-04-15 18:20:55 -06002266 }
2267
2268 // and anything after the last syncop
John Zulaufae842002021-04-15 18:20:55 -06002269 tag_range.end = ResourceUsageRecord::kMaxIndex;
2270 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2271 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002272 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002273 }
2274
2275 return skip;
2276}
2277
John Zulauf4fa68462021-04-26 21:04:22 -06002278void CommandBufferAccessContext::RecordExecutedCommandBuffer(const CommandBufferAccessContext &recorded_cb_context, CMD_TYPE cmd) {
2279 auto *events_context = GetCurrentEventsContext();
2280 auto *access_context = GetCurrentAccessContext();
2281 const AccessContext *recorded_context = recorded_cb_context.GetCurrentAccessContext();
2282 assert(recorded_context);
2283
2284 // Just run through the barriers ignoring the usage from the recorded context, as Resolve will overwrite outdated state
2285 const ResourceUsageTag base_tag = GetTagLimit();
2286 for (const auto &sync_op : recorded_cb_context.sync_ops_) {
2287 // we update the range to any include layout transition first use writes,
2288 // as they are stored along with the source scope (as effective barrier) when recorded
2289 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2290 }
2291
2292 ResourceUsageRange tag_range = ImportRecordedAccessLog(recorded_cb_context);
2293 assert(base_tag == tag_range.begin); // to ensure the to offset calculation agree
2294 ResolveRecordedContext(*recorded_context, tag_range.begin);
2295}
2296
2297void CommandBufferAccessContext::ResolveRecordedContext(const AccessContext &recorded_context, ResourceUsageTag offset) {
2298 auto tag_offset = [offset](ResourceAccessState *access) { access->OffsetTag(offset); };
2299
2300 auto *access_context = GetCurrentAccessContext();
2301 for (auto address_type : kAddressTypes) {
2302 recorded_context.ResolveAccessRange(address_type, kFullRange, tag_offset, &access_context->GetAccessStateMap(address_type),
2303 nullptr, false);
2304 }
2305}
2306
2307ResourceUsageRange CommandBufferAccessContext::ImportRecordedAccessLog(const CommandBufferAccessContext &recorded_context) {
2308 // The execution references ensure lifespan for the referenced child CB's...
2309 ResourceUsageRange tag_range(GetTagLimit(), 0);
John Zulauf3c2a0b32021-07-14 11:14:52 -06002310 cbs_referenced_.emplace(recorded_context.cb_state_);
John Zulauf4fa68462021-04-26 21:04:22 -06002311 access_log_.insert(access_log_.end(), recorded_context.access_log_.cbegin(), recorded_context.access_log_.end());
2312 tag_range.end = access_log_.size();
2313 return tag_range;
2314}
2315
John Zulauf41a9c7c2021-12-07 15:59:53 -07002316ResourceUsageTag CommandBufferAccessContext::NextSubcommandTag(CMD_TYPE command, ResourceUsageRecord::SubcommandType subcommand) {
2317 ResourceUsageTag next = access_log_.size();
2318 access_log_.emplace_back(command, command_number_, subcommand, ++subcommand_number_, cb_state_.get(), reset_count_);
2319 return next;
2320}
2321
2322ResourceUsageTag CommandBufferAccessContext::NextCommandTag(CMD_TYPE command, ResourceUsageRecord::SubcommandType subcommand) {
2323 command_number_++;
2324 subcommand_number_ = 0;
2325 ResourceUsageTag next = access_log_.size();
2326 access_log_.emplace_back(command, command_number_, subcommand, subcommand_number_, cb_state_.get(), reset_count_);
2327 return next;
2328}
2329
2330ResourceUsageTag CommandBufferAccessContext::NextIndexedCommandTag(CMD_TYPE command, uint32_t index) {
2331 if (index == 0) {
2332 return NextCommandTag(command, ResourceUsageRecord::SubcommandType::kIndex);
2333 }
2334 return NextSubcommandTag(command, ResourceUsageRecord::SubcommandType::kIndex);
2335}
2336
John Zulaufae842002021-04-15 18:20:55 -06002337class HazardDetectFirstUse {
2338 public:
2339 HazardDetectFirstUse(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range)
2340 : recorded_use_(recorded_use), tag_range_(tag_range) {}
2341 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
2342 return pos->second.DetectHazard(recorded_use_, tag_range_);
2343 }
2344 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
2345 return pos->second.DetectAsyncHazard(recorded_use_, tag_range_, start_tag);
2346 }
2347
2348 private:
2349 const ResourceAccessState &recorded_use_;
2350 const ResourceUsageRange &tag_range_;
2351};
2352
2353// This is called with the *recorded* command buffers access context, with the *active* access context pass in, againsts which
2354// hazards will be detected
2355HazardResult AccessContext::DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context) const {
2356 HazardResult hazard;
2357 for (const auto address_type : kAddressTypes) {
2358 const auto &recorded_access_map = GetAccessStateMap(address_type);
2359 for (const auto &recorded_access : recorded_access_map) {
2360 // Cull any entries not in the current tag range
2361 if (!recorded_access.second.FirstAccessInTagRange(tag_range)) continue;
2362 HazardDetectFirstUse detector(recorded_access.second, tag_range);
2363 hazard = access_context.DetectHazard(address_type, detector, recorded_access.first, DetectOptions::kDetectAll);
2364 if (hazard.hazard) break;
2365 }
2366 }
2367
2368 return hazard;
2369}
2370
John Zulauf64ffe552021-02-06 10:25:07 -07002371bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002372 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002373 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002374 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002375 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002376 if (!pipe) {
2377 return skip;
2378 }
2379
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002380 const auto raster_state = pipe->RasterizationState();
2381 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002382 return skip;
2383 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002384 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002385 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002386
John Zulauf1a224292020-06-30 14:52:13 -06002387 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002388 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002389 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2390 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002391 if (location >= subpass.colorAttachmentCount ||
2392 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002393 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002394 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002395 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2396 if (!view_gen.IsValid()) continue;
2397 HazardResult hazard =
2398 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2399 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002400 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002401 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002402 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002403 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002404 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002405 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002406 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002407 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002408 }
2409 }
2410 }
locke-lunarg37047832020-06-12 13:44:45 -06002411
2412 // PHASE1 TODO: Add layout based read/vs. write selection.
2413 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002414 const auto ds_state = pipe->DepthStencilState();
2415 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002416
2417 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2418 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2419 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002420 bool depth_write = false, stencil_write = false;
2421
2422 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002423 if (!FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable && ds_state->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002424 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2425 depth_write = true;
2426 }
2427 // PHASE1 TODO: It needs to check if stencil is writable.
2428 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2429 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2430 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002431 if (!FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002432 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2433 stencil_write = true;
2434 }
2435
2436 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2437 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002438 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2439 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2440 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002441 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002442 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002443 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002444 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002445 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002446 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2447 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002448 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002449 }
2450 }
2451 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002452 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2453 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2454 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002455 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002456 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002457 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002458 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002459 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002460 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2461 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002462 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002463 }
locke-lunarg61870c22020-06-09 14:51:50 -06002464 }
2465 }
2466 return skip;
2467}
2468
John Zulauf14940722021-04-12 15:19:02 -06002469void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002470 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002471 if (!pipe) {
2472 return;
2473 }
2474
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002475 const auto *raster_state = pipe->RasterizationState();
2476 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002477 return;
2478 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002479 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002480 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002481
John Zulauf1a224292020-06-30 14:52:13 -06002482 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002483 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002484 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2485 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002486 if (location >= subpass.colorAttachmentCount ||
2487 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002488 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002489 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002490 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2491 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2492 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2493 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002494 }
2495 }
locke-lunarg37047832020-06-12 13:44:45 -06002496
2497 // PHASE1 TODO: Add layout based read/vs. write selection.
2498 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002499 const auto *ds_state = pipe->DepthStencilState();
2500 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002501 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2502 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2503 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002504 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002505 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2506 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002507
2508 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002509 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable &&
2510 ds_state->depthWriteEnable && IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
locke-lunarg37047832020-06-12 13:44:45 -06002511 depth_write = true;
2512 }
2513 // PHASE1 TODO: It needs to check if stencil is writable.
2514 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2515 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2516 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002517 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002518 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2519 stencil_write = true;
2520 }
2521
John Zulaufd0ec59f2021-03-13 14:25:08 -07002522 if (depth_write || stencil_write) {
2523 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2524 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2525 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2526 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002527 }
locke-lunarg61870c22020-06-09 14:51:50 -06002528 }
2529}
2530
John Zulauf64ffe552021-02-06 10:25:07 -07002531bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002532 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002533 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002534 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002535 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002536 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002537 func_name);
2538
John Zulauf355e49b2020-04-24 15:11:15 -06002539 const auto next_subpass = current_subpass_ + 1;
ziga-lunarg31a3e772022-03-22 11:48:46 +01002540 if (next_subpass >= subpass_contexts_.size()) {
2541 return skip;
2542 }
John Zulauf1507ee42020-05-18 11:33:09 -06002543 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002544 skip |=
2545 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002546 if (!skip) {
2547 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2548 // on a copy of the (empty) next context.
2549 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2550 AccessContext temp_context(next_context);
John Zulaufee984022022-04-13 16:39:50 -06002551 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kInvalidTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002552 skip |=
2553 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002554 }
John Zulauf7635de32020-05-29 17:14:15 -06002555 return skip;
2556}
John Zulauf64ffe552021-02-06 10:25:07 -07002557bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002558 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002559 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002560 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002561 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002562 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2563
2564 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002565 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002566 return skip;
2567}
2568
John Zulauf64ffe552021-02-06 10:25:07 -07002569AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002570 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002571}
2572
John Zulauf64ffe552021-02-06 10:25:07 -07002573bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2574 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002575 bool skip = false;
2576
John Zulauf7635de32020-05-29 17:14:15 -06002577 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2578 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2579 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2580 // to apply and only copy then, if this proves a hot spot.
2581 std::unique_ptr<AccessContext> proxy_for_current;
2582
John Zulauf355e49b2020-04-24 15:11:15 -06002583 // Validate the "finalLayout" transitions to external
2584 // Get them from where there we're hidding in the extra entry.
2585 const auto &final_transitions = rp_state_->subpass_transitions.back();
2586 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002587 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002588 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2589 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002590 auto *context = trackback.context;
2591
2592 if (transition.prev_pass == current_subpass_) {
2593 if (!proxy_for_current) {
2594 // 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 -07002595 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002596 }
2597 context = proxy_for_current.get();
2598 }
2599
John Zulaufa0a98292020-09-18 09:30:10 -06002600 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2601 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002602 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002603 if (hazard.hazard) {
John Zulaufee984022022-04-13 16:39:50 -06002604 if (hazard.tag == kInvalidTag) {
2605 // Hazard vs. ILT
2606 skip |= ex_context.GetSyncState().LogError(
2607 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
2608 "%s: Hazard %s vs. store/resolve operations in subpass %" PRIu32 " for attachment %" PRIu32
2609 " final image layout transition (old_layout: %s, new_layout: %s).",
2610 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2611 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout));
2612 } else {
2613 skip |= ex_context.GetSyncState().LogError(
2614 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
2615 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2616 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2617 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2618 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
2619 ex_context.FormatUsage(hazard).c_str());
2620 }
John Zulauf355e49b2020-04-24 15:11:15 -06002621 }
2622 }
2623 return skip;
2624}
2625
John Zulauf14940722021-04-12 15:19:02 -06002626void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002627 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002628 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002629}
2630
John Zulauf14940722021-04-12 15:19:02 -06002631void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002632 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2633 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002634
2635 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2636 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002637 const AttachmentViewGen &view_gen = attachment_views_[i];
2638 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002639
2640 const auto &ci = attachment_ci[i];
2641 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002642 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002643 const bool is_color = !(has_depth || has_stencil);
2644
2645 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002646 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2647 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2648 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2649 SyncOrdering::kColorAttachment, tag);
2650 }
John Zulauf1507ee42020-05-18 11:33:09 -06002651 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002652 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002653 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2654 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2655 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2656 SyncOrdering::kDepthStencilAttachment, tag);
2657 }
John Zulauf1507ee42020-05-18 11:33:09 -06002658 }
2659 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002660 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2661 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2662 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2663 SyncOrdering::kDepthStencilAttachment, tag);
2664 }
John Zulauf1507ee42020-05-18 11:33:09 -06002665 }
2666 }
2667 }
2668 }
2669}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002670AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2671 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2672 AttachmentViewGenVector view_gens;
2673 VkExtent3D extent = CastTo3D(render_area.extent);
2674 VkOffset3D offset = CastTo3D(render_area.offset);
2675 view_gens.reserve(attachment_views.size());
2676 for (const auto *view : attachment_views) {
2677 view_gens.emplace_back(view, offset, extent);
2678 }
2679 return view_gens;
2680}
John Zulauf64ffe552021-02-06 10:25:07 -07002681RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2682 VkQueueFlags queue_flags,
2683 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2684 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002685 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002686 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002687 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002688 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002689 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002690 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002691 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002692}
John Zulauf41a9c7c2021-12-07 15:59:53 -07002693void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag barrier_tag, const ResourceUsageTag load_tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002694 assert(0 == current_subpass_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002695 subpass_contexts_[current_subpass_].SetStartTag(barrier_tag);
2696 RecordLayoutTransitions(barrier_tag);
2697 RecordLoadOperations(load_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002698}
John Zulauf1507ee42020-05-18 11:33:09 -06002699
John Zulauf41a9c7c2021-12-07 15:59:53 -07002700void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag store_tag, const ResourceUsageTag barrier_tag,
2701 const ResourceUsageTag load_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002702 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulauf41a9c7c2021-12-07 15:59:53 -07002703 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
2704 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002705
ziga-lunarg31a3e772022-03-22 11:48:46 +01002706 if (current_subpass_ + 1 >= subpass_contexts_.size()) {
2707 return;
2708 }
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002709 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2710 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002711 current_subpass_++;
John Zulauf41a9c7c2021-12-07 15:59:53 -07002712 subpass_contexts_[current_subpass_].SetStartTag(barrier_tag);
2713 RecordLayoutTransitions(barrier_tag);
2714 RecordLoadOperations(load_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002715}
2716
John Zulauf41a9c7c2021-12-07 15:59:53 -07002717void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag store_tag,
2718 const ResourceUsageTag barrier_tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002719 // Add the resolve and store accesses
John Zulauf41a9c7c2021-12-07 15:59:53 -07002720 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
2721 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002722
John Zulauf355e49b2020-04-24 15:11:15 -06002723 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002724 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002725
2726 // Add the "finalLayout" transitions to external
2727 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002728 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2729 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2730 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002731 const auto &final_transitions = rp_state_->subpass_transitions.back();
2732 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002733 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002734 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002735 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002736 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), barrier_tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002737 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002738 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002739 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002740 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002741 }
2742}
2743
Jeremy Gebben40a22942020-12-22 14:22:06 -07002744SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002745 SyncExecScope result;
2746 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002747 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2748 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002749 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2750 return result;
2751}
2752
Jeremy Gebben40a22942020-12-22 14:22:06 -07002753SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002754 SyncExecScope result;
2755 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002756 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2757 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002758 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2759 return result;
2760}
2761
2762SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002763 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002764 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002765 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002766 dst_access_scope = 0;
2767}
2768
2769template <typename Barrier>
2770SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002771 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002772 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002773 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002774 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2775}
2776
2777SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002778 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2779 if (barrier) {
2780 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002781 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002782 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002783
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002784 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002785 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002786 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2787
2788 } else {
2789 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002790 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002791 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2792
2793 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002794 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002795 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2796 }
2797}
2798
2799template <typename Barrier>
2800SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2801 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2802 src_exec_scope = src.exec_scope;
2803 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2804
2805 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002806 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002807 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002808}
2809
John Zulaufb02c1eb2020-10-06 16:33:36 -06002810// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2811void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2812 for (const auto &barrier : barriers) {
2813 ApplyBarrier(barrier, layout_transition);
2814 }
2815}
2816
John Zulauf89311b42020-09-29 16:28:47 -06002817// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2818// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2819// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002820void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002821 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002822 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002823 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002824 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002825 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002826 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002827 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002828}
John Zulauf9cb530d2019-09-30 14:14:10 -06002829HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2830 HazardResult hazard;
2831 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002832 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002833 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002834 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002835 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002836 }
2837 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002838 // Write operation:
2839 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2840 // If reads exists -- test only against them because either:
2841 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2842 // * 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
2843 // the current write happens after the reads, so just test the write against the reades
2844 // Otherwise test against last_write
2845 //
2846 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002847 if (last_reads.size()) {
2848 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002849 if (IsReadHazard(usage_stage, read_access)) {
2850 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2851 break;
2852 }
2853 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002854 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002855 // Write-After-Write check -- if we have a previous write to test against
2856 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002857 }
2858 }
2859 return hazard;
2860}
2861
John Zulauf4fa68462021-04-26 21:04:22 -06002862HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002863 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002864 return DetectHazard(usage_index, ordering);
2865}
2866
2867HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002868 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2869 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002870 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002871 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002872 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2873 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002874 if (IsRead(usage_bit)) {
2875 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2876 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2877 if (is_raw_hazard) {
2878 // NOTE: we know last_write is non-zero
2879 // See if the ordering rules save us from the simple RAW check above
2880 // First check to see if the current usage is covered by the ordering rules
2881 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2882 const bool usage_is_ordered =
2883 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2884 if (usage_is_ordered) {
2885 // Now see of the most recent write (or a subsequent read) are ordered
2886 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2887 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002888 }
2889 }
John Zulauf4285ee92020-09-23 10:20:52 -06002890 if (is_raw_hazard) {
2891 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2892 }
John Zulauf5c628d02021-05-04 15:46:36 -06002893 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2894 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2895 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002896 } else {
2897 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002898 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002899 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002900 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002901 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002902 if (usage_write_is_ordered) {
2903 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2904 ordered_stages = GetOrderedStages(ordering);
2905 }
2906 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2907 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002908 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002909 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2910 if (IsReadHazard(usage_stage, read_access)) {
2911 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2912 break;
2913 }
John Zulaufd14743a2020-07-03 09:42:39 -06002914 }
2915 }
John Zulauf2a344ca2021-09-09 17:07:19 -06002916 } else if (last_write.any() && !(last_write_is_ordered && usage_write_is_ordered)) {
2917 bool ilt_ilt_hazard = false;
2918 if ((usage_index == SYNC_IMAGE_LAYOUT_TRANSITION) && (usage_bit == last_write)) {
2919 // ILT after ILT is a special case where we check the 2nd access scope of the first ILT against the first access
2920 // scope of the second ILT, which has been passed (smuggled?) in the ordering barrier
2921 ilt_ilt_hazard = !(write_barriers & ordering.access_scope).any();
2922 }
2923 if (ilt_ilt_hazard || IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002924 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002925 }
John Zulauf69133422020-05-20 14:55:53 -06002926 }
2927 }
2928 return hazard;
2929}
2930
John Zulaufae842002021-04-15 18:20:55 -06002931HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2932 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002933 using Size = FirstAccesses::size_type;
2934 const auto &recorded_accesses = recorded_use.first_accesses_;
2935 Size count = recorded_accesses.size();
2936 if (count) {
2937 const auto &last_access = recorded_accesses.back();
2938 bool do_write_last = IsWrite(last_access.usage_index);
2939 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06002940
John Zulauf4fa68462021-04-26 21:04:22 -06002941 for (Size i = 0; i < count; ++count) {
2942 const auto &first = recorded_accesses[i];
2943 // Skip and quit logic
2944 if (first.tag < tag_range.begin) continue;
2945 if (first.tag >= tag_range.end) {
2946 do_write_last = false; // ignore last since we know it can't be in tag_range
2947 break;
2948 }
2949
2950 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2951 if (hazard.hazard) {
2952 hazard.AddRecordedAccess(first);
2953 break;
2954 }
2955 }
2956
2957 if (do_write_last && tag_range.includes(last_access.tag)) {
2958 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
2959 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
2960 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2961 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
2962 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
2963 // the barrier that applies them
2964 barrier |= recorded_use.first_write_layout_ordering_;
2965 }
2966 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
2967 // the active context
2968 if (recorded_use.first_read_stages_) {
2969 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
2970 // reads in the active context are not "most recent" as all recorded context operations are *after* them
2971 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
2972 // active context.
2973 barrier.exec_scope |= recorded_use.first_read_stages_;
2974 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
2975 barrier.access_scope |= FlagBit(last_access.usage_index);
2976 }
2977 hazard = DetectHazard(last_access.usage_index, barrier);
2978 if (hazard.hazard) {
2979 hazard.AddRecordedAccess(last_access);
2980 }
2981 }
John Zulaufae842002021-04-15 18:20:55 -06002982 }
2983 return hazard;
2984}
2985
John Zulauf2f952d22020-02-10 11:34:51 -07002986// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002987HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002988 HazardResult hazard;
2989 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002990 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2991 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2992 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002993 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002994 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002995 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002996 }
2997 } else {
John Zulauf14940722021-04-12 15:19:02 -06002998 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002999 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07003000 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003001 // 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 -07003002 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003003 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003004 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003005 break;
3006 }
3007 }
John Zulauf2f952d22020-02-10 11:34:51 -07003008 }
3009 }
3010 return hazard;
3011}
3012
John Zulaufae842002021-04-15 18:20:55 -06003013HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
3014 ResourceUsageTag start_tag) const {
3015 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06003016 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06003017 // Skip and quit logic
3018 if (first.tag < tag_range.begin) continue;
3019 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06003020
3021 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06003022 if (hazard.hazard) {
3023 hazard.AddRecordedAccess(first);
3024 break;
3025 }
John Zulaufae842002021-04-15 18:20:55 -06003026 }
3027 return hazard;
3028}
3029
Jeremy Gebben40a22942020-12-22 14:22:06 -07003030HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003031 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003032 // Only supporting image layout transitions for now
3033 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3034 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003035 // only test for WAW if there no intervening read operations.
3036 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07003037 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06003038 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07003039 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003040 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003041 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003042 break;
3043 }
3044 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003045 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3046 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3047 }
3048
3049 return hazard;
3050}
3051
Jeremy Gebben40a22942020-12-22 14:22:06 -07003052HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07003053 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06003054 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07003055 // Only supporting image layout transitions for now
3056 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3057 HazardResult hazard;
3058 // only test for WAW if there no intervening read operations.
3059 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3060
John Zulaufab7756b2020-12-29 16:10:16 -07003061 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003062 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3063 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003064 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003065 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003066 // The read is in the events first synchronization scope, so we use a barrier hazard check
3067 // If the read stage is not in the src sync scope
3068 // *AND* not execution chained with an existing sync barrier (that's the or)
3069 // then the barrier access is unsafe (R/W after R)
3070 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3071 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3072 break;
3073 }
3074 } else {
3075 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3076 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3077 }
3078 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003079 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003080 // 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 -06003081 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003082 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3083 // So do a normal barrier hazard check
3084 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3085 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3086 }
3087 } else {
3088 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003089 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3090 }
John Zulaufd14743a2020-07-03 09:42:39 -06003091 }
John Zulauf361fb532020-07-22 10:45:39 -06003092
John Zulauf0cb5be22020-01-23 12:18:22 -07003093 return hazard;
3094}
3095
John Zulauf5f13a792020-03-10 07:31:21 -06003096// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3097// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3098// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3099void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003100 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003101 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3102 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003103 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003104 } else if (other.write_tag == write_tag) {
3105 // 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 -06003106 // dependency chaining logic or any stage expansion)
3107 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003108 pending_write_barriers |= other.pending_write_barriers;
3109 pending_layout_transition |= other.pending_layout_transition;
3110 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003111 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003112
John Zulaufd14743a2020-07-03 09:42:39 -06003113 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003114 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003115 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003116 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003117 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003118 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003119 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003120 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3121 // but we should wait on profiling data for that.
3122 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003123 auto &my_read = last_reads[my_read_index];
3124 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003125 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003126 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003127 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003128 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003129 my_read.pending_dep_chain = other_read.pending_dep_chain;
3130 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3131 // May require tracking more than one access per stage.
3132 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003133 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003134 // Since I'm overwriting the fragement stage read, also update the input attachment info
3135 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003136 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003137 }
John Zulauf14940722021-04-12 15:19:02 -06003138 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003139 // The read tags match so merge the barriers
3140 my_read.barriers |= other_read.barriers;
3141 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003142 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003143
John Zulauf5f13a792020-03-10 07:31:21 -06003144 break;
3145 }
3146 }
3147 } else {
3148 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003149 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003150 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003151 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003152 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003153 }
John Zulauf5f13a792020-03-10 07:31:21 -06003154 }
3155 }
John Zulauf361fb532020-07-22 10:45:39 -06003156 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003157 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3158 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003159
3160 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3161 // of the copy and other into this using the update first logic.
3162 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3163 // of the other first_accesses... )
3164 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3165 FirstAccesses firsts(std::move(first_accesses_));
3166 first_accesses_.clear();
3167 first_read_stages_ = 0U;
3168 auto a = firsts.begin();
3169 auto a_end = firsts.end();
3170 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003171 // TODO: Determine whether some tag offset will be needed for PHASE II
3172 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003173 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3174 ++a;
3175 }
3176 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3177 }
3178 for (; a != a_end; ++a) {
3179 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3180 }
3181 }
John Zulauf5f13a792020-03-10 07:31:21 -06003182}
3183
John Zulauf14940722021-04-12 15:19:02 -06003184void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003185 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3186 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003187 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003188 // Mulitple outstanding reads may be of interest and do dependency chains independently
3189 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3190 const auto usage_stage = PipelineStageBit(usage_index);
3191 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003192 for (auto &read_access : last_reads) {
3193 if (read_access.stage == usage_stage) {
3194 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003195 break;
3196 }
3197 }
3198 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003199 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003200 last_read_stages |= usage_stage;
3201 }
John Zulauf4285ee92020-09-23 10:20:52 -06003202
3203 // 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 -07003204 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003205 // TODO Revisit re: multiple reads for a given stage
3206 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003207 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003208 } else {
3209 // Assume write
3210 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003211 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003212 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003213 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003214}
John Zulauf5f13a792020-03-10 07:31:21 -06003215
John Zulauf89311b42020-09-29 16:28:47 -06003216// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3217// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3218// We can overwrite them as *this* write is now after them.
3219//
3220// 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 -06003221void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003222 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003223 last_read_stages = 0;
3224 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003225 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003226
3227 write_barriers = 0;
3228 write_dependency_chain = 0;
3229 write_tag = tag;
3230 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003231}
3232
John Zulauf89311b42020-09-29 16:28:47 -06003233// Apply the memory barrier without updating the existing barriers. The execution barrier
3234// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3235// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3236// replace the current write barriers or add to them, so accumulate to pending as well.
3237void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3238 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3239 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003240 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3241 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3242 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3243 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003244 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003245 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003246 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003247 if (layout_transition) {
3248 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3249 }
John Zulaufa0a98292020-09-18 09:30:10 -06003250 }
John Zulauf89311b42020-09-29 16:28:47 -06003251 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3252 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003253
John Zulauf89311b42020-09-29 16:28:47 -06003254 if (!pending_layout_transition) {
3255 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3256 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003257 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003258 // 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 -07003259 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3260 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003261 }
3262 }
John Zulaufa0a98292020-09-18 09:30:10 -06003263 }
John Zulaufa0a98292020-09-18 09:30:10 -06003264}
3265
John Zulauf4a6105a2020-11-17 15:11:05 -07003266// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3267// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003268void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003269 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3270 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3271 // in order to know if it's in the excecution scope
3272 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3273 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3274 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003275 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003276 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003277 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003278 if (layout_transition) {
3279 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3280 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003281 }
3282 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3283 pending_layout_transition |= layout_transition;
3284
3285 if (!pending_layout_transition) {
3286 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3287 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003288 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003289 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3290 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3291 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3292 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3293 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3294 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3295 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003296 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 -07003297 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003298 }
3299 }
3300 }
3301}
John Zulauf14940722021-04-12 15:19:02 -06003302void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003303 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003304 // 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 -06003305 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003306 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003307 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3308 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003309 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003310 }
John Zulauf89311b42020-09-29 16:28:47 -06003311
3312 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003313 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003314 for (auto &read_access : last_reads) {
3315 read_access.barriers |= read_access.pending_dep_chain;
3316 read_execution_barriers |= read_access.barriers;
3317 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003318 }
3319
3320 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3321 write_dependency_chain |= pending_write_dep_chain;
3322 write_barriers |= pending_write_barriers;
3323 pending_write_dep_chain = 0;
3324 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003325}
3326
John Zulaufae842002021-04-15 18:20:55 -06003327bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3328 if (!first_accesses_.size()) return false;
3329 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3330 return tag_range.intersects(first_access_range);
3331}
3332
John Zulauf59e25072020-07-17 10:55:21 -06003333// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003334VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3335 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003336
John Zulaufab7756b2020-12-29 16:10:16 -07003337 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003338 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003339 barriers = read_access.barriers;
3340 break;
John Zulauf59e25072020-07-17 10:55:21 -06003341 }
3342 }
John Zulauf4285ee92020-09-23 10:20:52 -06003343
John Zulauf59e25072020-07-17 10:55:21 -06003344 return barriers;
3345}
3346
Jeremy Gebben40a22942020-12-22 14:22:06 -07003347inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003348 assert(IsRead(usage));
3349 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3350 // * the previous reads are not hazards, and thus last_write must be visible and available to
3351 // any reads that happen after.
3352 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3353 // 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 -07003354 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003355}
3356
Jeremy Gebben40a22942020-12-22 14:22:06 -07003357VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003358 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003359 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003360 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003361 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003362 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003363 // 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 -07003364 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003365 }
3366
3367 return ordered_stages;
3368}
3369
John Zulauf14940722021-04-12 15:19:02 -06003370void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003371 // Only record until we record a write.
3372 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003373 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003374 if (0 == (usage_stage & first_read_stages_)) {
3375 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003376 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003377 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003378 if (0 == (read_execution_barriers & usage_stage)) {
3379 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3380 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3381 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003382 }
3383 }
3384}
3385
John Zulauf4fa68462021-04-26 21:04:22 -06003386void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3387 // Only call this after recording an image layout transition
3388 assert(first_accesses_.size());
3389 if (first_accesses_.back().tag == tag) {
3390 // 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 +02003391 assert(first_accesses_.back().usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
John Zulauf4fa68462021-04-26 21:04:22 -06003392 first_write_layout_ordering_ = layout_ordering;
3393 }
3394}
3395
John Zulaufee984022022-04-13 16:39:50 -06003396void ResourceAccessState::ReadState::Set(VkPipelineStageFlags2KHR stage_, const SyncStageAccessFlags &access_,
3397 VkPipelineStageFlags2KHR barriers_, ResourceUsageTag tag_) {
3398 stage = stage_;
3399 access = access_;
3400 barriers = barriers_;
3401 tag = tag_;
3402 pending_dep_chain = 0; // If this is a new read, we aren't applying a barrier set.
3403}
3404
John Zulaufd1f85d42020-04-15 12:23:15 -06003405void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003406 auto *access_context = GetAccessContextNoInsert(command_buffer);
3407 if (access_context) {
3408 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003409 }
3410}
3411
John Zulaufd1f85d42020-04-15 12:23:15 -06003412void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3413 auto access_found = cb_access_state.find(command_buffer);
3414 if (access_found != cb_access_state.end()) {
3415 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003416 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003417 cb_access_state.erase(access_found);
3418 }
3419}
3420
John Zulauf9cb530d2019-09-30 14:14:10 -06003421bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3422 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3423 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003424 const auto *cb_context = GetAccessContext(commandBuffer);
3425 assert(cb_context);
3426 if (!cb_context) return skip;
3427 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003428
John Zulauf3d84f1b2020-03-09 13:33:25 -06003429 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003430 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3431 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003432
3433 for (uint32_t region = 0; region < regionCount; region++) {
3434 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003435 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003436 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003437 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003438 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003439 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003440 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003441 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003442 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003443 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003444 }
John Zulauf16adfc92020-04-08 10:28:33 -06003445 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003446 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003447 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003448 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003449 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003450 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003451 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003452 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003453 }
3454 }
3455 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003456 }
3457 return skip;
3458}
3459
3460void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3461 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003462 auto *cb_context = GetAccessContext(commandBuffer);
3463 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003464 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003465 auto *context = cb_context->GetCurrentAccessContext();
3466
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003467 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3468 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003469
3470 for (uint32_t region = 0; region < regionCount; region++) {
3471 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003472 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003473 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003474 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003475 }
John Zulauf16adfc92020-04-08 10:28:33 -06003476 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003477 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003478 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003479 }
3480 }
3481}
3482
John Zulauf4a6105a2020-11-17 15:11:05 -07003483void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3484 // Clear out events from the command buffer contexts
3485 for (auto &cb_context : cb_access_state) {
3486 cb_context.second->RecordDestroyEvent(event);
3487 }
3488}
3489
Tony-LunarGef035472021-11-02 10:23:33 -06003490bool SyncValidator::ValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos,
3491 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003492 bool skip = false;
3493 const auto *cb_context = GetAccessContext(commandBuffer);
3494 assert(cb_context);
3495 if (!cb_context) return skip;
3496 const auto *context = cb_context->GetCurrentAccessContext();
Tony-LunarGef035472021-11-02 10:23:33 -06003497 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003498
3499 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003500 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3501 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003502
3503 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3504 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3505 if (src_buffer) {
3506 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003507 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003508 if (hazard.hazard) {
3509 // TODO -- add tag information to log msg when useful.
3510 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003511 "%s(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003512 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003513 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003514 }
3515 }
3516 if (dst_buffer && !skip) {
3517 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003518 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003519 if (hazard.hazard) {
3520 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003521 "%s(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003522 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003523 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003524 }
3525 }
3526 if (skip) break;
3527 }
3528 return skip;
3529}
3530
Tony-LunarGef035472021-11-02 10:23:33 -06003531bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3532 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3533 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3534}
3535
3536bool SyncValidator::PreCallValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) const {
3537 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3538}
3539
3540void SyncValidator::RecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003541 auto *cb_context = GetAccessContext(commandBuffer);
3542 assert(cb_context);
Tony-LunarGef035472021-11-02 10:23:33 -06003543 const auto tag = cb_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003544 auto *context = cb_context->GetCurrentAccessContext();
3545
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003546 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3547 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003548
3549 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3550 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3551 if (src_buffer) {
3552 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003553 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003554 }
3555 if (dst_buffer) {
3556 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003557 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003558 }
3559 }
3560}
3561
Tony-LunarGef035472021-11-02 10:23:33 -06003562void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3563 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3564}
3565
3566void SyncValidator::PreCallRecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) {
3567 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3568}
3569
John Zulauf5c5e88d2019-12-26 11:22:02 -07003570bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3571 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3572 const VkImageCopy *pRegions) const {
3573 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003574 const auto *cb_access_context = GetAccessContext(commandBuffer);
3575 assert(cb_access_context);
3576 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003577
John Zulauf3d84f1b2020-03-09 13:33:25 -06003578 const auto *context = cb_access_context->GetCurrentAccessContext();
3579 assert(context);
3580 if (!context) return skip;
3581
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003582 auto src_image = Get<IMAGE_STATE>(srcImage);
3583 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003584 for (uint32_t region = 0; region < regionCount; region++) {
3585 const auto &copy_region = pRegions[region];
3586 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003587 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003588 copy_region.srcOffset, copy_region.extent);
3589 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003590 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003591 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003592 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003593 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003594 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003595 }
3596
3597 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003598 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003599 copy_region.dstOffset, copy_region.extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003600 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003601 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003602 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003603 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003604 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003605 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003606 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003607 }
3608 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003609
John Zulauf5c5e88d2019-12-26 11:22:02 -07003610 return skip;
3611}
3612
3613void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3614 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3615 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003616 auto *cb_access_context = GetAccessContext(commandBuffer);
3617 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003618 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003619 auto *context = cb_access_context->GetCurrentAccessContext();
3620 assert(context);
3621
Jeremy Gebben9f537102021-10-05 16:37:12 -06003622 auto src_image = Get<IMAGE_STATE>(srcImage);
3623 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003624
3625 for (uint32_t region = 0; region < regionCount; region++) {
3626 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003627 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003628 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003629 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003630 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003631 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003632 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003633 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003634 }
3635 }
3636}
3637
Tony-LunarGb61514a2021-11-02 12:36:51 -06003638bool SyncValidator::ValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo,
3639 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003640 bool skip = false;
3641 const auto *cb_access_context = GetAccessContext(commandBuffer);
3642 assert(cb_access_context);
3643 if (!cb_access_context) return skip;
3644
3645 const auto *context = cb_access_context->GetCurrentAccessContext();
3646 assert(context);
3647 if (!context) return skip;
3648
Tony-LunarGb61514a2021-11-02 12:36:51 -06003649 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003650 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3651 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003652
Jeff Leger178b1e52020-10-05 12:22:23 -04003653 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3654 const auto &copy_region = pCopyImageInfo->pRegions[region];
3655 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003656 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003657 copy_region.srcOffset, copy_region.extent);
3658 if (hazard.hazard) {
3659 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003660 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003661 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003662 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003663 }
3664 }
3665
3666 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003667 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003668 copy_region.dstOffset, copy_region.extent);
Jeff Leger178b1e52020-10-05 12:22:23 -04003669 if (hazard.hazard) {
3670 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003671 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003672 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003673 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003674 }
3675 if (skip) break;
3676 }
3677 }
3678
3679 return skip;
3680}
3681
Tony-LunarGb61514a2021-11-02 12:36:51 -06003682bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3683 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3684 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3685}
3686
3687bool SyncValidator::PreCallValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) const {
3688 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3689}
3690
3691void SyncValidator::RecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003692 auto *cb_access_context = GetAccessContext(commandBuffer);
3693 assert(cb_access_context);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003694 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003695 auto *context = cb_access_context->GetCurrentAccessContext();
3696 assert(context);
3697
Jeremy Gebben9f537102021-10-05 16:37:12 -06003698 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3699 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04003700
3701 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3702 const auto &copy_region = pCopyImageInfo->pRegions[region];
3703 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003704 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003705 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003706 }
3707 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003708 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003709 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003710 }
3711 }
3712}
3713
Tony-LunarGb61514a2021-11-02 12:36:51 -06003714void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3715 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3716}
3717
3718void SyncValidator::PreCallRecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) {
3719 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3720}
3721
John Zulauf9cb530d2019-09-30 14:14:10 -06003722bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3723 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3724 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3725 uint32_t bufferMemoryBarrierCount,
3726 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3727 uint32_t imageMemoryBarrierCount,
3728 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3729 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003730 const auto *cb_access_context = GetAccessContext(commandBuffer);
3731 assert(cb_access_context);
3732 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003733
John Zulauf36ef9282021-02-02 11:47:24 -07003734 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3735 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3736 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3737 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003738 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003739 return skip;
3740}
3741
3742void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3743 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3744 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3745 uint32_t bufferMemoryBarrierCount,
3746 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3747 uint32_t imageMemoryBarrierCount,
3748 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003749 auto *cb_access_context = GetAccessContext(commandBuffer);
3750 assert(cb_access_context);
3751 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003752
John Zulauf1bf30522021-09-03 15:39:06 -06003753 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(),
3754 srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount,
3755 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3756 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06003757}
3758
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003759bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3760 const VkDependencyInfoKHR *pDependencyInfo) const {
3761 bool skip = false;
3762 const auto *cb_access_context = GetAccessContext(commandBuffer);
3763 assert(cb_access_context);
3764 if (!cb_access_context) return skip;
3765
3766 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3767 skip = pipeline_barrier.Validate(*cb_access_context);
3768 return skip;
3769}
3770
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003771bool SyncValidator::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
3772 const VkDependencyInfo *pDependencyInfo) const {
3773 bool skip = false;
3774 const auto *cb_access_context = GetAccessContext(commandBuffer);
3775 assert(cb_access_context);
3776 if (!cb_access_context) return skip;
3777
3778 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3779 skip = pipeline_barrier.Validate(*cb_access_context);
3780 return skip;
3781}
3782
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003783void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3784 auto *cb_access_context = GetAccessContext(commandBuffer);
3785 assert(cb_access_context);
3786 if (!cb_access_context) return;
3787
John Zulauf1bf30522021-09-03 15:39:06 -06003788 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(),
3789 *pDependencyInfo);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003790}
3791
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003792void SyncValidator::PreCallRecordCmdPipelineBarrier2(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) {
3793 auto *cb_access_context = GetAccessContext(commandBuffer);
3794 assert(cb_access_context);
3795 if (!cb_access_context) return;
3796
3797 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(),
3798 *pDependencyInfo);
3799}
3800
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003801void SyncValidator::CreateDevice(const VkDeviceCreateInfo *pCreateInfo) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003802 // The state tracker sets up the device state
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003803 StateTracker::CreateDevice(pCreateInfo);
John Zulauf9cb530d2019-09-30 14:14:10 -06003804
John Zulauf5f13a792020-03-10 07:31:21 -06003805 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3806 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003807 // TODO: Find a good way to do this hooklessly.
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003808 SetCommandBufferResetCallback([this](VkCommandBuffer command_buffer) -> void { ResetCommandBufferCallback(command_buffer); });
3809 SetCommandBufferFreeCallback([this](VkCommandBuffer command_buffer) -> void { FreeCommandBufferCallback(command_buffer); });
John Zulauf9cb530d2019-09-30 14:14:10 -06003810}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003811
John Zulauf355e49b2020-04-24 15:11:15 -06003812bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003813 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003814 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003815 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003816 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003817 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003818 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003819 }
John Zulauf355e49b2020-04-24 15:11:15 -06003820 return skip;
3821}
3822
3823bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3824 VkSubpassContents contents) const {
3825 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003826 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003827 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003828 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003829 return skip;
3830}
3831
3832bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003833 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003834 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003835 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003836 return skip;
3837}
3838
3839bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3840 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003841 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003842 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003843 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003844 return skip;
3845}
3846
John Zulauf3d84f1b2020-03-09 13:33:25 -06003847void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3848 VkResult result) {
3849 // The state tracker sets up the command buffer state
3850 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3851
3852 // Create/initialize the structure that trackers accesses at the command buffer scope.
3853 auto cb_access_context = GetAccessContext(commandBuffer);
3854 assert(cb_access_context);
3855 cb_access_context->Reset();
3856}
3857
3858void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003859 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003860 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003861 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003862 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003863 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003864 }
3865}
3866
3867void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3868 VkSubpassContents contents) {
3869 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003870 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003871 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003872 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003873}
3874
3875void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3876 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3877 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003878 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003879}
3880
3881void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3882 const VkRenderPassBeginInfo *pRenderPassBegin,
3883 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3884 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003885 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003886}
3887
Mike Schuchardt2df08912020-12-15 16:28:09 -08003888bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003889 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003890 bool skip = false;
3891
3892 auto cb_context = GetAccessContext(commandBuffer);
3893 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003894 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003895 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003896 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003897}
3898
3899bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3900 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003901 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003902 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003903 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003904 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3905 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003906 return skip;
3907}
3908
Mike Schuchardt2df08912020-12-15 16:28:09 -08003909bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3910 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003911 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003912 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003913 return skip;
3914}
3915
3916bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3917 const VkSubpassEndInfo *pSubpassEndInfo) const {
3918 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003919 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003920 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003921}
3922
3923void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003924 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003925 auto cb_context = GetAccessContext(commandBuffer);
3926 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003927 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003928
sfricke-samsung85584a72021-09-30 21:43:38 -07003929 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003930 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003931}
3932
3933void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3934 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003935 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003936 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003937 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003938}
3939
3940void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3941 const VkSubpassEndInfo *pSubpassEndInfo) {
3942 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003943 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003944}
3945
3946void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3947 const VkSubpassEndInfo *pSubpassEndInfo) {
3948 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003949 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003950}
3951
sfricke-samsung85584a72021-09-30 21:43:38 -07003952bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3953 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003954 bool skip = false;
3955
3956 auto cb_context = GetAccessContext(commandBuffer);
3957 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003958 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003959
sfricke-samsung85584a72021-09-30 21:43:38 -07003960 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003961 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003962 return skip;
3963}
3964
3965bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3966 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003967 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003968 return skip;
3969}
3970
Mike Schuchardt2df08912020-12-15 16:28:09 -08003971bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003972 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003973 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003974 return skip;
3975}
3976
3977bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003978 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003979 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003980 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003981 return skip;
3982}
3983
sfricke-samsung85584a72021-09-30 21:43:38 -07003984void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003985 // Resolve the all subpass contexts to the command buffer contexts
3986 auto cb_context = GetAccessContext(commandBuffer);
3987 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003988 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003989
sfricke-samsung85584a72021-09-30 21:43:38 -07003990 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003991 sync_op.Record(cb_context);
3992 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003993}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003994
John Zulauf33fc1d52020-07-17 11:01:10 -06003995// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3996// updates to a resource which do not conflict at the byte level.
3997// TODO: Revisit this rule to see if it needs to be tighter or looser
3998// TODO: Add programatic control over suppression heuristics
3999bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
4000 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
4001}
4002
John Zulauf3d84f1b2020-03-09 13:33:25 -06004003void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004004 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06004005 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004006}
4007
4008void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004009 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004010 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004011}
4012
4013void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07004014 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06004015 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004016}
locke-lunarga19c71d2020-03-02 18:17:04 -07004017
sfricke-samsung71f04e32022-03-16 01:21:21 -05004018template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004019bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004020 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4021 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004022 bool skip = false;
4023 const auto *cb_access_context = GetAccessContext(commandBuffer);
4024 assert(cb_access_context);
4025 if (!cb_access_context) return skip;
4026
Tony Barbour845d29b2021-11-09 11:43:14 -07004027 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004028
locke-lunarga19c71d2020-03-02 18:17:04 -07004029 const auto *context = cb_access_context->GetCurrentAccessContext();
4030 assert(context);
4031 if (!context) return skip;
4032
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004033 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4034 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004035
4036 for (uint32_t region = 0; region < regionCount; region++) {
4037 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07004038 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07004039 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004040 if (src_buffer) {
4041 ResourceAccessRange src_range =
4042 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004043 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07004044 if (hazard.hazard) {
4045 // PHASE1 TODO -- add tag information to log msg when useful.
4046 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
4047 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4048 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004049 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004050 }
4051 }
4052
Jeremy Gebben40a22942020-12-22 14:22:06 -07004053 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07004054 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004055 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004056 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004057 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004058 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004059 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004060 }
4061 if (skip) break;
4062 }
4063 if (skip) break;
4064 }
4065 return skip;
4066}
4067
Jeff Leger178b1e52020-10-05 12:22:23 -04004068bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4069 VkImageLayout dstImageLayout, uint32_t regionCount,
4070 const VkBufferImageCopy *pRegions) const {
4071 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
Tony Barbour845d29b2021-11-09 11:43:14 -07004072 CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004073}
4074
4075bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4076 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4077 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4078 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004079 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4080}
4081
4082bool SyncValidator::PreCallValidateCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4083 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) const {
4084 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4085 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4086 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004087}
4088
sfricke-samsung71f04e32022-03-16 01:21:21 -05004089template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004090void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004091 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4092 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004093 auto *cb_access_context = GetAccessContext(commandBuffer);
4094 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004095
Jeff Leger178b1e52020-10-05 12:22:23 -04004096 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004097 auto *context = cb_access_context->GetCurrentAccessContext();
4098 assert(context);
4099
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004100 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4101 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004102
4103 for (uint32_t region = 0; region < regionCount; region++) {
4104 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07004105 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004106 if (src_buffer) {
4107 ResourceAccessRange src_range =
4108 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004109 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004110 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07004111 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004112 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004113 }
4114 }
4115}
4116
Jeff Leger178b1e52020-10-05 12:22:23 -04004117void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4118 VkImageLayout dstImageLayout, uint32_t regionCount,
4119 const VkBufferImageCopy *pRegions) {
4120 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
Tony Barbour845d29b2021-11-09 11:43:14 -07004121 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004122}
4123
4124void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4125 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4126 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4127 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4128 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004129 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4130}
4131
4132void SyncValidator::PreCallRecordCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4133 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) {
4134 StateTracker::PreCallRecordCmdCopyBufferToImage2(commandBuffer, pCopyBufferToImageInfo);
4135 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4136 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4137 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004138}
4139
sfricke-samsung71f04e32022-03-16 01:21:21 -05004140template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004141bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004142 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
4143 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004144 bool skip = false;
4145 const auto *cb_access_context = GetAccessContext(commandBuffer);
4146 assert(cb_access_context);
4147 if (!cb_access_context) return skip;
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004148 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004149
locke-lunarga19c71d2020-03-02 18:17:04 -07004150 const auto *context = cb_access_context->GetCurrentAccessContext();
4151 assert(context);
4152 if (!context) return skip;
4153
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004154 auto src_image = Get<IMAGE_STATE>(srcImage);
4155 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004156 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004157 for (uint32_t region = 0; region < regionCount; region++) {
4158 const auto &copy_region = pRegions[region];
4159 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004160 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004161 copy_region.imageOffset, copy_region.imageExtent);
4162 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004163 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004164 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004165 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004166 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004167 }
John Zulauf477700e2021-01-06 11:41:49 -07004168 if (dst_mem) {
4169 ResourceAccessRange dst_range =
4170 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004171 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004172 if (hazard.hazard) {
4173 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4174 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4175 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004176 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004177 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004178 }
4179 }
4180 if (skip) break;
4181 }
4182 return skip;
4183}
4184
Jeff Leger178b1e52020-10-05 12:22:23 -04004185bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4186 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4187 const VkBufferImageCopy *pRegions) const {
4188 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004189 CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004190}
4191
4192bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4193 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4194 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4195 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004196 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4197}
4198
4199bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4200 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) const {
4201 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4202 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4203 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004204}
4205
sfricke-samsung71f04e32022-03-16 01:21:21 -05004206template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004207void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004208 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004209 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004210 auto *cb_access_context = GetAccessContext(commandBuffer);
4211 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004212
Jeff Leger178b1e52020-10-05 12:22:23 -04004213 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004214 auto *context = cb_access_context->GetCurrentAccessContext();
4215 assert(context);
4216
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004217 auto src_image = Get<IMAGE_STATE>(srcImage);
Jeremy Gebben9f537102021-10-05 16:37:12 -06004218 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004219 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004220 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004221
4222 for (uint32_t region = 0; region < regionCount; region++) {
4223 const auto &copy_region = pRegions[region];
4224 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004225 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004226 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004227 if (dst_buffer) {
4228 ResourceAccessRange dst_range =
4229 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004230 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004231 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004232 }
4233 }
4234}
4235
Jeff Leger178b1e52020-10-05 12:22:23 -04004236void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4237 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4238 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004239 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004240}
4241
4242void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4243 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4244 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4245 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4246 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004247 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4248}
4249
4250void SyncValidator::PreCallRecordCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4251 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) {
4252 StateTracker::PreCallRecordCmdCopyImageToBuffer2(commandBuffer, pCopyImageToBufferInfo);
4253 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4254 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4255 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004256}
4257
4258template <typename RegionType>
4259bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4260 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4261 const RegionType *pRegions, VkFilter filter, const char *apiName) 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;
4266
4267 const auto *context = cb_access_context->GetCurrentAccessContext();
4268 assert(context);
4269 if (!context) return skip;
4270
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004271 auto src_image = Get<IMAGE_STATE>(srcImage);
4272 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004273
4274 for (uint32_t region = 0; region < regionCount; region++) {
4275 const auto &blit_region = pRegions[region];
4276 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004277 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4278 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4279 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4280 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4281 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4282 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004283 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004284 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004285 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004286 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004287 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004288 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004289 }
4290 }
4291
4292 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004293 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4294 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4295 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4296 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4297 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4298 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004299 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004300 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004301 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004302 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004303 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004304 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004305 }
4306 if (skip) break;
4307 }
4308 }
4309
4310 return skip;
4311}
4312
Jeff Leger178b1e52020-10-05 12:22:23 -04004313bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4314 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4315 const VkImageBlit *pRegions, VkFilter filter) const {
4316 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4317 "vkCmdBlitImage");
4318}
4319
4320bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4321 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4322 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4323 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4324 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4325}
4326
Tony-LunarG542ae912021-11-04 16:06:44 -06004327bool SyncValidator::PreCallValidateCmdBlitImage2(VkCommandBuffer commandBuffer,
4328 const VkBlitImageInfo2 *pBlitImageInfo) const {
4329 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4330 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4331 pBlitImageInfo->filter, "vkCmdBlitImage2");
4332}
4333
Jeff Leger178b1e52020-10-05 12:22:23 -04004334template <typename RegionType>
4335void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4336 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4337 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004338 auto *cb_access_context = GetAccessContext(commandBuffer);
4339 assert(cb_access_context);
4340 auto *context = cb_access_context->GetCurrentAccessContext();
4341 assert(context);
4342
Jeremy Gebben9f537102021-10-05 16:37:12 -06004343 auto src_image = Get<IMAGE_STATE>(srcImage);
4344 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004345
4346 for (uint32_t region = 0; region < regionCount; region++) {
4347 const auto &blit_region = pRegions[region];
4348 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004349 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4350 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4351 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4352 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4353 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4354 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004355 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004356 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004357 }
4358 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004359 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4360 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4361 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4362 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4363 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4364 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004365 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004366 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004367 }
4368 }
4369}
locke-lunarg36ba2592020-04-03 09:42:04 -06004370
Jeff Leger178b1e52020-10-05 12:22:23 -04004371void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4372 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4373 const VkImageBlit *pRegions, VkFilter filter) {
4374 auto *cb_access_context = GetAccessContext(commandBuffer);
4375 assert(cb_access_context);
4376 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4377 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4378 pRegions, filter);
4379 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4380}
4381
4382void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4383 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4384 auto *cb_access_context = GetAccessContext(commandBuffer);
4385 assert(cb_access_context);
4386 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4387 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4388 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4389 pBlitImageInfo->filter, tag);
4390}
4391
Tony-LunarG542ae912021-11-04 16:06:44 -06004392void SyncValidator::PreCallRecordCmdBlitImage2(VkCommandBuffer commandBuffer, const VkBlitImageInfo2 *pBlitImageInfo) {
4393 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4394 auto *cb_access_context = GetAccessContext(commandBuffer);
4395 assert(cb_access_context);
4396 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2);
4397 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4398 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4399 pBlitImageInfo->filter, tag);
4400}
4401
John Zulauffaea0ee2021-01-14 14:01:32 -07004402bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4403 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4404 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4405 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004406 bool skip = false;
4407 if (drawCount == 0) return skip;
4408
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004409 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004410 VkDeviceSize size = struct_size;
4411 if (drawCount == 1 || stride == size) {
4412 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004413 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004414 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4415 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004416 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004417 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004418 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004419 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004420 }
4421 } else {
4422 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004423 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004424 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4425 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004426 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004427 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4428 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004429 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004430 break;
4431 }
4432 }
4433 }
4434 return skip;
4435}
4436
John Zulauf14940722021-04-12 15:19:02 -06004437void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004438 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4439 uint32_t stride) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004440 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004441 VkDeviceSize size = struct_size;
4442 if (drawCount == 1 || stride == size) {
4443 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004444 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004445 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004446 } else {
4447 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004448 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004449 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4450 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004451 }
4452 }
4453}
4454
John Zulauffaea0ee2021-01-14 14:01:32 -07004455bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4456 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4457 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004458 bool skip = false;
4459
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004460 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004461 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004462 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4463 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004464 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004465 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004466 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004467 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004468 }
4469 return skip;
4470}
4471
John Zulauf14940722021-04-12 15:19:02 -06004472void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004473 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004474 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004475 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004476}
4477
locke-lunarg36ba2592020-04-03 09:42:04 -06004478bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004479 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004480 const auto *cb_access_context = GetAccessContext(commandBuffer);
4481 assert(cb_access_context);
4482 if (!cb_access_context) return skip;
4483
locke-lunarg61870c22020-06-09 14:51:50 -06004484 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004485 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004486}
4487
4488void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004489 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004490 auto *cb_access_context = GetAccessContext(commandBuffer);
4491 assert(cb_access_context);
4492 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004493
locke-lunarg61870c22020-06-09 14:51:50 -06004494 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004495}
locke-lunarge1a67022020-04-29 00:15:36 -06004496
4497bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004498 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004499 const auto *cb_access_context = GetAccessContext(commandBuffer);
4500 assert(cb_access_context);
4501 if (!cb_access_context) return skip;
4502
4503 const auto *context = cb_access_context->GetCurrentAccessContext();
4504 assert(context);
4505 if (!context) return skip;
4506
locke-lunarg61870c22020-06-09 14:51:50 -06004507 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004508 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4509 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004510 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004511}
4512
4513void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004514 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004515 auto *cb_access_context = GetAccessContext(commandBuffer);
4516 assert(cb_access_context);
4517 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4518 auto *context = cb_access_context->GetCurrentAccessContext();
4519 assert(context);
4520
locke-lunarg61870c22020-06-09 14:51:50 -06004521 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4522 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004523}
4524
4525bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4526 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004527 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004528 const auto *cb_access_context = GetAccessContext(commandBuffer);
4529 assert(cb_access_context);
4530 if (!cb_access_context) return skip;
4531
locke-lunarg61870c22020-06-09 14:51:50 -06004532 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4533 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4534 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004535 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004536}
4537
4538void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4539 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004540 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004541 auto *cb_access_context = GetAccessContext(commandBuffer);
4542 assert(cb_access_context);
4543 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004544
locke-lunarg61870c22020-06-09 14:51:50 -06004545 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4546 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4547 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004548}
4549
4550bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4551 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004552 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004553 const auto *cb_access_context = GetAccessContext(commandBuffer);
4554 assert(cb_access_context);
4555 if (!cb_access_context) return skip;
4556
locke-lunarg61870c22020-06-09 14:51:50 -06004557 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4558 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4559 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004560 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004561}
4562
4563void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4564 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004565 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004566 auto *cb_access_context = GetAccessContext(commandBuffer);
4567 assert(cb_access_context);
4568 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004569
locke-lunarg61870c22020-06-09 14:51:50 -06004570 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4571 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4572 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004573}
4574
4575bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4576 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004577 bool skip = false;
4578 if (drawCount == 0) return skip;
4579
locke-lunargff255f92020-05-13 18:53:52 -06004580 const auto *cb_access_context = GetAccessContext(commandBuffer);
4581 assert(cb_access_context);
4582 if (!cb_access_context) return skip;
4583
4584 const auto *context = cb_access_context->GetCurrentAccessContext();
4585 assert(context);
4586 if (!context) return skip;
4587
locke-lunarg61870c22020-06-09 14:51:50 -06004588 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4589 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004590 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4591 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004592
4593 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4594 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4595 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004596 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004597 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004598}
4599
4600void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4601 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004602 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004603 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004604 auto *cb_access_context = GetAccessContext(commandBuffer);
4605 assert(cb_access_context);
4606 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4607 auto *context = cb_access_context->GetCurrentAccessContext();
4608 assert(context);
4609
locke-lunarg61870c22020-06-09 14:51:50 -06004610 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4611 cb_access_context->RecordDrawSubpassAttachment(tag);
4612 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004613
4614 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4615 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4616 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004617 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004618}
4619
4620bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4621 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004622 bool skip = false;
4623 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004624 const auto *cb_access_context = GetAccessContext(commandBuffer);
4625 assert(cb_access_context);
4626 if (!cb_access_context) return skip;
4627
4628 const auto *context = cb_access_context->GetCurrentAccessContext();
4629 assert(context);
4630 if (!context) return skip;
4631
locke-lunarg61870c22020-06-09 14:51:50 -06004632 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4633 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004634 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4635 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004636
4637 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4638 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4639 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004640 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004641 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004642}
4643
4644void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4645 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004646 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004647 auto *cb_access_context = GetAccessContext(commandBuffer);
4648 assert(cb_access_context);
4649 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4650 auto *context = cb_access_context->GetCurrentAccessContext();
4651 assert(context);
4652
locke-lunarg61870c22020-06-09 14:51:50 -06004653 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4654 cb_access_context->RecordDrawSubpassAttachment(tag);
4655 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004656
4657 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4658 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4659 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004660 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004661}
4662
4663bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4664 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4665 uint32_t stride, const char *function) const {
4666 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004667 const auto *cb_access_context = GetAccessContext(commandBuffer);
4668 assert(cb_access_context);
4669 if (!cb_access_context) return skip;
4670
4671 const auto *context = cb_access_context->GetCurrentAccessContext();
4672 assert(context);
4673 if (!context) return skip;
4674
locke-lunarg61870c22020-06-09 14:51:50 -06004675 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4676 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004677 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4678 maxDrawCount, stride, function);
4679 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004680
4681 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4682 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4683 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004684 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004685 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004686}
4687
4688bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4689 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4690 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004691 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4692 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004693}
4694
sfricke-samsung85584a72021-09-30 21:43:38 -07004695void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4696 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4697 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004698 auto *cb_access_context = GetAccessContext(commandBuffer);
4699 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004700 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004701 auto *context = cb_access_context->GetCurrentAccessContext();
4702 assert(context);
4703
locke-lunarg61870c22020-06-09 14:51:50 -06004704 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4705 cb_access_context->RecordDrawSubpassAttachment(tag);
4706 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4707 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004708
4709 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4710 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4711 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004712 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004713}
4714
sfricke-samsung85584a72021-09-30 21:43:38 -07004715void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4716 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4717 uint32_t stride) {
4718 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4719 stride);
4720 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4721 CMD_DRAWINDIRECTCOUNT);
4722}
locke-lunarge1a67022020-04-29 00:15:36 -06004723bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4724 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4725 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004726 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4727 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004728}
4729
4730void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4731 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4732 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004733 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4734 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004735 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4736 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004737}
4738
4739bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4740 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4741 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004742 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4743 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004744}
4745
4746void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4747 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4748 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004749 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4750 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004751 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4752 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004753}
4754
4755bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4756 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4757 uint32_t stride, const char *function) const {
4758 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004759 const auto *cb_access_context = GetAccessContext(commandBuffer);
4760 assert(cb_access_context);
4761 if (!cb_access_context) return skip;
4762
4763 const auto *context = cb_access_context->GetCurrentAccessContext();
4764 assert(context);
4765 if (!context) return skip;
4766
locke-lunarg61870c22020-06-09 14:51:50 -06004767 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4768 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004769 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4770 offset, maxDrawCount, stride, function);
4771 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004772
4773 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4774 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4775 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004776 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004777 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004778}
4779
4780bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4781 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4782 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004783 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4784 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004785}
4786
sfricke-samsung85584a72021-09-30 21:43:38 -07004787void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4788 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4789 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004790 auto *cb_access_context = GetAccessContext(commandBuffer);
4791 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004792 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004793 auto *context = cb_access_context->GetCurrentAccessContext();
4794 assert(context);
4795
locke-lunarg61870c22020-06-09 14:51:50 -06004796 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4797 cb_access_context->RecordDrawSubpassAttachment(tag);
4798 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4799 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004800
4801 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4802 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004803 // We will update the index and vertex buffer in SubmitQueue in the future.
4804 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004805}
4806
sfricke-samsung85584a72021-09-30 21:43:38 -07004807void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4808 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4809 uint32_t maxDrawCount, uint32_t stride) {
4810 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4811 maxDrawCount, stride);
4812 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4813 CMD_DRAWINDEXEDINDIRECTCOUNT);
4814}
4815
locke-lunarge1a67022020-04-29 00:15:36 -06004816bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4817 VkDeviceSize offset, VkBuffer countBuffer,
4818 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4819 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004820 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4821 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004822}
4823
4824void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4825 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4826 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004827 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4828 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004829 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4830 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004831}
4832
4833bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4834 VkDeviceSize offset, VkBuffer countBuffer,
4835 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4836 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004837 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4838 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004839}
4840
4841void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4842 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4843 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004844 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4845 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004846 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4847 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004848}
4849
4850bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4851 const VkClearColorValue *pColor, uint32_t rangeCount,
4852 const VkImageSubresourceRange *pRanges) const {
4853 bool skip = false;
4854 const auto *cb_access_context = GetAccessContext(commandBuffer);
4855 assert(cb_access_context);
4856 if (!cb_access_context) return skip;
4857
4858 const auto *context = cb_access_context->GetCurrentAccessContext();
4859 assert(context);
4860 if (!context) return skip;
4861
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004862 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004863
4864 for (uint32_t index = 0; index < rangeCount; index++) {
4865 const auto &range = pRanges[index];
4866 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004867 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004868 if (hazard.hazard) {
4869 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004870 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004871 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004872 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004873 }
4874 }
4875 }
4876 return skip;
4877}
4878
4879void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4880 const VkClearColorValue *pColor, uint32_t rangeCount,
4881 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004882 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004883 auto *cb_access_context = GetAccessContext(commandBuffer);
4884 assert(cb_access_context);
4885 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4886 auto *context = cb_access_context->GetCurrentAccessContext();
4887 assert(context);
4888
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004889 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004890
4891 for (uint32_t index = 0; index < rangeCount; index++) {
4892 const auto &range = pRanges[index];
4893 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004894 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004895 }
4896 }
4897}
4898
4899bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4900 VkImageLayout imageLayout,
4901 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4902 const VkImageSubresourceRange *pRanges) const {
4903 bool skip = false;
4904 const auto *cb_access_context = GetAccessContext(commandBuffer);
4905 assert(cb_access_context);
4906 if (!cb_access_context) return skip;
4907
4908 const auto *context = cb_access_context->GetCurrentAccessContext();
4909 assert(context);
4910 if (!context) return skip;
4911
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004912 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004913
4914 for (uint32_t index = 0; index < rangeCount; index++) {
4915 const auto &range = pRanges[index];
4916 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004917 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004918 if (hazard.hazard) {
4919 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004920 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004921 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004922 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004923 }
4924 }
4925 }
4926 return skip;
4927}
4928
4929void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4930 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4931 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004932 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004933 auto *cb_access_context = GetAccessContext(commandBuffer);
4934 assert(cb_access_context);
4935 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4936 auto *context = cb_access_context->GetCurrentAccessContext();
4937 assert(context);
4938
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004939 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004940
4941 for (uint32_t index = 0; index < rangeCount; index++) {
4942 const auto &range = pRanges[index];
4943 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004944 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004945 }
4946 }
4947}
4948
4949bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4950 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4951 VkDeviceSize dstOffset, VkDeviceSize stride,
4952 VkQueryResultFlags flags) const {
4953 bool skip = false;
4954 const auto *cb_access_context = GetAccessContext(commandBuffer);
4955 assert(cb_access_context);
4956 if (!cb_access_context) return skip;
4957
4958 const auto *context = cb_access_context->GetCurrentAccessContext();
4959 assert(context);
4960 if (!context) return skip;
4961
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004962 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004963
4964 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004965 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004966 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004967 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004968 skip |=
4969 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4970 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004971 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004972 }
4973 }
locke-lunargff255f92020-05-13 18:53:52 -06004974
4975 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004976 return skip;
4977}
4978
4979void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4980 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4981 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004982 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4983 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004984 auto *cb_access_context = GetAccessContext(commandBuffer);
4985 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004986 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004987 auto *context = cb_access_context->GetCurrentAccessContext();
4988 assert(context);
4989
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004990 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004991
4992 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004993 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004994 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004995 }
locke-lunargff255f92020-05-13 18:53:52 -06004996
4997 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004998}
4999
5000bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5001 VkDeviceSize size, uint32_t data) const {
5002 bool skip = false;
5003 const auto *cb_access_context = GetAccessContext(commandBuffer);
5004 assert(cb_access_context);
5005 if (!cb_access_context) return skip;
5006
5007 const auto *context = cb_access_context->GetCurrentAccessContext();
5008 assert(context);
5009 if (!context) return skip;
5010
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005011 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005012
5013 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005014 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005015 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005016 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005017 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005018 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005019 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005020 }
5021 }
5022 return skip;
5023}
5024
5025void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5026 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005027 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06005028 auto *cb_access_context = GetAccessContext(commandBuffer);
5029 assert(cb_access_context);
5030 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
5031 auto *context = cb_access_context->GetCurrentAccessContext();
5032 assert(context);
5033
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005034 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005035
5036 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005037 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005038 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005039 }
5040}
5041
5042bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5043 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5044 const VkImageResolve *pRegions) const {
5045 bool skip = false;
5046 const auto *cb_access_context = GetAccessContext(commandBuffer);
5047 assert(cb_access_context);
5048 if (!cb_access_context) return skip;
5049
5050 const auto *context = cb_access_context->GetCurrentAccessContext();
5051 assert(context);
5052 if (!context) return skip;
5053
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005054 auto src_image = Get<IMAGE_STATE>(srcImage);
5055 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005056
5057 for (uint32_t region = 0; region < regionCount; region++) {
5058 const auto &resolve_region = pRegions[region];
5059 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005060 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005061 resolve_region.srcOffset, resolve_region.extent);
5062 if (hazard.hazard) {
5063 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005064 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005065 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005066 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005067 }
5068 }
5069
5070 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005071 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005072 resolve_region.dstOffset, resolve_region.extent);
5073 if (hazard.hazard) {
5074 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005075 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005076 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005077 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005078 }
5079 if (skip) break;
5080 }
5081 }
5082
5083 return skip;
5084}
5085
5086void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5087 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5088 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005089 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5090 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005091 auto *cb_access_context = GetAccessContext(commandBuffer);
5092 assert(cb_access_context);
5093 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5094 auto *context = cb_access_context->GetCurrentAccessContext();
5095 assert(context);
5096
Jeremy Gebben9f537102021-10-05 16:37:12 -06005097 auto src_image = Get<IMAGE_STATE>(srcImage);
5098 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005099
5100 for (uint32_t region = 0; region < regionCount; region++) {
5101 const auto &resolve_region = pRegions[region];
5102 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005103 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005104 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005105 }
5106 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005107 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005108 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005109 }
5110 }
5111}
5112
Tony-LunarG562fc102021-11-12 13:58:35 -07005113bool SyncValidator::ValidateCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5114 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04005115 bool skip = false;
5116 const auto *cb_access_context = GetAccessContext(commandBuffer);
5117 assert(cb_access_context);
5118 if (!cb_access_context) return skip;
5119
5120 const auto *context = cb_access_context->GetCurrentAccessContext();
5121 assert(context);
5122 if (!context) return skip;
5123
Tony-LunarG562fc102021-11-12 13:58:35 -07005124 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005125 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5126 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005127
5128 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5129 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5130 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005131 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005132 resolve_region.srcOffset, resolve_region.extent);
5133 if (hazard.hazard) {
5134 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005135 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005136 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005137 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005138 }
5139 }
5140
5141 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005142 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005143 resolve_region.dstOffset, resolve_region.extent);
5144 if (hazard.hazard) {
5145 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005146 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005147 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005148 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005149 }
5150 if (skip) break;
5151 }
5152 }
5153
5154 return skip;
5155}
5156
Tony-LunarG562fc102021-11-12 13:58:35 -07005157bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5158 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5159 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5160}
5161
5162bool SyncValidator::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
5163 const VkResolveImageInfo2 *pResolveImageInfo) const {
5164 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5165}
5166
5167void SyncValidator::RecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5168 CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04005169 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5170 auto *cb_access_context = GetAccessContext(commandBuffer);
5171 assert(cb_access_context);
Tony-LunarG562fc102021-11-12 13:58:35 -07005172 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04005173 auto *context = cb_access_context->GetCurrentAccessContext();
5174 assert(context);
5175
Jeremy Gebben9f537102021-10-05 16:37:12 -06005176 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5177 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005178
5179 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5180 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5181 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005182 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005183 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005184 }
5185 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005186 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005187 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005188 }
5189 }
5190}
5191
Tony-LunarG562fc102021-11-12 13:58:35 -07005192void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5193 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5194 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5195}
5196
5197void SyncValidator::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2 *pResolveImageInfo) {
5198 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5199}
5200
locke-lunarge1a67022020-04-29 00:15:36 -06005201bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5202 VkDeviceSize dataSize, const void *pData) const {
5203 bool skip = false;
5204 const auto *cb_access_context = GetAccessContext(commandBuffer);
5205 assert(cb_access_context);
5206 if (!cb_access_context) return skip;
5207
5208 const auto *context = cb_access_context->GetCurrentAccessContext();
5209 assert(context);
5210 if (!context) return skip;
5211
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005212 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005213
5214 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005215 // VK_WHOLE_SIZE not allowed
5216 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005217 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005218 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005219 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005220 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005221 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005222 }
5223 }
5224 return skip;
5225}
5226
5227void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5228 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005229 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005230 auto *cb_access_context = GetAccessContext(commandBuffer);
5231 assert(cb_access_context);
5232 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5233 auto *context = cb_access_context->GetCurrentAccessContext();
5234 assert(context);
5235
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005236 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005237
5238 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005239 // VK_WHOLE_SIZE not allowed
5240 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005241 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005242 }
5243}
locke-lunargff255f92020-05-13 18:53:52 -06005244
5245bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5246 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5247 bool skip = false;
5248 const auto *cb_access_context = GetAccessContext(commandBuffer);
5249 assert(cb_access_context);
5250 if (!cb_access_context) return skip;
5251
5252 const auto *context = cb_access_context->GetCurrentAccessContext();
5253 assert(context);
5254 if (!context) return skip;
5255
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005256 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005257
5258 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005259 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005260 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005261 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005262 skip |=
5263 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5264 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005265 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005266 }
5267 }
5268 return skip;
5269}
5270
5271void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5272 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005273 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005274 auto *cb_access_context = GetAccessContext(commandBuffer);
5275 assert(cb_access_context);
5276 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5277 auto *context = cb_access_context->GetCurrentAccessContext();
5278 assert(context);
5279
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005280 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005281
5282 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005283 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005284 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005285 }
5286}
John Zulauf49beb112020-11-04 16:06:31 -07005287
5288bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5289 bool skip = false;
5290 const auto *cb_context = GetAccessContext(commandBuffer);
5291 assert(cb_context);
5292 if (!cb_context) return skip;
5293
John Zulauf36ef9282021-02-02 11:47:24 -07005294 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005295 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005296}
5297
5298void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5299 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5300 auto *cb_context = GetAccessContext(commandBuffer);
5301 assert(cb_context);
5302 if (!cb_context) return;
John Zulauf1bf30522021-09-03 15:39:06 -06005303 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005304}
5305
John Zulauf4edde622021-02-15 08:54:50 -07005306bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5307 const VkDependencyInfoKHR *pDependencyInfo) const {
5308 bool skip = false;
5309 const auto *cb_context = GetAccessContext(commandBuffer);
5310 assert(cb_context);
5311 if (!cb_context || !pDependencyInfo) return skip;
5312
5313 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5314 return set_event_op.Validate(*cb_context);
5315}
5316
Tony-LunarGc43525f2021-11-15 16:12:38 -07005317bool SyncValidator::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5318 const VkDependencyInfo *pDependencyInfo) const {
5319 bool skip = false;
5320 const auto *cb_context = GetAccessContext(commandBuffer);
5321 assert(cb_context);
5322 if (!cb_context || !pDependencyInfo) return skip;
5323
5324 SyncOpSetEvent set_event_op(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5325 return set_event_op.Validate(*cb_context);
5326}
5327
John Zulauf4edde622021-02-15 08:54:50 -07005328void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5329 const VkDependencyInfoKHR *pDependencyInfo) {
5330 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5331 auto *cb_context = GetAccessContext(commandBuffer);
5332 assert(cb_context);
5333 if (!cb_context || !pDependencyInfo) return;
5334
John Zulauf1bf30522021-09-03 15:39:06 -06005335 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
John Zulauf4edde622021-02-15 08:54:50 -07005336}
5337
Tony-LunarGc43525f2021-11-15 16:12:38 -07005338void SyncValidator::PostCallRecordCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5339 const VkDependencyInfo *pDependencyInfo) {
5340 StateTracker::PostCallRecordCmdSetEvent2(commandBuffer, event, pDependencyInfo);
5341 auto *cb_context = GetAccessContext(commandBuffer);
5342 assert(cb_context);
5343 if (!cb_context || !pDependencyInfo) return;
5344
5345 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5346}
5347
John Zulauf49beb112020-11-04 16:06:31 -07005348bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5349 VkPipelineStageFlags stageMask) const {
5350 bool skip = false;
5351 const auto *cb_context = GetAccessContext(commandBuffer);
5352 assert(cb_context);
5353 if (!cb_context) return skip;
5354
John Zulauf36ef9282021-02-02 11:47:24 -07005355 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005356 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005357}
5358
5359void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5360 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5361 auto *cb_context = GetAccessContext(commandBuffer);
5362 assert(cb_context);
5363 if (!cb_context) return;
5364
John Zulauf1bf30522021-09-03 15:39:06 -06005365 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005366}
5367
John Zulauf4edde622021-02-15 08:54:50 -07005368bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5369 VkPipelineStageFlags2KHR stageMask) const {
5370 bool skip = false;
5371 const auto *cb_context = GetAccessContext(commandBuffer);
5372 assert(cb_context);
5373 if (!cb_context) return skip;
5374
5375 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5376 return reset_event_op.Validate(*cb_context);
5377}
5378
Tony-LunarGa2662db2021-11-16 07:26:24 -07005379bool SyncValidator::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5380 VkPipelineStageFlags2 stageMask) const {
5381 bool skip = false;
5382 const auto *cb_context = GetAccessContext(commandBuffer);
5383 assert(cb_context);
5384 if (!cb_context) return skip;
5385
5386 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5387 return reset_event_op.Validate(*cb_context);
5388}
5389
John Zulauf4edde622021-02-15 08:54:50 -07005390void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5391 VkPipelineStageFlags2KHR stageMask) {
5392 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5393 auto *cb_context = GetAccessContext(commandBuffer);
5394 assert(cb_context);
5395 if (!cb_context) return;
5396
John Zulauf1bf30522021-09-03 15:39:06 -06005397 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4edde622021-02-15 08:54:50 -07005398}
5399
Tony-LunarGa2662db2021-11-16 07:26:24 -07005400void SyncValidator::PostCallRecordCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags2 stageMask) {
5401 StateTracker::PostCallRecordCmdResetEvent2(commandBuffer, event, stageMask);
5402 auto *cb_context = GetAccessContext(commandBuffer);
5403 assert(cb_context);
5404 if (!cb_context) return;
5405
5406 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5407}
5408
John Zulauf49beb112020-11-04 16:06:31 -07005409bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5410 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5411 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5412 uint32_t bufferMemoryBarrierCount,
5413 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5414 uint32_t imageMemoryBarrierCount,
5415 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5416 bool skip = false;
5417 const auto *cb_context = GetAccessContext(commandBuffer);
5418 assert(cb_context);
5419 if (!cb_context) return skip;
5420
John Zulauf36ef9282021-02-02 11:47:24 -07005421 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5422 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5423 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005424 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005425}
5426
5427void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5428 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5429 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5430 uint32_t bufferMemoryBarrierCount,
5431 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5432 uint32_t imageMemoryBarrierCount,
5433 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5434 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5435 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5436 imageMemoryBarrierCount, pImageMemoryBarriers);
5437
5438 auto *cb_context = GetAccessContext(commandBuffer);
5439 assert(cb_context);
5440 if (!cb_context) return;
5441
John Zulauf1bf30522021-09-03 15:39:06 -06005442 cb_context->RecordSyncOp<SyncOpWaitEvents>(
John Zulauf610e28c2021-08-03 17:46:23 -06005443 CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
John Zulauf1bf30522021-09-03 15:39:06 -06005444 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf4a6105a2020-11-17 15:11:05 -07005445}
5446
John Zulauf4edde622021-02-15 08:54:50 -07005447bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5448 const VkDependencyInfoKHR *pDependencyInfos) const {
5449 bool skip = false;
5450 const auto *cb_context = GetAccessContext(commandBuffer);
5451 assert(cb_context);
5452 if (!cb_context) return skip;
5453
5454 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5455 skip |= wait_events_op.Validate(*cb_context);
5456 return skip;
5457}
5458
5459void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5460 const VkDependencyInfoKHR *pDependencyInfos) {
5461 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5462
5463 auto *cb_context = GetAccessContext(commandBuffer);
5464 assert(cb_context);
5465 if (!cb_context) return;
5466
John Zulauf1bf30522021-09-03 15:39:06 -06005467 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5468 pDependencyInfos);
John Zulauf4edde622021-02-15 08:54:50 -07005469}
5470
Tony-LunarG1364cf52021-11-17 16:10:11 -07005471bool SyncValidator::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5472 const VkDependencyInfo *pDependencyInfos) const {
5473 bool skip = false;
5474 const auto *cb_context = GetAccessContext(commandBuffer);
5475 assert(cb_context);
5476 if (!cb_context) return skip;
5477
5478 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5479 skip |= wait_events_op.Validate(*cb_context);
5480 return skip;
5481}
5482
5483void SyncValidator::PostCallRecordCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5484 const VkDependencyInfo *pDependencyInfos) {
5485 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5486
5487 auto *cb_context = GetAccessContext(commandBuffer);
5488 assert(cb_context);
5489 if (!cb_context) return;
5490
5491 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5492 pDependencyInfos);
5493}
5494
John Zulauf4a6105a2020-11-17 15:11:05 -07005495void SyncEventState::ResetFirstScope() {
5496 for (const auto address_type : kAddressTypes) {
5497 first_scope[static_cast<size_t>(address_type)].clear();
5498 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005499 scope = SyncExecScope();
John Zulauf78b1f892021-09-20 15:02:09 -06005500 first_scope_set = false;
5501 first_scope_tag = 0;
John Zulauf4a6105a2020-11-17 15:11:05 -07005502}
5503
5504// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005505SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005506 IgnoreReason reason = NotIgnored;
5507
Tony-LunarG1364cf52021-11-17 16:10:11 -07005508 if ((CMD_WAITEVENTS2KHR == cmd || CMD_WAITEVENTS2 == cmd) && (CMD_SETEVENT == last_command)) {
John Zulauf4edde622021-02-15 08:54:50 -07005509 reason = SetVsWait2;
5510 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5511 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005512 } else if (unsynchronized_set) {
5513 reason = SetRace;
John Zulauf78b1f892021-09-20 15:02:09 -06005514 } else if (first_scope_set) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005515 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005516 if (missing_bits) reason = MissingStageBits;
5517 }
5518
5519 return reason;
5520}
5521
Jeremy Gebben40a22942020-12-22 14:22:06 -07005522bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005523 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5524 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5525 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005526}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005527
John Zulauf36ef9282021-02-02 11:47:24 -07005528SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5529 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5530 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005531 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5532 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5533 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005534 : SyncOpBase(cmd), barriers_(1) {
5535 auto &barrier_set = barriers_[0];
5536 barrier_set.dependency_flags = dependencyFlags;
5537 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5538 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005539 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005540 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5541 pMemoryBarriers);
5542 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5543 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5544 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5545 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005546}
5547
John Zulauf4edde622021-02-15 08:54:50 -07005548SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5549 const VkDependencyInfoKHR *dep_infos)
5550 : SyncOpBase(cmd), barriers_(event_count) {
5551 for (uint32_t i = 0; i < event_count; i++) {
5552 const auto &dep_info = dep_infos[i];
5553 auto &barrier_set = barriers_[i];
5554 barrier_set.dependency_flags = dep_info.dependencyFlags;
5555 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5556 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5557 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5558 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5559 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5560 dep_info.pMemoryBarriers);
5561 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5562 dep_info.pBufferMemoryBarriers);
5563 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5564 dep_info.pImageMemoryBarriers);
5565 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005566}
5567
John Zulauf36ef9282021-02-02 11:47:24 -07005568SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005569 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5570 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5571 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5572 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5573 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005574 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005575 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5576
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005577SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5578 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005579 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005580
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005581bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5582 bool skip = false;
5583 const auto *context = cb_context.GetCurrentAccessContext();
5584 assert(context);
5585 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005586 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5587
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005588 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005589 const auto &barrier_set = barriers_[0];
5590 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5591 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5592 const auto *image_state = image_barrier.image.get();
5593 if (!image_state) continue;
5594 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5595 if (hazard.hazard) {
5596 // PHASE1 TODO -- add tag information to log msg when useful.
5597 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005598 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005599 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5600 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5601 string_SyncHazard(hazard.hazard), image_barrier.index,
5602 sync_state.report_data->FormatHandle(image_handle).c_str(),
5603 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005604 }
5605 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005606 return skip;
5607}
5608
John Zulaufd5115702021-01-18 12:34:33 -07005609struct SyncOpPipelineBarrierFunctorFactory {
5610 using BarrierOpFunctor = PipelineBarrierOp;
5611 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5612 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5613 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5614 using BufferRange = ResourceAccessRange;
5615 using ImageRange = subresource_adapter::ImageRangeGenerator;
5616 using GlobalRange = ResourceAccessRange;
5617
5618 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5619 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5620 }
John Zulauf14940722021-04-12 15:19:02 -06005621 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005622 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5623 }
5624 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5625 return GlobalBarrierOpFunctor(barrier, false);
5626 }
5627
5628 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5629 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5630 const auto base_address = ResourceBaseAddress(buffer);
5631 return (range + base_address);
5632 }
John Zulauf110413c2021-03-20 05:38:38 -06005633 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005634 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005635
5636 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005637 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005638 return range_gen;
5639 }
5640 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5641};
5642
5643template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005644void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005645 AccessContext *context) {
5646 for (const auto &barrier : barriers) {
5647 const auto *state = barrier.GetState();
5648 if (state) {
5649 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5650 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5651 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5652 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5653 }
5654 }
5655}
5656
5657template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005658void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005659 AccessContext *access_context) {
5660 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5661 for (const auto &barrier : barriers) {
5662 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5663 }
5664 for (const auto address_type : kAddressTypes) {
5665 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5666 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5667 }
5668}
5669
John Zulauf8eda1562021-04-13 17:06:41 -06005670ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005671 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005672 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005673 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf4fa68462021-04-26 21:04:22 -06005674 DoRecord(tag, access_context, events_context);
5675 return tag;
5676}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005677
John Zulauf4fa68462021-04-26 21:04:22 -06005678void SyncOpPipelineBarrier::DoRecord(const ResourceUsageTag tag, AccessContext *access_context,
5679 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005680 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005681 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5682 assert(barriers_.size() == 1);
5683 const auto &barrier_set = barriers_[0];
5684 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5685 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5686 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005687 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005688 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005689 } else {
5690 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005691 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005692 }
5693 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005694}
5695
John Zulauf8eda1562021-04-13 17:06:41 -06005696bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005697 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005698 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5699 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005700 return false;
5701}
5702
John Zulauf4edde622021-02-15 08:54:50 -07005703void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5704 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5705 const VkMemoryBarrier *barriers) {
5706 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005707 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005708 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005709 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005710 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005711 }
5712 if (0 == memory_barrier_count) {
5713 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005714 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005715 }
John Zulauf4edde622021-02-15 08:54:50 -07005716 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005717}
5718
John Zulauf4edde622021-02-15 08:54:50 -07005719void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5720 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5721 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5722 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005723 for (uint32_t index = 0; index < barrier_count; index++) {
5724 const auto &barrier = barriers[index];
Jeremy Gebben9f537102021-10-05 16:37:12 -06005725 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005726 if (buffer) {
5727 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5728 const auto range = MakeRange(barrier.offset, barrier_size);
5729 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005730 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005731 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005732 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005733 }
5734 }
5735}
5736
John Zulauf4edde622021-02-15 08:54:50 -07005737void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005738 uint32_t memory_barrier_count, const VkMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005739 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005740 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005741 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005742 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5743 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5744 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005745 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005746 }
John Zulauf4edde622021-02-15 08:54:50 -07005747 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005748}
5749
John Zulauf4edde622021-02-15 08:54:50 -07005750void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5751 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005752 const VkBufferMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005753 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005754 for (uint32_t index = 0; index < barrier_count; index++) {
5755 const auto &barrier = barriers[index];
5756 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5757 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9f537102021-10-05 16:37:12 -06005758 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005759 if (buffer) {
5760 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5761 const auto range = MakeRange(barrier.offset, barrier_size);
5762 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005763 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005764 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005765 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005766 }
5767 }
5768}
5769
John Zulauf4edde622021-02-15 08:54:50 -07005770void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5771 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5772 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5773 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005774 for (uint32_t index = 0; index < barrier_count; index++) {
5775 const auto &barrier = barriers[index];
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005776 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005777 if (image) {
5778 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5779 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005780 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005781 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005782 image_memory_barriers.emplace_back();
5783 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005784 }
5785 }
5786}
John Zulaufd5115702021-01-18 12:34:33 -07005787
John Zulauf4edde622021-02-15 08:54:50 -07005788void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5789 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005790 const VkImageMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005791 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005792 for (uint32_t index = 0; index < barrier_count; index++) {
5793 const auto &barrier = barriers[index];
5794 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5795 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005796 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005797 if (image) {
5798 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5799 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005800 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005801 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005802 image_memory_barriers.emplace_back();
5803 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005804 }
5805 }
5806}
5807
John Zulauf36ef9282021-02-02 11:47:24 -07005808SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005809 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5810 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5811 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5812 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005813 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005814 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5815 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005816 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005817}
5818
John Zulauf4edde622021-02-15 08:54:50 -07005819SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5820 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5821 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5822 MakeEventsList(sync_state, eventCount, pEvents);
5823 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5824}
5825
John Zulauf610e28c2021-08-03 17:46:23 -06005826const char *const SyncOpWaitEvents::kIgnored = "Wait operation is ignored for this event.";
5827
John Zulaufd5115702021-01-18 12:34:33 -07005828bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005829 bool skip = false;
5830 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005831 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005832
John Zulauf610e28c2021-08-03 17:46:23 -06005833 // This is only interesting at record and not replay (Execute/Submit) time.
John Zulauf4edde622021-02-15 08:54:50 -07005834 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5835 const auto &barrier_set = barriers_[barrier_set_index];
5836 if (barrier_set.single_exec_scope) {
5837 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5838 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5839 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5840 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5841 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5842 } else {
5843 const auto &barriers = barrier_set.memory_barriers;
5844 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5845 const auto &barrier = barriers[barrier_index];
5846 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5847 const std::string vuid =
5848 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5849 skip =
5850 sync_state.LogInfo(command_buffer_handle, vuid,
5851 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5852 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5853 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5854 }
5855 }
5856 }
5857 }
John Zulaufd5115702021-01-18 12:34:33 -07005858 }
5859
John Zulauf610e28c2021-08-03 17:46:23 -06005860 // The rest is common to record time and replay time.
5861 skip |= DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5862 return skip;
5863}
5864
5865bool SyncOpWaitEvents::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
5866 bool skip = false;
5867 const auto &sync_state = cb_context.GetSyncState();
5868
Jeremy Gebben40a22942020-12-22 14:22:06 -07005869 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005870 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005871 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005872 const auto *events_context = cb_context.GetCurrentEventsContext();
5873 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005874 size_t barrier_set_index = 0;
5875 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06005876 for (const auto &event : events_) {
5877 const auto *sync_event = events_context->Get(event.get());
5878 const auto &barrier_set = barriers_[barrier_set_index];
5879 if (!sync_event) {
5880 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5881 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5882 // new validation error... wait without previously submitted set event...
5883 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 -07005884 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06005885 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005886 }
John Zulauf610e28c2021-08-03 17:46:23 -06005887
5888 // For replay calls, don't revalidate "same command buffer" events
5889 if (sync_event->last_command_tag > base_tag) continue;
5890
John Zulauf78394fc2021-07-12 15:41:40 -06005891 const auto event_handle = sync_event->event->event();
5892 // TODO add "destroyed" checks
5893
John Zulauf78b1f892021-09-20 15:02:09 -06005894 if (sync_event->first_scope_set) {
5895 // Only accumulate barrier and event stages if there is a pending set in the current context
5896 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5897 event_stage_masks |= sync_event->scope.mask_param;
5898 }
5899
John Zulauf78394fc2021-07-12 15:41:40 -06005900 const auto &src_exec_scope = barrier_set.src_exec_scope;
John Zulauf78b1f892021-09-20 15:02:09 -06005901
John Zulauf78394fc2021-07-12 15:41:40 -06005902 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5903 if (ignore_reason) {
5904 switch (ignore_reason) {
5905 case SyncEventState::ResetWaitRace:
5906 case SyncEventState::Reset2WaitRace: {
5907 // Four permuations of Reset and Wait calls...
5908 const char *vuid =
5909 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5910 if (ignore_reason == SyncEventState::Reset2WaitRace) {
Tony-LunarG279601c2021-11-16 10:50:51 -07005911 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2-event-03831"
5912 : "VUID-vkCmdResetEvent2-event-03832";
John Zulauf78394fc2021-07-12 15:41:40 -06005913 }
5914 const char *const message =
5915 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5916 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5917 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
John Zulauf610e28c2021-08-03 17:46:23 -06005918 CommandTypeString(sync_event->last_command), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005919 break;
5920 }
5921 case SyncEventState::SetRace: {
5922 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5923 // this event
5924 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5925 const char *const message =
5926 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5927 const char *const reason = "First synchronization scope is undefined.";
5928 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5929 sync_state.report_data->FormatHandle(event_handle).c_str(),
John Zulauf610e28c2021-08-03 17:46:23 -06005930 CommandTypeString(sync_event->last_command), reason, kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005931 break;
5932 }
5933 case SyncEventState::MissingStageBits: {
5934 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5935 // Issue error message that event waited for is not in wait events scope
5936 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5937 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5938 ". Bits missing from srcStageMask %s. %s";
5939 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5940 sync_state.report_data->FormatHandle(event_handle).c_str(),
5941 sync_event->scope.mask_param, src_exec_scope.mask_param,
John Zulauf610e28c2021-08-03 17:46:23 -06005942 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005943 break;
5944 }
5945 case SyncEventState::SetVsWait2: {
Tony-LunarG279601c2021-11-16 10:50:51 -07005946 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2-pEvents-03837",
John Zulauf78394fc2021-07-12 15:41:40 -06005947 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5948 sync_state.report_data->FormatHandle(event_handle).c_str(),
5949 CommandTypeString(sync_event->last_command));
5950 break;
5951 }
5952 default:
5953 assert(ignore_reason == SyncEventState::NotIgnored);
5954 }
5955 } else if (barrier_set.image_memory_barriers.size()) {
5956 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5957 const auto *context = cb_context.GetCurrentAccessContext();
5958 assert(context);
5959 for (const auto &image_memory_barrier : image_memory_barriers) {
5960 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5961 const auto *image_state = image_memory_barrier.image.get();
5962 if (!image_state) continue;
5963 const auto &subresource_range = image_memory_barrier.range;
5964 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5965 const auto hazard =
5966 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5967 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5968 if (hazard.hazard) {
5969 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
5970 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5971 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5972 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
5973 cb_context.FormatUsage(hazard).c_str());
5974 break;
5975 }
5976 }
5977 }
5978 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5979 // 03839
5980 barrier_set_index += barrier_set_incr;
5981 }
John Zulaufd5115702021-01-18 12:34:33 -07005982
5983 // 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 -07005984 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 -07005985 if (extra_stage_bits) {
5986 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005987 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5988 const char *const vuid =
Tony-LunarG279601c2021-11-16 10:50:51 -07005989 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005990 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005991 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulauf610e28c2021-08-03 17:46:23 -06005992 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005993 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005994 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005995 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005996 " vkCmdSetEvent may be in previously submitted command buffer.");
5997 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005998 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005999 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07006000 }
6001 }
6002 return skip;
6003}
6004
6005struct SyncOpWaitEventsFunctorFactory {
6006 using BarrierOpFunctor = WaitEventBarrierOp;
6007 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
6008 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
6009 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
6010 using BufferRange = EventSimpleRangeGenerator;
6011 using ImageRange = EventImageRangeGenerator;
6012 using GlobalRange = EventSimpleRangeGenerator;
6013
6014 // Need to restrict to only valid exec and access scope for this event
6015 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
6016 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07006017 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07006018 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
6019 return barrier;
6020 }
6021 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
6022 auto barrier = RestrictToEvent(barrier_arg);
6023 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
6024 }
John Zulauf14940722021-04-12 15:19:02 -06006025 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07006026 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
6027 }
6028 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
6029 auto barrier = RestrictToEvent(barrier_arg);
6030 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
6031 }
6032
6033 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
6034 const AccessAddressType address_type = GetAccessAddressType(buffer);
6035 const auto base_address = ResourceBaseAddress(buffer);
6036 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
6037 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
6038 return filtered_range_gen;
6039 }
John Zulauf110413c2021-03-20 05:38:38 -06006040 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07006041 if (!SimpleBinding(image)) return ImageRange();
6042 const auto address_type = GetAccessAddressType(image);
6043 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06006044 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07006045 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
6046
6047 return filtered_range_gen;
6048 }
6049 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
6050 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
6051 }
6052 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
6053 SyncEventState *sync_event;
6054};
6055
John Zulauf8eda1562021-04-13 17:06:41 -06006056ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006057 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07006058 auto *access_context = cb_context->GetCurrentAccessContext();
6059 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006060 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07006061 auto *events_context = cb_context->GetCurrentEventsContext();
6062 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006063 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07006064
John Zulauf610e28c2021-08-03 17:46:23 -06006065 DoRecord(tag, access_context, events_context);
6066 return tag;
6067}
6068
6069void SyncOpWaitEvents::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07006070 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
6071 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
6072 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
6073 access_context->ResolvePreviousAccesses();
6074
John Zulauf4edde622021-02-15 08:54:50 -07006075 size_t barrier_set_index = 0;
6076 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
6077 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07006078 for (auto &event_shared : events_) {
6079 if (!event_shared.get()) continue;
6080 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07006081
John Zulauf4edde622021-02-15 08:54:50 -07006082 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006083 sync_event->last_command_tag = tag;
John Zulaufd5115702021-01-18 12:34:33 -07006084
John Zulauf4edde622021-02-15 08:54:50 -07006085 const auto &barrier_set = barriers_[barrier_set_index];
6086 const auto &dst = barrier_set.dst_exec_scope;
6087 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07006088 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
6089 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
6090 // of the barriers is maintained.
6091 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07006092 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
6093 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
6094 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07006095
6096 // Apply the global barrier to the event itself (for race condition tracking)
6097 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
6098 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6099 sync_event->barriers |= dst.exec_scope;
6100 } else {
6101 // We ignored this wait, so we don't have any effective synchronization barriers for it.
6102 sync_event->barriers = 0U;
6103 }
John Zulauf4edde622021-02-15 08:54:50 -07006104 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07006105 }
6106
6107 // Apply the pending barriers
6108 ResolvePendingBarrierFunctor apply_pending_action(tag);
6109 access_context->ApplyToContext(apply_pending_action);
6110}
6111
John Zulauf8eda1562021-04-13 17:06:41 -06006112bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006113 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6114 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006115}
6116
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006117bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6118 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
6119 bool skip = false;
6120 const auto *cb_access_context = GetAccessContext(commandBuffer);
6121 assert(cb_access_context);
6122 if (!cb_access_context) return skip;
6123
6124 const auto *context = cb_access_context->GetCurrentAccessContext();
6125 assert(context);
6126 if (!context) return skip;
6127
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006128 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006129
6130 if (dst_buffer) {
6131 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6132 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
6133 if (hazard.hazard) {
6134 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
6135 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
6136 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06006137 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006138 }
6139 }
6140 return skip;
6141}
6142
John Zulauf669dfd52021-01-27 17:15:28 -07006143void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07006144 events_.reserve(event_count);
6145 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006146 events_.emplace_back(sync_state.Get<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07006147 }
6148}
John Zulauf6ce24372021-01-30 05:56:25 -07006149
John Zulauf36ef9282021-02-02 11:47:24 -07006150SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006151 VkPipelineStageFlags2KHR stageMask)
Jeremy Gebben9f537102021-10-05 16:37:12 -06006152 : SyncOpBase(cmd), event_(sync_state.Get<EVENT_STATE>(event)), exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006153
John Zulauf1bf30522021-09-03 15:39:06 -06006154bool SyncOpResetEvent::Validate(const CommandBufferAccessContext& cb_context) const {
6155 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6156}
6157
6158bool SyncOpResetEvent::DoValidate(const CommandBufferAccessContext & cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006159 auto *events_context = cb_context.GetCurrentEventsContext();
6160 assert(events_context);
6161 bool skip = false;
6162 if (!events_context) return skip;
6163
6164 const auto &sync_state = cb_context.GetSyncState();
6165 const auto *sync_event = events_context->Get(event_);
6166 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6167
John Zulauf1bf30522021-09-03 15:39:06 -06006168 if (sync_event->last_command_tag > base_tag) return skip; // if we validated this in recording of the secondary, don't repeat
6169
John Zulauf6ce24372021-01-30 05:56:25 -07006170 const char *const set_wait =
6171 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6172 "hazards.";
6173 const char *message = set_wait; // Only one message this call.
6174 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
6175 const char *vuid = nullptr;
6176 switch (sync_event->last_command) {
6177 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006178 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006179 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006180 // Needs a barrier between set and reset
6181 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
6182 break;
John Zulauf4edde622021-02-15 08:54:50 -07006183 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006184 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006185 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07006186 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
6187 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
6188 break;
6189 }
6190 default:
6191 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07006192 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
6193 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07006194 break;
6195 }
6196 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006197 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
6198 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006199 CommandTypeString(sync_event->last_command));
6200 }
6201 }
6202 return skip;
6203}
6204
John Zulauf8eda1562021-04-13 17:06:41 -06006205ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
6206 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006207 auto *events_context = cb_context->GetCurrentEventsContext();
6208 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006209 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006210
6211 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06006212 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006213
6214 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07006215 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006216 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006217 sync_event->unsynchronized_set = CMD_NONE;
6218 sync_event->ResetFirstScope();
6219 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06006220
6221 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006222}
6223
John Zulauf8eda1562021-04-13 17:06:41 -06006224bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006225 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf1bf30522021-09-03 15:39:06 -06006226 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006227}
6228
John Zulauf4fa68462021-04-26 21:04:22 -06006229void SyncOpResetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006230
John Zulauf36ef9282021-02-02 11:47:24 -07006231SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006232 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07006233 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006234 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006235 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
6236 dep_info_() {}
6237
6238SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
6239 const VkDependencyInfoKHR &dep_info)
6240 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006241 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006242 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
Tony-LunarG273f32f2021-09-28 08:56:30 -06006243 dep_info_(new safe_VkDependencyInfo(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006244
6245bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf610e28c2021-08-03 17:46:23 -06006246 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6247}
6248bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6249 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6250 assert(active_context);
6251 return DoValidate(*active_context, base_tag);
6252}
6253
6254bool SyncOpSetEvent::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006255 bool skip = false;
6256
6257 const auto &sync_state = cb_context.GetSyncState();
6258 auto *events_context = cb_context.GetCurrentEventsContext();
6259 assert(events_context);
6260 if (!events_context) return skip;
6261
6262 const auto *sync_event = events_context->Get(event_);
6263 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6264
John Zulauf610e28c2021-08-03 17:46:23 -06006265 if (sync_event->last_command_tag >= base_tag) return skip; // for replay we don't want to revalidate internal "last commmand"
6266
John Zulauf6ce24372021-01-30 05:56:25 -07006267 const char *const reset_set =
6268 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6269 "hazards.";
6270 const char *const wait =
6271 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6272
6273 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006274 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006275 const char *message = nullptr;
6276 switch (sync_event->last_command) {
6277 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006278 case CMD_RESETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006279 case CMD_RESETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006280 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006281 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006282 message = reset_set;
6283 break;
6284 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006285 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006286 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006287 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006288 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006289 message = reset_set;
6290 break;
6291 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006292 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006293 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006294 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006295 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006296 message = wait;
6297 break;
6298 default:
6299 // The only other valid last command that wasn't one.
6300 assert(sync_event->last_command == CMD_NONE);
6301 break;
6302 }
John Zulauf4edde622021-02-15 08:54:50 -07006303 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006304 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006305 std::string vuid("SYNC-");
6306 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006307 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6308 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006309 CommandTypeString(sync_event->last_command));
6310 }
6311 }
6312
6313 return skip;
6314}
6315
John Zulauf8eda1562021-04-13 17:06:41 -06006316ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006317 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006318 auto *events_context = cb_context->GetCurrentEventsContext();
6319 auto *access_context = cb_context->GetCurrentAccessContext();
6320 assert(events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006321 if (access_context && events_context) {
6322 DoRecord(tag, access_context, events_context);
6323 }
6324 return tag;
6325}
John Zulauf6ce24372021-01-30 05:56:25 -07006326
John Zulauf610e28c2021-08-03 17:46:23 -06006327void SyncOpSetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006328 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf610e28c2021-08-03 17:46:23 -06006329 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006330
6331 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6332 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6333 // any issues caused by naive scope setting here.
6334
6335 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6336 // Given:
6337 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6338 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6339
6340 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6341 sync_event->unsynchronized_set = sync_event->last_command;
6342 sync_event->ResetFirstScope();
John Zulauf78b1f892021-09-20 15:02:09 -06006343 } else if (!sync_event->first_scope_set) {
John Zulauf6ce24372021-01-30 05:56:25 -07006344 // We only set the scope if there isn't one
6345 sync_event->scope = src_exec_scope_;
6346
6347 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6348 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6349 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6350 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6351 }
6352 };
6353 access_context->ForAll(set_scope);
6354 sync_event->unsynchronized_set = CMD_NONE;
John Zulauf78b1f892021-09-20 15:02:09 -06006355 sync_event->first_scope_set = true;
John Zulauf6ce24372021-01-30 05:56:25 -07006356 sync_event->first_scope_tag = tag;
6357 }
John Zulauf4edde622021-02-15 08:54:50 -07006358 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6359 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006360 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006361 sync_event->barriers = 0U;
6362}
John Zulauf64ffe552021-02-06 10:25:07 -07006363
6364SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6365 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006366 const VkSubpassBeginInfo *pSubpassBeginInfo)
6367 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006368 if (pRenderPassBegin) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006369 rp_state_ = sync_state.Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
John Zulauf64ffe552021-02-06 10:25:07 -07006370 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006371 auto fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07006372 if (fb_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006373 shared_attachments_ = sync_state.GetAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
John Zulauf64ffe552021-02-06 10:25:07 -07006374 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6375 // Note that this a safe to presist as long as shared_attachments is not cleared
6376 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006377 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006378 attachments_.emplace_back(attachment.get());
6379 }
6380 }
6381 if (pSubpassBeginInfo) {
6382 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6383 }
6384 }
6385}
6386
6387bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6388 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6389 bool skip = false;
6390
6391 assert(rp_state_.get());
6392 if (nullptr == rp_state_.get()) return skip;
6393 auto &rp_state = *rp_state_.get();
6394
6395 const uint32_t subpass = 0;
6396
6397 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6398 // hasn't happened yet)
6399 const std::vector<AccessContext> empty_context_vector;
6400 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6401 cb_context.GetCurrentAccessContext());
6402
6403 // Validate attachment operations
6404 if (attachments_.size() == 0) return skip;
6405 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006406
6407 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6408 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6409 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6410 // operations (though it's currently a messy approach)
6411 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6412 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006413
6414 // Validate load operations if there were no layout transition hazards
6415 if (!skip) {
John Zulaufee984022022-04-13 16:39:50 -06006416 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kInvalidTag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07006417 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006418 }
6419
6420 return skip;
6421}
6422
John Zulauf8eda1562021-04-13 17:06:41 -06006423ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006424 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6425 assert(rp_state_.get());
John Zulauf41a9c7c2021-12-07 15:59:53 -07006426 if (nullptr == rp_state_.get()) return cb_context->NextCommandTag(cmd_);
6427 return cb_context->RecordBeginRenderPass(cmd_, *rp_state_.get(), renderpass_begin_info_.renderArea, attachments_);
John Zulauf64ffe552021-02-06 10:25:07 -07006428}
6429
John Zulauf8eda1562021-04-13 17:06:41 -06006430bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006431 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006432 return false;
6433}
6434
John Zulauf4fa68462021-04-26 21:04:22 -06006435void SyncOpBeginRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6436}
John Zulauf8eda1562021-04-13 17:06:41 -06006437
John Zulauf64ffe552021-02-06 10:25:07 -07006438SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006439 const VkSubpassEndInfo *pSubpassEndInfo)
6440 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006441 if (pSubpassBeginInfo) {
6442 subpass_begin_info_.initialize(pSubpassBeginInfo);
6443 }
6444 if (pSubpassEndInfo) {
6445 subpass_end_info_.initialize(pSubpassEndInfo);
6446 }
6447}
6448
6449bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6450 bool skip = false;
6451 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6452 if (!renderpass_context) return skip;
6453
6454 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6455 return skip;
6456}
6457
John Zulauf8eda1562021-04-13 17:06:41 -06006458ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006459 return cb_context->RecordNextSubpass(cmd_);
John Zulauf8eda1562021-04-13 17:06:41 -06006460}
6461
6462bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006463 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006464 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006465}
6466
sfricke-samsung85584a72021-09-30 21:43:38 -07006467SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6468 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006469 if (pSubpassEndInfo) {
6470 subpass_end_info_.initialize(pSubpassEndInfo);
6471 }
6472}
6473
John Zulauf4fa68462021-04-26 21:04:22 -06006474void SyncOpNextSubpass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006475
John Zulauf64ffe552021-02-06 10:25:07 -07006476bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6477 bool skip = false;
6478 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6479
6480 if (!renderpass_context) return skip;
6481 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6482 return skip;
6483}
6484
John Zulauf8eda1562021-04-13 17:06:41 -06006485ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006486 return cb_context->RecordEndRenderPass(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006487}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006488
John Zulauf8eda1562021-04-13 17:06:41 -06006489bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006490 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006491 return false;
6492}
6493
John Zulauf4fa68462021-04-26 21:04:22 -06006494void SyncOpEndRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006495
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006496void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6497 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6498 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6499 auto *cb_access_context = GetAccessContext(commandBuffer);
6500 assert(cb_access_context);
6501 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6502 auto *context = cb_access_context->GetCurrentAccessContext();
6503 assert(context);
6504
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006505 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006506
6507 if (dst_buffer) {
6508 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6509 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6510 }
6511}
John Zulaufd05c5842021-03-26 11:32:16 -06006512
John Zulaufae842002021-04-15 18:20:55 -06006513bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6514 const VkCommandBuffer *pCommandBuffers) const {
6515 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6516 const char *func_name = "vkCmdExecuteCommands";
6517 const auto *cb_context = GetAccessContext(commandBuffer);
6518 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006519
6520 // Heavyweight, but we need a proxy copy of the active command buffer access context
6521 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006522
6523 // Make working copies of the access and events contexts
John Zulaufae842002021-04-15 18:20:55 -06006524 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006525 proxy_cb_context.NextIndexedCommandTag(CMD_EXECUTECOMMANDS, cb_index);
6526
John Zulaufae842002021-04-15 18:20:55 -06006527 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6528 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006529
6530 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6531 assert(recorded_context);
6532 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6533
6534 // The barriers have already been applied in ValidatFirstUse
6535 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6536 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006537 }
6538
John Zulaufae842002021-04-15 18:20:55 -06006539 return skip;
6540}
6541
6542void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6543 const VkCommandBuffer *pCommandBuffers) {
6544 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006545 auto *cb_context = GetAccessContext(commandBuffer);
6546 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006547 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006548 cb_context->NextIndexedCommandTag(CMD_EXECUTECOMMANDS, cb_index);
John Zulauf4fa68462021-04-26 21:04:22 -06006549 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6550 if (!recorded_cb_context) continue;
6551 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6552 }
John Zulaufae842002021-04-15 18:20:55 -06006553}
6554
John Zulaufd0ec59f2021-03-13 14:25:08 -07006555AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6556 : view_(view), view_mask_(), gen_store_() {
6557 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6558 const IMAGE_STATE &image_state = *view_->image_state.get();
6559 const auto base_address = ResourceBaseAddress(image_state);
6560 const auto *encoder = image_state.fragment_encoder.get();
6561 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006562 // Get offset and extent for the view, accounting for possible depth slicing
6563 const VkOffset3D zero_offset = view->GetOffset();
6564 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006565 // Intentional copy
6566 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6567 view_mask_ = subres_range.aspectMask;
6568 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6569 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6570
6571 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6572 if (depth && (depth != view_mask_)) {
6573 subres_range.aspectMask = depth;
6574 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6575 }
6576 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6577 if (stencil && (stencil != view_mask_)) {
6578 subres_range.aspectMask = stencil;
6579 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6580 }
6581}
6582
6583const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6584 const ImageRangeGen *got = nullptr;
6585 switch (gen_type) {
6586 case kViewSubresource:
6587 got = &gen_store_[kViewSubresource];
6588 break;
6589 case kRenderArea:
6590 got = &gen_store_[kRenderArea];
6591 break;
6592 case kDepthOnlyRenderArea:
6593 got =
6594 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6595 break;
6596 case kStencilOnlyRenderArea:
6597 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6598 : &gen_store_[Gen::kStencilOnlyRenderArea];
6599 break;
6600 default:
6601 assert(got);
6602 }
6603 return got;
6604}
6605
6606AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6607 assert(IsValid());
6608 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6609 if (depth_op) {
6610 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6611 if (stencil_op) {
6612 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6613 return kRenderArea;
6614 }
6615 return kDepthOnlyRenderArea;
6616 }
6617 if (stencil_op) {
6618 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6619 return kStencilOnlyRenderArea;
6620 }
6621
6622 assert(depth_op || stencil_op);
6623 return kRenderArea;
6624}
6625
6626AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006627
6628void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6629 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6630 for (auto &event_pair : map_) {
6631 assert(event_pair.second); // Shouldn't be storing empty
6632 auto &sync_event = *event_pair.second;
6633 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6634 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6635 sync_event.barriers |= dst.exec_scope;
6636 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6637 }
6638 }
6639}