blob: 94dc8b16b208265285c51f0315e3a3def1ddec01 [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 Zulauf7635de32020-05-29 17:14:15 -0600295// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulauf14940722021-04-12 15:19:02 -0600296static const ResourceUsageTag kCurrentCommandTag(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);
874 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700875 if (previous_barrier) {
876 assert(bool(*previous_barrier));
877 (*previous_barrier)(access);
878 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600879 }
880 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700881 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600882};
883
884// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
885// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
886// *different* map from dest.
887// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
888// range [first, last)
889template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600890static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
891 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600892 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600893 auto at = entry;
894 for (auto pos = first; pos != last; ++pos) {
895 // Every member of the input iterator range must fit within the remaining portion of entry
896 assert(at->first.includes(pos->first));
897 assert(at != dest->end());
898 // Trim up at to the same size as the entry to resolve
899 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600900 auto access = pos->second; // intentional copy
901 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600902 at->second.Resolve(access);
903 ++at; // Go to the remaining unused section of entry
904 }
905}
906
John Zulaufa0a98292020-09-18 09:30:10 -0600907static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
908 SyncBarrier merged = {};
909 for (const auto &barrier : barriers) {
910 merged.Merge(barrier);
911 }
912 return merged;
913}
914
John Zulaufb02c1eb2020-10-06 16:33:36 -0600915template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700916void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600917 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
918 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600919 if (!range.non_empty()) return;
920
John Zulauf355e49b2020-04-24 15:11:15 -0600921 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
922 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600923 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600924 if (current->pos_B->valid) {
925 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600926 auto access = src_pos->second; // intentional copy
927 barrier_action(&access);
928
John Zulauf16adfc92020-04-08 10:28:33 -0600929 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600930 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
931 trimmed->second.Resolve(access);
932 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600933 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600934 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600935 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600936 }
John Zulauf16adfc92020-04-08 10:28:33 -0600937 } else {
938 // we have to descend to fill this gap
939 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700940 ResourceAccessRange recurrence_range = current_range;
941 // The current context is empty for the current range, so recur to fill the gap.
942 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
943 // is not valid, to minimize that recurrence
944 if (current->pos_B.at_end()) {
945 // Do the remainder here....
946 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600947 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700948 // Recur only over the range until B becomes valid (within the limits of range).
949 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600950 }
John Zulauf22aefed2021-03-11 18:14:35 -0700951 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
952
John Zulauf355e49b2020-04-24 15:11:15 -0600953 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
954 // iterator of the outer while.
955
956 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
957 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
958 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700959 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 -0600960 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600961 current.seek(seek_to);
962 } else if (!current->pos_A->valid && infill_state) {
963 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
964 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
965 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600966 }
John Zulauf5f13a792020-03-10 07:31:21 -0600967 }
ziga-lunargf0e27ad2022-03-28 00:44:12 +0200968 if (current->range.non_empty()) {
969 ++current;
970 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600971 }
John Zulauf1a224292020-06-30 14:52:13 -0600972
973 // Infill if range goes passed both the current and resolve map prior contents
974 if (recur_to_infill && (current->range.end < range.end)) {
975 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700976 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600977 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600978}
979
John Zulauf22aefed2021-03-11 18:14:35 -0700980template <typename BarrierAction>
981void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
982 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
983 const BarrierAction &previous_barrier) const {
984 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
985 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
986}
987
John Zulauf43cc7462020-12-03 12:33:12 -0700988void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700989 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
990 const ResourceAccessStateFunction *previous_barrier) const {
991 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600992 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700993 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
994 ResourceAccessState state_copy;
995 if (previous_barrier) {
996 assert(bool(*previous_barrier));
997 state_copy = *infill_state;
998 (*previous_barrier)(&state_copy);
999 infill_state = &state_copy;
1000 }
1001 sparse_container::update_range_value(*descent_map, range, *infill_state,
1002 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -06001003 }
1004 } else {
1005 // Look for something to fill the gap further along.
1006 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -07001007 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001008 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001009 }
John Zulauf5f13a792020-03-10 07:31:21 -06001010 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001011}
1012
John Zulauf4a6105a2020-11-17 15:11:05 -07001013// Non-lazy import of all accesses, WaitEvents needs this.
1014void AccessContext::ResolvePreviousAccesses() {
1015 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -07001016 if (!prev_.size()) return; // If no previous contexts, nothing to do
1017
John Zulauf4a6105a2020-11-17 15:11:05 -07001018 for (const auto address_type : kAddressTypes) {
1019 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
1020 }
1021}
1022
John Zulauf43cc7462020-12-03 12:33:12 -07001023AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
1024 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -06001025}
1026
John Zulauf1507ee42020-05-18 11:33:09 -06001027static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001028 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1029 ? SYNC_ACCESS_INDEX_NONE
1030 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
1031 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001032 return stage_access;
1033}
1034static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001035 const auto stage_access =
1036 (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1037 ? SYNC_ACCESS_INDEX_NONE
1038 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
1039 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001040 return stage_access;
1041}
1042
John Zulauf7635de32020-05-29 17:14:15 -06001043// Caller must manage returned pointer
1044static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001045 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -06001046 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001047 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
1048 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -06001049 return proxy;
1050}
1051
John Zulaufb02c1eb2020-10-06 16:33:36 -06001052template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001053void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1054 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
1055 const ResourceAccessState *infill_state) const {
1056 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1057 if (!attachment_gen) return;
1058
1059 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1060 const AccessAddressType address_type = view_gen.GetAddressType();
1061 for (; range_gen->non_empty(); ++range_gen) {
1062 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001063 }
John Zulauf62f10592020-04-03 12:20:02 -06001064}
1065
John Zulauf7635de32020-05-29 17:14:15 -06001066// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -07001067bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001068 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001069 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001070 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001071 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
1072 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
1073 // those affects have not been recorded yet.
1074 //
1075 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1076 // to apply and only copy then, if this proves a hot spot.
1077 std::unique_ptr<AccessContext> proxy_for_prev;
1078 TrackBack proxy_track_back;
1079
John Zulauf355e49b2020-04-24 15:11:15 -06001080 const auto &transitions = rp_state.subpass_transitions[subpass];
1081 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001082 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1083
1084 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001085 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001086 if (prev_needs_proxy) {
1087 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001088 proxy_for_prev.reset(
1089 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -06001090 proxy_track_back = *track_back;
1091 proxy_track_back.context = proxy_for_prev.get();
1092 }
1093 track_back = &proxy_track_back;
1094 }
1095 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001096 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001097 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001098 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1099 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1100 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1101 string_VkImageLayout(transition.old_layout),
1102 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001103 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001104 }
1105 }
1106 return skip;
1107}
1108
John Zulauf64ffe552021-02-06 10:25:07 -07001109bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001110 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001111 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001112 bool skip = false;
1113 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001114
John Zulauf1507ee42020-05-18 11:33:09 -06001115 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1116 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001117 const auto &view_gen = attachment_views[i];
1118 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001119 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001120
1121 // Need check in the following way
1122 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1123 // vs. transition
1124 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1125 // for each aspect loaded.
1126
1127 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001128 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001129 const bool is_color = !(has_depth || has_stencil);
1130
1131 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001132 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001133
John Zulaufaff20662020-06-01 14:07:58 -06001134 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001135 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001136
John Zulaufb02c1eb2020-10-06 16:33:36 -06001137 bool checked_stencil = false;
John Zulauf57261402021-08-13 11:32:06 -06001138 if (is_color && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001139 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001140 aspect = "color";
1141 } else {
John Zulauf57261402021-08-13 11:32:06 -06001142 if (has_depth && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001143 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1144 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001145 aspect = "depth";
1146 }
John Zulauf57261402021-08-13 11:32:06 -06001147 if (!hazard.hazard && has_stencil && (stencil_load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001148 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1149 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001150 aspect = "stencil";
1151 checked_stencil = true;
1152 }
1153 }
1154
1155 if (hazard.hazard) {
1156 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001157 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001158 if (hazard.tag == kCurrentCommandTag) {
1159 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001160 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001161 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1162 " aspect %s during load with loadOp %s.",
1163 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1164 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001165 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001166 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001167 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001168 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001169 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001170 }
1171 }
1172 }
1173 }
1174 return skip;
1175}
1176
John Zulaufaff20662020-06-01 14:07:58 -06001177// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1178// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1179// store is part of the same Next/End operation.
1180// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001181bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001182 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001183 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001184 bool skip = false;
1185 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001186
1187 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1188 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001189 const AttachmentViewGen &view_gen = attachment_views[i];
1190 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001191 const auto &ci = attachment_ci[i];
1192
1193 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1194 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1195 // sake, we treat DONT_CARE as writing.
1196 const bool has_depth = FormatHasDepth(ci.format);
1197 const bool has_stencil = FormatHasStencil(ci.format);
1198 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001199 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001200 if (!has_stencil && !store_op_stores) continue;
1201
1202 HazardResult hazard;
1203 const char *aspect = nullptr;
1204 bool checked_stencil = false;
1205 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001206 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1207 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001208 aspect = "color";
1209 } else {
John Zulauf57261402021-08-13 11:32:06 -06001210 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001211 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001212 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1213 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001214 aspect = "depth";
1215 }
1216 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001217 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1218 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001219 aspect = "stencil";
1220 checked_stencil = true;
1221 }
1222 }
1223
1224 if (hazard.hazard) {
1225 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1226 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001227 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001228 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1229 " %s aspect during store with %s %s. Access info %s",
1230 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001231 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001232 }
1233 }
1234 }
1235 return skip;
1236}
1237
John Zulauf64ffe552021-02-06 10:25:07 -07001238bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001239 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1240 const char *func_name, uint32_t subpass) const {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001241 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001242 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001243 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001244}
1245
John Zulauf3d84f1b2020-03-09 13:33:25 -06001246class HazardDetector {
1247 SyncStageAccessIndex usage_index_;
1248
1249 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001250 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf14940722021-04-12 15:19:02 -06001251 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001252 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001253 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001254 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001255};
1256
John Zulauf69133422020-05-20 14:55:53 -06001257class HazardDetectorWithOrdering {
1258 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001259 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001260
1261 public:
1262 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001263 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001264 }
John Zulauf14940722021-04-12 15:19:02 -06001265 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001266 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001267 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001268 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001269};
1270
John Zulauf16adfc92020-04-08 10:28:33 -06001271HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001272 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001273 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001274 const auto base_address = ResourceBaseAddress(buffer);
1275 HazardDetector detector(usage_index);
1276 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001277}
1278
John Zulauf69133422020-05-20 14:55:53 -06001279template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001280HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1281 DetectOptions options) const {
1282 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1283 if (!attachment_gen) return HazardResult();
1284
1285 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1286 const auto address_type = view_gen.GetAddressType();
1287 for (; range_gen->non_empty(); ++range_gen) {
1288 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1289 if (hazard.hazard) return hazard;
1290 }
1291
1292 return HazardResult();
1293}
1294
1295template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001296HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1297 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1298 const VkExtent3D &extent, DetectOptions options) const {
1299 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001300 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001301 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1302 base_address);
1303 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001304 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001305 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001306 if (hazard.hazard) return hazard;
1307 }
1308 return HazardResult();
1309}
John Zulauf110413c2021-03-20 05:38:38 -06001310template <typename Detector>
1311HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1312 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1313 if (!SimpleBinding(image)) return HazardResult();
1314 const auto base_address = ResourceBaseAddress(image);
1315 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1316 const auto address_type = ImageAddressType(image);
1317 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001318 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1319 if (hazard.hazard) return hazard;
1320 }
1321 return HazardResult();
1322}
John Zulauf69133422020-05-20 14:55:53 -06001323
John Zulauf540266b2020-04-06 18:54:53 -06001324HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1325 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1326 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001327 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1328 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001329 HazardDetector detector(current_usage);
1330 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001331}
1332
1333HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001334 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001335 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001336 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001337}
1338
John Zulaufd0ec59f2021-03-13 14:25:08 -07001339HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1340 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1341 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1342 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1343}
1344
John Zulauf69133422020-05-20 14:55:53 -06001345HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001346 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001347 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001348 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001349 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001350}
1351
John Zulauf3d84f1b2020-03-09 13:33:25 -06001352class BarrierHazardDetector {
1353 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001354 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001355 SyncStageAccessFlags src_access_scope)
1356 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1357
John Zulauf5f13a792020-03-10 07:31:21 -06001358 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1359 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001360 }
John Zulauf14940722021-04-12 15:19:02 -06001361 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001362 // 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 -07001363 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001364 }
1365
1366 private:
1367 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001368 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001369 SyncStageAccessFlags src_access_scope_;
1370};
1371
John Zulauf4a6105a2020-11-17 15:11:05 -07001372class EventBarrierHazardDetector {
1373 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001374 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001375 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
John Zulauf14940722021-04-12 15:19:02 -06001376 ResourceUsageTag scope_tag)
John Zulauf4a6105a2020-11-17 15:11:05 -07001377 : usage_index_(usage_index),
1378 src_exec_scope_(src_exec_scope),
1379 src_access_scope_(src_access_scope),
1380 event_scope_(event_scope),
1381 scope_pos_(event_scope.cbegin()),
1382 scope_end_(event_scope.cend()),
1383 scope_tag_(scope_tag) {}
1384
1385 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1386 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1387 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1388 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1389 if (scope_pos_ == scope_end_) return HazardResult();
1390 if (!scope_pos_->first.intersects(pos->first)) {
1391 event_scope_.lower_bound(pos->first);
1392 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1393 }
1394
1395 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1396 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1397 }
John Zulauf14940722021-04-12 15:19:02 -06001398 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07001399 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1400 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1401 }
1402
1403 private:
1404 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001405 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001406 SyncStageAccessFlags src_access_scope_;
1407 const SyncEventState::ScopeMap &event_scope_;
1408 SyncEventState::ScopeMap::const_iterator scope_pos_;
1409 SyncEventState::ScopeMap::const_iterator scope_end_;
John Zulauf14940722021-04-12 15:19:02 -06001410 const ResourceUsageTag scope_tag_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001411};
1412
Jeremy Gebben40a22942020-12-22 14:22:06 -07001413HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001414 const SyncStageAccessFlags &src_access_scope,
1415 const VkImageSubresourceRange &subresource_range,
1416 const SyncEventState &sync_event, DetectOptions options) const {
1417 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1418 // first access scope map to use, and there's no easy way to plumb it in below.
1419 const auto address_type = ImageAddressType(image);
1420 const auto &event_scope = sync_event.FirstScope(address_type);
1421
1422 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1423 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001424 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001425}
1426
John Zulaufd0ec59f2021-03-13 14:25:08 -07001427HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1428 DetectOptions options) const {
1429 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1430 barrier.src_access_scope);
1431 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1432}
1433
Jeremy Gebben40a22942020-12-22 14:22:06 -07001434HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001435 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001436 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001437 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001438 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001439 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001440}
1441
Jeremy Gebben40a22942020-12-22 14:22:06 -07001442HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001443 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001444 const VkImageMemoryBarrier &barrier) const {
1445 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1446 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1447 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1448}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001449HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001450 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001451 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001452}
John Zulauf355e49b2020-04-24 15:11:15 -06001453
John Zulauf9cb530d2019-09-30 14:14:10 -06001454template <typename Flags, typename Map>
1455SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1456 SyncStageAccessFlags scope = 0;
1457 for (const auto &bit_scope : map) {
1458 if (flag_mask < bit_scope.first) break;
1459
1460 if (flag_mask & bit_scope.first) {
1461 scope |= bit_scope.second;
1462 }
1463 }
1464 return scope;
1465}
1466
Jeremy Gebben40a22942020-12-22 14:22:06 -07001467SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001468 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1469}
1470
Jeremy Gebben40a22942020-12-22 14:22:06 -07001471SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1472 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001473}
1474
Jeremy Gebben40a22942020-12-22 14:22:06 -07001475// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1476SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001477 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1478 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1479 // 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 -06001480 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1481}
1482
1483template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001484void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001485 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1486 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001487 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001488 auto pos = accesses->lower_bound(range);
1489 if (pos == accesses->end() || !pos->first.intersects(range)) {
1490 // The range is empty, fill it with a default value.
1491 pos = action.Infill(accesses, pos, range);
1492 } else if (range.begin < pos->first.begin) {
1493 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001494 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001495 } else if (pos->first.begin < range.begin) {
1496 // Trim the beginning if needed
1497 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1498 ++pos;
1499 }
1500
1501 const auto the_end = accesses->end();
1502 while ((pos != the_end) && pos->first.intersects(range)) {
1503 if (pos->first.end > range.end) {
1504 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1505 }
1506
1507 pos = action(accesses, pos);
1508 if (pos == the_end) break;
1509
1510 auto next = pos;
1511 ++next;
1512 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1513 // Need to infill if next is disjoint
1514 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001515 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001516 next = action.Infill(accesses, next, new_range);
1517 }
1518 pos = next;
1519 }
1520}
John Zulaufd5115702021-01-18 12:34:33 -07001521
1522// Give a comparable interface for range generators and ranges
1523template <typename Action>
1524inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1525 assert(range);
1526 UpdateMemoryAccessState(accesses, *range, action);
1527}
1528
John Zulauf4a6105a2020-11-17 15:11:05 -07001529template <typename Action, typename RangeGen>
1530void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1531 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001532 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 -07001533 for (; range_gen->non_empty(); ++range_gen) {
1534 UpdateMemoryAccessState(accesses, *range_gen, action);
1535 }
1536}
John Zulauf9cb530d2019-09-30 14:14:10 -06001537
John Zulaufd0ec59f2021-03-13 14:25:08 -07001538template <typename Action, typename RangeGen>
1539void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1540 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1541 for (; range_gen->non_empty(); ++range_gen) {
1542 UpdateMemoryAccessState(accesses, *range_gen, action);
1543 }
1544}
John Zulauf9cb530d2019-09-30 14:14:10 -06001545struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001546 using Iterator = ResourceAccessRangeMap::iterator;
1547 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001548 // this is only called on gaps, and never returns a gap.
1549 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001550 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001551 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001552 }
John Zulauf5f13a792020-03-10 07:31:21 -06001553
John Zulauf5c5e88d2019-12-26 11:22:02 -07001554 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001555 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001556 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001557 return pos;
1558 }
1559
John Zulauf43cc7462020-12-03 12:33:12 -07001560 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf14940722021-04-12 15:19:02 -06001561 SyncOrdering ordering_rule_, ResourceUsageTag tag_)
John Zulauf8e3c3e92021-01-06 11:19:36 -07001562 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001563 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001564 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001565 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001566 const SyncOrdering ordering_rule;
John Zulauf14940722021-04-12 15:19:02 -06001567 const ResourceUsageTag tag;
John Zulauf9cb530d2019-09-30 14:14:10 -06001568};
1569
John Zulauf4a6105a2020-11-17 15:11:05 -07001570// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001571struct PipelineBarrierOp {
1572 SyncBarrier barrier;
1573 bool layout_transition;
1574 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1575 : barrier(barrier_), layout_transition(layout_transition_) {}
1576 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001577 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001578 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1579};
John Zulauf4a6105a2020-11-17 15:11:05 -07001580// The barrier operation for wait events
1581struct WaitEventBarrierOp {
John Zulauf14940722021-04-12 15:19:02 -06001582 ResourceUsageTag scope_tag;
John Zulauf4a6105a2020-11-17 15:11:05 -07001583 SyncBarrier barrier;
1584 bool layout_transition;
John Zulauf14940722021-04-12 15:19:02 -06001585 WaitEventBarrierOp(const ResourceUsageTag scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1586 : scope_tag(scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
John Zulauf4a6105a2020-11-17 15:11:05 -07001587 WaitEventBarrierOp() = default;
John Zulauf14940722021-04-12 15:19:02 -06001588 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(scope_tag, barrier, layout_transition); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001589};
John Zulauf1e331ec2020-12-04 18:29:38 -07001590
John Zulauf4a6105a2020-11-17 15:11:05 -07001591// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1592// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1593// of a collection is known/present.
John Zulauf5c628d02021-05-04 15:46:36 -06001594template <typename BarrierOp, typename OpVector = std::vector<BarrierOp>>
John Zulauf89311b42020-09-29 16:28:47 -06001595class ApplyBarrierOpsFunctor {
1596 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001597 using Iterator = ResourceAccessRangeMap::iterator;
John Zulauf5c628d02021-05-04 15:46:36 -06001598 // Only called with a gap, and pos at the lower_bound(range)
1599 inline Iterator Infill(ResourceAccessRangeMap *accesses, const Iterator &pos, const ResourceAccessRange &range) const {
1600 if (!infill_default_) {
1601 return pos;
1602 }
1603 ResourceAccessState default_state;
1604 auto inserted = accesses->insert(pos, std::make_pair(range, default_state));
1605 return inserted;
1606 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001607
John Zulauf5c628d02021-05-04 15:46:36 -06001608 Iterator operator()(ResourceAccessRangeMap *accesses, const Iterator &pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001609 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001610 for (const auto &op : barrier_ops_) {
1611 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001612 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001613
John Zulauf89311b42020-09-29 16:28:47 -06001614 if (resolve_) {
1615 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1616 // another walk
1617 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001618 }
1619 return pos;
1620 }
1621
John Zulauf89311b42020-09-29 16:28:47 -06001622 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf5c628d02021-05-04 15:46:36 -06001623 ApplyBarrierOpsFunctor(bool resolve, typename OpVector::size_type size_hint, ResourceUsageTag tag)
1624 : resolve_(resolve), infill_default_(false), barrier_ops_(), tag_(tag) {
John Zulaufd5115702021-01-18 12:34:33 -07001625 barrier_ops_.reserve(size_hint);
1626 }
John Zulauf5c628d02021-05-04 15:46:36 -06001627 void EmplaceBack(const BarrierOp &op) {
1628 barrier_ops_.emplace_back(op);
1629 infill_default_ |= op.layout_transition;
1630 }
John Zulauf89311b42020-09-29 16:28:47 -06001631
1632 private:
1633 bool resolve_;
John Zulauf5c628d02021-05-04 15:46:36 -06001634 bool infill_default_;
1635 OpVector barrier_ops_;
John Zulauf14940722021-04-12 15:19:02 -06001636 const ResourceUsageTag tag_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001637};
1638
John Zulauf4a6105a2020-11-17 15:11:05 -07001639// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1640// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1641template <typename BarrierOp>
John Zulauf5c628d02021-05-04 15:46:36 -06001642class ApplyBarrierFunctor : public ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>> {
1643 using Base = ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>>;
1644
John Zulauf4a6105a2020-11-17 15:11:05 -07001645 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001646 ApplyBarrierFunctor(const BarrierOp &barrier_op) : Base(false, 1, kCurrentCommandTag) { Base::EmplaceBack(barrier_op); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001647};
1648
John Zulauf1e331ec2020-12-04 18:29:38 -07001649// This functor resolves the pendinging state.
John Zulauf5c628d02021-05-04 15:46:36 -06001650class ResolvePendingBarrierFunctor : public ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>> {
1651 using Base = ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>>;
1652
John Zulauf1e331ec2020-12-04 18:29:38 -07001653 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001654 ResolvePendingBarrierFunctor(ResourceUsageTag tag) : Base(true, 0, tag) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001655};
1656
John Zulauf8e3c3e92021-01-06 11:19:36 -07001657void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001658 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001659 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001660 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001661}
1662
John Zulauf8e3c3e92021-01-06 11:19:36 -07001663void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001664 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001665 if (!SimpleBinding(buffer)) return;
1666 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001667 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001668}
John Zulauf355e49b2020-04-24 15:11:15 -06001669
John Zulauf8e3c3e92021-01-06 11:19:36 -07001670void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001671 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1672 if (!SimpleBinding(image)) return;
1673 const auto base_address = ResourceBaseAddress(image);
1674 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1675 const auto address_type = ImageAddressType(image);
1676 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1677 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1678}
1679void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001680 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001681 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001682 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001683 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001684 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1685 base_address);
1686 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001687 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001688 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001689}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001690
1691void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
John Zulauf14940722021-04-12 15:19:02 -06001692 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001693 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1694 if (!gen) return;
1695 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1696 const auto address_type = view_gen.GetAddressType();
1697 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1698 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001699}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001700
John Zulauf8e3c3e92021-01-06 11:19:36 -07001701void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001702 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001703 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001704 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1705 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001706 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001707}
1708
John Zulaufd0ec59f2021-03-13 14:25:08 -07001709template <typename Action, typename RangeGen>
1710void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1711 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1712 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001713}
1714
1715template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001716void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1717 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1718 if (!gen) return;
1719 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001720}
1721
John Zulaufd0ec59f2021-03-13 14:25:08 -07001722void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1723 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001724 const ResourceUsageTag tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001725 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001726 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001727}
1728
John Zulaufd0ec59f2021-03-13 14:25:08 -07001729void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06001730 uint32_t subpass, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001731 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001732
1733 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1734 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001735 const auto &view_gen = attachment_views[i];
1736 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001737
1738 const auto &ci = attachment_ci[i];
1739 const bool has_depth = FormatHasDepth(ci.format);
1740 const bool has_stencil = FormatHasStencil(ci.format);
1741 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001742 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001743
1744 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001745 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1746 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001747 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001748 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001749 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1750 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001751 }
John Zulauf57261402021-08-13 11:32:06 -06001752 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001753 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001754 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1755 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001756 }
1757 }
1758 }
1759 }
1760}
1761
John Zulauf540266b2020-04-06 18:54:53 -06001762template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001763void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001764 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001765 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001766 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001767 }
1768}
1769
1770void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001771 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1772 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001773 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001774 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001775 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001776 }
1777 }
1778}
1779
John Zulauf4fa68462021-04-26 21:04:22 -06001780// Caller must ensure that lifespan of this is less than from
1781void AccessContext::ImportAsyncContexts(const AccessContext &from) { async_ = from.async_; }
1782
John Zulauf355e49b2020-04-24 15:11:15 -06001783// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001784HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1785 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001786
John Zulauf355e49b2020-04-24 15:11:15 -06001787 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001788 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001789
1790 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001791 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1792 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001793 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001794 if (!hazard.hazard) {
1795 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001796 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001797 }
John Zulaufa0a98292020-09-18 09:30:10 -06001798
John Zulauf355e49b2020-04-24 15:11:15 -06001799 return hazard;
1800}
1801
John Zulaufb02c1eb2020-10-06 16:33:36 -06001802void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001803 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001804 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001805 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001806 for (const auto &transition : transitions) {
1807 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001808 const auto &view_gen = attachment_views[transition.attachment];
1809 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001810
1811 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1812 assert(trackback);
1813
1814 // Import the attachments into the current context
1815 const auto *prev_context = trackback->context;
1816 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001817 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001818 auto &target_map = GetAccessStateMap(address_type);
1819 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001820 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1821 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001822 }
1823
John Zulauf86356ca2020-10-19 11:46:41 -06001824 // If there were no transitions skip this global map walk
1825 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001826 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001827 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001828 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001829}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001830
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001831void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulauf669dfd52021-01-27 17:15:28 -07001832 auto *events_context = GetCurrentEventsContext();
1833 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06001834 events_context->ApplyBarrier(src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001835}
1836
locke-lunarg61870c22020-06-09 14:51:50 -06001837bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1838 const char *func_name) const {
1839 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001840 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001841 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001842 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001843 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001844 return skip;
1845 }
1846
1847 using DescriptorClass = cvdescriptorset::DescriptorClass;
1848 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1849 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001850 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1851
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001852 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001853 const auto raster_state = pipe->RasterizationState();
1854 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001855 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001856 }
locke-lunarg61870c22020-06-09 14:51:50 -06001857 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001858 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001859 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001860 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001861 const auto descriptor_type = binding_it.GetType();
1862 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1863 auto array_idx = 0;
1864
1865 if (binding_it.IsVariableDescriptorCount()) {
1866 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1867 }
1868 SyncStageAccessIndex sync_index =
1869 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1870
1871 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1872 uint32_t index = i - index_range.start;
1873 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1874 switch (descriptor->GetClass()) {
1875 case DescriptorClass::ImageSampler:
1876 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001877 if (descriptor->Invalid()) {
1878 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001879 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07001880
1881 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
1882 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1883 const auto *img_view_state = image_descriptor->GetImageViewState();
1884 VkImageLayout image_layout = image_descriptor->GetImageLayout();
1885
John Zulauf361fb532020-07-22 10:45:39 -06001886 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001887 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1888 // Descriptors, so we do not have to worry about depth slicing here.
1889 // See: VUID 00343
1890 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001891 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001892 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001893
1894 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1895 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1896 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001897 // Input attachments are subject to raster ordering rules
1898 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001899 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001900 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001901 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001902 }
John Zulauf110413c2021-03-20 05:38:38 -06001903
John Zulauf33fc1d52020-07-17 11:01:10 -06001904 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001905 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001906 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001907 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1908 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001909 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001910 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1911 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1912 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001913 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1914 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001915 set_binding.first.binding, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001916 }
1917 break;
1918 }
1919 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001920 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
1921 if (texel_descriptor->Invalid()) {
1922 continue;
1923 }
1924 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
1925 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001926 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001927 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001928 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001929 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001930 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001931 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1932 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001933 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1934 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1935 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001936 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001937 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001938 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001939 }
1940 break;
1941 }
1942 case DescriptorClass::GeneralBuffer: {
1943 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07001944 if (buffer_descriptor->Invalid()) {
1945 continue;
1946 }
1947 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06001948 const ResourceAccessRange range =
1949 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001950 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001951 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001952 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001953 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001954 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1955 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001956 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1957 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1958 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001959 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001960 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001961 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001962 }
1963 break;
1964 }
1965 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1966 default:
1967 break;
1968 }
1969 }
1970 }
1971 }
1972 return skip;
1973}
1974
1975void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
John Zulauf14940722021-04-12 15:19:02 -06001976 const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001977 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001978 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001979 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001980 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001981 return;
1982 }
1983
1984 using DescriptorClass = cvdescriptorset::DescriptorClass;
1985 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1986 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001987 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1988
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001989 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001990 const auto raster_state = pipe->RasterizationState();
1991 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001992 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001993 }
locke-lunarg61870c22020-06-09 14:51:50 -06001994 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001995 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001996 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001997 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001998 const auto descriptor_type = binding_it.GetType();
1999 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2000 auto array_idx = 0;
2001
2002 if (binding_it.IsVariableDescriptorCount()) {
2003 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2004 }
2005 SyncStageAccessIndex sync_index =
2006 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2007
2008 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2009 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2010 switch (descriptor->GetClass()) {
2011 case DescriptorClass::ImageSampler:
2012 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002013 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
2014 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
2015 if (image_descriptor->Invalid()) {
2016 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002017 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07002018 const auto *img_view_state = image_descriptor->GetImageViewState();
Jeremy Gebben11a68a32021-07-29 11:59:22 -06002019 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
2020 // Descriptors, so we do not have to worry about depth slicing here.
2021 // See: VUID 00343
2022 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06002023 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002024 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06002025 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2026 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2027 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
2028 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002029 } else {
John Zulauf110413c2021-03-20 05:38:38 -06002030 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
2031 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002032 }
locke-lunarg61870c22020-06-09 14:51:50 -06002033 break;
2034 }
2035 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002036 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
2037 if (texel_descriptor->Invalid()) {
2038 continue;
2039 }
2040 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
2041 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002042 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002043 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002044 break;
2045 }
2046 case DescriptorClass::GeneralBuffer: {
2047 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07002048 if (buffer_descriptor->Invalid()) {
2049 continue;
2050 }
2051 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06002052 const ResourceAccessRange range =
2053 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002054 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002055 break;
2056 }
2057 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2058 default:
2059 break;
2060 }
2061 }
2062 }
2063 }
2064}
2065
2066bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2067 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002068 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002069 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002070 return skip;
2071 }
2072
2073 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2074 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002075 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002076
2077 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002078 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002079 if (binding_description.binding < binding_buffers_size) {
2080 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002081 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002082
locke-lunarg1ae57d62020-11-18 10:49:19 -07002083 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002084 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2085 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002086 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002087 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002088 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002089 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
2090 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2091 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002092 }
2093 }
2094 }
2095 return skip;
2096}
2097
John Zulauf14940722021-04-12 15:19:02 -06002098void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002099 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002100 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002101 return;
2102 }
2103 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2104 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002105 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002106
2107 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002108 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002109 if (binding_description.binding < binding_buffers_size) {
2110 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002111 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002112
locke-lunarg1ae57d62020-11-18 10:49:19 -07002113 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002114 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2115 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002116 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2117 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002118 }
2119 }
2120}
2121
2122bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2123 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002124 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002125 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002126 }
locke-lunarg61870c22020-06-09 14:51:50 -06002127
locke-lunarg1ae57d62020-11-18 10:49:19 -07002128 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002129 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002130 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2131 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002132 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002133 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002134 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002135 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2136 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2137 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002138 }
2139
2140 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2141 // We will detect more accurate range in the future.
2142 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2143 return skip;
2144}
2145
John Zulauf14940722021-04-12 15:19:02 -06002146void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002147 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 -06002148
locke-lunarg1ae57d62020-11-18 10:49:19 -07002149 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002150 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002151 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2152 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002153 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002154
2155 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2156 // We will detect more accurate range in the future.
2157 RecordDrawVertex(UINT32_MAX, 0, tag);
2158}
2159
2160bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002161 bool skip = false;
2162 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002163 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002164 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002165}
2166
John Zulauf14940722021-04-12 15:19:02 -06002167void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002168 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002169 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002170 }
locke-lunarg61870c22020-06-09 14:51:50 -06002171}
2172
John Zulauf41a9c7c2021-12-07 15:59:53 -07002173ResourceUsageTag CommandBufferAccessContext::RecordBeginRenderPass(CMD_TYPE cmd, const RENDER_PASS_STATE &rp_state,
2174 const VkRect2D &render_area,
2175 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
John Zulauf355e49b2020-04-24 15:11:15 -06002176 // Create an access context the current renderpass.
John Zulauf41a9c7c2021-12-07 15:59:53 -07002177 const auto barrier_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2178 const auto load_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kLoadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07002179 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002180 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002181 current_renderpass_context_->RecordBeginRenderPass(barrier_tag, load_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002182 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002183 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002184}
2185
John Zulauf41a9c7c2021-12-07 15:59:53 -07002186ResourceUsageTag CommandBufferAccessContext::RecordNextSubpass(const CMD_TYPE cmd) {
John Zulauf16adfc92020-04-08 10:28:33 -06002187 assert(current_renderpass_context_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002188 if (!current_renderpass_context_) return NextCommandTag(cmd);
2189
2190 auto store_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kStoreOp);
2191 auto barrier_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2192 auto load_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kLoadOp);
2193
2194 current_renderpass_context_->RecordNextSubpass(store_tag, barrier_tag, load_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002195 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002196 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002197}
2198
John Zulauf41a9c7c2021-12-07 15:59:53 -07002199ResourceUsageTag CommandBufferAccessContext::RecordEndRenderPass(const CMD_TYPE cmd) {
John Zulauf16adfc92020-04-08 10:28:33 -06002200 assert(current_renderpass_context_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002201 if (!current_renderpass_context_) return NextCommandTag(cmd);
John Zulauf16adfc92020-04-08 10:28:33 -06002202
John Zulauf41a9c7c2021-12-07 15:59:53 -07002203 auto store_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kStoreOp);
2204 auto barrier_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2205
2206 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, store_tag, barrier_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002207 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002208 current_renderpass_context_ = nullptr;
John Zulauf41a9c7c2021-12-07 15:59:53 -07002209 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002210}
2211
John Zulauf4a6105a2020-11-17 15:11:05 -07002212void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2213 // Erase is okay with the key not being
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002214 auto event_state = sync_state_->Get<EVENT_STATE>(event);
John Zulauf669dfd52021-01-27 17:15:28 -07002215 if (event_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002216 GetCurrentEventsContext()->Destroy(event_state.get());
John Zulaufd5115702021-01-18 12:34:33 -07002217 }
2218}
2219
John Zulaufae842002021-04-15 18:20:55 -06002220// The is the recorded cb context
John Zulauf4fa68462021-04-26 21:04:22 -06002221bool CommandBufferAccessContext::ValidateFirstUse(CommandBufferAccessContext *proxy_context, const char *func_name,
2222 uint32_t index) const {
2223 assert(proxy_context);
2224 auto *events_context = proxy_context->GetCurrentEventsContext();
2225 auto *access_context = proxy_context->GetCurrentAccessContext();
2226 const ResourceUsageTag base_tag = proxy_context->GetTagLimit();
John Zulaufae842002021-04-15 18:20:55 -06002227 bool skip = false;
2228 ResourceUsageRange tag_range = {0, 0};
2229 const AccessContext *recorded_context = GetCurrentAccessContext();
2230 assert(recorded_context);
2231 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002232 auto log_msg = [this](const HazardResult &hazard, const CommandBufferAccessContext &active_context, const char *func_name,
John Zulaufae842002021-04-15 18:20:55 -06002233 uint32_t index) {
2234 const auto cb_handle = active_context.cb_state_->commandBuffer();
2235 const auto recorded_handle = cb_state_->commandBuffer();
John Zulauf4fa68462021-04-26 21:04:22 -06002236 const auto *report_data = sync_state_->report_data;
John Zulaufae842002021-04-15 18:20:55 -06002237 return sync_state_->LogError(cb_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf4fa68462021-04-26 21:04:22 -06002238 "%s: Hazard %s for entry %" PRIu32 ", %s, Recorded access info %s. Access info %s.", func_name,
2239 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(recorded_handle).c_str(),
2240 FormatUsage(*hazard.recorded_access).c_str(), active_context.FormatUsage(hazard).c_str());
John Zulaufae842002021-04-15 18:20:55 -06002241 };
2242 for (const auto &sync_op : sync_ops_) {
John Zulauf4fa68462021-04-26 21:04:22 -06002243 // we update the range to any include layout transition first use writes,
2244 // as they are stored along with the source scope (as effective barrier) when recorded
2245 tag_range.end = sync_op.tag + 1;
John Zulauf610e28c2021-08-03 17:46:23 -06002246 skip |= sync_op.sync_op->ReplayValidate(sync_op.tag, *this, base_tag, proxy_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002247
John Zulaufae842002021-04-15 18:20:55 -06002248 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2249 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002250 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002251 }
2252 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
John Zulauf4fa68462021-04-26 21:04:22 -06002253 // Record the barrier into the proxy context.
2254 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2255 tag_range.begin = tag_range.end;
John Zulaufae842002021-04-15 18:20:55 -06002256 }
2257
2258 // and anything after the last syncop
John Zulaufae842002021-04-15 18:20:55 -06002259 tag_range.end = ResourceUsageRecord::kMaxIndex;
2260 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2261 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002262 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002263 }
2264
2265 return skip;
2266}
2267
John Zulauf4fa68462021-04-26 21:04:22 -06002268void CommandBufferAccessContext::RecordExecutedCommandBuffer(const CommandBufferAccessContext &recorded_cb_context, CMD_TYPE cmd) {
2269 auto *events_context = GetCurrentEventsContext();
2270 auto *access_context = GetCurrentAccessContext();
2271 const AccessContext *recorded_context = recorded_cb_context.GetCurrentAccessContext();
2272 assert(recorded_context);
2273
2274 // Just run through the barriers ignoring the usage from the recorded context, as Resolve will overwrite outdated state
2275 const ResourceUsageTag base_tag = GetTagLimit();
2276 for (const auto &sync_op : recorded_cb_context.sync_ops_) {
2277 // we update the range to any include layout transition first use writes,
2278 // as they are stored along with the source scope (as effective barrier) when recorded
2279 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2280 }
2281
2282 ResourceUsageRange tag_range = ImportRecordedAccessLog(recorded_cb_context);
2283 assert(base_tag == tag_range.begin); // to ensure the to offset calculation agree
2284 ResolveRecordedContext(*recorded_context, tag_range.begin);
2285}
2286
2287void CommandBufferAccessContext::ResolveRecordedContext(const AccessContext &recorded_context, ResourceUsageTag offset) {
2288 auto tag_offset = [offset](ResourceAccessState *access) { access->OffsetTag(offset); };
2289
2290 auto *access_context = GetCurrentAccessContext();
2291 for (auto address_type : kAddressTypes) {
2292 recorded_context.ResolveAccessRange(address_type, kFullRange, tag_offset, &access_context->GetAccessStateMap(address_type),
2293 nullptr, false);
2294 }
2295}
2296
2297ResourceUsageRange CommandBufferAccessContext::ImportRecordedAccessLog(const CommandBufferAccessContext &recorded_context) {
2298 // The execution references ensure lifespan for the referenced child CB's...
2299 ResourceUsageRange tag_range(GetTagLimit(), 0);
John Zulauf3c2a0b32021-07-14 11:14:52 -06002300 cbs_referenced_.emplace(recorded_context.cb_state_);
John Zulauf4fa68462021-04-26 21:04:22 -06002301 access_log_.insert(access_log_.end(), recorded_context.access_log_.cbegin(), recorded_context.access_log_.end());
2302 tag_range.end = access_log_.size();
2303 return tag_range;
2304}
2305
John Zulauf41a9c7c2021-12-07 15:59:53 -07002306ResourceUsageTag CommandBufferAccessContext::NextSubcommandTag(CMD_TYPE command, ResourceUsageRecord::SubcommandType subcommand) {
2307 ResourceUsageTag next = access_log_.size();
2308 access_log_.emplace_back(command, command_number_, subcommand, ++subcommand_number_, cb_state_.get(), reset_count_);
2309 return next;
2310}
2311
2312ResourceUsageTag CommandBufferAccessContext::NextCommandTag(CMD_TYPE command, ResourceUsageRecord::SubcommandType subcommand) {
2313 command_number_++;
2314 subcommand_number_ = 0;
2315 ResourceUsageTag next = access_log_.size();
2316 access_log_.emplace_back(command, command_number_, subcommand, subcommand_number_, cb_state_.get(), reset_count_);
2317 return next;
2318}
2319
2320ResourceUsageTag CommandBufferAccessContext::NextIndexedCommandTag(CMD_TYPE command, uint32_t index) {
2321 if (index == 0) {
2322 return NextCommandTag(command, ResourceUsageRecord::SubcommandType::kIndex);
2323 }
2324 return NextSubcommandTag(command, ResourceUsageRecord::SubcommandType::kIndex);
2325}
2326
John Zulaufae842002021-04-15 18:20:55 -06002327class HazardDetectFirstUse {
2328 public:
2329 HazardDetectFirstUse(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range)
2330 : recorded_use_(recorded_use), tag_range_(tag_range) {}
2331 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
2332 return pos->second.DetectHazard(recorded_use_, tag_range_);
2333 }
2334 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
2335 return pos->second.DetectAsyncHazard(recorded_use_, tag_range_, start_tag);
2336 }
2337
2338 private:
2339 const ResourceAccessState &recorded_use_;
2340 const ResourceUsageRange &tag_range_;
2341};
2342
2343// This is called with the *recorded* command buffers access context, with the *active* access context pass in, againsts which
2344// hazards will be detected
2345HazardResult AccessContext::DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context) const {
2346 HazardResult hazard;
2347 for (const auto address_type : kAddressTypes) {
2348 const auto &recorded_access_map = GetAccessStateMap(address_type);
2349 for (const auto &recorded_access : recorded_access_map) {
2350 // Cull any entries not in the current tag range
2351 if (!recorded_access.second.FirstAccessInTagRange(tag_range)) continue;
2352 HazardDetectFirstUse detector(recorded_access.second, tag_range);
2353 hazard = access_context.DetectHazard(address_type, detector, recorded_access.first, DetectOptions::kDetectAll);
2354 if (hazard.hazard) break;
2355 }
2356 }
2357
2358 return hazard;
2359}
2360
John Zulauf64ffe552021-02-06 10:25:07 -07002361bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002362 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002363 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002364 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002365 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002366 if (!pipe) {
2367 return skip;
2368 }
2369
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002370 const auto raster_state = pipe->RasterizationState();
2371 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002372 return skip;
2373 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002374 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002375 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002376
John Zulauf1a224292020-06-30 14:52:13 -06002377 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002378 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002379 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2380 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002381 if (location >= subpass.colorAttachmentCount ||
2382 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002383 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002384 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002385 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2386 if (!view_gen.IsValid()) continue;
2387 HazardResult hazard =
2388 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2389 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002390 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002391 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002392 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002393 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002394 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002395 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002396 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002397 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002398 }
2399 }
2400 }
locke-lunarg37047832020-06-12 13:44:45 -06002401
2402 // PHASE1 TODO: Add layout based read/vs. write selection.
2403 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002404 const auto ds_state = pipe->DepthStencilState();
2405 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002406
2407 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2408 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2409 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002410 bool depth_write = false, stencil_write = false;
2411
2412 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002413 if (!FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable && ds_state->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002414 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2415 depth_write = true;
2416 }
2417 // PHASE1 TODO: It needs to check if stencil is writable.
2418 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2419 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2420 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002421 if (!FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002422 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2423 stencil_write = true;
2424 }
2425
2426 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2427 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002428 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2429 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2430 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002431 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002432 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002433 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002434 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002435 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002436 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2437 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002438 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002439 }
2440 }
2441 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002442 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2443 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2444 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002445 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002446 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002447 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002448 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002449 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002450 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2451 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002452 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002453 }
locke-lunarg61870c22020-06-09 14:51:50 -06002454 }
2455 }
2456 return skip;
2457}
2458
John Zulauf14940722021-04-12 15:19:02 -06002459void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002460 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002461 if (!pipe) {
2462 return;
2463 }
2464
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002465 const auto *raster_state = pipe->RasterizationState();
2466 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002467 return;
2468 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002469 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002470 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002471
John Zulauf1a224292020-06-30 14:52:13 -06002472 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002473 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002474 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2475 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002476 if (location >= subpass.colorAttachmentCount ||
2477 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002478 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002479 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002480 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2481 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2482 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2483 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002484 }
2485 }
locke-lunarg37047832020-06-12 13:44:45 -06002486
2487 // PHASE1 TODO: Add layout based read/vs. write selection.
2488 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002489 const auto *ds_state = pipe->DepthStencilState();
2490 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002491 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2492 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2493 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002494 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002495 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2496 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002497
2498 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002499 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable &&
2500 ds_state->depthWriteEnable && IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
locke-lunarg37047832020-06-12 13:44:45 -06002501 depth_write = true;
2502 }
2503 // PHASE1 TODO: It needs to check if stencil is writable.
2504 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2505 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2506 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002507 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002508 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2509 stencil_write = true;
2510 }
2511
John Zulaufd0ec59f2021-03-13 14:25:08 -07002512 if (depth_write || stencil_write) {
2513 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2514 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2515 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2516 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002517 }
locke-lunarg61870c22020-06-09 14:51:50 -06002518 }
2519}
2520
John Zulauf64ffe552021-02-06 10:25:07 -07002521bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002522 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002523 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002524 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002525 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002526 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002527 func_name);
2528
John Zulauf355e49b2020-04-24 15:11:15 -06002529 const auto next_subpass = current_subpass_ + 1;
ziga-lunarg31a3e772022-03-22 11:48:46 +01002530 if (next_subpass >= subpass_contexts_.size()) {
2531 return skip;
2532 }
John Zulauf1507ee42020-05-18 11:33:09 -06002533 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002534 skip |=
2535 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002536 if (!skip) {
2537 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2538 // on a copy of the (empty) next context.
2539 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2540 AccessContext temp_context(next_context);
2541 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002542 skip |=
2543 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002544 }
John Zulauf7635de32020-05-29 17:14:15 -06002545 return skip;
2546}
John Zulauf64ffe552021-02-06 10:25:07 -07002547bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002548 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002549 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002550 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002551 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002552 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2553
2554 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002555 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002556 return skip;
2557}
2558
John Zulauf64ffe552021-02-06 10:25:07 -07002559AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002560 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002561}
2562
John Zulauf64ffe552021-02-06 10:25:07 -07002563bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2564 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002565 bool skip = false;
2566
John Zulauf7635de32020-05-29 17:14:15 -06002567 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2568 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2569 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2570 // to apply and only copy then, if this proves a hot spot.
2571 std::unique_ptr<AccessContext> proxy_for_current;
2572
John Zulauf355e49b2020-04-24 15:11:15 -06002573 // Validate the "finalLayout" transitions to external
2574 // Get them from where there we're hidding in the extra entry.
2575 const auto &final_transitions = rp_state_->subpass_transitions.back();
2576 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002577 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002578 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2579 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002580 auto *context = trackback.context;
2581
2582 if (transition.prev_pass == current_subpass_) {
2583 if (!proxy_for_current) {
2584 // 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 -07002585 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002586 }
2587 context = proxy_for_current.get();
2588 }
2589
John Zulaufa0a98292020-09-18 09:30:10 -06002590 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2591 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002592 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002593 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002594 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002595 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002596 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2597 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2598 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2599 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002600 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002601 }
2602 }
2603 return skip;
2604}
2605
John Zulauf14940722021-04-12 15:19:02 -06002606void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002607 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002608 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002609}
2610
John Zulauf14940722021-04-12 15:19:02 -06002611void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002612 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2613 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002614
2615 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2616 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002617 const AttachmentViewGen &view_gen = attachment_views_[i];
2618 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002619
2620 const auto &ci = attachment_ci[i];
2621 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002622 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002623 const bool is_color = !(has_depth || has_stencil);
2624
2625 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002626 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2627 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2628 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2629 SyncOrdering::kColorAttachment, tag);
2630 }
John Zulauf1507ee42020-05-18 11:33:09 -06002631 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002632 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002633 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2634 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2635 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2636 SyncOrdering::kDepthStencilAttachment, tag);
2637 }
John Zulauf1507ee42020-05-18 11:33:09 -06002638 }
2639 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002640 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2641 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2642 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2643 SyncOrdering::kDepthStencilAttachment, tag);
2644 }
John Zulauf1507ee42020-05-18 11:33:09 -06002645 }
2646 }
2647 }
2648 }
2649}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002650AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2651 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2652 AttachmentViewGenVector view_gens;
2653 VkExtent3D extent = CastTo3D(render_area.extent);
2654 VkOffset3D offset = CastTo3D(render_area.offset);
2655 view_gens.reserve(attachment_views.size());
2656 for (const auto *view : attachment_views) {
2657 view_gens.emplace_back(view, offset, extent);
2658 }
2659 return view_gens;
2660}
John Zulauf64ffe552021-02-06 10:25:07 -07002661RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2662 VkQueueFlags queue_flags,
2663 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2664 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002665 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002666 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002667 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002668 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002669 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002670 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002671 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002672}
John Zulauf41a9c7c2021-12-07 15:59:53 -07002673void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag barrier_tag, const ResourceUsageTag load_tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002674 assert(0 == current_subpass_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002675 subpass_contexts_[current_subpass_].SetStartTag(barrier_tag);
2676 RecordLayoutTransitions(barrier_tag);
2677 RecordLoadOperations(load_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002678}
John Zulauf1507ee42020-05-18 11:33:09 -06002679
John Zulauf41a9c7c2021-12-07 15:59:53 -07002680void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag store_tag, const ResourceUsageTag barrier_tag,
2681 const ResourceUsageTag load_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002682 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulauf41a9c7c2021-12-07 15:59:53 -07002683 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
2684 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002685
ziga-lunarg31a3e772022-03-22 11:48:46 +01002686 if (current_subpass_ + 1 >= subpass_contexts_.size()) {
2687 return;
2688 }
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002689 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2690 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002691 current_subpass_++;
John Zulauf41a9c7c2021-12-07 15:59:53 -07002692 subpass_contexts_[current_subpass_].SetStartTag(barrier_tag);
2693 RecordLayoutTransitions(barrier_tag);
2694 RecordLoadOperations(load_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002695}
2696
John Zulauf41a9c7c2021-12-07 15:59:53 -07002697void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag store_tag,
2698 const ResourceUsageTag barrier_tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002699 // Add the resolve and store accesses
John Zulauf41a9c7c2021-12-07 15:59:53 -07002700 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
2701 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002702
John Zulauf355e49b2020-04-24 15:11:15 -06002703 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002704 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002705
2706 // Add the "finalLayout" transitions to external
2707 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002708 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2709 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2710 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002711 const auto &final_transitions = rp_state_->subpass_transitions.back();
2712 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002713 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002714 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002715 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002716 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), barrier_tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002717 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002718 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002719 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002720 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002721 }
2722}
2723
Jeremy Gebben40a22942020-12-22 14:22:06 -07002724SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002725 SyncExecScope result;
2726 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002727 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2728 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002729 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2730 return result;
2731}
2732
Jeremy Gebben40a22942020-12-22 14:22:06 -07002733SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002734 SyncExecScope result;
2735 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002736 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2737 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002738 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2739 return result;
2740}
2741
2742SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002743 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002744 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002745 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002746 dst_access_scope = 0;
2747}
2748
2749template <typename Barrier>
2750SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002751 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002752 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002753 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002754 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2755}
2756
2757SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002758 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2759 if (barrier) {
2760 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002761 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002762 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002763
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002764 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002765 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002766 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2767
2768 } else {
2769 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002770 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002771 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2772
2773 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002774 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002775 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2776 }
2777}
2778
2779template <typename Barrier>
2780SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2781 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2782 src_exec_scope = src.exec_scope;
2783 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2784
2785 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002786 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002787 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002788}
2789
John Zulaufb02c1eb2020-10-06 16:33:36 -06002790// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2791void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2792 for (const auto &barrier : barriers) {
2793 ApplyBarrier(barrier, layout_transition);
2794 }
2795}
2796
John Zulauf89311b42020-09-29 16:28:47 -06002797// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2798// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2799// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002800void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002801 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002802 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002803 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002804 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002805 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002806 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002807 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002808}
John Zulauf9cb530d2019-09-30 14:14:10 -06002809HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2810 HazardResult hazard;
2811 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002812 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002813 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002814 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002815 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002816 }
2817 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002818 // Write operation:
2819 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2820 // If reads exists -- test only against them because either:
2821 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2822 // * 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
2823 // the current write happens after the reads, so just test the write against the reades
2824 // Otherwise test against last_write
2825 //
2826 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002827 if (last_reads.size()) {
2828 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002829 if (IsReadHazard(usage_stage, read_access)) {
2830 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2831 break;
2832 }
2833 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002834 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002835 // Write-After-Write check -- if we have a previous write to test against
2836 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002837 }
2838 }
2839 return hazard;
2840}
2841
John Zulauf4fa68462021-04-26 21:04:22 -06002842HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002843 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002844 return DetectHazard(usage_index, ordering);
2845}
2846
2847HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002848 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2849 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002850 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002851 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002852 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2853 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002854 if (IsRead(usage_bit)) {
2855 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2856 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2857 if (is_raw_hazard) {
2858 // NOTE: we know last_write is non-zero
2859 // See if the ordering rules save us from the simple RAW check above
2860 // First check to see if the current usage is covered by the ordering rules
2861 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2862 const bool usage_is_ordered =
2863 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2864 if (usage_is_ordered) {
2865 // Now see of the most recent write (or a subsequent read) are ordered
2866 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2867 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002868 }
2869 }
John Zulauf4285ee92020-09-23 10:20:52 -06002870 if (is_raw_hazard) {
2871 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2872 }
John Zulauf5c628d02021-05-04 15:46:36 -06002873 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2874 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2875 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002876 } else {
2877 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002878 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002879 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002880 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002881 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002882 if (usage_write_is_ordered) {
2883 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2884 ordered_stages = GetOrderedStages(ordering);
2885 }
2886 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2887 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002888 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002889 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2890 if (IsReadHazard(usage_stage, read_access)) {
2891 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2892 break;
2893 }
John Zulaufd14743a2020-07-03 09:42:39 -06002894 }
2895 }
John Zulauf2a344ca2021-09-09 17:07:19 -06002896 } else if (last_write.any() && !(last_write_is_ordered && usage_write_is_ordered)) {
2897 bool ilt_ilt_hazard = false;
2898 if ((usage_index == SYNC_IMAGE_LAYOUT_TRANSITION) && (usage_bit == last_write)) {
2899 // ILT after ILT is a special case where we check the 2nd access scope of the first ILT against the first access
2900 // scope of the second ILT, which has been passed (smuggled?) in the ordering barrier
2901 ilt_ilt_hazard = !(write_barriers & ordering.access_scope).any();
2902 }
2903 if (ilt_ilt_hazard || IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002904 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002905 }
John Zulauf69133422020-05-20 14:55:53 -06002906 }
2907 }
2908 return hazard;
2909}
2910
John Zulaufae842002021-04-15 18:20:55 -06002911HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2912 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002913 using Size = FirstAccesses::size_type;
2914 const auto &recorded_accesses = recorded_use.first_accesses_;
2915 Size count = recorded_accesses.size();
2916 if (count) {
2917 const auto &last_access = recorded_accesses.back();
2918 bool do_write_last = IsWrite(last_access.usage_index);
2919 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06002920
John Zulauf4fa68462021-04-26 21:04:22 -06002921 for (Size i = 0; i < count; ++count) {
2922 const auto &first = recorded_accesses[i];
2923 // Skip and quit logic
2924 if (first.tag < tag_range.begin) continue;
2925 if (first.tag >= tag_range.end) {
2926 do_write_last = false; // ignore last since we know it can't be in tag_range
2927 break;
2928 }
2929
2930 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2931 if (hazard.hazard) {
2932 hazard.AddRecordedAccess(first);
2933 break;
2934 }
2935 }
2936
2937 if (do_write_last && tag_range.includes(last_access.tag)) {
2938 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
2939 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
2940 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2941 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
2942 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
2943 // the barrier that applies them
2944 barrier |= recorded_use.first_write_layout_ordering_;
2945 }
2946 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
2947 // the active context
2948 if (recorded_use.first_read_stages_) {
2949 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
2950 // reads in the active context are not "most recent" as all recorded context operations are *after* them
2951 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
2952 // active context.
2953 barrier.exec_scope |= recorded_use.first_read_stages_;
2954 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
2955 barrier.access_scope |= FlagBit(last_access.usage_index);
2956 }
2957 hazard = DetectHazard(last_access.usage_index, barrier);
2958 if (hazard.hazard) {
2959 hazard.AddRecordedAccess(last_access);
2960 }
2961 }
John Zulaufae842002021-04-15 18:20:55 -06002962 }
2963 return hazard;
2964}
2965
John Zulauf2f952d22020-02-10 11:34:51 -07002966// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002967HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002968 HazardResult hazard;
2969 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002970 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2971 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2972 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002973 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002974 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002975 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002976 }
2977 } else {
John Zulauf14940722021-04-12 15:19:02 -06002978 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002979 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002980 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002981 // 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 -07002982 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002983 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002984 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002985 break;
2986 }
2987 }
John Zulauf2f952d22020-02-10 11:34:51 -07002988 }
2989 }
2990 return hazard;
2991}
2992
John Zulaufae842002021-04-15 18:20:55 -06002993HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2994 ResourceUsageTag start_tag) const {
2995 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002996 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06002997 // Skip and quit logic
2998 if (first.tag < tag_range.begin) continue;
2999 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06003000
3001 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06003002 if (hazard.hazard) {
3003 hazard.AddRecordedAccess(first);
3004 break;
3005 }
John Zulaufae842002021-04-15 18:20:55 -06003006 }
3007 return hazard;
3008}
3009
Jeremy Gebben40a22942020-12-22 14:22:06 -07003010HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003011 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003012 // Only supporting image layout transitions for now
3013 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3014 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003015 // only test for WAW if there no intervening read operations.
3016 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07003017 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06003018 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07003019 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003020 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003021 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003022 break;
3023 }
3024 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003025 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3026 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3027 }
3028
3029 return hazard;
3030}
3031
Jeremy Gebben40a22942020-12-22 14:22:06 -07003032HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07003033 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06003034 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07003035 // Only supporting image layout transitions for now
3036 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3037 HazardResult hazard;
3038 // only test for WAW if there no intervening read operations.
3039 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3040
John Zulaufab7756b2020-12-29 16:10:16 -07003041 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003042 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3043 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003044 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003045 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003046 // The read is in the events first synchronization scope, so we use a barrier hazard check
3047 // If the read stage is not in the src sync scope
3048 // *AND* not execution chained with an existing sync barrier (that's the or)
3049 // then the barrier access is unsafe (R/W after R)
3050 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3051 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3052 break;
3053 }
3054 } else {
3055 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3056 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3057 }
3058 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003059 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003060 // 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 -06003061 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003062 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3063 // So do a normal barrier hazard check
3064 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3065 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3066 }
3067 } else {
3068 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003069 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3070 }
John Zulaufd14743a2020-07-03 09:42:39 -06003071 }
John Zulauf361fb532020-07-22 10:45:39 -06003072
John Zulauf0cb5be22020-01-23 12:18:22 -07003073 return hazard;
3074}
3075
John Zulauf5f13a792020-03-10 07:31:21 -06003076// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3077// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3078// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3079void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003080 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003081 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3082 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003083 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003084 } else if (other.write_tag == write_tag) {
3085 // 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 -06003086 // dependency chaining logic or any stage expansion)
3087 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003088 pending_write_barriers |= other.pending_write_barriers;
3089 pending_layout_transition |= other.pending_layout_transition;
3090 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003091 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003092
John Zulaufd14743a2020-07-03 09:42:39 -06003093 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003094 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003095 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003096 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003097 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003098 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003099 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003100 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3101 // but we should wait on profiling data for that.
3102 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003103 auto &my_read = last_reads[my_read_index];
3104 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003105 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003106 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003107 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003108 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003109 my_read.pending_dep_chain = other_read.pending_dep_chain;
3110 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3111 // May require tracking more than one access per stage.
3112 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003113 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003114 // Since I'm overwriting the fragement stage read, also update the input attachment info
3115 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003116 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003117 }
John Zulauf14940722021-04-12 15:19:02 -06003118 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003119 // The read tags match so merge the barriers
3120 my_read.barriers |= other_read.barriers;
3121 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003122 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003123
John Zulauf5f13a792020-03-10 07:31:21 -06003124 break;
3125 }
3126 }
3127 } else {
3128 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003129 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003130 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003131 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003132 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003133 }
John Zulauf5f13a792020-03-10 07:31:21 -06003134 }
3135 }
John Zulauf361fb532020-07-22 10:45:39 -06003136 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003137 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3138 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003139
3140 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3141 // of the copy and other into this using the update first logic.
3142 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3143 // of the other first_accesses... )
3144 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3145 FirstAccesses firsts(std::move(first_accesses_));
3146 first_accesses_.clear();
3147 first_read_stages_ = 0U;
3148 auto a = firsts.begin();
3149 auto a_end = firsts.end();
3150 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003151 // TODO: Determine whether some tag offset will be needed for PHASE II
3152 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003153 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3154 ++a;
3155 }
3156 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3157 }
3158 for (; a != a_end; ++a) {
3159 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3160 }
3161 }
John Zulauf5f13a792020-03-10 07:31:21 -06003162}
3163
John Zulauf14940722021-04-12 15:19:02 -06003164void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003165 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3166 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003167 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003168 // Mulitple outstanding reads may be of interest and do dependency chains independently
3169 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3170 const auto usage_stage = PipelineStageBit(usage_index);
3171 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003172 for (auto &read_access : last_reads) {
3173 if (read_access.stage == usage_stage) {
3174 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003175 break;
3176 }
3177 }
3178 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003179 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003180 last_read_stages |= usage_stage;
3181 }
John Zulauf4285ee92020-09-23 10:20:52 -06003182
3183 // 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 -07003184 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003185 // TODO Revisit re: multiple reads for a given stage
3186 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003187 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003188 } else {
3189 // Assume write
3190 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003191 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003192 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003193 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003194}
John Zulauf5f13a792020-03-10 07:31:21 -06003195
John Zulauf89311b42020-09-29 16:28:47 -06003196// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3197// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3198// We can overwrite them as *this* write is now after them.
3199//
3200// 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 -06003201void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003202 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003203 last_read_stages = 0;
3204 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003205 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003206
3207 write_barriers = 0;
3208 write_dependency_chain = 0;
3209 write_tag = tag;
3210 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003211}
3212
John Zulauf89311b42020-09-29 16:28:47 -06003213// Apply the memory barrier without updating the existing barriers. The execution barrier
3214// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3215// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3216// replace the current write barriers or add to them, so accumulate to pending as well.
3217void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3218 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3219 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003220 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3221 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3222 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3223 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003224 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003225 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003226 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003227 if (layout_transition) {
3228 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3229 }
John Zulaufa0a98292020-09-18 09:30:10 -06003230 }
John Zulauf89311b42020-09-29 16:28:47 -06003231 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3232 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003233
John Zulauf89311b42020-09-29 16:28:47 -06003234 if (!pending_layout_transition) {
3235 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3236 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003237 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003238 // 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 -07003239 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3240 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003241 }
3242 }
John Zulaufa0a98292020-09-18 09:30:10 -06003243 }
John Zulaufa0a98292020-09-18 09:30:10 -06003244}
3245
John Zulauf4a6105a2020-11-17 15:11:05 -07003246// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3247// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003248void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003249 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3250 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3251 // in order to know if it's in the excecution scope
3252 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3253 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3254 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003255 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003256 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003257 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003258 if (layout_transition) {
3259 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3260 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003261 }
3262 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3263 pending_layout_transition |= layout_transition;
3264
3265 if (!pending_layout_transition) {
3266 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3267 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003268 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003269 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3270 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3271 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3272 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3273 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3274 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3275 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003276 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 -07003277 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003278 }
3279 }
3280 }
3281}
John Zulauf14940722021-04-12 15:19:02 -06003282void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003283 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003284 // 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 -06003285 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003286 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003287 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3288 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003289 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003290 }
John Zulauf89311b42020-09-29 16:28:47 -06003291
3292 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003293 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003294 for (auto &read_access : last_reads) {
3295 read_access.barriers |= read_access.pending_dep_chain;
3296 read_execution_barriers |= read_access.barriers;
3297 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003298 }
3299
3300 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3301 write_dependency_chain |= pending_write_dep_chain;
3302 write_barriers |= pending_write_barriers;
3303 pending_write_dep_chain = 0;
3304 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003305}
3306
John Zulaufae842002021-04-15 18:20:55 -06003307bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3308 if (!first_accesses_.size()) return false;
3309 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3310 return tag_range.intersects(first_access_range);
3311}
3312
John Zulauf59e25072020-07-17 10:55:21 -06003313// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003314VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3315 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003316
John Zulaufab7756b2020-12-29 16:10:16 -07003317 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003318 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003319 barriers = read_access.barriers;
3320 break;
John Zulauf59e25072020-07-17 10:55:21 -06003321 }
3322 }
John Zulauf4285ee92020-09-23 10:20:52 -06003323
John Zulauf59e25072020-07-17 10:55:21 -06003324 return barriers;
3325}
3326
Jeremy Gebben40a22942020-12-22 14:22:06 -07003327inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003328 assert(IsRead(usage));
3329 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3330 // * the previous reads are not hazards, and thus last_write must be visible and available to
3331 // any reads that happen after.
3332 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3333 // 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 -07003334 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003335}
3336
Jeremy Gebben40a22942020-12-22 14:22:06 -07003337VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003338 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003339 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003340 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003341 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003342 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003343 // 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 -07003344 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003345 }
3346
3347 return ordered_stages;
3348}
3349
John Zulauf14940722021-04-12 15:19:02 -06003350void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003351 // Only record until we record a write.
3352 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003353 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003354 if (0 == (usage_stage & first_read_stages_)) {
3355 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003356 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003357 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003358 if (0 == (read_execution_barriers & usage_stage)) {
3359 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3360 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3361 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003362 }
3363 }
3364}
3365
John Zulauf4fa68462021-04-26 21:04:22 -06003366void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3367 // Only call this after recording an image layout transition
3368 assert(first_accesses_.size());
3369 if (first_accesses_.back().tag == tag) {
3370 // 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 +02003371 assert(first_accesses_.back().usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
John Zulauf4fa68462021-04-26 21:04:22 -06003372 first_write_layout_ordering_ = layout_ordering;
3373 }
3374}
3375
John Zulaufd1f85d42020-04-15 12:23:15 -06003376void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003377 auto *access_context = GetAccessContextNoInsert(command_buffer);
3378 if (access_context) {
3379 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003380 }
3381}
3382
John Zulaufd1f85d42020-04-15 12:23:15 -06003383void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3384 auto access_found = cb_access_state.find(command_buffer);
3385 if (access_found != cb_access_state.end()) {
3386 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003387 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003388 cb_access_state.erase(access_found);
3389 }
3390}
3391
John Zulauf9cb530d2019-09-30 14:14:10 -06003392bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3393 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3394 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003395 const auto *cb_context = GetAccessContext(commandBuffer);
3396 assert(cb_context);
3397 if (!cb_context) return skip;
3398 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003399
John Zulauf3d84f1b2020-03-09 13:33:25 -06003400 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003401 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3402 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003403
3404 for (uint32_t region = 0; region < regionCount; region++) {
3405 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003406 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003407 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003408 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003409 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003410 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003411 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003412 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003413 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003414 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003415 }
John Zulauf16adfc92020-04-08 10:28:33 -06003416 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003417 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003418 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003419 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003420 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003421 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003422 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003423 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003424 }
3425 }
3426 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003427 }
3428 return skip;
3429}
3430
3431void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3432 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003433 auto *cb_context = GetAccessContext(commandBuffer);
3434 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003435 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003436 auto *context = cb_context->GetCurrentAccessContext();
3437
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003438 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3439 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003440
3441 for (uint32_t region = 0; region < regionCount; region++) {
3442 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003443 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003444 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003445 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003446 }
John Zulauf16adfc92020-04-08 10:28:33 -06003447 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003448 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003449 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003450 }
3451 }
3452}
3453
John Zulauf4a6105a2020-11-17 15:11:05 -07003454void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3455 // Clear out events from the command buffer contexts
3456 for (auto &cb_context : cb_access_state) {
3457 cb_context.second->RecordDestroyEvent(event);
3458 }
3459}
3460
Tony-LunarGef035472021-11-02 10:23:33 -06003461bool SyncValidator::ValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos,
3462 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003463 bool skip = false;
3464 const auto *cb_context = GetAccessContext(commandBuffer);
3465 assert(cb_context);
3466 if (!cb_context) return skip;
3467 const auto *context = cb_context->GetCurrentAccessContext();
Tony-LunarGef035472021-11-02 10:23:33 -06003468 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003469
3470 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003471 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3472 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003473
3474 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3475 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3476 if (src_buffer) {
3477 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003478 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003479 if (hazard.hazard) {
3480 // TODO -- add tag information to log msg when useful.
3481 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003482 "%s(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003483 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003484 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003485 }
3486 }
3487 if (dst_buffer && !skip) {
3488 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003489 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003490 if (hazard.hazard) {
3491 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003492 "%s(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003493 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003494 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003495 }
3496 }
3497 if (skip) break;
3498 }
3499 return skip;
3500}
3501
Tony-LunarGef035472021-11-02 10:23:33 -06003502bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3503 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3504 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3505}
3506
3507bool SyncValidator::PreCallValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) const {
3508 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3509}
3510
3511void SyncValidator::RecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003512 auto *cb_context = GetAccessContext(commandBuffer);
3513 assert(cb_context);
Tony-LunarGef035472021-11-02 10:23:33 -06003514 const auto tag = cb_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003515 auto *context = cb_context->GetCurrentAccessContext();
3516
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003517 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3518 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003519
3520 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3521 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3522 if (src_buffer) {
3523 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003524 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003525 }
3526 if (dst_buffer) {
3527 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003528 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003529 }
3530 }
3531}
3532
Tony-LunarGef035472021-11-02 10:23:33 -06003533void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3534 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3535}
3536
3537void SyncValidator::PreCallRecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) {
3538 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3539}
3540
John Zulauf5c5e88d2019-12-26 11:22:02 -07003541bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3542 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3543 const VkImageCopy *pRegions) const {
3544 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003545 const auto *cb_access_context = GetAccessContext(commandBuffer);
3546 assert(cb_access_context);
3547 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003548
John Zulauf3d84f1b2020-03-09 13:33:25 -06003549 const auto *context = cb_access_context->GetCurrentAccessContext();
3550 assert(context);
3551 if (!context) return skip;
3552
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003553 auto src_image = Get<IMAGE_STATE>(srcImage);
3554 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003555 for (uint32_t region = 0; region < regionCount; region++) {
3556 const auto &copy_region = pRegions[region];
3557 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003558 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003559 copy_region.srcOffset, copy_region.extent);
3560 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003561 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003562 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003563 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003564 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003565 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003566 }
3567
3568 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003569 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003570 copy_region.dstOffset, copy_region.extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003571 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003572 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003573 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003574 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003575 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003576 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003577 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003578 }
3579 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003580
John Zulauf5c5e88d2019-12-26 11:22:02 -07003581 return skip;
3582}
3583
3584void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3585 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3586 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003587 auto *cb_access_context = GetAccessContext(commandBuffer);
3588 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003589 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003590 auto *context = cb_access_context->GetCurrentAccessContext();
3591 assert(context);
3592
Jeremy Gebben9f537102021-10-05 16:37:12 -06003593 auto src_image = Get<IMAGE_STATE>(srcImage);
3594 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003595
3596 for (uint32_t region = 0; region < regionCount; region++) {
3597 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003598 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003599 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003600 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003601 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003602 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003603 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003604 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003605 }
3606 }
3607}
3608
Tony-LunarGb61514a2021-11-02 12:36:51 -06003609bool SyncValidator::ValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo,
3610 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003611 bool skip = false;
3612 const auto *cb_access_context = GetAccessContext(commandBuffer);
3613 assert(cb_access_context);
3614 if (!cb_access_context) return skip;
3615
3616 const auto *context = cb_access_context->GetCurrentAccessContext();
3617 assert(context);
3618 if (!context) return skip;
3619
Tony-LunarGb61514a2021-11-02 12:36:51 -06003620 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003621 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3622 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003623
Jeff Leger178b1e52020-10-05 12:22:23 -04003624 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3625 const auto &copy_region = pCopyImageInfo->pRegions[region];
3626 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003627 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003628 copy_region.srcOffset, copy_region.extent);
3629 if (hazard.hazard) {
3630 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003631 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003632 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003633 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003634 }
3635 }
3636
3637 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003638 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003639 copy_region.dstOffset, copy_region.extent);
Jeff Leger178b1e52020-10-05 12:22:23 -04003640 if (hazard.hazard) {
3641 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003642 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003643 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003644 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003645 }
3646 if (skip) break;
3647 }
3648 }
3649
3650 return skip;
3651}
3652
Tony-LunarGb61514a2021-11-02 12:36:51 -06003653bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3654 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3655 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3656}
3657
3658bool SyncValidator::PreCallValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) const {
3659 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3660}
3661
3662void SyncValidator::RecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003663 auto *cb_access_context = GetAccessContext(commandBuffer);
3664 assert(cb_access_context);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003665 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003666 auto *context = cb_access_context->GetCurrentAccessContext();
3667 assert(context);
3668
Jeremy Gebben9f537102021-10-05 16:37:12 -06003669 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3670 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04003671
3672 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3673 const auto &copy_region = pCopyImageInfo->pRegions[region];
3674 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003675 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003676 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003677 }
3678 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003679 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003680 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003681 }
3682 }
3683}
3684
Tony-LunarGb61514a2021-11-02 12:36:51 -06003685void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3686 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3687}
3688
3689void SyncValidator::PreCallRecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) {
3690 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3691}
3692
John Zulauf9cb530d2019-09-30 14:14:10 -06003693bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3694 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3695 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3696 uint32_t bufferMemoryBarrierCount,
3697 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3698 uint32_t imageMemoryBarrierCount,
3699 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3700 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003701 const auto *cb_access_context = GetAccessContext(commandBuffer);
3702 assert(cb_access_context);
3703 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003704
John Zulauf36ef9282021-02-02 11:47:24 -07003705 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3706 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3707 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3708 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003709 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003710 return skip;
3711}
3712
3713void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3714 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3715 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3716 uint32_t bufferMemoryBarrierCount,
3717 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3718 uint32_t imageMemoryBarrierCount,
3719 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003720 auto *cb_access_context = GetAccessContext(commandBuffer);
3721 assert(cb_access_context);
3722 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003723
John Zulauf1bf30522021-09-03 15:39:06 -06003724 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(),
3725 srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount,
3726 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3727 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06003728}
3729
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003730bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3731 const VkDependencyInfoKHR *pDependencyInfo) const {
3732 bool skip = false;
3733 const auto *cb_access_context = GetAccessContext(commandBuffer);
3734 assert(cb_access_context);
3735 if (!cb_access_context) return skip;
3736
3737 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3738 skip = pipeline_barrier.Validate(*cb_access_context);
3739 return skip;
3740}
3741
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003742bool SyncValidator::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
3743 const VkDependencyInfo *pDependencyInfo) const {
3744 bool skip = false;
3745 const auto *cb_access_context = GetAccessContext(commandBuffer);
3746 assert(cb_access_context);
3747 if (!cb_access_context) return skip;
3748
3749 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3750 skip = pipeline_barrier.Validate(*cb_access_context);
3751 return skip;
3752}
3753
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003754void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3755 auto *cb_access_context = GetAccessContext(commandBuffer);
3756 assert(cb_access_context);
3757 if (!cb_access_context) return;
3758
John Zulauf1bf30522021-09-03 15:39:06 -06003759 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(),
3760 *pDependencyInfo);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003761}
3762
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003763void SyncValidator::PreCallRecordCmdPipelineBarrier2(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) {
3764 auto *cb_access_context = GetAccessContext(commandBuffer);
3765 assert(cb_access_context);
3766 if (!cb_access_context) return;
3767
3768 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(),
3769 *pDependencyInfo);
3770}
3771
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003772void SyncValidator::CreateDevice(const VkDeviceCreateInfo *pCreateInfo) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003773 // The state tracker sets up the device state
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003774 StateTracker::CreateDevice(pCreateInfo);
John Zulauf9cb530d2019-09-30 14:14:10 -06003775
John Zulauf5f13a792020-03-10 07:31:21 -06003776 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3777 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003778 // TODO: Find a good way to do this hooklessly.
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003779 SetCommandBufferResetCallback([this](VkCommandBuffer command_buffer) -> void { ResetCommandBufferCallback(command_buffer); });
3780 SetCommandBufferFreeCallback([this](VkCommandBuffer command_buffer) -> void { FreeCommandBufferCallback(command_buffer); });
John Zulauf9cb530d2019-09-30 14:14:10 -06003781}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003782
John Zulauf355e49b2020-04-24 15:11:15 -06003783bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003784 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003785 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003786 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003787 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003788 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003789 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003790 }
John Zulauf355e49b2020-04-24 15:11:15 -06003791 return skip;
3792}
3793
3794bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3795 VkSubpassContents contents) const {
3796 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003797 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003798 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003799 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003800 return skip;
3801}
3802
3803bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003804 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003805 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003806 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003807 return skip;
3808}
3809
3810bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3811 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003812 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003813 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003814 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003815 return skip;
3816}
3817
John Zulauf3d84f1b2020-03-09 13:33:25 -06003818void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3819 VkResult result) {
3820 // The state tracker sets up the command buffer state
3821 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3822
3823 // Create/initialize the structure that trackers accesses at the command buffer scope.
3824 auto cb_access_context = GetAccessContext(commandBuffer);
3825 assert(cb_access_context);
3826 cb_access_context->Reset();
3827}
3828
3829void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003830 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003831 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003832 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003833 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003834 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003835 }
3836}
3837
3838void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3839 VkSubpassContents contents) {
3840 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003841 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003842 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003843 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003844}
3845
3846void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3847 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3848 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003849 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003850}
3851
3852void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3853 const VkRenderPassBeginInfo *pRenderPassBegin,
3854 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3855 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003856 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003857}
3858
Mike Schuchardt2df08912020-12-15 16:28:09 -08003859bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003860 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003861 bool skip = false;
3862
3863 auto cb_context = GetAccessContext(commandBuffer);
3864 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003865 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003866 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003867 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003868}
3869
3870bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3871 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003872 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003873 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003874 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003875 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3876 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003877 return skip;
3878}
3879
Mike Schuchardt2df08912020-12-15 16:28:09 -08003880bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3881 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003882 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003883 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003884 return skip;
3885}
3886
3887bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3888 const VkSubpassEndInfo *pSubpassEndInfo) const {
3889 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003890 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003891 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003892}
3893
3894void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003895 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003896 auto cb_context = GetAccessContext(commandBuffer);
3897 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003898 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003899
sfricke-samsung85584a72021-09-30 21:43:38 -07003900 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003901 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003902}
3903
3904void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3905 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003906 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003907 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003908 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003909}
3910
3911void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3912 const VkSubpassEndInfo *pSubpassEndInfo) {
3913 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003914 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003915}
3916
3917void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3918 const VkSubpassEndInfo *pSubpassEndInfo) {
3919 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003920 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003921}
3922
sfricke-samsung85584a72021-09-30 21:43:38 -07003923bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3924 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003925 bool skip = false;
3926
3927 auto cb_context = GetAccessContext(commandBuffer);
3928 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003929 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003930
sfricke-samsung85584a72021-09-30 21:43:38 -07003931 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003932 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003933 return skip;
3934}
3935
3936bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3937 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003938 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003939 return skip;
3940}
3941
Mike Schuchardt2df08912020-12-15 16:28:09 -08003942bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003943 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003944 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003945 return skip;
3946}
3947
3948bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003949 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003950 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003951 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003952 return skip;
3953}
3954
sfricke-samsung85584a72021-09-30 21:43:38 -07003955void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003956 // Resolve the all subpass contexts to the command buffer contexts
3957 auto cb_context = GetAccessContext(commandBuffer);
3958 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003959 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003960
sfricke-samsung85584a72021-09-30 21:43:38 -07003961 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003962 sync_op.Record(cb_context);
3963 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003964}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003965
John Zulauf33fc1d52020-07-17 11:01:10 -06003966// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3967// updates to a resource which do not conflict at the byte level.
3968// TODO: Revisit this rule to see if it needs to be tighter or looser
3969// TODO: Add programatic control over suppression heuristics
3970bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3971 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3972}
3973
John Zulauf3d84f1b2020-03-09 13:33:25 -06003974void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003975 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003976 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003977}
3978
3979void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003980 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003981 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003982}
3983
3984void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003985 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003986 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003987}
locke-lunarga19c71d2020-03-02 18:17:04 -07003988
sfricke-samsung71f04e32022-03-16 01:21:21 -05003989template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04003990bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05003991 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
3992 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003993 bool skip = false;
3994 const auto *cb_access_context = GetAccessContext(commandBuffer);
3995 assert(cb_access_context);
3996 if (!cb_access_context) return skip;
3997
Tony Barbour845d29b2021-11-09 11:43:14 -07003998 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003999
locke-lunarga19c71d2020-03-02 18:17:04 -07004000 const auto *context = cb_access_context->GetCurrentAccessContext();
4001 assert(context);
4002 if (!context) return skip;
4003
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004004 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4005 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004006
4007 for (uint32_t region = 0; region < regionCount; region++) {
4008 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07004009 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07004010 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004011 if (src_buffer) {
4012 ResourceAccessRange src_range =
4013 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004014 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07004015 if (hazard.hazard) {
4016 // PHASE1 TODO -- add tag information to log msg when useful.
4017 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
4018 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4019 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004020 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004021 }
4022 }
4023
Jeremy Gebben40a22942020-12-22 14:22:06 -07004024 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07004025 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004026 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004027 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004028 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004029 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004030 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004031 }
4032 if (skip) break;
4033 }
4034 if (skip) break;
4035 }
4036 return skip;
4037}
4038
Jeff Leger178b1e52020-10-05 12:22:23 -04004039bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4040 VkImageLayout dstImageLayout, uint32_t regionCount,
4041 const VkBufferImageCopy *pRegions) const {
4042 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
Tony Barbour845d29b2021-11-09 11:43:14 -07004043 CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004044}
4045
4046bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4047 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4048 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4049 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004050 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4051}
4052
4053bool SyncValidator::PreCallValidateCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4054 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) const {
4055 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4056 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4057 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004058}
4059
sfricke-samsung71f04e32022-03-16 01:21:21 -05004060template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004061void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004062 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4063 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004064 auto *cb_access_context = GetAccessContext(commandBuffer);
4065 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004066
Jeff Leger178b1e52020-10-05 12:22:23 -04004067 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004068 auto *context = cb_access_context->GetCurrentAccessContext();
4069 assert(context);
4070
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004071 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4072 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004073
4074 for (uint32_t region = 0; region < regionCount; region++) {
4075 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07004076 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004077 if (src_buffer) {
4078 ResourceAccessRange src_range =
4079 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004080 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004081 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07004082 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004083 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004084 }
4085 }
4086}
4087
Jeff Leger178b1e52020-10-05 12:22:23 -04004088void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4089 VkImageLayout dstImageLayout, uint32_t regionCount,
4090 const VkBufferImageCopy *pRegions) {
4091 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
Tony Barbour845d29b2021-11-09 11:43:14 -07004092 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004093}
4094
4095void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4096 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4097 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4098 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4099 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004100 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4101}
4102
4103void SyncValidator::PreCallRecordCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4104 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) {
4105 StateTracker::PreCallRecordCmdCopyBufferToImage2(commandBuffer, pCopyBufferToImageInfo);
4106 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4107 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4108 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004109}
4110
sfricke-samsung71f04e32022-03-16 01:21:21 -05004111template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004112bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004113 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
4114 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004115 bool skip = false;
4116 const auto *cb_access_context = GetAccessContext(commandBuffer);
4117 assert(cb_access_context);
4118 if (!cb_access_context) return skip;
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004119 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004120
locke-lunarga19c71d2020-03-02 18:17:04 -07004121 const auto *context = cb_access_context->GetCurrentAccessContext();
4122 assert(context);
4123 if (!context) return skip;
4124
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004125 auto src_image = Get<IMAGE_STATE>(srcImage);
4126 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004127 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004128 for (uint32_t region = 0; region < regionCount; region++) {
4129 const auto &copy_region = pRegions[region];
4130 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004131 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004132 copy_region.imageOffset, copy_region.imageExtent);
4133 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004134 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004135 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004136 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004137 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004138 }
John Zulauf477700e2021-01-06 11:41:49 -07004139 if (dst_mem) {
4140 ResourceAccessRange dst_range =
4141 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004142 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004143 if (hazard.hazard) {
4144 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4145 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4146 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004147 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004148 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004149 }
4150 }
4151 if (skip) break;
4152 }
4153 return skip;
4154}
4155
Jeff Leger178b1e52020-10-05 12:22:23 -04004156bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4157 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4158 const VkBufferImageCopy *pRegions) const {
4159 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004160 CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004161}
4162
4163bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4164 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4165 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4166 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004167 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4168}
4169
4170bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4171 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) const {
4172 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4173 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4174 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004175}
4176
sfricke-samsung71f04e32022-03-16 01:21:21 -05004177template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004178void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004179 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004180 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004181 auto *cb_access_context = GetAccessContext(commandBuffer);
4182 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004183
Jeff Leger178b1e52020-10-05 12:22:23 -04004184 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004185 auto *context = cb_access_context->GetCurrentAccessContext();
4186 assert(context);
4187
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004188 auto src_image = Get<IMAGE_STATE>(srcImage);
Jeremy Gebben9f537102021-10-05 16:37:12 -06004189 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004190 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004191 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004192
4193 for (uint32_t region = 0; region < regionCount; region++) {
4194 const auto &copy_region = pRegions[region];
4195 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004196 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004197 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004198 if (dst_buffer) {
4199 ResourceAccessRange dst_range =
4200 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004201 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004202 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004203 }
4204 }
4205}
4206
Jeff Leger178b1e52020-10-05 12:22:23 -04004207void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4208 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4209 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004210 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004211}
4212
4213void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4214 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4215 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4216 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4217 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004218 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4219}
4220
4221void SyncValidator::PreCallRecordCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4222 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) {
4223 StateTracker::PreCallRecordCmdCopyImageToBuffer2(commandBuffer, pCopyImageToBufferInfo);
4224 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4225 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4226 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004227}
4228
4229template <typename RegionType>
4230bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4231 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4232 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004233 bool skip = false;
4234 const auto *cb_access_context = GetAccessContext(commandBuffer);
4235 assert(cb_access_context);
4236 if (!cb_access_context) return skip;
4237
4238 const auto *context = cb_access_context->GetCurrentAccessContext();
4239 assert(context);
4240 if (!context) return skip;
4241
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004242 auto src_image = Get<IMAGE_STATE>(srcImage);
4243 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004244
4245 for (uint32_t region = 0; region < regionCount; region++) {
4246 const auto &blit_region = pRegions[region];
4247 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004248 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4249 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4250 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4251 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4252 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4253 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004254 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004255 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004256 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004257 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004258 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004259 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004260 }
4261 }
4262
4263 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004264 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4265 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4266 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4267 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4268 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4269 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004270 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004271 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004272 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004273 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004274 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004275 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004276 }
4277 if (skip) break;
4278 }
4279 }
4280
4281 return skip;
4282}
4283
Jeff Leger178b1e52020-10-05 12:22:23 -04004284bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4285 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4286 const VkImageBlit *pRegions, VkFilter filter) const {
4287 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4288 "vkCmdBlitImage");
4289}
4290
4291bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4292 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4293 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4294 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4295 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4296}
4297
Tony-LunarG542ae912021-11-04 16:06:44 -06004298bool SyncValidator::PreCallValidateCmdBlitImage2(VkCommandBuffer commandBuffer,
4299 const VkBlitImageInfo2 *pBlitImageInfo) const {
4300 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4301 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4302 pBlitImageInfo->filter, "vkCmdBlitImage2");
4303}
4304
Jeff Leger178b1e52020-10-05 12:22:23 -04004305template <typename RegionType>
4306void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4307 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4308 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004309 auto *cb_access_context = GetAccessContext(commandBuffer);
4310 assert(cb_access_context);
4311 auto *context = cb_access_context->GetCurrentAccessContext();
4312 assert(context);
4313
Jeremy Gebben9f537102021-10-05 16:37:12 -06004314 auto src_image = Get<IMAGE_STATE>(srcImage);
4315 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004316
4317 for (uint32_t region = 0; region < regionCount; region++) {
4318 const auto &blit_region = pRegions[region];
4319 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004320 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4321 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4322 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4323 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4324 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4325 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004326 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004327 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004328 }
4329 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004330 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4331 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4332 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4333 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4334 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4335 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004336 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004337 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004338 }
4339 }
4340}
locke-lunarg36ba2592020-04-03 09:42:04 -06004341
Jeff Leger178b1e52020-10-05 12:22:23 -04004342void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4343 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4344 const VkImageBlit *pRegions, VkFilter filter) {
4345 auto *cb_access_context = GetAccessContext(commandBuffer);
4346 assert(cb_access_context);
4347 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4348 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4349 pRegions, filter);
4350 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4351}
4352
4353void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4354 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4355 auto *cb_access_context = GetAccessContext(commandBuffer);
4356 assert(cb_access_context);
4357 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4358 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4359 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4360 pBlitImageInfo->filter, tag);
4361}
4362
Tony-LunarG542ae912021-11-04 16:06:44 -06004363void SyncValidator::PreCallRecordCmdBlitImage2(VkCommandBuffer commandBuffer, const VkBlitImageInfo2 *pBlitImageInfo) {
4364 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4365 auto *cb_access_context = GetAccessContext(commandBuffer);
4366 assert(cb_access_context);
4367 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2);
4368 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4369 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4370 pBlitImageInfo->filter, tag);
4371}
4372
John Zulauffaea0ee2021-01-14 14:01:32 -07004373bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4374 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4375 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4376 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004377 bool skip = false;
4378 if (drawCount == 0) return skip;
4379
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004380 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004381 VkDeviceSize size = struct_size;
4382 if (drawCount == 1 || stride == size) {
4383 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004384 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004385 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4386 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004387 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004388 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004389 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004390 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004391 }
4392 } else {
4393 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004394 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004395 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4396 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004397 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004398 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4399 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004400 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004401 break;
4402 }
4403 }
4404 }
4405 return skip;
4406}
4407
John Zulauf14940722021-04-12 15:19:02 -06004408void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004409 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4410 uint32_t stride) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004411 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004412 VkDeviceSize size = struct_size;
4413 if (drawCount == 1 || stride == size) {
4414 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004415 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004416 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004417 } else {
4418 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004419 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004420 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4421 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004422 }
4423 }
4424}
4425
John Zulauffaea0ee2021-01-14 14:01:32 -07004426bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4427 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4428 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004429 bool skip = false;
4430
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004431 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004432 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004433 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4434 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004435 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004436 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004437 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004438 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004439 }
4440 return skip;
4441}
4442
John Zulauf14940722021-04-12 15:19:02 -06004443void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004444 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004445 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004446 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004447}
4448
locke-lunarg36ba2592020-04-03 09:42:04 -06004449bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004450 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004451 const auto *cb_access_context = GetAccessContext(commandBuffer);
4452 assert(cb_access_context);
4453 if (!cb_access_context) return skip;
4454
locke-lunarg61870c22020-06-09 14:51:50 -06004455 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004456 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004457}
4458
4459void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004460 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004461 auto *cb_access_context = GetAccessContext(commandBuffer);
4462 assert(cb_access_context);
4463 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004464
locke-lunarg61870c22020-06-09 14:51:50 -06004465 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004466}
locke-lunarge1a67022020-04-29 00:15:36 -06004467
4468bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004469 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004470 const auto *cb_access_context = GetAccessContext(commandBuffer);
4471 assert(cb_access_context);
4472 if (!cb_access_context) return skip;
4473
4474 const auto *context = cb_access_context->GetCurrentAccessContext();
4475 assert(context);
4476 if (!context) return skip;
4477
locke-lunarg61870c22020-06-09 14:51:50 -06004478 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004479 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4480 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004481 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004482}
4483
4484void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004485 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004486 auto *cb_access_context = GetAccessContext(commandBuffer);
4487 assert(cb_access_context);
4488 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4489 auto *context = cb_access_context->GetCurrentAccessContext();
4490 assert(context);
4491
locke-lunarg61870c22020-06-09 14:51:50 -06004492 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4493 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004494}
4495
4496bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4497 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -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
locke-lunarg61870c22020-06-09 14:51:50 -06004503 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4504 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4505 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004506 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004507}
4508
4509void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4510 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004511 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004512 auto *cb_access_context = GetAccessContext(commandBuffer);
4513 assert(cb_access_context);
4514 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004515
locke-lunarg61870c22020-06-09 14:51:50 -06004516 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4517 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4518 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004519}
4520
4521bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4522 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004523 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004524 const auto *cb_access_context = GetAccessContext(commandBuffer);
4525 assert(cb_access_context);
4526 if (!cb_access_context) return skip;
4527
locke-lunarg61870c22020-06-09 14:51:50 -06004528 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4529 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4530 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004531 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004532}
4533
4534void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4535 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004536 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004537 auto *cb_access_context = GetAccessContext(commandBuffer);
4538 assert(cb_access_context);
4539 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004540
locke-lunarg61870c22020-06-09 14:51:50 -06004541 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4542 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4543 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004544}
4545
4546bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4547 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004548 bool skip = false;
4549 if (drawCount == 0) return skip;
4550
locke-lunargff255f92020-05-13 18:53:52 -06004551 const auto *cb_access_context = GetAccessContext(commandBuffer);
4552 assert(cb_access_context);
4553 if (!cb_access_context) return skip;
4554
4555 const auto *context = cb_access_context->GetCurrentAccessContext();
4556 assert(context);
4557 if (!context) return skip;
4558
locke-lunarg61870c22020-06-09 14:51:50 -06004559 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4560 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004561 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4562 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004563
4564 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4565 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4566 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004567 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004568 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004569}
4570
4571void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4572 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004573 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004574 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004575 auto *cb_access_context = GetAccessContext(commandBuffer);
4576 assert(cb_access_context);
4577 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4578 auto *context = cb_access_context->GetCurrentAccessContext();
4579 assert(context);
4580
locke-lunarg61870c22020-06-09 14:51:50 -06004581 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4582 cb_access_context->RecordDrawSubpassAttachment(tag);
4583 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004584
4585 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4586 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4587 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004588 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004589}
4590
4591bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4592 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004593 bool skip = false;
4594 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004595 const auto *cb_access_context = GetAccessContext(commandBuffer);
4596 assert(cb_access_context);
4597 if (!cb_access_context) return skip;
4598
4599 const auto *context = cb_access_context->GetCurrentAccessContext();
4600 assert(context);
4601 if (!context) return skip;
4602
locke-lunarg61870c22020-06-09 14:51:50 -06004603 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4604 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004605 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4606 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004607
4608 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4609 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4610 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004611 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004612 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004613}
4614
4615void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4616 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004617 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004618 auto *cb_access_context = GetAccessContext(commandBuffer);
4619 assert(cb_access_context);
4620 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4621 auto *context = cb_access_context->GetCurrentAccessContext();
4622 assert(context);
4623
locke-lunarg61870c22020-06-09 14:51:50 -06004624 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4625 cb_access_context->RecordDrawSubpassAttachment(tag);
4626 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004627
4628 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4629 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4630 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004631 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004632}
4633
4634bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4635 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4636 uint32_t stride, const char *function) const {
4637 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004638 const auto *cb_access_context = GetAccessContext(commandBuffer);
4639 assert(cb_access_context);
4640 if (!cb_access_context) return skip;
4641
4642 const auto *context = cb_access_context->GetCurrentAccessContext();
4643 assert(context);
4644 if (!context) return skip;
4645
locke-lunarg61870c22020-06-09 14:51:50 -06004646 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4647 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004648 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4649 maxDrawCount, stride, function);
4650 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004651
4652 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4653 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4654 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004655 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004656 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004657}
4658
4659bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4660 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4661 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004662 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4663 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004664}
4665
sfricke-samsung85584a72021-09-30 21:43:38 -07004666void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4667 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4668 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004669 auto *cb_access_context = GetAccessContext(commandBuffer);
4670 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004671 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004672 auto *context = cb_access_context->GetCurrentAccessContext();
4673 assert(context);
4674
locke-lunarg61870c22020-06-09 14:51:50 -06004675 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4676 cb_access_context->RecordDrawSubpassAttachment(tag);
4677 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4678 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004679
4680 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4681 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4682 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004683 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004684}
4685
sfricke-samsung85584a72021-09-30 21:43:38 -07004686void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4687 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4688 uint32_t stride) {
4689 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4690 stride);
4691 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4692 CMD_DRAWINDIRECTCOUNT);
4693}
locke-lunarge1a67022020-04-29 00:15:36 -06004694bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4695 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4696 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004697 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4698 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004699}
4700
4701void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4702 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4703 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004704 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4705 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004706 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4707 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004708}
4709
4710bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4711 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4712 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004713 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4714 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004715}
4716
4717void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4718 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4719 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004720 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4721 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004722 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4723 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004724}
4725
4726bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4727 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4728 uint32_t stride, const char *function) const {
4729 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004730 const auto *cb_access_context = GetAccessContext(commandBuffer);
4731 assert(cb_access_context);
4732 if (!cb_access_context) return skip;
4733
4734 const auto *context = cb_access_context->GetCurrentAccessContext();
4735 assert(context);
4736 if (!context) return skip;
4737
locke-lunarg61870c22020-06-09 14:51:50 -06004738 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4739 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004740 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4741 offset, maxDrawCount, stride, function);
4742 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004743
4744 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4745 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4746 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004747 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004748 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004749}
4750
4751bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4752 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4753 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004754 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4755 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004756}
4757
sfricke-samsung85584a72021-09-30 21:43:38 -07004758void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4759 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4760 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004761 auto *cb_access_context = GetAccessContext(commandBuffer);
4762 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004763 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004764 auto *context = cb_access_context->GetCurrentAccessContext();
4765 assert(context);
4766
locke-lunarg61870c22020-06-09 14:51:50 -06004767 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4768 cb_access_context->RecordDrawSubpassAttachment(tag);
4769 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4770 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004771
4772 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4773 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004774 // We will update the index and vertex buffer in SubmitQueue in the future.
4775 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004776}
4777
sfricke-samsung85584a72021-09-30 21:43:38 -07004778void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4779 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4780 uint32_t maxDrawCount, uint32_t stride) {
4781 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4782 maxDrawCount, stride);
4783 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4784 CMD_DRAWINDEXEDINDIRECTCOUNT);
4785}
4786
locke-lunarge1a67022020-04-29 00:15:36 -06004787bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4788 VkDeviceSize offset, VkBuffer countBuffer,
4789 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4790 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004791 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4792 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004793}
4794
4795void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4796 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4797 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004798 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4799 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004800 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4801 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004802}
4803
4804bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4805 VkDeviceSize offset, VkBuffer countBuffer,
4806 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4807 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004808 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4809 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004810}
4811
4812void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4813 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4814 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004815 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4816 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004817 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4818 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004819}
4820
4821bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4822 const VkClearColorValue *pColor, uint32_t rangeCount,
4823 const VkImageSubresourceRange *pRanges) const {
4824 bool skip = false;
4825 const auto *cb_access_context = GetAccessContext(commandBuffer);
4826 assert(cb_access_context);
4827 if (!cb_access_context) return skip;
4828
4829 const auto *context = cb_access_context->GetCurrentAccessContext();
4830 assert(context);
4831 if (!context) return skip;
4832
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004833 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004834
4835 for (uint32_t index = 0; index < rangeCount; index++) {
4836 const auto &range = pRanges[index];
4837 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004838 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004839 if (hazard.hazard) {
4840 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004841 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004842 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004843 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004844 }
4845 }
4846 }
4847 return skip;
4848}
4849
4850void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4851 const VkClearColorValue *pColor, uint32_t rangeCount,
4852 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004853 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004854 auto *cb_access_context = GetAccessContext(commandBuffer);
4855 assert(cb_access_context);
4856 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4857 auto *context = cb_access_context->GetCurrentAccessContext();
4858 assert(context);
4859
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004860 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004861
4862 for (uint32_t index = 0; index < rangeCount; index++) {
4863 const auto &range = pRanges[index];
4864 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004865 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004866 }
4867 }
4868}
4869
4870bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4871 VkImageLayout imageLayout,
4872 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4873 const VkImageSubresourceRange *pRanges) const {
4874 bool skip = false;
4875 const auto *cb_access_context = GetAccessContext(commandBuffer);
4876 assert(cb_access_context);
4877 if (!cb_access_context) return skip;
4878
4879 const auto *context = cb_access_context->GetCurrentAccessContext();
4880 assert(context);
4881 if (!context) return skip;
4882
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004883 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004884
4885 for (uint32_t index = 0; index < rangeCount; index++) {
4886 const auto &range = pRanges[index];
4887 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004888 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004889 if (hazard.hazard) {
4890 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004891 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004892 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004893 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004894 }
4895 }
4896 }
4897 return skip;
4898}
4899
4900void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4901 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4902 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004903 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004904 auto *cb_access_context = GetAccessContext(commandBuffer);
4905 assert(cb_access_context);
4906 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4907 auto *context = cb_access_context->GetCurrentAccessContext();
4908 assert(context);
4909
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004910 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004911
4912 for (uint32_t index = 0; index < rangeCount; index++) {
4913 const auto &range = pRanges[index];
4914 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004915 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004916 }
4917 }
4918}
4919
4920bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4921 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4922 VkDeviceSize dstOffset, VkDeviceSize stride,
4923 VkQueryResultFlags flags) const {
4924 bool skip = false;
4925 const auto *cb_access_context = GetAccessContext(commandBuffer);
4926 assert(cb_access_context);
4927 if (!cb_access_context) return skip;
4928
4929 const auto *context = cb_access_context->GetCurrentAccessContext();
4930 assert(context);
4931 if (!context) return skip;
4932
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004933 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004934
4935 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004936 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004937 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004938 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004939 skip |=
4940 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4941 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004942 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004943 }
4944 }
locke-lunargff255f92020-05-13 18:53:52 -06004945
4946 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004947 return skip;
4948}
4949
4950void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4951 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4952 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004953 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4954 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004955 auto *cb_access_context = GetAccessContext(commandBuffer);
4956 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004957 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004958 auto *context = cb_access_context->GetCurrentAccessContext();
4959 assert(context);
4960
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004961 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004962
4963 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004964 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004965 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004966 }
locke-lunargff255f92020-05-13 18:53:52 -06004967
4968 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004969}
4970
4971bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4972 VkDeviceSize size, uint32_t data) const {
4973 bool skip = false;
4974 const auto *cb_access_context = GetAccessContext(commandBuffer);
4975 assert(cb_access_context);
4976 if (!cb_access_context) return skip;
4977
4978 const auto *context = cb_access_context->GetCurrentAccessContext();
4979 assert(context);
4980 if (!context) return skip;
4981
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004982 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004983
4984 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004985 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004986 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004987 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004988 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004989 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004990 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004991 }
4992 }
4993 return skip;
4994}
4995
4996void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4997 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004998 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004999 auto *cb_access_context = GetAccessContext(commandBuffer);
5000 assert(cb_access_context);
5001 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
5002 auto *context = cb_access_context->GetCurrentAccessContext();
5003 assert(context);
5004
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005005 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005006
5007 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005008 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005009 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005010 }
5011}
5012
5013bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5014 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5015 const VkImageResolve *pRegions) const {
5016 bool skip = false;
5017 const auto *cb_access_context = GetAccessContext(commandBuffer);
5018 assert(cb_access_context);
5019 if (!cb_access_context) return skip;
5020
5021 const auto *context = cb_access_context->GetCurrentAccessContext();
5022 assert(context);
5023 if (!context) return skip;
5024
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005025 auto src_image = Get<IMAGE_STATE>(srcImage);
5026 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005027
5028 for (uint32_t region = 0; region < regionCount; region++) {
5029 const auto &resolve_region = pRegions[region];
5030 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005031 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005032 resolve_region.srcOffset, resolve_region.extent);
5033 if (hazard.hazard) {
5034 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005035 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005036 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005037 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005038 }
5039 }
5040
5041 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005042 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005043 resolve_region.dstOffset, resolve_region.extent);
5044 if (hazard.hazard) {
5045 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005046 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005047 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005048 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005049 }
5050 if (skip) break;
5051 }
5052 }
5053
5054 return skip;
5055}
5056
5057void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5058 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5059 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005060 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5061 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005062 auto *cb_access_context = GetAccessContext(commandBuffer);
5063 assert(cb_access_context);
5064 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5065 auto *context = cb_access_context->GetCurrentAccessContext();
5066 assert(context);
5067
Jeremy Gebben9f537102021-10-05 16:37:12 -06005068 auto src_image = Get<IMAGE_STATE>(srcImage);
5069 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005070
5071 for (uint32_t region = 0; region < regionCount; region++) {
5072 const auto &resolve_region = pRegions[region];
5073 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005074 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005075 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005076 }
5077 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005078 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005079 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005080 }
5081 }
5082}
5083
Tony-LunarG562fc102021-11-12 13:58:35 -07005084bool SyncValidator::ValidateCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5085 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04005086 bool skip = false;
5087 const auto *cb_access_context = GetAccessContext(commandBuffer);
5088 assert(cb_access_context);
5089 if (!cb_access_context) return skip;
5090
5091 const auto *context = cb_access_context->GetCurrentAccessContext();
5092 assert(context);
5093 if (!context) return skip;
5094
Tony-LunarG562fc102021-11-12 13:58:35 -07005095 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005096 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5097 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005098
5099 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5100 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5101 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005102 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005103 resolve_region.srcOffset, resolve_region.extent);
5104 if (hazard.hazard) {
5105 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005106 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005107 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005108 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005109 }
5110 }
5111
5112 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005113 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005114 resolve_region.dstOffset, resolve_region.extent);
5115 if (hazard.hazard) {
5116 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005117 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005118 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005119 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005120 }
5121 if (skip) break;
5122 }
5123 }
5124
5125 return skip;
5126}
5127
Tony-LunarG562fc102021-11-12 13:58:35 -07005128bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5129 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5130 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5131}
5132
5133bool SyncValidator::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
5134 const VkResolveImageInfo2 *pResolveImageInfo) const {
5135 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5136}
5137
5138void SyncValidator::RecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5139 CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04005140 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5141 auto *cb_access_context = GetAccessContext(commandBuffer);
5142 assert(cb_access_context);
Tony-LunarG562fc102021-11-12 13:58:35 -07005143 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04005144 auto *context = cb_access_context->GetCurrentAccessContext();
5145 assert(context);
5146
Jeremy Gebben9f537102021-10-05 16:37:12 -06005147 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5148 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005149
5150 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5151 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5152 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005153 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005154 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005155 }
5156 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005157 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005158 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005159 }
5160 }
5161}
5162
Tony-LunarG562fc102021-11-12 13:58:35 -07005163void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5164 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5165 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5166}
5167
5168void SyncValidator::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2 *pResolveImageInfo) {
5169 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5170}
5171
locke-lunarge1a67022020-04-29 00:15:36 -06005172bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5173 VkDeviceSize dataSize, const void *pData) const {
5174 bool skip = false;
5175 const auto *cb_access_context = GetAccessContext(commandBuffer);
5176 assert(cb_access_context);
5177 if (!cb_access_context) return skip;
5178
5179 const auto *context = cb_access_context->GetCurrentAccessContext();
5180 assert(context);
5181 if (!context) return skip;
5182
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005183 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005184
5185 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005186 // VK_WHOLE_SIZE not allowed
5187 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005188 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005189 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005190 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005191 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005192 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005193 }
5194 }
5195 return skip;
5196}
5197
5198void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5199 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005200 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005201 auto *cb_access_context = GetAccessContext(commandBuffer);
5202 assert(cb_access_context);
5203 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5204 auto *context = cb_access_context->GetCurrentAccessContext();
5205 assert(context);
5206
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005207 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005208
5209 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005210 // VK_WHOLE_SIZE not allowed
5211 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005212 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005213 }
5214}
locke-lunargff255f92020-05-13 18:53:52 -06005215
5216bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5217 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5218 bool skip = false;
5219 const auto *cb_access_context = GetAccessContext(commandBuffer);
5220 assert(cb_access_context);
5221 if (!cb_access_context) return skip;
5222
5223 const auto *context = cb_access_context->GetCurrentAccessContext();
5224 assert(context);
5225 if (!context) return skip;
5226
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005227 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005228
5229 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005230 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005231 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005232 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005233 skip |=
5234 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5235 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005236 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005237 }
5238 }
5239 return skip;
5240}
5241
5242void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5243 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005244 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005245 auto *cb_access_context = GetAccessContext(commandBuffer);
5246 assert(cb_access_context);
5247 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5248 auto *context = cb_access_context->GetCurrentAccessContext();
5249 assert(context);
5250
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005251 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005252
5253 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005254 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005255 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005256 }
5257}
John Zulauf49beb112020-11-04 16:06:31 -07005258
5259bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5260 bool skip = false;
5261 const auto *cb_context = GetAccessContext(commandBuffer);
5262 assert(cb_context);
5263 if (!cb_context) return skip;
5264
John Zulauf36ef9282021-02-02 11:47:24 -07005265 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005266 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005267}
5268
5269void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5270 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5271 auto *cb_context = GetAccessContext(commandBuffer);
5272 assert(cb_context);
5273 if (!cb_context) return;
John Zulauf1bf30522021-09-03 15:39:06 -06005274 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005275}
5276
John Zulauf4edde622021-02-15 08:54:50 -07005277bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5278 const VkDependencyInfoKHR *pDependencyInfo) const {
5279 bool skip = false;
5280 const auto *cb_context = GetAccessContext(commandBuffer);
5281 assert(cb_context);
5282 if (!cb_context || !pDependencyInfo) return skip;
5283
5284 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5285 return set_event_op.Validate(*cb_context);
5286}
5287
Tony-LunarGc43525f2021-11-15 16:12:38 -07005288bool SyncValidator::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5289 const VkDependencyInfo *pDependencyInfo) const {
5290 bool skip = false;
5291 const auto *cb_context = GetAccessContext(commandBuffer);
5292 assert(cb_context);
5293 if (!cb_context || !pDependencyInfo) return skip;
5294
5295 SyncOpSetEvent set_event_op(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5296 return set_event_op.Validate(*cb_context);
5297}
5298
John Zulauf4edde622021-02-15 08:54:50 -07005299void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5300 const VkDependencyInfoKHR *pDependencyInfo) {
5301 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5302 auto *cb_context = GetAccessContext(commandBuffer);
5303 assert(cb_context);
5304 if (!cb_context || !pDependencyInfo) return;
5305
John Zulauf1bf30522021-09-03 15:39:06 -06005306 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
John Zulauf4edde622021-02-15 08:54:50 -07005307}
5308
Tony-LunarGc43525f2021-11-15 16:12:38 -07005309void SyncValidator::PostCallRecordCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5310 const VkDependencyInfo *pDependencyInfo) {
5311 StateTracker::PostCallRecordCmdSetEvent2(commandBuffer, event, pDependencyInfo);
5312 auto *cb_context = GetAccessContext(commandBuffer);
5313 assert(cb_context);
5314 if (!cb_context || !pDependencyInfo) return;
5315
5316 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5317}
5318
John Zulauf49beb112020-11-04 16:06:31 -07005319bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5320 VkPipelineStageFlags stageMask) const {
5321 bool skip = false;
5322 const auto *cb_context = GetAccessContext(commandBuffer);
5323 assert(cb_context);
5324 if (!cb_context) return skip;
5325
John Zulauf36ef9282021-02-02 11:47:24 -07005326 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005327 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005328}
5329
5330void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5331 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5332 auto *cb_context = GetAccessContext(commandBuffer);
5333 assert(cb_context);
5334 if (!cb_context) return;
5335
John Zulauf1bf30522021-09-03 15:39:06 -06005336 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005337}
5338
John Zulauf4edde622021-02-15 08:54:50 -07005339bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5340 VkPipelineStageFlags2KHR stageMask) const {
5341 bool skip = false;
5342 const auto *cb_context = GetAccessContext(commandBuffer);
5343 assert(cb_context);
5344 if (!cb_context) return skip;
5345
5346 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5347 return reset_event_op.Validate(*cb_context);
5348}
5349
Tony-LunarGa2662db2021-11-16 07:26:24 -07005350bool SyncValidator::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5351 VkPipelineStageFlags2 stageMask) const {
5352 bool skip = false;
5353 const auto *cb_context = GetAccessContext(commandBuffer);
5354 assert(cb_context);
5355 if (!cb_context) return skip;
5356
5357 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5358 return reset_event_op.Validate(*cb_context);
5359}
5360
John Zulauf4edde622021-02-15 08:54:50 -07005361void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5362 VkPipelineStageFlags2KHR stageMask) {
5363 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5364 auto *cb_context = GetAccessContext(commandBuffer);
5365 assert(cb_context);
5366 if (!cb_context) return;
5367
John Zulauf1bf30522021-09-03 15:39:06 -06005368 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4edde622021-02-15 08:54:50 -07005369}
5370
Tony-LunarGa2662db2021-11-16 07:26:24 -07005371void SyncValidator::PostCallRecordCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags2 stageMask) {
5372 StateTracker::PostCallRecordCmdResetEvent2(commandBuffer, event, stageMask);
5373 auto *cb_context = GetAccessContext(commandBuffer);
5374 assert(cb_context);
5375 if (!cb_context) return;
5376
5377 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5378}
5379
John Zulauf49beb112020-11-04 16:06:31 -07005380bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5381 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5382 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5383 uint32_t bufferMemoryBarrierCount,
5384 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5385 uint32_t imageMemoryBarrierCount,
5386 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5387 bool skip = false;
5388 const auto *cb_context = GetAccessContext(commandBuffer);
5389 assert(cb_context);
5390 if (!cb_context) return skip;
5391
John Zulauf36ef9282021-02-02 11:47:24 -07005392 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5393 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5394 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005395 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005396}
5397
5398void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5399 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5400 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5401 uint32_t bufferMemoryBarrierCount,
5402 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5403 uint32_t imageMemoryBarrierCount,
5404 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5405 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5406 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5407 imageMemoryBarrierCount, pImageMemoryBarriers);
5408
5409 auto *cb_context = GetAccessContext(commandBuffer);
5410 assert(cb_context);
5411 if (!cb_context) return;
5412
John Zulauf1bf30522021-09-03 15:39:06 -06005413 cb_context->RecordSyncOp<SyncOpWaitEvents>(
John Zulauf610e28c2021-08-03 17:46:23 -06005414 CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
John Zulauf1bf30522021-09-03 15:39:06 -06005415 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf4a6105a2020-11-17 15:11:05 -07005416}
5417
John Zulauf4edde622021-02-15 08:54:50 -07005418bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5419 const VkDependencyInfoKHR *pDependencyInfos) const {
5420 bool skip = false;
5421 const auto *cb_context = GetAccessContext(commandBuffer);
5422 assert(cb_context);
5423 if (!cb_context) return skip;
5424
5425 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5426 skip |= wait_events_op.Validate(*cb_context);
5427 return skip;
5428}
5429
5430void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5431 const VkDependencyInfoKHR *pDependencyInfos) {
5432 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5433
5434 auto *cb_context = GetAccessContext(commandBuffer);
5435 assert(cb_context);
5436 if (!cb_context) return;
5437
John Zulauf1bf30522021-09-03 15:39:06 -06005438 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5439 pDependencyInfos);
John Zulauf4edde622021-02-15 08:54:50 -07005440}
5441
Tony-LunarG1364cf52021-11-17 16:10:11 -07005442bool SyncValidator::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5443 const VkDependencyInfo *pDependencyInfos) const {
5444 bool skip = false;
5445 const auto *cb_context = GetAccessContext(commandBuffer);
5446 assert(cb_context);
5447 if (!cb_context) return skip;
5448
5449 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5450 skip |= wait_events_op.Validate(*cb_context);
5451 return skip;
5452}
5453
5454void SyncValidator::PostCallRecordCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5455 const VkDependencyInfo *pDependencyInfos) {
5456 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5457
5458 auto *cb_context = GetAccessContext(commandBuffer);
5459 assert(cb_context);
5460 if (!cb_context) return;
5461
5462 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5463 pDependencyInfos);
5464}
5465
John Zulauf4a6105a2020-11-17 15:11:05 -07005466void SyncEventState::ResetFirstScope() {
5467 for (const auto address_type : kAddressTypes) {
5468 first_scope[static_cast<size_t>(address_type)].clear();
5469 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005470 scope = SyncExecScope();
John Zulauf78b1f892021-09-20 15:02:09 -06005471 first_scope_set = false;
5472 first_scope_tag = 0;
John Zulauf4a6105a2020-11-17 15:11:05 -07005473}
5474
5475// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005476SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005477 IgnoreReason reason = NotIgnored;
5478
Tony-LunarG1364cf52021-11-17 16:10:11 -07005479 if ((CMD_WAITEVENTS2KHR == cmd || CMD_WAITEVENTS2 == cmd) && (CMD_SETEVENT == last_command)) {
John Zulauf4edde622021-02-15 08:54:50 -07005480 reason = SetVsWait2;
5481 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5482 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005483 } else if (unsynchronized_set) {
5484 reason = SetRace;
John Zulauf78b1f892021-09-20 15:02:09 -06005485 } else if (first_scope_set) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005486 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005487 if (missing_bits) reason = MissingStageBits;
5488 }
5489
5490 return reason;
5491}
5492
Jeremy Gebben40a22942020-12-22 14:22:06 -07005493bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005494 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5495 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5496 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005497}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005498
John Zulauf36ef9282021-02-02 11:47:24 -07005499SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5500 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5501 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005502 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5503 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5504 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005505 : SyncOpBase(cmd), barriers_(1) {
5506 auto &barrier_set = barriers_[0];
5507 barrier_set.dependency_flags = dependencyFlags;
5508 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5509 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005510 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005511 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5512 pMemoryBarriers);
5513 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5514 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5515 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5516 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005517}
5518
John Zulauf4edde622021-02-15 08:54:50 -07005519SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5520 const VkDependencyInfoKHR *dep_infos)
5521 : SyncOpBase(cmd), barriers_(event_count) {
5522 for (uint32_t i = 0; i < event_count; i++) {
5523 const auto &dep_info = dep_infos[i];
5524 auto &barrier_set = barriers_[i];
5525 barrier_set.dependency_flags = dep_info.dependencyFlags;
5526 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5527 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5528 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5529 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5530 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5531 dep_info.pMemoryBarriers);
5532 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5533 dep_info.pBufferMemoryBarriers);
5534 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5535 dep_info.pImageMemoryBarriers);
5536 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005537}
5538
John Zulauf36ef9282021-02-02 11:47:24 -07005539SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005540 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5541 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5542 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5543 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5544 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005545 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005546 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5547
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005548SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5549 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005550 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005551
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005552bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5553 bool skip = false;
5554 const auto *context = cb_context.GetCurrentAccessContext();
5555 assert(context);
5556 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005557 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5558
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005559 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005560 const auto &barrier_set = barriers_[0];
5561 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5562 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5563 const auto *image_state = image_barrier.image.get();
5564 if (!image_state) continue;
5565 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5566 if (hazard.hazard) {
5567 // PHASE1 TODO -- add tag information to log msg when useful.
5568 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005569 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005570 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5571 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5572 string_SyncHazard(hazard.hazard), image_barrier.index,
5573 sync_state.report_data->FormatHandle(image_handle).c_str(),
5574 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005575 }
5576 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005577 return skip;
5578}
5579
John Zulaufd5115702021-01-18 12:34:33 -07005580struct SyncOpPipelineBarrierFunctorFactory {
5581 using BarrierOpFunctor = PipelineBarrierOp;
5582 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5583 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5584 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5585 using BufferRange = ResourceAccessRange;
5586 using ImageRange = subresource_adapter::ImageRangeGenerator;
5587 using GlobalRange = ResourceAccessRange;
5588
5589 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5590 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5591 }
John Zulauf14940722021-04-12 15:19:02 -06005592 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005593 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5594 }
5595 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5596 return GlobalBarrierOpFunctor(barrier, false);
5597 }
5598
5599 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5600 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5601 const auto base_address = ResourceBaseAddress(buffer);
5602 return (range + base_address);
5603 }
John Zulauf110413c2021-03-20 05:38:38 -06005604 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005605 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005606
5607 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005608 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005609 return range_gen;
5610 }
5611 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5612};
5613
5614template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005615void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005616 AccessContext *context) {
5617 for (const auto &barrier : barriers) {
5618 const auto *state = barrier.GetState();
5619 if (state) {
5620 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5621 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5622 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5623 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5624 }
5625 }
5626}
5627
5628template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005629void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005630 AccessContext *access_context) {
5631 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5632 for (const auto &barrier : barriers) {
5633 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5634 }
5635 for (const auto address_type : kAddressTypes) {
5636 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5637 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5638 }
5639}
5640
John Zulauf8eda1562021-04-13 17:06:41 -06005641ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005642 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005643 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005644 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf4fa68462021-04-26 21:04:22 -06005645 DoRecord(tag, access_context, events_context);
5646 return tag;
5647}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005648
John Zulauf4fa68462021-04-26 21:04:22 -06005649void SyncOpPipelineBarrier::DoRecord(const ResourceUsageTag tag, AccessContext *access_context,
5650 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005651 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005652 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5653 assert(barriers_.size() == 1);
5654 const auto &barrier_set = barriers_[0];
5655 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5656 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5657 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005658 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005659 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005660 } else {
5661 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005662 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005663 }
5664 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005665}
5666
John Zulauf8eda1562021-04-13 17:06:41 -06005667bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005668 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005669 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5670 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005671 return false;
5672}
5673
John Zulauf4edde622021-02-15 08:54:50 -07005674void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5675 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5676 const VkMemoryBarrier *barriers) {
5677 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005678 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005679 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005680 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005681 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005682 }
5683 if (0 == memory_barrier_count) {
5684 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005685 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005686 }
John Zulauf4edde622021-02-15 08:54:50 -07005687 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005688}
5689
John Zulauf4edde622021-02-15 08:54:50 -07005690void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5691 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5692 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5693 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005694 for (uint32_t index = 0; index < barrier_count; index++) {
5695 const auto &barrier = barriers[index];
Jeremy Gebben9f537102021-10-05 16:37:12 -06005696 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005697 if (buffer) {
5698 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5699 const auto range = MakeRange(barrier.offset, barrier_size);
5700 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005701 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005702 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005703 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005704 }
5705 }
5706}
5707
John Zulauf4edde622021-02-15 08:54:50 -07005708void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005709 uint32_t memory_barrier_count, const VkMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005710 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005711 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005712 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005713 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5714 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5715 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005716 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005717 }
John Zulauf4edde622021-02-15 08:54:50 -07005718 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005719}
5720
John Zulauf4edde622021-02-15 08:54:50 -07005721void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5722 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005723 const VkBufferMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005724 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005725 for (uint32_t index = 0; index < barrier_count; index++) {
5726 const auto &barrier = barriers[index];
5727 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5728 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9f537102021-10-05 16:37:12 -06005729 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005730 if (buffer) {
5731 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5732 const auto range = MakeRange(barrier.offset, barrier_size);
5733 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005734 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005735 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005736 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005737 }
5738 }
5739}
5740
John Zulauf4edde622021-02-15 08:54:50 -07005741void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5742 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5743 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5744 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005745 for (uint32_t index = 0; index < barrier_count; index++) {
5746 const auto &barrier = barriers[index];
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005747 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005748 if (image) {
5749 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5750 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005751 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005752 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005753 image_memory_barriers.emplace_back();
5754 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005755 }
5756 }
5757}
John Zulaufd5115702021-01-18 12:34:33 -07005758
John Zulauf4edde622021-02-15 08:54:50 -07005759void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5760 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005761 const VkImageMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005762 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005763 for (uint32_t index = 0; index < barrier_count; index++) {
5764 const auto &barrier = barriers[index];
5765 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5766 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005767 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005768 if (image) {
5769 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5770 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005771 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005772 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005773 image_memory_barriers.emplace_back();
5774 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005775 }
5776 }
5777}
5778
John Zulauf36ef9282021-02-02 11:47:24 -07005779SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005780 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5781 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5782 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5783 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005784 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005785 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5786 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005787 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005788}
5789
John Zulauf4edde622021-02-15 08:54:50 -07005790SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5791 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5792 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5793 MakeEventsList(sync_state, eventCount, pEvents);
5794 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5795}
5796
John Zulauf610e28c2021-08-03 17:46:23 -06005797const char *const SyncOpWaitEvents::kIgnored = "Wait operation is ignored for this event.";
5798
John Zulaufd5115702021-01-18 12:34:33 -07005799bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005800 bool skip = false;
5801 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005802 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005803
John Zulauf610e28c2021-08-03 17:46:23 -06005804 // This is only interesting at record and not replay (Execute/Submit) time.
John Zulauf4edde622021-02-15 08:54:50 -07005805 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5806 const auto &barrier_set = barriers_[barrier_set_index];
5807 if (barrier_set.single_exec_scope) {
5808 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5809 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5810 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5811 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5812 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5813 } else {
5814 const auto &barriers = barrier_set.memory_barriers;
5815 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5816 const auto &barrier = barriers[barrier_index];
5817 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5818 const std::string vuid =
5819 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5820 skip =
5821 sync_state.LogInfo(command_buffer_handle, vuid,
5822 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5823 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5824 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5825 }
5826 }
5827 }
5828 }
John Zulaufd5115702021-01-18 12:34:33 -07005829 }
5830
John Zulauf610e28c2021-08-03 17:46:23 -06005831 // The rest is common to record time and replay time.
5832 skip |= DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5833 return skip;
5834}
5835
5836bool SyncOpWaitEvents::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
5837 bool skip = false;
5838 const auto &sync_state = cb_context.GetSyncState();
5839
Jeremy Gebben40a22942020-12-22 14:22:06 -07005840 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005841 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005842 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005843 const auto *events_context = cb_context.GetCurrentEventsContext();
5844 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005845 size_t barrier_set_index = 0;
5846 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06005847 for (const auto &event : events_) {
5848 const auto *sync_event = events_context->Get(event.get());
5849 const auto &barrier_set = barriers_[barrier_set_index];
5850 if (!sync_event) {
5851 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5852 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5853 // new validation error... wait without previously submitted set event...
5854 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 -07005855 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06005856 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005857 }
John Zulauf610e28c2021-08-03 17:46:23 -06005858
5859 // For replay calls, don't revalidate "same command buffer" events
5860 if (sync_event->last_command_tag > base_tag) continue;
5861
John Zulauf78394fc2021-07-12 15:41:40 -06005862 const auto event_handle = sync_event->event->event();
5863 // TODO add "destroyed" checks
5864
John Zulauf78b1f892021-09-20 15:02:09 -06005865 if (sync_event->first_scope_set) {
5866 // Only accumulate barrier and event stages if there is a pending set in the current context
5867 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5868 event_stage_masks |= sync_event->scope.mask_param;
5869 }
5870
John Zulauf78394fc2021-07-12 15:41:40 -06005871 const auto &src_exec_scope = barrier_set.src_exec_scope;
John Zulauf78b1f892021-09-20 15:02:09 -06005872
John Zulauf78394fc2021-07-12 15:41:40 -06005873 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5874 if (ignore_reason) {
5875 switch (ignore_reason) {
5876 case SyncEventState::ResetWaitRace:
5877 case SyncEventState::Reset2WaitRace: {
5878 // Four permuations of Reset and Wait calls...
5879 const char *vuid =
5880 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5881 if (ignore_reason == SyncEventState::Reset2WaitRace) {
Tony-LunarG279601c2021-11-16 10:50:51 -07005882 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2-event-03831"
5883 : "VUID-vkCmdResetEvent2-event-03832";
John Zulauf78394fc2021-07-12 15:41:40 -06005884 }
5885 const char *const message =
5886 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5887 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5888 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
John Zulauf610e28c2021-08-03 17:46:23 -06005889 CommandTypeString(sync_event->last_command), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005890 break;
5891 }
5892 case SyncEventState::SetRace: {
5893 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5894 // this event
5895 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5896 const char *const message =
5897 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5898 const char *const reason = "First synchronization scope is undefined.";
5899 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5900 sync_state.report_data->FormatHandle(event_handle).c_str(),
John Zulauf610e28c2021-08-03 17:46:23 -06005901 CommandTypeString(sync_event->last_command), reason, kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005902 break;
5903 }
5904 case SyncEventState::MissingStageBits: {
5905 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5906 // Issue error message that event waited for is not in wait events scope
5907 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5908 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5909 ". Bits missing from srcStageMask %s. %s";
5910 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5911 sync_state.report_data->FormatHandle(event_handle).c_str(),
5912 sync_event->scope.mask_param, src_exec_scope.mask_param,
John Zulauf610e28c2021-08-03 17:46:23 -06005913 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005914 break;
5915 }
5916 case SyncEventState::SetVsWait2: {
Tony-LunarG279601c2021-11-16 10:50:51 -07005917 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2-pEvents-03837",
John Zulauf78394fc2021-07-12 15:41:40 -06005918 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5919 sync_state.report_data->FormatHandle(event_handle).c_str(),
5920 CommandTypeString(sync_event->last_command));
5921 break;
5922 }
5923 default:
5924 assert(ignore_reason == SyncEventState::NotIgnored);
5925 }
5926 } else if (barrier_set.image_memory_barriers.size()) {
5927 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5928 const auto *context = cb_context.GetCurrentAccessContext();
5929 assert(context);
5930 for (const auto &image_memory_barrier : image_memory_barriers) {
5931 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5932 const auto *image_state = image_memory_barrier.image.get();
5933 if (!image_state) continue;
5934 const auto &subresource_range = image_memory_barrier.range;
5935 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5936 const auto hazard =
5937 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5938 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5939 if (hazard.hazard) {
5940 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
5941 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5942 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5943 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
5944 cb_context.FormatUsage(hazard).c_str());
5945 break;
5946 }
5947 }
5948 }
5949 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5950 // 03839
5951 barrier_set_index += barrier_set_incr;
5952 }
John Zulaufd5115702021-01-18 12:34:33 -07005953
5954 // 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 -07005955 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 -07005956 if (extra_stage_bits) {
5957 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005958 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5959 const char *const vuid =
Tony-LunarG279601c2021-11-16 10:50:51 -07005960 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005961 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005962 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulauf610e28c2021-08-03 17:46:23 -06005963 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005964 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005965 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005966 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005967 " vkCmdSetEvent may be in previously submitted command buffer.");
5968 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005969 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005970 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005971 }
5972 }
5973 return skip;
5974}
5975
5976struct SyncOpWaitEventsFunctorFactory {
5977 using BarrierOpFunctor = WaitEventBarrierOp;
5978 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5979 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5980 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5981 using BufferRange = EventSimpleRangeGenerator;
5982 using ImageRange = EventImageRangeGenerator;
5983 using GlobalRange = EventSimpleRangeGenerator;
5984
5985 // Need to restrict to only valid exec and access scope for this event
5986 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5987 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005988 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005989 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5990 return barrier;
5991 }
5992 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5993 auto barrier = RestrictToEvent(barrier_arg);
5994 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5995 }
John Zulauf14940722021-04-12 15:19:02 -06005996 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005997 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5998 }
5999 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
6000 auto barrier = RestrictToEvent(barrier_arg);
6001 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
6002 }
6003
6004 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
6005 const AccessAddressType address_type = GetAccessAddressType(buffer);
6006 const auto base_address = ResourceBaseAddress(buffer);
6007 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
6008 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
6009 return filtered_range_gen;
6010 }
John Zulauf110413c2021-03-20 05:38:38 -06006011 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07006012 if (!SimpleBinding(image)) return ImageRange();
6013 const auto address_type = GetAccessAddressType(image);
6014 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06006015 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07006016 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
6017
6018 return filtered_range_gen;
6019 }
6020 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
6021 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
6022 }
6023 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
6024 SyncEventState *sync_event;
6025};
6026
John Zulauf8eda1562021-04-13 17:06:41 -06006027ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006028 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07006029 auto *access_context = cb_context->GetCurrentAccessContext();
6030 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006031 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07006032 auto *events_context = cb_context->GetCurrentEventsContext();
6033 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006034 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07006035
John Zulauf610e28c2021-08-03 17:46:23 -06006036 DoRecord(tag, access_context, events_context);
6037 return tag;
6038}
6039
6040void SyncOpWaitEvents::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07006041 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
6042 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
6043 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
6044 access_context->ResolvePreviousAccesses();
6045
John Zulauf4edde622021-02-15 08:54:50 -07006046 size_t barrier_set_index = 0;
6047 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
6048 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07006049 for (auto &event_shared : events_) {
6050 if (!event_shared.get()) continue;
6051 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07006052
John Zulauf4edde622021-02-15 08:54:50 -07006053 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006054 sync_event->last_command_tag = tag;
John Zulaufd5115702021-01-18 12:34:33 -07006055
John Zulauf4edde622021-02-15 08:54:50 -07006056 const auto &barrier_set = barriers_[barrier_set_index];
6057 const auto &dst = barrier_set.dst_exec_scope;
6058 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07006059 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
6060 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
6061 // of the barriers is maintained.
6062 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07006063 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
6064 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
6065 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07006066
6067 // Apply the global barrier to the event itself (for race condition tracking)
6068 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
6069 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6070 sync_event->barriers |= dst.exec_scope;
6071 } else {
6072 // We ignored this wait, so we don't have any effective synchronization barriers for it.
6073 sync_event->barriers = 0U;
6074 }
John Zulauf4edde622021-02-15 08:54:50 -07006075 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07006076 }
6077
6078 // Apply the pending barriers
6079 ResolvePendingBarrierFunctor apply_pending_action(tag);
6080 access_context->ApplyToContext(apply_pending_action);
6081}
6082
John Zulauf8eda1562021-04-13 17:06:41 -06006083bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006084 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6085 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006086}
6087
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006088bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6089 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
6090 bool skip = false;
6091 const auto *cb_access_context = GetAccessContext(commandBuffer);
6092 assert(cb_access_context);
6093 if (!cb_access_context) return skip;
6094
6095 const auto *context = cb_access_context->GetCurrentAccessContext();
6096 assert(context);
6097 if (!context) return skip;
6098
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006099 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006100
6101 if (dst_buffer) {
6102 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6103 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
6104 if (hazard.hazard) {
6105 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
6106 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
6107 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06006108 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006109 }
6110 }
6111 return skip;
6112}
6113
John Zulauf669dfd52021-01-27 17:15:28 -07006114void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07006115 events_.reserve(event_count);
6116 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006117 events_.emplace_back(sync_state.Get<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07006118 }
6119}
John Zulauf6ce24372021-01-30 05:56:25 -07006120
John Zulauf36ef9282021-02-02 11:47:24 -07006121SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006122 VkPipelineStageFlags2KHR stageMask)
Jeremy Gebben9f537102021-10-05 16:37:12 -06006123 : SyncOpBase(cmd), event_(sync_state.Get<EVENT_STATE>(event)), exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006124
John Zulauf1bf30522021-09-03 15:39:06 -06006125bool SyncOpResetEvent::Validate(const CommandBufferAccessContext& cb_context) const {
6126 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6127}
6128
6129bool SyncOpResetEvent::DoValidate(const CommandBufferAccessContext & cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006130 auto *events_context = cb_context.GetCurrentEventsContext();
6131 assert(events_context);
6132 bool skip = false;
6133 if (!events_context) return skip;
6134
6135 const auto &sync_state = cb_context.GetSyncState();
6136 const auto *sync_event = events_context->Get(event_);
6137 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6138
John Zulauf1bf30522021-09-03 15:39:06 -06006139 if (sync_event->last_command_tag > base_tag) return skip; // if we validated this in recording of the secondary, don't repeat
6140
John Zulauf6ce24372021-01-30 05:56:25 -07006141 const char *const set_wait =
6142 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6143 "hazards.";
6144 const char *message = set_wait; // Only one message this call.
6145 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
6146 const char *vuid = nullptr;
6147 switch (sync_event->last_command) {
6148 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006149 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006150 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006151 // Needs a barrier between set and reset
6152 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
6153 break;
John Zulauf4edde622021-02-15 08:54:50 -07006154 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006155 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006156 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07006157 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
6158 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
6159 break;
6160 }
6161 default:
6162 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07006163 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
6164 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07006165 break;
6166 }
6167 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006168 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
6169 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006170 CommandTypeString(sync_event->last_command));
6171 }
6172 }
6173 return skip;
6174}
6175
John Zulauf8eda1562021-04-13 17:06:41 -06006176ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
6177 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006178 auto *events_context = cb_context->GetCurrentEventsContext();
6179 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006180 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006181
6182 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06006183 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006184
6185 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07006186 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006187 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006188 sync_event->unsynchronized_set = CMD_NONE;
6189 sync_event->ResetFirstScope();
6190 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06006191
6192 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006193}
6194
John Zulauf8eda1562021-04-13 17:06:41 -06006195bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006196 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf1bf30522021-09-03 15:39:06 -06006197 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006198}
6199
John Zulauf4fa68462021-04-26 21:04:22 -06006200void SyncOpResetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006201
John Zulauf36ef9282021-02-02 11:47:24 -07006202SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006203 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07006204 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006205 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006206 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
6207 dep_info_() {}
6208
6209SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
6210 const VkDependencyInfoKHR &dep_info)
6211 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006212 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006213 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
Tony-LunarG273f32f2021-09-28 08:56:30 -06006214 dep_info_(new safe_VkDependencyInfo(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006215
6216bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf610e28c2021-08-03 17:46:23 -06006217 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6218}
6219bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6220 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6221 assert(active_context);
6222 return DoValidate(*active_context, base_tag);
6223}
6224
6225bool SyncOpSetEvent::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006226 bool skip = false;
6227
6228 const auto &sync_state = cb_context.GetSyncState();
6229 auto *events_context = cb_context.GetCurrentEventsContext();
6230 assert(events_context);
6231 if (!events_context) return skip;
6232
6233 const auto *sync_event = events_context->Get(event_);
6234 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6235
John Zulauf610e28c2021-08-03 17:46:23 -06006236 if (sync_event->last_command_tag >= base_tag) return skip; // for replay we don't want to revalidate internal "last commmand"
6237
John Zulauf6ce24372021-01-30 05:56:25 -07006238 const char *const reset_set =
6239 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6240 "hazards.";
6241 const char *const wait =
6242 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6243
6244 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006245 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006246 const char *message = nullptr;
6247 switch (sync_event->last_command) {
6248 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006249 case CMD_RESETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006250 case CMD_RESETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006251 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006252 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006253 message = reset_set;
6254 break;
6255 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006256 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006257 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006258 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006259 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006260 message = reset_set;
6261 break;
6262 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006263 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006264 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006265 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006266 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006267 message = wait;
6268 break;
6269 default:
6270 // The only other valid last command that wasn't one.
6271 assert(sync_event->last_command == CMD_NONE);
6272 break;
6273 }
John Zulauf4edde622021-02-15 08:54:50 -07006274 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006275 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006276 std::string vuid("SYNC-");
6277 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006278 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6279 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006280 CommandTypeString(sync_event->last_command));
6281 }
6282 }
6283
6284 return skip;
6285}
6286
John Zulauf8eda1562021-04-13 17:06:41 -06006287ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006288 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006289 auto *events_context = cb_context->GetCurrentEventsContext();
6290 auto *access_context = cb_context->GetCurrentAccessContext();
6291 assert(events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006292 if (access_context && events_context) {
6293 DoRecord(tag, access_context, events_context);
6294 }
6295 return tag;
6296}
John Zulauf6ce24372021-01-30 05:56:25 -07006297
John Zulauf610e28c2021-08-03 17:46:23 -06006298void SyncOpSetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006299 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf610e28c2021-08-03 17:46:23 -06006300 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006301
6302 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6303 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6304 // any issues caused by naive scope setting here.
6305
6306 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6307 // Given:
6308 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6309 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6310
6311 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6312 sync_event->unsynchronized_set = sync_event->last_command;
6313 sync_event->ResetFirstScope();
John Zulauf78b1f892021-09-20 15:02:09 -06006314 } else if (!sync_event->first_scope_set) {
John Zulauf6ce24372021-01-30 05:56:25 -07006315 // We only set the scope if there isn't one
6316 sync_event->scope = src_exec_scope_;
6317
6318 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6319 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6320 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6321 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6322 }
6323 };
6324 access_context->ForAll(set_scope);
6325 sync_event->unsynchronized_set = CMD_NONE;
John Zulauf78b1f892021-09-20 15:02:09 -06006326 sync_event->first_scope_set = true;
John Zulauf6ce24372021-01-30 05:56:25 -07006327 sync_event->first_scope_tag = tag;
6328 }
John Zulauf4edde622021-02-15 08:54:50 -07006329 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6330 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006331 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006332 sync_event->barriers = 0U;
6333}
John Zulauf64ffe552021-02-06 10:25:07 -07006334
6335SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6336 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006337 const VkSubpassBeginInfo *pSubpassBeginInfo)
6338 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006339 if (pRenderPassBegin) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006340 rp_state_ = sync_state.Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
John Zulauf64ffe552021-02-06 10:25:07 -07006341 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006342 auto fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07006343 if (fb_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006344 shared_attachments_ = sync_state.GetAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
John Zulauf64ffe552021-02-06 10:25:07 -07006345 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6346 // Note that this a safe to presist as long as shared_attachments is not cleared
6347 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006348 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006349 attachments_.emplace_back(attachment.get());
6350 }
6351 }
6352 if (pSubpassBeginInfo) {
6353 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6354 }
6355 }
6356}
6357
6358bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6359 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6360 bool skip = false;
6361
6362 assert(rp_state_.get());
6363 if (nullptr == rp_state_.get()) return skip;
6364 auto &rp_state = *rp_state_.get();
6365
6366 const uint32_t subpass = 0;
6367
6368 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6369 // hasn't happened yet)
6370 const std::vector<AccessContext> empty_context_vector;
6371 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6372 cb_context.GetCurrentAccessContext());
6373
6374 // Validate attachment operations
6375 if (attachments_.size() == 0) return skip;
6376 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006377
6378 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6379 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6380 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6381 // operations (though it's currently a messy approach)
6382 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6383 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006384
6385 // Validate load operations if there were no layout transition hazards
6386 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07006387 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
6388 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006389 }
6390
6391 return skip;
6392}
6393
John Zulauf8eda1562021-04-13 17:06:41 -06006394ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006395 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6396 assert(rp_state_.get());
John Zulauf41a9c7c2021-12-07 15:59:53 -07006397 if (nullptr == rp_state_.get()) return cb_context->NextCommandTag(cmd_);
6398 return cb_context->RecordBeginRenderPass(cmd_, *rp_state_.get(), renderpass_begin_info_.renderArea, attachments_);
John Zulauf64ffe552021-02-06 10:25:07 -07006399}
6400
John Zulauf8eda1562021-04-13 17:06:41 -06006401bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006402 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006403 return false;
6404}
6405
John Zulauf4fa68462021-04-26 21:04:22 -06006406void SyncOpBeginRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6407}
John Zulauf8eda1562021-04-13 17:06:41 -06006408
John Zulauf64ffe552021-02-06 10:25:07 -07006409SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006410 const VkSubpassEndInfo *pSubpassEndInfo)
6411 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006412 if (pSubpassBeginInfo) {
6413 subpass_begin_info_.initialize(pSubpassBeginInfo);
6414 }
6415 if (pSubpassEndInfo) {
6416 subpass_end_info_.initialize(pSubpassEndInfo);
6417 }
6418}
6419
6420bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6421 bool skip = false;
6422 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6423 if (!renderpass_context) return skip;
6424
6425 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6426 return skip;
6427}
6428
John Zulauf8eda1562021-04-13 17:06:41 -06006429ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006430 return cb_context->RecordNextSubpass(cmd_);
John Zulauf8eda1562021-04-13 17:06:41 -06006431}
6432
6433bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006434 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006435 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006436}
6437
sfricke-samsung85584a72021-09-30 21:43:38 -07006438SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6439 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006440 if (pSubpassEndInfo) {
6441 subpass_end_info_.initialize(pSubpassEndInfo);
6442 }
6443}
6444
John Zulauf4fa68462021-04-26 21:04:22 -06006445void SyncOpNextSubpass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006446
John Zulauf64ffe552021-02-06 10:25:07 -07006447bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6448 bool skip = false;
6449 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6450
6451 if (!renderpass_context) return skip;
6452 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6453 return skip;
6454}
6455
John Zulauf8eda1562021-04-13 17:06:41 -06006456ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006457 return cb_context->RecordEndRenderPass(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006458}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006459
John Zulauf8eda1562021-04-13 17:06:41 -06006460bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006461 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006462 return false;
6463}
6464
John Zulauf4fa68462021-04-26 21:04:22 -06006465void SyncOpEndRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006466
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006467void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6468 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6469 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6470 auto *cb_access_context = GetAccessContext(commandBuffer);
6471 assert(cb_access_context);
6472 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6473 auto *context = cb_access_context->GetCurrentAccessContext();
6474 assert(context);
6475
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006476 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006477
6478 if (dst_buffer) {
6479 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6480 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6481 }
6482}
John Zulaufd05c5842021-03-26 11:32:16 -06006483
John Zulaufae842002021-04-15 18:20:55 -06006484bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6485 const VkCommandBuffer *pCommandBuffers) const {
6486 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6487 const char *func_name = "vkCmdExecuteCommands";
6488 const auto *cb_context = GetAccessContext(commandBuffer);
6489 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006490
6491 // Heavyweight, but we need a proxy copy of the active command buffer access context
6492 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006493
6494 // Make working copies of the access and events contexts
John Zulaufae842002021-04-15 18:20:55 -06006495 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006496 proxy_cb_context.NextIndexedCommandTag(CMD_EXECUTECOMMANDS, cb_index);
6497
John Zulaufae842002021-04-15 18:20:55 -06006498 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6499 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006500
6501 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6502 assert(recorded_context);
6503 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6504
6505 // The barriers have already been applied in ValidatFirstUse
6506 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6507 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006508 }
6509
John Zulaufae842002021-04-15 18:20:55 -06006510 return skip;
6511}
6512
6513void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6514 const VkCommandBuffer *pCommandBuffers) {
6515 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006516 auto *cb_context = GetAccessContext(commandBuffer);
6517 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006518 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006519 cb_context->NextIndexedCommandTag(CMD_EXECUTECOMMANDS, cb_index);
John Zulauf4fa68462021-04-26 21:04:22 -06006520 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6521 if (!recorded_cb_context) continue;
6522 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6523 }
John Zulaufae842002021-04-15 18:20:55 -06006524}
6525
John Zulaufd0ec59f2021-03-13 14:25:08 -07006526AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6527 : view_(view), view_mask_(), gen_store_() {
6528 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6529 const IMAGE_STATE &image_state = *view_->image_state.get();
6530 const auto base_address = ResourceBaseAddress(image_state);
6531 const auto *encoder = image_state.fragment_encoder.get();
6532 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006533 // Get offset and extent for the view, accounting for possible depth slicing
6534 const VkOffset3D zero_offset = view->GetOffset();
6535 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006536 // Intentional copy
6537 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6538 view_mask_ = subres_range.aspectMask;
6539 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6540 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6541
6542 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6543 if (depth && (depth != view_mask_)) {
6544 subres_range.aspectMask = depth;
6545 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6546 }
6547 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6548 if (stencil && (stencil != view_mask_)) {
6549 subres_range.aspectMask = stencil;
6550 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6551 }
6552}
6553
6554const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6555 const ImageRangeGen *got = nullptr;
6556 switch (gen_type) {
6557 case kViewSubresource:
6558 got = &gen_store_[kViewSubresource];
6559 break;
6560 case kRenderArea:
6561 got = &gen_store_[kRenderArea];
6562 break;
6563 case kDepthOnlyRenderArea:
6564 got =
6565 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6566 break;
6567 case kStencilOnlyRenderArea:
6568 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6569 : &gen_store_[Gen::kStencilOnlyRenderArea];
6570 break;
6571 default:
6572 assert(got);
6573 }
6574 return got;
6575}
6576
6577AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6578 assert(IsValid());
6579 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6580 if (depth_op) {
6581 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6582 if (stencil_op) {
6583 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6584 return kRenderArea;
6585 }
6586 return kDepthOnlyRenderArea;
6587 }
6588 if (stencil_op) {
6589 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6590 return kStencilOnlyRenderArea;
6591 }
6592
6593 assert(depth_op || stencil_op);
6594 return kRenderArea;
6595}
6596
6597AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006598
6599void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6600 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6601 for (auto &event_pair : map_) {
6602 assert(event_pair.second); // Shouldn't be storing empty
6603 auto &sync_event = *event_pair.second;
6604 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6605 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6606 sync_event.barriers |= dst.exec_scope;
6607 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6608 }
6609 }
6610}