blob: 1d13136112efcdd8a407076fa52b97f0ad4cc345 [file] [log] [blame]
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001/* Copyright (c) 2019-2022 The Khronos Group Inc.
2 * Copyright (c) 2019-2022 Valve Corporation
3 * Copyright (c) 2019-2022 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
John Zulaufab7756b2020-12-29 16:10:16 -070018 * Author: Locke Lin <locke@lunarg.com>
19 * Author: Jeremy Gebben <jeremyg@lunarg.com>
John Zulauf9cb530d2019-09-30 14:14:10 -060020 */
21
22#include <limits>
23#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060024#include <memory>
25#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060026#include "synchronization_validation.h"
Jeremy Gebben5f585ae2021-02-02 09:03:06 -070027#include "sync_utils.h"
John Zulauf9cb530d2019-09-30 14:14:10 -060028
Jeremy Gebben6fbf8242021-06-21 09:14:46 -060029static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.Binding(); }
John Zulauf264cce02021-02-05 14:40:47 -070030
John Zulauf29d00532021-03-04 13:28:54 -070031static bool SimpleBinding(const IMAGE_STATE &image_state) {
Jeremy Gebben62c3bf42021-07-21 15:38:24 -060032 bool simple =
Jeremy Gebben82e11d52021-07-26 09:19:37 -060033 SimpleBinding(static_cast<const BINDABLE &>(image_state)) || image_state.IsSwapchainImage() || image_state.bind_swapchain;
John Zulauf29d00532021-03-04 13:28:54 -070034
35 // If it's not simple we must have an encoder.
36 assert(!simple || image_state.fragment_encoder.get());
37 return simple;
38}
39
John Zulauf4fa68462021-04-26 21:04:22 -060040static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
41static const std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
John Zulauf43cc7462020-12-03 12:33:12 -070042 AccessAddressType::kLinear, AccessAddressType::kIdealized};
43
John Zulaufd5115702021-01-18 12:34:33 -070044static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070045static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
46 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
47}
John Zulaufd5115702021-01-18 12:34:33 -070048
John Zulauf9cb530d2019-09-30 14:14:10 -060049static const char *string_SyncHazardVUID(SyncHazard hazard) {
50 switch (hazard) {
51 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070052 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060053 break;
54 case SyncHazard::READ_AFTER_WRITE:
55 return "SYNC-HAZARD-READ_AFTER_WRITE";
56 break;
57 case SyncHazard::WRITE_AFTER_READ:
58 return "SYNC-HAZARD-WRITE_AFTER_READ";
59 break;
60 case SyncHazard::WRITE_AFTER_WRITE:
61 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
62 break;
John Zulauf2f952d22020-02-10 11:34:51 -070063 case SyncHazard::READ_RACING_WRITE:
64 return "SYNC-HAZARD-READ-RACING-WRITE";
65 break;
66 case SyncHazard::WRITE_RACING_WRITE:
67 return "SYNC-HAZARD-WRITE-RACING-WRITE";
68 break;
69 case SyncHazard::WRITE_RACING_READ:
70 return "SYNC-HAZARD-WRITE-RACING-READ";
71 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060072 default:
73 assert(0);
74 }
75 return "SYNC-HAZARD-INVALID";
76}
77
John Zulauf59e25072020-07-17 10:55:21 -060078static bool IsHazardVsRead(SyncHazard hazard) {
79 switch (hazard) {
80 case SyncHazard::NONE:
81 return false;
82 break;
83 case SyncHazard::READ_AFTER_WRITE:
84 return false;
85 break;
86 case SyncHazard::WRITE_AFTER_READ:
87 return true;
88 break;
89 case SyncHazard::WRITE_AFTER_WRITE:
90 return false;
91 break;
92 case SyncHazard::READ_RACING_WRITE:
93 return false;
94 break;
95 case SyncHazard::WRITE_RACING_WRITE:
96 return false;
97 break;
98 case SyncHazard::WRITE_RACING_READ:
99 return true;
100 break;
101 default:
102 assert(0);
103 }
104 return false;
105}
106
John Zulauf9cb530d2019-09-30 14:14:10 -0600107static const char *string_SyncHazard(SyncHazard hazard) {
108 switch (hazard) {
109 case SyncHazard::NONE:
110 return "NONR";
111 break;
112 case SyncHazard::READ_AFTER_WRITE:
113 return "READ_AFTER_WRITE";
114 break;
115 case SyncHazard::WRITE_AFTER_READ:
116 return "WRITE_AFTER_READ";
117 break;
118 case SyncHazard::WRITE_AFTER_WRITE:
119 return "WRITE_AFTER_WRITE";
120 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700121 case SyncHazard::READ_RACING_WRITE:
122 return "READ_RACING_WRITE";
123 break;
124 case SyncHazard::WRITE_RACING_WRITE:
125 return "WRITE_RACING_WRITE";
126 break;
127 case SyncHazard::WRITE_RACING_READ:
128 return "WRITE_RACING_READ";
129 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600130 default:
131 assert(0);
132 }
133 return "INVALID HAZARD";
134}
135
John Zulauf37ceaed2020-07-03 16:18:15 -0600136static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
137 // Return the info for the first bit found
138 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700139 for (size_t i = 0; i < flags.size(); i++) {
140 if (flags.test(i)) {
141 info = &syncStageAccessInfoByStageAccessIndex[i];
142 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600143 }
144 }
145 return info;
146}
147
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700148static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600149 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700150 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600151 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700152 } else {
153 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
154 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
155 if ((flags & info.stage_access_bit).any()) {
156 if (!out_str.empty()) {
157 out_str.append(sep);
158 }
159 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600160 }
John Zulauf59e25072020-07-17 10:55:21 -0600161 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700162 if (out_str.length() == 0) {
163 out_str.append("Unhandled SyncStageAccess");
164 }
John Zulauf59e25072020-07-17 10:55:21 -0600165 }
166 return out_str;
167}
168
John Zulauf14940722021-04-12 15:19:02 -0600169static std::string string_UsageTag(const ResourceUsageRecord &tag) {
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700170 std::stringstream out;
171
John Zulauffaea0ee2021-01-14 14:01:32 -0700172 out << "command: " << CommandTypeString(tag.command);
173 out << ", seq_no: " << tag.seq_num;
174 if (tag.sub_command != 0) {
175 out << ", subcmd: " << tag.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700176 }
177 return out.str();
178}
John Zulauf4fa68462021-04-26 21:04:22 -0600179static std::string string_UsageIndex(SyncStageAccessIndex usage_index) {
180 const char *stage_access_name = "INVALID_STAGE_ACCESS";
181 if (usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size())) {
182 stage_access_name = syncStageAccessInfoByStageAccessIndex[usage_index].name;
183 }
184 return std::string(stage_access_name);
185}
186
187struct NoopBarrierAction {
188 explicit NoopBarrierAction() {}
189 void operator()(ResourceAccessState *access) const {}
John Zulauf5c628d02021-05-04 15:46:36 -0600190 const bool layout_transition = false;
John Zulauf4fa68462021-04-26 21:04:22 -0600191};
192
193// NOTE: Make sure the proxy doesn't outlive from, as the proxy is pointing directly to access contexts owned by from.
194CommandBufferAccessContext::CommandBufferAccessContext(const CommandBufferAccessContext &from, AsProxyContext dummy)
195 : CommandBufferAccessContext(from.sync_state_) {
196 // Copy only the needed fields out of from for a temporary, proxy command buffer context
197 cb_state_ = from.cb_state_;
198 queue_flags_ = from.queue_flags_;
199 destroyed_ = from.destroyed_;
200 access_log_ = from.access_log_; // potentially large, but no choice given tagging lookup.
201 command_number_ = from.command_number_;
202 subcommand_number_ = from.subcommand_number_;
203 reset_count_ = from.reset_count_;
204
205 const auto *from_context = from.GetCurrentAccessContext();
206 assert(from_context);
207
208 // Construct a fully resolved single access context out of from
209 const NoopBarrierAction noop_barrier;
210 for (AccessAddressType address_type : kAddressTypes) {
211 from_context->ResolveAccessRange(address_type, kFullRange, noop_barrier,
212 &cb_access_context_.GetAccessStateMap(address_type), nullptr);
213 }
214 // The proxy has flatten the current render pass context (if any), but the async contexts are needed for hazard detection
215 cb_access_context_.ImportAsyncContexts(*from_context);
216
217 events_context_ = from.events_context_;
218
219 // We don't want to copy the full render_pass_context_ history just for the proxy.
220}
221
222std::string CommandBufferAccessContext::FormatUsage(const ResourceUsageTag tag) const {
223 std::stringstream out;
224 assert(tag < access_log_.size());
225 const auto &record = access_log_[tag];
226 out << string_UsageTag(record);
227 if (record.cb_state != cb_state_.get()) {
228 out << ", command_buffer: " << sync_state_->report_data->FormatHandle(record.cb_state->commandBuffer()).c_str();
229 if (record.cb_state->Destroyed()) {
230 out << " (destroyed)";
231 }
232
John Zulauf4fa68462021-04-26 21:04:22 -0600233 }
John Zulaufd142c9a2022-04-12 14:22:44 -0600234 out << ", reset_no: " << std::to_string(record.reset_count);
John Zulauf4fa68462021-04-26 21:04:22 -0600235 return out.str();
236}
237std::string CommandBufferAccessContext::FormatUsage(const ResourceFirstAccess &access) const {
238 std::stringstream out;
239 out << "(recorded_usage: " << string_UsageIndex(access.usage_index);
240 out << ", " << FormatUsage(access.tag) << ")";
241 return out.str();
242}
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700243
John Zulauffaea0ee2021-01-14 14:01:32 -0700244std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600245 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600246 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
247 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600248 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600249 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
250 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf4fa68462021-04-26 21:04:22 -0600251 out << "(";
252 if (!hazard.recorded_access.get()) {
253 // if we have a recorded usage the usage is reported from the recorded contexts point of view
254 out << "usage: " << usage_info.name << ", ";
255 }
256 out << "prior_usage: " << stage_access_name;
John Zulauf59e25072020-07-17 10:55:21 -0600257 if (IsHazardVsRead(hazard.hazard)) {
258 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700259 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600260 } else {
261 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
262 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
263 }
264
ziga-lunarg0f248902022-03-24 16:42:45 +0100265 if (tag < access_log_.size()) {
266 out << ", " << FormatUsage(tag) << ")";
267 }
John Zulauf1dae9192020-06-16 15:46:44 -0600268 return out.str();
269}
270
John Zulaufd14743a2020-07-03 09:42:39 -0600271// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
272// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
273// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700274static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700275static const SyncStageAccessFlags kColorAttachmentAccessScope =
276 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
277 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
278 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
279 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700280static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
281 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT_KHR | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700282static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
283 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
284 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
285 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700286static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700287static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600288
John Zulauf8e3c3e92021-01-06 11:19:36 -0700289ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700290 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700291 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
292 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
293 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
294
John Zulaufee984022022-04-13 16:39:50 -0600295// Sometimes we have an internal access conflict, and we using the kInvalidTag to set and detect in temporary/proxy contexts
296static const ResourceUsageTag kInvalidTag(ResourceUsageRecord::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600297
Jeremy Gebben62c3bf42021-07-21 15:38:24 -0600298static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) { return bindable.GetFakeBaseAddress(); }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600299
locke-lunarg3c038002020-04-30 23:08:08 -0600300inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
301 if (size == VK_WHOLE_SIZE) {
302 return (whole_size - offset);
303 }
304 return size;
305}
306
John Zulauf3e86bf02020-09-12 10:47:57 -0600307static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
308 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
309}
310
John Zulauf16adfc92020-04-08 10:28:33 -0600311template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600312static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600313 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
314}
315
John Zulauf355e49b2020-04-24 15:11:15 -0600316static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600317
John Zulauf3e86bf02020-09-12 10:47:57 -0600318static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
319 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
320}
321
322static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
323 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
324}
325
John Zulauf4a6105a2020-11-17 15:11:05 -0700326// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
327//
John Zulauf10f1f522020-12-18 12:00:35 -0700328// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
329//
John Zulauf4a6105a2020-11-17 15:11:05 -0700330// Usage:
331// Constructor() -- initializes the generator to point to the begin of the space declared.
332// * -- the current range of the generator empty signfies end
333// ++ -- advance to the next non-empty range (or end)
334
335// A wrapper for a single range with the same semantics as the actual generators below
336template <typename KeyType>
337class SingleRangeGenerator {
338 public:
339 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700340 const KeyType &operator*() const { return current_; }
341 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700342 SingleRangeGenerator &operator++() {
343 current_ = KeyType(); // just one real range
344 return *this;
345 }
346
347 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
348
349 private:
350 SingleRangeGenerator() = default;
351 const KeyType range_;
352 KeyType current_;
353};
354
John Zulaufae842002021-04-15 18:20:55 -0600355// Generate the ranges that are the intersection of range and the entries in the RangeMap
356template <typename RangeMap, typename KeyType = typename RangeMap::key_type>
357class MapRangesRangeGenerator {
John Zulauf4a6105a2020-11-17 15:11:05 -0700358 public:
John Zulaufd5115702021-01-18 12:34:33 -0700359 // Default constructed is safe to dereference for "empty" test, but for no other operation.
John Zulaufae842002021-04-15 18:20:55 -0600360 MapRangesRangeGenerator() : range_(), map_(nullptr), map_pos_(), current_() {
John Zulaufd5115702021-01-18 12:34:33 -0700361 // Default construction for KeyType *must* be empty range
362 assert(current_.empty());
363 }
John Zulaufae842002021-04-15 18:20:55 -0600364 MapRangesRangeGenerator(const RangeMap &filter, const KeyType &range) : range_(range), map_(&filter), map_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700365 SeekBegin();
366 }
John Zulaufae842002021-04-15 18:20:55 -0600367 MapRangesRangeGenerator(const MapRangesRangeGenerator &from) = default;
John Zulaufd5115702021-01-18 12:34:33 -0700368
John Zulauf4a6105a2020-11-17 15:11:05 -0700369 const KeyType &operator*() const { return current_; }
370 const KeyType *operator->() const { return &current_; }
John Zulaufae842002021-04-15 18:20:55 -0600371 MapRangesRangeGenerator &operator++() {
372 ++map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700373 UpdateCurrent();
374 return *this;
375 }
376
John Zulaufae842002021-04-15 18:20:55 -0600377 bool operator==(const MapRangesRangeGenerator &other) const { return current_ == other.current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700378
John Zulaufae842002021-04-15 18:20:55 -0600379 protected:
John Zulauf4a6105a2020-11-17 15:11:05 -0700380 void UpdateCurrent() {
John Zulaufae842002021-04-15 18:20:55 -0600381 if (map_pos_ != map_->cend()) {
382 current_ = range_ & map_pos_->first;
John Zulauf4a6105a2020-11-17 15:11:05 -0700383 } else {
384 current_ = KeyType();
385 }
386 }
387 void SeekBegin() {
John Zulaufae842002021-04-15 18:20:55 -0600388 map_pos_ = map_->lower_bound(range_);
John Zulauf4a6105a2020-11-17 15:11:05 -0700389 UpdateCurrent();
390 }
John Zulaufae842002021-04-15 18:20:55 -0600391
392 // Adding this functionality here, to avoid gratuitous Base:: qualifiers in the derived class
393 // Note: Not exposed in this classes public interface to encourage using a consistent ++/empty generator semantic
394 template <typename Pred>
395 MapRangesRangeGenerator &PredicatedIncrement(Pred &pred) {
396 do {
397 ++map_pos_;
398 } while (map_pos_ != map_->cend() && map_pos_->first.intersects(range_) && !pred(map_pos_));
399 UpdateCurrent();
400 return *this;
401 }
402
John Zulauf4a6105a2020-11-17 15:11:05 -0700403 const KeyType range_;
John Zulaufae842002021-04-15 18:20:55 -0600404 const RangeMap *map_;
405 typename RangeMap::const_iterator map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700406 KeyType current_;
407};
John Zulaufd5115702021-01-18 12:34:33 -0700408using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulaufae842002021-04-15 18:20:55 -0600409using EventSimpleRangeGenerator = MapRangesRangeGenerator<SyncEventState::ScopeMap>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700410
John Zulaufae842002021-04-15 18:20:55 -0600411// Generate the ranges for entries meeting the predicate that are the intersection of range and the entries in the RangeMap
412template <typename RangeMap, typename Predicate, typename KeyType = typename RangeMap::key_type>
413class PredicatedMapRangesRangeGenerator : public MapRangesRangeGenerator<RangeMap, KeyType> {
414 public:
415 using Base = MapRangesRangeGenerator<RangeMap, KeyType>;
416 // Default constructed is safe to dereference for "empty" test, but for no other operation.
417 PredicatedMapRangesRangeGenerator() : Base(), pred_() {}
418 PredicatedMapRangesRangeGenerator(const RangeMap &filter, const KeyType &range, Predicate pred)
419 : Base(filter, range), pred_(pred) {}
420 PredicatedMapRangesRangeGenerator(const PredicatedMapRangesRangeGenerator &from) = default;
421
422 PredicatedMapRangesRangeGenerator &operator++() {
423 Base::PredicatedIncrement(pred_);
424 return *this;
425 }
426
427 protected:
428 Predicate pred_;
429};
John Zulauf4a6105a2020-11-17 15:11:05 -0700430
431// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulaufae842002021-04-15 18:20:55 -0600432// Templated to allow for different Range generators or map sources...
433template <typename RangeMap, typename RangeGen, typename KeyType = typename RangeMap::key_type>
John Zulauf4a6105a2020-11-17 15:11:05 -0700434class FilteredGeneratorGenerator {
435 public:
John Zulaufd5115702021-01-18 12:34:33 -0700436 // Default constructed is safe to dereference for "empty" test, but for no other operation.
437 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
438 // Default construction for KeyType *must* be empty range
439 assert(current_.empty());
440 }
John Zulaufae842002021-04-15 18:20:55 -0600441 FilteredGeneratorGenerator(const RangeMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700442 SeekBegin();
443 }
John Zulaufd5115702021-01-18 12:34:33 -0700444 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700445 const KeyType &operator*() const { return current_; }
446 const KeyType *operator->() const { return &current_; }
447 FilteredGeneratorGenerator &operator++() {
448 KeyType gen_range = GenRange();
449 KeyType filter_range = FilterRange();
450 current_ = KeyType();
451 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
452 if (gen_range.end > filter_range.end) {
453 // if the generated range is beyond the filter_range, advance the filter range
454 filter_range = AdvanceFilter();
455 } else {
456 gen_range = AdvanceGen();
457 }
458 current_ = gen_range & filter_range;
459 }
460 return *this;
461 }
462
463 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
464
465 private:
466 KeyType AdvanceFilter() {
467 ++filter_pos_;
468 auto filter_range = FilterRange();
469 if (filter_range.valid()) {
470 FastForwardGen(filter_range);
471 }
472 return filter_range;
473 }
474 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700475 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700476 auto gen_range = GenRange();
477 if (gen_range.valid()) {
478 FastForwardFilter(gen_range);
479 }
480 return gen_range;
481 }
482
483 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700484 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700485
486 KeyType FastForwardFilter(const KeyType &range) {
487 auto filter_range = FilterRange();
488 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700489 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700490 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
491 if (retry_count < kRetryLimit) {
492 ++filter_pos_;
493 filter_range = FilterRange();
494 retry_count++;
495 } else {
496 // Okay we've tried walking, do a seek.
497 filter_pos_ = filter_->lower_bound(range);
498 break;
499 }
500 }
501 return FilterRange();
502 }
503
504 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
505 // faster.
506 KeyType FastForwardGen(const KeyType &range) {
507 auto gen_range = GenRange();
508 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700509 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700510 gen_range = GenRange();
511 }
512 return gen_range;
513 }
514
515 void SeekBegin() {
516 auto gen_range = GenRange();
517 if (gen_range.empty()) {
518 current_ = KeyType();
519 filter_pos_ = filter_->cend();
520 } else {
521 filter_pos_ = filter_->lower_bound(gen_range);
522 current_ = gen_range & FilterRange();
523 }
524 }
525
John Zulaufae842002021-04-15 18:20:55 -0600526 const RangeMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700527 RangeGen gen_;
John Zulaufae842002021-04-15 18:20:55 -0600528 typename RangeMap::const_iterator filter_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700529 KeyType current_;
530};
531
532using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
533
John Zulauf5c5e88d2019-12-26 11:22:02 -0700534
John Zulauf3e86bf02020-09-12 10:47:57 -0600535ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
536 VkDeviceSize stride) {
537 VkDeviceSize range_start = offset + first_index * stride;
538 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600539 if (count == UINT32_MAX) {
540 range_size = buf_whole_size - range_start;
541 } else {
542 range_size = count * stride;
543 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600544 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600545}
546
locke-lunarg654e3692020-06-04 17:19:15 -0600547SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
548 VkShaderStageFlagBits stage_flag) {
549 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
550 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
551 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
552 }
553 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
554 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
555 assert(0);
556 }
557 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
558 return stage_access->second.uniform_read;
559 }
560
561 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
562 // Because if write hazard happens, read hazard might or might not happen.
563 // But if write hazard doesn't happen, read hazard is impossible to happen.
564 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700565 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600566 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700567 // TODO: sampled_read
568 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600569}
570
locke-lunarg37047832020-06-12 13:44:45 -0600571bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
572 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
573 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
574 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
575 ? true
576 : false;
577}
578
579bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
580 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
581 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
582 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
583 ? true
584 : false;
585}
586
John Zulauf355e49b2020-04-24 15:11:15 -0600587// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600588template <typename Action>
589static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
590 Action &action) {
591 // At this point the "apply over range" logic only supports a single memory binding
592 if (!SimpleBinding(image_state)) return;
593 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600594 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700595 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
596 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600597 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700598 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600599 }
600}
601
John Zulauf7635de32020-05-29 17:14:15 -0600602// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
603// Used by both validation and record operations
604//
605// The signature for Action() reflect the needs of both uses.
606template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700607void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
608 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600609 const auto &rp_ci = rp_state.createInfo;
610 const auto *attachment_ci = rp_ci.pAttachments;
611 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
612
613 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
614 const auto *color_attachments = subpass_ci.pColorAttachments;
615 const auto *color_resolve = subpass_ci.pResolveAttachments;
616 if (color_resolve && color_attachments) {
617 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
618 const auto &color_attach = color_attachments[i].attachment;
619 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
620 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
621 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700622 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
623 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600624 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700625 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
626 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600627 }
628 }
629 }
630
631 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700632 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600633 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
634 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
635 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
636 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
637 const auto src_ci = attachment_ci[src_at];
638 // The formats are required to match so we can pick either
639 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
640 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
641 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600642
643 // Figure out which aspects are actually touched during resolve operations
644 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700645 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600646 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600647 aspect_string = "depth/stencil";
648 } else if (resolve_depth) {
649 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700650 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600651 aspect_string = "depth";
652 } else if (resolve_stencil) {
653 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700654 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600655 aspect_string = "stencil";
656 }
657
John Zulaufd0ec59f2021-03-13 14:25:08 -0700658 if (aspect_string) {
659 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
660 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
661 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
662 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600663 }
664 }
665}
666
667// Action for validating resolve operations
668class ValidateResolveAction {
669 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700670 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulaufbb890452021-12-14 11:30:18 -0700671 const CommandExecutionContext &exec_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600672 : render_pass_(render_pass),
673 subpass_(subpass),
674 context_(context),
John Zulaufbb890452021-12-14 11:30:18 -0700675 exec_context_(exec_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 Zulaufbb890452021-12-14 11:30:18 -0700685 exec_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
686 "%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,
689 attachment_name, src_at, dst_at, exec_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 Zulaufbb890452021-12-14 11:30:18 -0700699 const CommandExecutionContext &exec_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600700 const char *func_name_;
701 bool skip_;
702};
703
704// Update action for resolve operations
705class UpdateStateResolveAction {
706 public:
John Zulauf14940722021-04-12 15:19:02 -0600707 UpdateStateResolveAction(AccessContext &context, ResourceUsageTag tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700708 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
709 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600710 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700711 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600712 }
713
714 private:
715 AccessContext &context_;
John Zulauf14940722021-04-12 15:19:02 -0600716 const ResourceUsageTag tag_;
John Zulauf7635de32020-05-29 17:14:15 -0600717};
718
John Zulauf59e25072020-07-17 10:55:21 -0600719void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
John Zulauf14940722021-04-12 15:19:02 -0600720 const SyncStageAccessFlags &prior_, const ResourceUsageTag tag_) {
John Zulauf4fa68462021-04-26 21:04:22 -0600721 access_state = layer_data::make_unique<const ResourceAccessState>(*access_state_);
John Zulauf59e25072020-07-17 10:55:21 -0600722 usage_index = usage_index_;
723 hazard = hazard_;
724 prior_access = prior_;
725 tag = tag_;
726}
727
John Zulauf4fa68462021-04-26 21:04:22 -0600728void HazardResult::AddRecordedAccess(const ResourceFirstAccess &first_access) {
729 recorded_access = layer_data::make_unique<const ResourceFirstAccess>(first_access);
730}
731
John Zulauf540266b2020-04-06 18:54:53 -0600732AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
733 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600734 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600735 Reset();
736 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700737 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
738 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600739 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600740 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600741 const auto prev_pass = prev_dep.first->pass;
742 const auto &prev_barriers = prev_dep.second;
743 assert(prev_dep.second.size());
744 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
745 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700746 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600747
748 async_.reserve(subpass_dep.async.size());
749 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700750 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600751 }
John Zulauf22aefed2021-03-11 18:14:35 -0700752 if (has_barrier_from_external) {
753 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
754 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
755 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600756 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600757 if (subpass_dep.barrier_to_external.size()) {
758 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600759 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700760}
761
John Zulauf5f13a792020-03-10 07:31:21 -0600762template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700763HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600764 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600765 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600766 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600767
768 HazardResult hazard;
769 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
770 hazard = detector.Detect(prev);
771 }
772 return hazard;
773}
774
John Zulauf4a6105a2020-11-17 15:11:05 -0700775template <typename Action>
776void AccessContext::ForAll(Action &&action) {
777 for (const auto address_type : kAddressTypes) {
778 auto &accesses = GetAccessStateMap(address_type);
779 for (const auto &access : accesses) {
780 action(address_type, access);
781 }
782 }
783}
784
John Zulauf3d84f1b2020-03-09 13:33:25 -0600785// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
786// the DAG of the contexts (for example subpasses)
787template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700788HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600789 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600790 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600791
John Zulauf1a224292020-06-30 14:52:13 -0600792 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600793 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
794 // so we'll check these first
795 for (const auto &async_context : async_) {
796 hazard = async_context->DetectAsyncHazard(type, detector, range);
797 if (hazard.hazard) return hazard;
798 }
John Zulauf5f13a792020-03-10 07:31:21 -0600799 }
800
John Zulauf1a224292020-06-30 14:52:13 -0600801 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600802
John Zulauf69133422020-05-20 14:55:53 -0600803 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600804 const auto the_end = accesses.cend(); // End is not invalidated
805 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600806 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600807
John Zulauf3cafbf72021-03-26 16:55:19 -0600808 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600809 // Cover any leading gap, or gap between entries
810 if (detect_prev) {
811 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
812 // Cover any leading gap, or gap between entries
813 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600814 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600815 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600816 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600817 if (hazard.hazard) return hazard;
818 }
John Zulauf69133422020-05-20 14:55:53 -0600819 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
820 gap.begin = pos->first.end;
821 }
822
823 hazard = detector.Detect(pos);
824 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600825 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600826 }
827
828 if (detect_prev) {
829 // Detect in the trailing empty as needed
830 gap.end = range.end;
831 if (gap.non_empty()) {
832 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600833 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600834 }
835
836 return hazard;
837}
838
839// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
840template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700841HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
842 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600843 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600844 auto pos = accesses.lower_bound(range);
845 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600846
John Zulauf3d84f1b2020-03-09 13:33:25 -0600847 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600848 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700849 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600850 if (hazard.hazard) break;
851 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600852 }
John Zulauf16adfc92020-04-08 10:28:33 -0600853
John Zulauf3d84f1b2020-03-09 13:33:25 -0600854 return hazard;
855}
856
John Zulaufb02c1eb2020-10-06 16:33:36 -0600857struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700858 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600859 void operator()(ResourceAccessState *access) const {
860 assert(access);
861 access->ApplyBarriers(barriers, true);
862 }
863 const std::vector<SyncBarrier> &barriers;
864};
865
John Zulauf22aefed2021-03-11 18:14:35 -0700866struct ApplyTrackbackStackAction {
867 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
868 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
869 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600870 void operator()(ResourceAccessState *access) const {
871 assert(access);
872 assert(!access->HasPendingState());
873 access->ApplyBarriers(barriers, false);
John Zulaufee984022022-04-13 16:39:50 -0600874 // NOTE: We can use invalid tag, as these barriers do no include layout transitions (would assert in SetWrite)
875 access->ApplyPendingBarriers(kInvalidTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700876 if (previous_barrier) {
877 assert(bool(*previous_barrier));
878 (*previous_barrier)(access);
879 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600880 }
881 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700882 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600883};
884
885// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
886// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
887// *different* map from dest.
888// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
889// range [first, last)
890template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600891static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
892 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600893 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600894 auto at = entry;
895 for (auto pos = first; pos != last; ++pos) {
896 // Every member of the input iterator range must fit within the remaining portion of entry
897 assert(at->first.includes(pos->first));
898 assert(at != dest->end());
899 // Trim up at to the same size as the entry to resolve
900 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600901 auto access = pos->second; // intentional copy
902 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600903 at->second.Resolve(access);
904 ++at; // Go to the remaining unused section of entry
905 }
906}
907
John Zulaufa0a98292020-09-18 09:30:10 -0600908static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
909 SyncBarrier merged = {};
910 for (const auto &barrier : barriers) {
911 merged.Merge(barrier);
912 }
913 return merged;
914}
915
John Zulaufb02c1eb2020-10-06 16:33:36 -0600916template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700917void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600918 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
919 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600920 if (!range.non_empty()) return;
921
John Zulauf355e49b2020-04-24 15:11:15 -0600922 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
923 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600924 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600925 if (current->pos_B->valid) {
926 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600927 auto access = src_pos->second; // intentional copy
928 barrier_action(&access);
929
John Zulauf16adfc92020-04-08 10:28:33 -0600930 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600931 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
932 trimmed->second.Resolve(access);
933 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600934 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600935 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600936 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600937 }
John Zulauf16adfc92020-04-08 10:28:33 -0600938 } else {
939 // we have to descend to fill this gap
940 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700941 ResourceAccessRange recurrence_range = current_range;
942 // The current context is empty for the current range, so recur to fill the gap.
943 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
944 // is not valid, to minimize that recurrence
945 if (current->pos_B.at_end()) {
946 // Do the remainder here....
947 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600948 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700949 // Recur only over the range until B becomes valid (within the limits of range).
950 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600951 }
John Zulauf22aefed2021-03-11 18:14:35 -0700952 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
953
John Zulauf355e49b2020-04-24 15:11:15 -0600954 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
955 // iterator of the outer while.
956
957 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
958 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
959 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700960 const auto seek_to = recurrence_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
locke-lunarg88dbb542020-06-23 22:05:42 -0600961 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600962 current.seek(seek_to);
963 } else if (!current->pos_A->valid && infill_state) {
964 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
965 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
966 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600967 }
John Zulauf5f13a792020-03-10 07:31:21 -0600968 }
ziga-lunargf0e27ad2022-03-28 00:44:12 +0200969 if (current->range.non_empty()) {
970 ++current;
971 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600972 }
John Zulauf1a224292020-06-30 14:52:13 -0600973
974 // Infill if range goes passed both the current and resolve map prior contents
975 if (recur_to_infill && (current->range.end < range.end)) {
976 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700977 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600978 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600979}
980
John Zulauf22aefed2021-03-11 18:14:35 -0700981template <typename BarrierAction>
982void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
983 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
984 const BarrierAction &previous_barrier) const {
985 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
986 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
987}
988
John Zulauf43cc7462020-12-03 12:33:12 -0700989void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700990 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
991 const ResourceAccessStateFunction *previous_barrier) const {
992 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600993 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700994 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
995 ResourceAccessState state_copy;
996 if (previous_barrier) {
997 assert(bool(*previous_barrier));
998 state_copy = *infill_state;
999 (*previous_barrier)(&state_copy);
1000 infill_state = &state_copy;
1001 }
1002 sparse_container::update_range_value(*descent_map, range, *infill_state,
1003 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -06001004 }
1005 } else {
1006 // Look for something to fill the gap further along.
1007 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -07001008 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufbb890452021-12-14 11:30:18 -07001009 prev_dep.source_subpass->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001010 }
John Zulauf5f13a792020-03-10 07:31:21 -06001011 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001012}
1013
John Zulauf4a6105a2020-11-17 15:11:05 -07001014// Non-lazy import of all accesses, WaitEvents needs this.
1015void AccessContext::ResolvePreviousAccesses() {
1016 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -07001017 if (!prev_.size()) return; // If no previous contexts, nothing to do
1018
John Zulauf4a6105a2020-11-17 15:11:05 -07001019 for (const auto address_type : kAddressTypes) {
1020 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
1021 }
1022}
1023
John Zulauf43cc7462020-12-03 12:33:12 -07001024AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
1025 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -06001026}
1027
John Zulauf1507ee42020-05-18 11:33:09 -06001028static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001029 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1030 ? SYNC_ACCESS_INDEX_NONE
1031 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
1032 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001033 return stage_access;
1034}
1035static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001036 const auto stage_access =
1037 (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1038 ? SYNC_ACCESS_INDEX_NONE
1039 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
1040 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001041 return stage_access;
1042}
1043
John Zulauf7635de32020-05-29 17:14:15 -06001044// Caller must manage returned pointer
1045static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001046 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -06001047 auto *proxy = new AccessContext(context);
John Zulaufee984022022-04-13 16:39:50 -06001048 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kInvalidTag);
1049 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kInvalidTag);
John Zulauf7635de32020-05-29 17:14:15 -06001050 return proxy;
1051}
1052
John Zulaufb02c1eb2020-10-06 16:33:36 -06001053template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001054void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1055 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
1056 const ResourceAccessState *infill_state) const {
1057 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1058 if (!attachment_gen) return;
1059
1060 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1061 const AccessAddressType address_type = view_gen.GetAddressType();
1062 for (; range_gen->non_empty(); ++range_gen) {
1063 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001064 }
John Zulauf62f10592020-04-03 12:20:02 -06001065}
1066
John Zulauf7635de32020-05-29 17:14:15 -06001067// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulaufbb890452021-12-14 11:30:18 -07001068bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &exec_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001069 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001070 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001071 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001072 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
1073 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
1074 // those affects have not been recorded yet.
1075 //
1076 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1077 // to apply and only copy then, if this proves a hot spot.
1078 std::unique_ptr<AccessContext> proxy_for_prev;
1079 TrackBack proxy_track_back;
1080
John Zulauf355e49b2020-04-24 15:11:15 -06001081 const auto &transitions = rp_state.subpass_transitions[subpass];
1082 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001083 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1084
1085 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001086 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001087 if (prev_needs_proxy) {
1088 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001089 proxy_for_prev.reset(
John Zulaufbb890452021-12-14 11:30:18 -07001090 CreateStoreResolveProxyContext(*track_back->source_subpass, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -06001091 proxy_track_back = *track_back;
John Zulaufbb890452021-12-14 11:30:18 -07001092 proxy_track_back.source_subpass = proxy_for_prev.get();
John Zulauf7635de32020-05-29 17:14:15 -06001093 }
1094 track_back = &proxy_track_back;
1095 }
1096 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001097 if (hazard.hazard) {
John Zulaufee984022022-04-13 16:39:50 -06001098 if (hazard.tag == kInvalidTag) {
John Zulaufbb890452021-12-14 11:30:18 -07001099 skip |= exec_context.GetSyncState().LogError(
John Zulaufee984022022-04-13 16:39:50 -06001100 rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
1101 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1102 " image layout transition (old_layout: %s, new_layout: %s) after store/resolve operation in subpass %" PRIu32,
1103 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1104 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout), transition.prev_pass);
1105 } else {
John Zulaufbb890452021-12-14 11:30:18 -07001106 skip |= exec_context.GetSyncState().LogError(
John Zulaufee984022022-04-13 16:39:50 -06001107 rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
1108 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1109 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1110 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1111 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulaufbb890452021-12-14 11:30:18 -07001112 exec_context.FormatUsage(hazard).c_str());
John Zulaufee984022022-04-13 16:39:50 -06001113 }
John Zulauf355e49b2020-04-24 15:11:15 -06001114 }
1115 }
1116 return skip;
1117}
1118
John Zulaufbb890452021-12-14 11:30:18 -07001119bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &exec_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001120 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001121 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001122 bool skip = false;
1123 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001124
John Zulauf1507ee42020-05-18 11:33:09 -06001125 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1126 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001127 const auto &view_gen = attachment_views[i];
1128 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001129 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001130
1131 // Need check in the following way
1132 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1133 // vs. transition
1134 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1135 // for each aspect loaded.
1136
1137 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001138 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001139 const bool is_color = !(has_depth || has_stencil);
1140
1141 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001142 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001143
John Zulaufaff20662020-06-01 14:07:58 -06001144 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001145 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001146
John Zulaufb02c1eb2020-10-06 16:33:36 -06001147 bool checked_stencil = false;
John Zulauf57261402021-08-13 11:32:06 -06001148 if (is_color && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001149 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001150 aspect = "color";
1151 } else {
John Zulauf57261402021-08-13 11:32:06 -06001152 if (has_depth && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001153 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1154 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001155 aspect = "depth";
1156 }
John Zulauf57261402021-08-13 11:32:06 -06001157 if (!hazard.hazard && has_stencil && (stencil_load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001158 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1159 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001160 aspect = "stencil";
1161 checked_stencil = true;
1162 }
1163 }
1164
1165 if (hazard.hazard) {
1166 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulaufbb890452021-12-14 11:30:18 -07001167 const auto &sync_state = exec_context.GetSyncState();
John Zulaufee984022022-04-13 16:39:50 -06001168 if (hazard.tag == kInvalidTag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001169 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001170 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001171 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1172 " aspect %s during load with loadOp %s.",
1173 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1174 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001175 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001176 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001177 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001178 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulaufbb890452021-12-14 11:30:18 -07001179 exec_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001180 }
1181 }
1182 }
1183 }
1184 return skip;
1185}
1186
John Zulaufaff20662020-06-01 14:07:58 -06001187// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1188// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1189// store is part of the same Next/End operation.
1190// The latter is handled in layout transistion validation directly
John Zulaufbb890452021-12-14 11:30:18 -07001191bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &exec_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001192 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001193 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001194 bool skip = false;
1195 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001196
1197 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1198 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001199 const AttachmentViewGen &view_gen = attachment_views[i];
1200 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001201 const auto &ci = attachment_ci[i];
1202
1203 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1204 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1205 // sake, we treat DONT_CARE as writing.
1206 const bool has_depth = FormatHasDepth(ci.format);
1207 const bool has_stencil = FormatHasStencil(ci.format);
1208 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001209 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001210 if (!has_stencil && !store_op_stores) continue;
1211
1212 HazardResult hazard;
1213 const char *aspect = nullptr;
1214 bool checked_stencil = false;
1215 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001216 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1217 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001218 aspect = "color";
1219 } else {
John Zulauf57261402021-08-13 11:32:06 -06001220 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001221 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001222 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1223 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001224 aspect = "depth";
1225 }
1226 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001227 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1228 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001229 aspect = "stencil";
1230 checked_stencil = true;
1231 }
1232 }
1233
1234 if (hazard.hazard) {
1235 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1236 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
John Zulaufbb890452021-12-14 11:30:18 -07001237 skip |=
1238 exec_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
1239 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1240 " %s aspect during store with %s %s. Access info %s",
1241 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
1242 op_type_string, store_op_string, exec_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001243 }
1244 }
1245 }
1246 return skip;
1247}
1248
John Zulaufbb890452021-12-14 11:30:18 -07001249bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &exec_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001250 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1251 const char *func_name, uint32_t subpass) const {
John Zulaufbb890452021-12-14 11:30:18 -07001252 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, exec_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001253 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001254 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001255}
1256
John Zulauf3d84f1b2020-03-09 13:33:25 -06001257class HazardDetector {
1258 SyncStageAccessIndex usage_index_;
1259
1260 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001261 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf14940722021-04-12 15:19:02 -06001262 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001263 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001264 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001265 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001266};
1267
John Zulauf69133422020-05-20 14:55:53 -06001268class HazardDetectorWithOrdering {
1269 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001270 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001271
1272 public:
1273 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001274 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001275 }
John Zulauf14940722021-04-12 15:19:02 -06001276 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001277 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001278 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001279 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001280};
1281
John Zulauf16adfc92020-04-08 10:28:33 -06001282HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001283 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001284 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001285 const auto base_address = ResourceBaseAddress(buffer);
1286 HazardDetector detector(usage_index);
1287 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001288}
1289
John Zulauf69133422020-05-20 14:55:53 -06001290template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001291HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1292 DetectOptions options) const {
1293 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1294 if (!attachment_gen) return HazardResult();
1295
1296 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1297 const auto address_type = view_gen.GetAddressType();
1298 for (; range_gen->non_empty(); ++range_gen) {
1299 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1300 if (hazard.hazard) return hazard;
1301 }
1302
1303 return HazardResult();
1304}
1305
1306template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001307HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1308 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1309 const VkExtent3D &extent, DetectOptions options) const {
1310 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001311 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001312 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1313 base_address);
1314 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001315 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001316 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001317 if (hazard.hazard) return hazard;
1318 }
1319 return HazardResult();
1320}
John Zulauf110413c2021-03-20 05:38:38 -06001321template <typename Detector>
1322HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1323 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1324 if (!SimpleBinding(image)) return HazardResult();
1325 const auto base_address = ResourceBaseAddress(image);
1326 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1327 const auto address_type = ImageAddressType(image);
1328 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001329 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1330 if (hazard.hazard) return hazard;
1331 }
1332 return HazardResult();
1333}
John Zulauf69133422020-05-20 14:55:53 -06001334
John Zulauf540266b2020-04-06 18:54:53 -06001335HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1336 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1337 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001338 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1339 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001340 HazardDetector detector(current_usage);
1341 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001342}
1343
1344HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001345 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001346 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001347 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001348}
1349
John Zulaufd0ec59f2021-03-13 14:25:08 -07001350HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1351 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1352 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1353 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1354}
1355
John Zulauf69133422020-05-20 14:55:53 -06001356HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001357 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001358 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001359 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001360 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001361}
1362
John Zulauf3d84f1b2020-03-09 13:33:25 -06001363class BarrierHazardDetector {
1364 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001365 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001366 SyncStageAccessFlags src_access_scope)
1367 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1368
John Zulauf5f13a792020-03-10 07:31:21 -06001369 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1370 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001371 }
John Zulauf14940722021-04-12 15:19:02 -06001372 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001373 // 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 -07001374 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001375 }
1376
1377 private:
1378 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001379 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001380 SyncStageAccessFlags src_access_scope_;
1381};
1382
John Zulauf4a6105a2020-11-17 15:11:05 -07001383class EventBarrierHazardDetector {
1384 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001385 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001386 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
John Zulauf14940722021-04-12 15:19:02 -06001387 ResourceUsageTag scope_tag)
John Zulauf4a6105a2020-11-17 15:11:05 -07001388 : usage_index_(usage_index),
1389 src_exec_scope_(src_exec_scope),
1390 src_access_scope_(src_access_scope),
1391 event_scope_(event_scope),
1392 scope_pos_(event_scope.cbegin()),
1393 scope_end_(event_scope.cend()),
1394 scope_tag_(scope_tag) {}
1395
1396 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1397 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1398 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1399 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1400 if (scope_pos_ == scope_end_) return HazardResult();
1401 if (!scope_pos_->first.intersects(pos->first)) {
1402 event_scope_.lower_bound(pos->first);
1403 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1404 }
1405
1406 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1407 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1408 }
John Zulauf14940722021-04-12 15:19:02 -06001409 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07001410 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1411 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1412 }
1413
1414 private:
1415 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001416 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001417 SyncStageAccessFlags src_access_scope_;
1418 const SyncEventState::ScopeMap &event_scope_;
1419 SyncEventState::ScopeMap::const_iterator scope_pos_;
1420 SyncEventState::ScopeMap::const_iterator scope_end_;
John Zulauf14940722021-04-12 15:19:02 -06001421 const ResourceUsageTag scope_tag_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001422};
1423
Jeremy Gebben40a22942020-12-22 14:22:06 -07001424HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001425 const SyncStageAccessFlags &src_access_scope,
1426 const VkImageSubresourceRange &subresource_range,
1427 const SyncEventState &sync_event, DetectOptions options) const {
1428 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1429 // first access scope map to use, and there's no easy way to plumb it in below.
1430 const auto address_type = ImageAddressType(image);
1431 const auto &event_scope = sync_event.FirstScope(address_type);
1432
1433 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1434 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001435 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001436}
1437
John Zulaufd0ec59f2021-03-13 14:25:08 -07001438HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1439 DetectOptions options) const {
1440 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1441 barrier.src_access_scope);
1442 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1443}
1444
Jeremy Gebben40a22942020-12-22 14:22:06 -07001445HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001446 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001447 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001448 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001449 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001450 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001451}
1452
Jeremy Gebben40a22942020-12-22 14:22:06 -07001453HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001454 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001455 const VkImageMemoryBarrier &barrier) const {
1456 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1457 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1458 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1459}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001460HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001461 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001462 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001463}
John Zulauf355e49b2020-04-24 15:11:15 -06001464
John Zulauf9cb530d2019-09-30 14:14:10 -06001465template <typename Flags, typename Map>
1466SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1467 SyncStageAccessFlags scope = 0;
1468 for (const auto &bit_scope : map) {
1469 if (flag_mask < bit_scope.first) break;
1470
1471 if (flag_mask & bit_scope.first) {
1472 scope |= bit_scope.second;
1473 }
1474 }
1475 return scope;
1476}
1477
Jeremy Gebben40a22942020-12-22 14:22:06 -07001478SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001479 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1480}
1481
Jeremy Gebben40a22942020-12-22 14:22:06 -07001482SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1483 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001484}
1485
Jeremy Gebben40a22942020-12-22 14:22:06 -07001486// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1487SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001488 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1489 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1490 // 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 -06001491 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1492}
1493
1494template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001495void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001496 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1497 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001498 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001499 auto pos = accesses->lower_bound(range);
1500 if (pos == accesses->end() || !pos->first.intersects(range)) {
1501 // The range is empty, fill it with a default value.
1502 pos = action.Infill(accesses, pos, range);
1503 } else if (range.begin < pos->first.begin) {
1504 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001505 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001506 } else if (pos->first.begin < range.begin) {
1507 // Trim the beginning if needed
1508 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1509 ++pos;
1510 }
1511
1512 const auto the_end = accesses->end();
1513 while ((pos != the_end) && pos->first.intersects(range)) {
1514 if (pos->first.end > range.end) {
1515 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1516 }
1517
1518 pos = action(accesses, pos);
1519 if (pos == the_end) break;
1520
1521 auto next = pos;
1522 ++next;
1523 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1524 // Need to infill if next is disjoint
1525 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001526 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001527 next = action.Infill(accesses, next, new_range);
1528 }
1529 pos = next;
1530 }
1531}
John Zulaufd5115702021-01-18 12:34:33 -07001532
1533// Give a comparable interface for range generators and ranges
1534template <typename Action>
1535inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1536 assert(range);
1537 UpdateMemoryAccessState(accesses, *range, action);
1538}
1539
John Zulauf4a6105a2020-11-17 15:11:05 -07001540template <typename Action, typename RangeGen>
1541void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1542 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001543 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 -07001544 for (; range_gen->non_empty(); ++range_gen) {
1545 UpdateMemoryAccessState(accesses, *range_gen, action);
1546 }
1547}
John Zulauf9cb530d2019-09-30 14:14:10 -06001548
John Zulaufd0ec59f2021-03-13 14:25:08 -07001549template <typename Action, typename RangeGen>
1550void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1551 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1552 for (; range_gen->non_empty(); ++range_gen) {
1553 UpdateMemoryAccessState(accesses, *range_gen, action);
1554 }
1555}
John Zulauf9cb530d2019-09-30 14:14:10 -06001556struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001557 using Iterator = ResourceAccessRangeMap::iterator;
1558 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001559 // this is only called on gaps, and never returns a gap.
1560 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001561 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001562 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001563 }
John Zulauf5f13a792020-03-10 07:31:21 -06001564
John Zulauf5c5e88d2019-12-26 11:22:02 -07001565 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001566 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001567 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001568 return pos;
1569 }
1570
John Zulauf43cc7462020-12-03 12:33:12 -07001571 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf14940722021-04-12 15:19:02 -06001572 SyncOrdering ordering_rule_, ResourceUsageTag tag_)
John Zulauf8e3c3e92021-01-06 11:19:36 -07001573 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001574 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001575 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001576 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001577 const SyncOrdering ordering_rule;
John Zulauf14940722021-04-12 15:19:02 -06001578 const ResourceUsageTag tag;
John Zulauf9cb530d2019-09-30 14:14:10 -06001579};
1580
John Zulauf4a6105a2020-11-17 15:11:05 -07001581// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001582struct PipelineBarrierOp {
1583 SyncBarrier barrier;
1584 bool layout_transition;
1585 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1586 : barrier(barrier_), layout_transition(layout_transition_) {}
1587 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001588 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001589 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1590};
John Zulauf4a6105a2020-11-17 15:11:05 -07001591// The barrier operation for wait events
1592struct WaitEventBarrierOp {
John Zulauf14940722021-04-12 15:19:02 -06001593 ResourceUsageTag scope_tag;
John Zulauf4a6105a2020-11-17 15:11:05 -07001594 SyncBarrier barrier;
1595 bool layout_transition;
John Zulauf14940722021-04-12 15:19:02 -06001596 WaitEventBarrierOp(const ResourceUsageTag scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1597 : scope_tag(scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
John Zulauf4a6105a2020-11-17 15:11:05 -07001598 WaitEventBarrierOp() = default;
John Zulauf14940722021-04-12 15:19:02 -06001599 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(scope_tag, barrier, layout_transition); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001600};
John Zulauf1e331ec2020-12-04 18:29:38 -07001601
John Zulauf4a6105a2020-11-17 15:11:05 -07001602// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1603// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1604// of a collection is known/present.
John Zulauf5c628d02021-05-04 15:46:36 -06001605template <typename BarrierOp, typename OpVector = std::vector<BarrierOp>>
John Zulauf89311b42020-09-29 16:28:47 -06001606class ApplyBarrierOpsFunctor {
1607 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001608 using Iterator = ResourceAccessRangeMap::iterator;
John Zulauf5c628d02021-05-04 15:46:36 -06001609 // Only called with a gap, and pos at the lower_bound(range)
1610 inline Iterator Infill(ResourceAccessRangeMap *accesses, const Iterator &pos, const ResourceAccessRange &range) const {
1611 if (!infill_default_) {
1612 return pos;
1613 }
1614 ResourceAccessState default_state;
1615 auto inserted = accesses->insert(pos, std::make_pair(range, default_state));
1616 return inserted;
1617 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001618
John Zulauf5c628d02021-05-04 15:46:36 -06001619 Iterator operator()(ResourceAccessRangeMap *accesses, const Iterator &pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001620 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001621 for (const auto &op : barrier_ops_) {
1622 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001623 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001624
John Zulauf89311b42020-09-29 16:28:47 -06001625 if (resolve_) {
1626 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1627 // another walk
1628 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001629 }
1630 return pos;
1631 }
1632
John Zulauf89311b42020-09-29 16:28:47 -06001633 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf5c628d02021-05-04 15:46:36 -06001634 ApplyBarrierOpsFunctor(bool resolve, typename OpVector::size_type size_hint, ResourceUsageTag tag)
1635 : resolve_(resolve), infill_default_(false), barrier_ops_(), tag_(tag) {
John Zulaufd5115702021-01-18 12:34:33 -07001636 barrier_ops_.reserve(size_hint);
1637 }
John Zulauf5c628d02021-05-04 15:46:36 -06001638 void EmplaceBack(const BarrierOp &op) {
1639 barrier_ops_.emplace_back(op);
1640 infill_default_ |= op.layout_transition;
1641 }
John Zulauf89311b42020-09-29 16:28:47 -06001642
1643 private:
1644 bool resolve_;
John Zulauf5c628d02021-05-04 15:46:36 -06001645 bool infill_default_;
1646 OpVector barrier_ops_;
John Zulauf14940722021-04-12 15:19:02 -06001647 const ResourceUsageTag tag_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001648};
1649
John Zulauf4a6105a2020-11-17 15:11:05 -07001650// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1651// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1652template <typename BarrierOp>
John Zulauf5c628d02021-05-04 15:46:36 -06001653class ApplyBarrierFunctor : public ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>> {
1654 using Base = ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>>;
1655
John Zulauf4a6105a2020-11-17 15:11:05 -07001656 public:
John Zulaufee984022022-04-13 16:39:50 -06001657 ApplyBarrierFunctor(const BarrierOp &barrier_op) : Base(false, 1, kInvalidTag) { Base::EmplaceBack(barrier_op); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001658};
1659
John Zulauf1e331ec2020-12-04 18:29:38 -07001660// This functor resolves the pendinging state.
John Zulauf5c628d02021-05-04 15:46:36 -06001661class ResolvePendingBarrierFunctor : public ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>> {
1662 using Base = ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>>;
1663
John Zulauf1e331ec2020-12-04 18:29:38 -07001664 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001665 ResolvePendingBarrierFunctor(ResourceUsageTag tag) : Base(true, 0, tag) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001666};
1667
John Zulauf8e3c3e92021-01-06 11:19:36 -07001668void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001669 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001670 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001671 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001672}
1673
John Zulauf8e3c3e92021-01-06 11:19:36 -07001674void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001675 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001676 if (!SimpleBinding(buffer)) return;
1677 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001678 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001679}
John Zulauf355e49b2020-04-24 15:11:15 -06001680
John Zulauf8e3c3e92021-01-06 11:19:36 -07001681void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001682 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1683 if (!SimpleBinding(image)) return;
1684 const auto base_address = ResourceBaseAddress(image);
1685 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1686 const auto address_type = ImageAddressType(image);
1687 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1688 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1689}
1690void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001691 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001692 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001693 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001694 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001695 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1696 base_address);
1697 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001698 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001699 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001700}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001701
1702void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
John Zulauf14940722021-04-12 15:19:02 -06001703 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001704 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1705 if (!gen) return;
1706 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1707 const auto address_type = view_gen.GetAddressType();
1708 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1709 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001710}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001711
John Zulauf8e3c3e92021-01-06 11:19:36 -07001712void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001713 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001714 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001715 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1716 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001717 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001718}
1719
John Zulaufd0ec59f2021-03-13 14:25:08 -07001720template <typename Action, typename RangeGen>
1721void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1722 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1723 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001724}
1725
1726template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001727void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1728 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1729 if (!gen) return;
1730 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001731}
1732
John Zulaufd0ec59f2021-03-13 14:25:08 -07001733void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1734 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001735 const ResourceUsageTag tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001736 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001737 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001738}
1739
John Zulaufd0ec59f2021-03-13 14:25:08 -07001740void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06001741 uint32_t subpass, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001742 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001743
1744 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1745 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001746 const auto &view_gen = attachment_views[i];
1747 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001748
1749 const auto &ci = attachment_ci[i];
1750 const bool has_depth = FormatHasDepth(ci.format);
1751 const bool has_stencil = FormatHasStencil(ci.format);
1752 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001753 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001754
1755 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001756 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1757 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001758 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001759 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001760 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1761 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001762 }
John Zulauf57261402021-08-13 11:32:06 -06001763 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001764 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001765 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1766 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001767 }
1768 }
1769 }
1770 }
1771}
1772
John Zulauf540266b2020-04-06 18:54:53 -06001773template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001774void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001775 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001776 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001777 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001778 }
1779}
1780
1781void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001782 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1783 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001784 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001785 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001786 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001787 }
1788 }
1789}
1790
John Zulauf4fa68462021-04-26 21:04:22 -06001791// Caller must ensure that lifespan of this is less than from
1792void AccessContext::ImportAsyncContexts(const AccessContext &from) { async_ = from.async_; }
1793
John Zulauf355e49b2020-04-24 15:11:15 -06001794// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001795HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1796 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001797
John Zulauf355e49b2020-04-24 15:11:15 -06001798 // We should never ask for a transition from a context we don't have
John Zulaufbb890452021-12-14 11:30:18 -07001799 assert(track_back.source_subpass);
John Zulauf355e49b2020-04-24 15:11:15 -06001800
1801 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001802 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1803 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufbb890452021-12-14 11:30:18 -07001804 HazardResult hazard = track_back.source_subpass->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001805 if (!hazard.hazard) {
1806 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001807 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001808 }
John Zulaufa0a98292020-09-18 09:30:10 -06001809
John Zulauf355e49b2020-04-24 15:11:15 -06001810 return hazard;
1811}
1812
John Zulaufb02c1eb2020-10-06 16:33:36 -06001813void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001814 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001815 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001816 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001817 for (const auto &transition : transitions) {
1818 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001819 const auto &view_gen = attachment_views[transition.attachment];
1820 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001821
1822 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1823 assert(trackback);
1824
1825 // Import the attachments into the current context
John Zulaufbb890452021-12-14 11:30:18 -07001826 const auto *prev_context = trackback->source_subpass;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001827 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001828 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001829 auto &target_map = GetAccessStateMap(address_type);
1830 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001831 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1832 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001833 }
1834
John Zulauf86356ca2020-10-19 11:46:41 -06001835 // If there were no transitions skip this global map walk
1836 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001837 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001838 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001839 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001840}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001841
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001842void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulauf669dfd52021-01-27 17:15:28 -07001843 auto *events_context = GetCurrentEventsContext();
1844 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06001845 events_context->ApplyBarrier(src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001846}
1847
locke-lunarg61870c22020-06-09 14:51:50 -06001848bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1849 const char *func_name) const {
1850 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001851 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001852 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001853 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001854 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001855 return skip;
1856 }
1857
1858 using DescriptorClass = cvdescriptorset::DescriptorClass;
1859 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1860 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001861 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1862
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001863 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001864 const auto raster_state = pipe->RasterizationState();
1865 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001866 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001867 }
locke-lunarg61870c22020-06-09 14:51:50 -06001868 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001869 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001870 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001871 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001872 const auto descriptor_type = binding_it.GetType();
1873 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1874 auto array_idx = 0;
1875
1876 if (binding_it.IsVariableDescriptorCount()) {
1877 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1878 }
1879 SyncStageAccessIndex sync_index =
1880 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1881
1882 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1883 uint32_t index = i - index_range.start;
1884 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1885 switch (descriptor->GetClass()) {
1886 case DescriptorClass::ImageSampler:
1887 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001888 if (descriptor->Invalid()) {
1889 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001890 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07001891
1892 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
1893 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1894 const auto *img_view_state = image_descriptor->GetImageViewState();
1895 VkImageLayout image_layout = image_descriptor->GetImageLayout();
1896
John Zulauf361fb532020-07-22 10:45:39 -06001897 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001898 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1899 // Descriptors, so we do not have to worry about depth slicing here.
1900 // See: VUID 00343
1901 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001902 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001903 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001904
1905 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1906 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1907 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001908 // Input attachments are subject to raster ordering rules
1909 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001910 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001911 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001912 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001913 }
John Zulauf110413c2021-03-20 05:38:38 -06001914
John Zulauf33fc1d52020-07-17 11:01:10 -06001915 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001916 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001917 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001918 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1919 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001920 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001921 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1922 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1923 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001924 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1925 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001926 set_binding.first.binding, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001927 }
1928 break;
1929 }
1930 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001931 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
1932 if (texel_descriptor->Invalid()) {
1933 continue;
1934 }
1935 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
1936 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001937 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001938 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001939 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001940 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001941 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001942 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1943 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001944 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1945 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1946 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001947 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001948 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001949 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001950 }
1951 break;
1952 }
1953 case DescriptorClass::GeneralBuffer: {
1954 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07001955 if (buffer_descriptor->Invalid()) {
1956 continue;
1957 }
1958 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06001959 const ResourceAccessRange range =
1960 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001961 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001962 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001963 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001964 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001965 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1966 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001967 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1968 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1969 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001970 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001971 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001972 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001973 }
1974 break;
1975 }
1976 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1977 default:
1978 break;
1979 }
1980 }
1981 }
1982 }
1983 return skip;
1984}
1985
1986void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
John Zulauf14940722021-04-12 15:19:02 -06001987 const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001988 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001989 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001990 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001991 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001992 return;
1993 }
1994
1995 using DescriptorClass = cvdescriptorset::DescriptorClass;
1996 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1997 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001998 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1999
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002000 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002001 const auto raster_state = pipe->RasterizationState();
2002 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06002003 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002004 }
locke-lunarg61870c22020-06-09 14:51:50 -06002005 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07002006 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002007 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002008 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06002009 const auto descriptor_type = binding_it.GetType();
2010 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2011 auto array_idx = 0;
2012
2013 if (binding_it.IsVariableDescriptorCount()) {
2014 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2015 }
2016 SyncStageAccessIndex sync_index =
2017 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2018
2019 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2020 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2021 switch (descriptor->GetClass()) {
2022 case DescriptorClass::ImageSampler:
2023 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002024 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
2025 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
2026 if (image_descriptor->Invalid()) {
2027 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002028 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07002029 const auto *img_view_state = image_descriptor->GetImageViewState();
Jeremy Gebben11a68a32021-07-29 11:59:22 -06002030 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
2031 // Descriptors, so we do not have to worry about depth slicing here.
2032 // See: VUID 00343
2033 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06002034 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002035 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06002036 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2037 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2038 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
2039 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002040 } else {
John Zulauf110413c2021-03-20 05:38:38 -06002041 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
2042 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002043 }
locke-lunarg61870c22020-06-09 14:51:50 -06002044 break;
2045 }
2046 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002047 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
2048 if (texel_descriptor->Invalid()) {
2049 continue;
2050 }
2051 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
2052 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002053 const ResourceAccessRange range = MakeRange(*buf_view_state);
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 case DescriptorClass::GeneralBuffer: {
2058 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07002059 if (buffer_descriptor->Invalid()) {
2060 continue;
2061 }
2062 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06002063 const ResourceAccessRange range =
2064 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002065 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002066 break;
2067 }
2068 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2069 default:
2070 break;
2071 }
2072 }
2073 }
2074 }
2075}
2076
2077bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2078 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002079 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002080 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002081 return skip;
2082 }
2083
2084 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2085 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002086 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002087
2088 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002089 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002090 if (binding_description.binding < binding_buffers_size) {
2091 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002092 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002093
locke-lunarg1ae57d62020-11-18 10:49:19 -07002094 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002095 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2096 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002097 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002098 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002099 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002100 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
2101 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2102 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002103 }
2104 }
2105 }
2106 return skip;
2107}
2108
John Zulauf14940722021-04-12 15:19:02 -06002109void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002110 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002111 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002112 return;
2113 }
2114 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2115 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002116 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002117
2118 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002119 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002120 if (binding_description.binding < binding_buffers_size) {
2121 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002122 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002123
locke-lunarg1ae57d62020-11-18 10:49:19 -07002124 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002125 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2126 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002127 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2128 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002129 }
2130 }
2131}
2132
2133bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2134 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002135 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002136 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002137 }
locke-lunarg61870c22020-06-09 14:51:50 -06002138
locke-lunarg1ae57d62020-11-18 10:49:19 -07002139 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002140 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002141 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2142 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002143 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002144 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002145 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002146 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2147 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2148 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002149 }
2150
2151 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2152 // We will detect more accurate range in the future.
2153 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2154 return skip;
2155}
2156
John Zulauf14940722021-04-12 15:19:02 -06002157void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002158 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 -06002159
locke-lunarg1ae57d62020-11-18 10:49:19 -07002160 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002161 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002162 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2163 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002164 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002165
2166 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2167 // We will detect more accurate range in the future.
2168 RecordDrawVertex(UINT32_MAX, 0, tag);
2169}
2170
2171bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002172 bool skip = false;
2173 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002174 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002175 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002176}
2177
John Zulauf14940722021-04-12 15:19:02 -06002178void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002179 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002180 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002181 }
locke-lunarg61870c22020-06-09 14:51:50 -06002182}
2183
John Zulauf41a9c7c2021-12-07 15:59:53 -07002184ResourceUsageTag CommandBufferAccessContext::RecordBeginRenderPass(CMD_TYPE cmd, const RENDER_PASS_STATE &rp_state,
2185 const VkRect2D &render_area,
2186 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
John Zulauf355e49b2020-04-24 15:11:15 -06002187 // Create an access context the current renderpass.
John Zulauf41a9c7c2021-12-07 15:59:53 -07002188 const auto barrier_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2189 const auto load_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kLoadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07002190 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002191 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002192 current_renderpass_context_->RecordBeginRenderPass(barrier_tag, load_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002193 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002194 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002195}
2196
John Zulauf41a9c7c2021-12-07 15:59:53 -07002197ResourceUsageTag CommandBufferAccessContext::RecordNextSubpass(const CMD_TYPE cmd) {
John Zulauf16adfc92020-04-08 10:28:33 -06002198 assert(current_renderpass_context_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002199 if (!current_renderpass_context_) return NextCommandTag(cmd);
2200
2201 auto store_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kStoreOp);
2202 auto barrier_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2203 auto load_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kLoadOp);
2204
2205 current_renderpass_context_->RecordNextSubpass(store_tag, barrier_tag, load_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002206 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf41a9c7c2021-12-07 15:59:53 -07002207 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002208}
2209
John Zulauf41a9c7c2021-12-07 15:59:53 -07002210ResourceUsageTag CommandBufferAccessContext::RecordEndRenderPass(const CMD_TYPE cmd) {
John Zulauf16adfc92020-04-08 10:28:33 -06002211 assert(current_renderpass_context_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002212 if (!current_renderpass_context_) return NextCommandTag(cmd);
John Zulauf16adfc92020-04-08 10:28:33 -06002213
John Zulauf41a9c7c2021-12-07 15:59:53 -07002214 auto store_tag = NextCommandTag(cmd, ResourceUsageRecord::SubcommandType::kStoreOp);
2215 auto barrier_tag = NextSubcommandTag(cmd, ResourceUsageRecord::SubcommandType::kSubpassTransition);
2216
2217 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, store_tag, barrier_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002218 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002219 current_renderpass_context_ = nullptr;
John Zulauf41a9c7c2021-12-07 15:59:53 -07002220 return barrier_tag;
John Zulauf16adfc92020-04-08 10:28:33 -06002221}
2222
John Zulauf4a6105a2020-11-17 15:11:05 -07002223void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2224 // Erase is okay with the key not being
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002225 auto event_state = sync_state_->Get<EVENT_STATE>(event);
John Zulauf669dfd52021-01-27 17:15:28 -07002226 if (event_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002227 GetCurrentEventsContext()->Destroy(event_state.get());
John Zulaufd5115702021-01-18 12:34:33 -07002228 }
2229}
2230
John Zulaufae842002021-04-15 18:20:55 -06002231// The is the recorded cb context
John Zulaufbb890452021-12-14 11:30:18 -07002232bool CommandBufferAccessContext::ValidateFirstUse(CommandExecutionContext *proxy_context, const char *func_name,
John Zulauf4fa68462021-04-26 21:04:22 -06002233 uint32_t index) const {
2234 assert(proxy_context);
2235 auto *events_context = proxy_context->GetCurrentEventsContext();
2236 auto *access_context = proxy_context->GetCurrentAccessContext();
2237 const ResourceUsageTag base_tag = proxy_context->GetTagLimit();
John Zulaufae842002021-04-15 18:20:55 -06002238 bool skip = false;
2239 ResourceUsageRange tag_range = {0, 0};
2240 const AccessContext *recorded_context = GetCurrentAccessContext();
2241 assert(recorded_context);
2242 HazardResult hazard;
John Zulaufbb890452021-12-14 11:30:18 -07002243 auto log_msg = [this](const HazardResult &hazard, const CommandExecutionContext &exec_context, const char *func_name,
John Zulaufae842002021-04-15 18:20:55 -06002244 uint32_t index) {
John Zulaufbb890452021-12-14 11:30:18 -07002245 const auto handle = exec_context.Handle();
John Zulaufae842002021-04-15 18:20:55 -06002246 const auto recorded_handle = cb_state_->commandBuffer();
John Zulauf4fa68462021-04-26 21:04:22 -06002247 const auto *report_data = sync_state_->report_data;
John Zulaufbb890452021-12-14 11:30:18 -07002248 return sync_state_->LogError(handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf4fa68462021-04-26 21:04:22 -06002249 "%s: Hazard %s for entry %" PRIu32 ", %s, Recorded access info %s. Access info %s.", func_name,
2250 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(recorded_handle).c_str(),
John Zulaufbb890452021-12-14 11:30:18 -07002251 FormatUsage(*hazard.recorded_access).c_str(), exec_context.FormatUsage(hazard).c_str());
John Zulaufae842002021-04-15 18:20:55 -06002252 };
John Zulaufbb890452021-12-14 11:30:18 -07002253 const ReplayTrackbackBarriersAction *replay_context = nullptr;
John Zulaufae842002021-04-15 18:20:55 -06002254 for (const auto &sync_op : sync_ops_) {
John Zulauf4fa68462021-04-26 21:04:22 -06002255 // we update the range to any include layout transition first use writes,
2256 // as they are stored along with the source scope (as effective barrier) when recorded
2257 tag_range.end = sync_op.tag + 1;
John Zulauf610e28c2021-08-03 17:46:23 -06002258 skip |= sync_op.sync_op->ReplayValidate(sync_op.tag, *this, base_tag, proxy_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002259
John Zulaufbb890452021-12-14 11:30:18 -07002260 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context, replay_context);
John Zulaufae842002021-04-15 18:20:55 -06002261 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 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
John Zulauf4fa68462021-04-26 21:04:22 -06002265 // Record the barrier into the proxy context.
John Zulaufbb890452021-12-14 11:30:18 -07002266 sync_op.sync_op->ReplayRecord(base_tag + sync_op.tag, access_context, events_context);
2267 replay_context = sync_op.sync_op->GetReplayTrackback();
John Zulauf4fa68462021-04-26 21:04:22 -06002268 tag_range.begin = tag_range.end;
John Zulaufae842002021-04-15 18:20:55 -06002269 }
2270
John Zulaufbb890452021-12-14 11:30:18 -07002271 // Renderpasses may not cross command buffer boundaries
2272 assert(replay_context == nullptr);
2273
John Zulaufae842002021-04-15 18:20:55 -06002274 // and anything after the last syncop
John Zulaufae842002021-04-15 18:20:55 -06002275 tag_range.end = ResourceUsageRecord::kMaxIndex;
John Zulaufbb890452021-12-14 11:30:18 -07002276 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context, replay_context);
John Zulaufae842002021-04-15 18:20:55 -06002277 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002278 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002279 }
2280
2281 return skip;
2282}
2283
John Zulauf4fa68462021-04-26 21:04:22 -06002284void CommandBufferAccessContext::RecordExecutedCommandBuffer(const CommandBufferAccessContext &recorded_cb_context, CMD_TYPE cmd) {
2285 auto *events_context = GetCurrentEventsContext();
2286 auto *access_context = GetCurrentAccessContext();
2287 const AccessContext *recorded_context = recorded_cb_context.GetCurrentAccessContext();
2288 assert(recorded_context);
2289
2290 // Just run through the barriers ignoring the usage from the recorded context, as Resolve will overwrite outdated state
2291 const ResourceUsageTag base_tag = GetTagLimit();
2292 for (const auto &sync_op : recorded_cb_context.sync_ops_) {
2293 // we update the range to any include layout transition first use writes,
2294 // as they are stored along with the source scope (as effective barrier) when recorded
John Zulaufbb890452021-12-14 11:30:18 -07002295 sync_op.sync_op->ReplayRecord(base_tag + sync_op.tag, access_context, events_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002296 }
2297
2298 ResourceUsageRange tag_range = ImportRecordedAccessLog(recorded_cb_context);
2299 assert(base_tag == tag_range.begin); // to ensure the to offset calculation agree
2300 ResolveRecordedContext(*recorded_context, tag_range.begin);
2301}
2302
2303void CommandBufferAccessContext::ResolveRecordedContext(const AccessContext &recorded_context, ResourceUsageTag offset) {
2304 auto tag_offset = [offset](ResourceAccessState *access) { access->OffsetTag(offset); };
2305
2306 auto *access_context = GetCurrentAccessContext();
2307 for (auto address_type : kAddressTypes) {
2308 recorded_context.ResolveAccessRange(address_type, kFullRange, tag_offset, &access_context->GetAccessStateMap(address_type),
2309 nullptr, false);
2310 }
2311}
2312
2313ResourceUsageRange CommandBufferAccessContext::ImportRecordedAccessLog(const CommandBufferAccessContext &recorded_context) {
2314 // The execution references ensure lifespan for the referenced child CB's...
2315 ResourceUsageRange tag_range(GetTagLimit(), 0);
John Zulauf3c2a0b32021-07-14 11:14:52 -06002316 cbs_referenced_.emplace(recorded_context.cb_state_);
John Zulauf4fa68462021-04-26 21:04:22 -06002317 access_log_.insert(access_log_.end(), recorded_context.access_log_.cbegin(), recorded_context.access_log_.end());
2318 tag_range.end = access_log_.size();
2319 return tag_range;
2320}
2321
John Zulauf41a9c7c2021-12-07 15:59:53 -07002322ResourceUsageTag CommandBufferAccessContext::NextSubcommandTag(CMD_TYPE command, ResourceUsageRecord::SubcommandType subcommand) {
2323 ResourceUsageTag next = access_log_.size();
2324 access_log_.emplace_back(command, command_number_, subcommand, ++subcommand_number_, cb_state_.get(), reset_count_);
2325 return next;
2326}
2327
2328ResourceUsageTag CommandBufferAccessContext::NextCommandTag(CMD_TYPE command, ResourceUsageRecord::SubcommandType subcommand) {
2329 command_number_++;
2330 subcommand_number_ = 0;
2331 ResourceUsageTag next = access_log_.size();
2332 access_log_.emplace_back(command, command_number_, subcommand, subcommand_number_, cb_state_.get(), reset_count_);
2333 return next;
2334}
2335
2336ResourceUsageTag CommandBufferAccessContext::NextIndexedCommandTag(CMD_TYPE command, uint32_t index) {
2337 if (index == 0) {
2338 return NextCommandTag(command, ResourceUsageRecord::SubcommandType::kIndex);
2339 }
2340 return NextSubcommandTag(command, ResourceUsageRecord::SubcommandType::kIndex);
2341}
2342
John Zulaufbb890452021-12-14 11:30:18 -07002343void CommandBufferAccessContext::RecordSyncOp(SyncOpPointer &&sync_op) {
2344 auto tag = sync_op->Record(this);
2345 // As renderpass operations can have side effects on the command buffer access context,
2346 // update the sync operation to record these if any.
2347 if (current_renderpass_context_) {
2348 const auto &rpc = *current_renderpass_context_;
2349 sync_op->SetReplayContext(rpc.GetCurrentSubpass(), rpc.GetReplayContext());
2350 }
2351 sync_ops_.emplace_back(tag, std::move(sync_op));
2352}
2353
John Zulaufae842002021-04-15 18:20:55 -06002354class HazardDetectFirstUse {
2355 public:
John Zulaufbb890452021-12-14 11:30:18 -07002356 HazardDetectFirstUse(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2357 const ReplayTrackbackBarriersAction *replay_barrier)
2358 : recorded_use_(recorded_use), tag_range_(tag_range), replay_barrier_(replay_barrier) {}
John Zulaufae842002021-04-15 18:20:55 -06002359 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulaufbb890452021-12-14 11:30:18 -07002360 if (replay_barrier_) {
2361 // Intentional copy to apply the replay barrier
2362 auto access = pos->second;
2363 (*replay_barrier_)(&access);
2364 return access.DetectHazard(recorded_use_, tag_range_);
2365 }
John Zulaufae842002021-04-15 18:20:55 -06002366 return pos->second.DetectHazard(recorded_use_, tag_range_);
2367 }
2368 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
2369 return pos->second.DetectAsyncHazard(recorded_use_, tag_range_, start_tag);
2370 }
2371
2372 private:
2373 const ResourceAccessState &recorded_use_;
2374 const ResourceUsageRange &tag_range_;
John Zulaufbb890452021-12-14 11:30:18 -07002375 const ReplayTrackbackBarriersAction *replay_barrier_;
John Zulaufae842002021-04-15 18:20:55 -06002376};
2377
2378// This is called with the *recorded* command buffers access context, with the *active* access context pass in, againsts which
2379// hazards will be detected
John Zulaufbb890452021-12-14 11:30:18 -07002380HazardResult AccessContext::DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context,
2381 const ReplayTrackbackBarriersAction *replay_barrier) const {
John Zulaufae842002021-04-15 18:20:55 -06002382 HazardResult hazard;
2383 for (const auto address_type : kAddressTypes) {
2384 const auto &recorded_access_map = GetAccessStateMap(address_type);
2385 for (const auto &recorded_access : recorded_access_map) {
2386 // Cull any entries not in the current tag range
2387 if (!recorded_access.second.FirstAccessInTagRange(tag_range)) continue;
John Zulaufbb890452021-12-14 11:30:18 -07002388 HazardDetectFirstUse detector(recorded_access.second, tag_range, replay_barrier);
John Zulaufae842002021-04-15 18:20:55 -06002389 hazard = access_context.DetectHazard(address_type, detector, recorded_access.first, DetectOptions::kDetectAll);
2390 if (hazard.hazard) break;
2391 }
2392 }
2393
2394 return hazard;
2395}
2396
John Zulaufbb890452021-12-14 11:30:18 -07002397bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &exec_context,
2398 const CMD_BUFFER_STATE &cmd, const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002399 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07002400 const auto &sync_state = exec_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002401 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002402 if (!pipe) {
2403 return skip;
2404 }
2405
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002406 const auto raster_state = pipe->RasterizationState();
2407 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002408 return skip;
2409 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002410 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002411 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002412
John Zulauf1a224292020-06-30 14:52:13 -06002413 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002414 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002415 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2416 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002417 if (location >= subpass.colorAttachmentCount ||
2418 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002419 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002420 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002421 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2422 if (!view_gen.IsValid()) continue;
2423 HazardResult hazard =
2424 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2425 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002426 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002427 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002428 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002429 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002430 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002431 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002432 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulaufbb890452021-12-14 11:30:18 -07002433 location, exec_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002434 }
2435 }
2436 }
locke-lunarg37047832020-06-12 13:44:45 -06002437
2438 // PHASE1 TODO: Add layout based read/vs. write selection.
2439 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002440 const auto ds_state = pipe->DepthStencilState();
2441 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002442
2443 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2444 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2445 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002446 bool depth_write = false, stencil_write = false;
2447
2448 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002449 if (!FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable && ds_state->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002450 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2451 depth_write = true;
2452 }
2453 // PHASE1 TODO: It needs to check if stencil is writable.
2454 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2455 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2456 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002457 if (!FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002458 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2459 stencil_write = true;
2460 }
2461
2462 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2463 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002464 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2465 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2466 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002467 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002468 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002469 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002470 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002471 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002472 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2473 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulaufbb890452021-12-14 11:30:18 -07002474 exec_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002475 }
2476 }
2477 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002478 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2479 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2480 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002481 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002482 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002483 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002484 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002485 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002486 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2487 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulaufbb890452021-12-14 11:30:18 -07002488 exec_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002489 }
locke-lunarg61870c22020-06-09 14:51:50 -06002490 }
2491 }
2492 return skip;
2493}
2494
John Zulauf14940722021-04-12 15:19:02 -06002495void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002496 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002497 if (!pipe) {
2498 return;
2499 }
2500
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002501 const auto *raster_state = pipe->RasterizationState();
2502 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002503 return;
2504 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002505 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002506 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002507
John Zulauf1a224292020-06-30 14:52:13 -06002508 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002509 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002510 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2511 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002512 if (location >= subpass.colorAttachmentCount ||
2513 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002514 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002515 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002516 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2517 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2518 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2519 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002520 }
2521 }
locke-lunarg37047832020-06-12 13:44:45 -06002522
2523 // PHASE1 TODO: Add layout based read/vs. write selection.
2524 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002525 const auto *ds_state = pipe->DepthStencilState();
2526 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002527 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2528 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2529 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002530 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002531 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2532 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002533
2534 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002535 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable &&
2536 ds_state->depthWriteEnable && IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
locke-lunarg37047832020-06-12 13:44:45 -06002537 depth_write = true;
2538 }
2539 // PHASE1 TODO: It needs to check if stencil is writable.
2540 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2541 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2542 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002543 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002544 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2545 stencil_write = true;
2546 }
2547
John Zulaufd0ec59f2021-03-13 14:25:08 -07002548 if (depth_write || stencil_write) {
2549 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2550 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2551 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2552 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002553 }
locke-lunarg61870c22020-06-09 14:51:50 -06002554 }
2555}
2556
John Zulaufbb890452021-12-14 11:30:18 -07002557bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &exec_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002558 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002559 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07002560 skip |= CurrentContext().ValidateResolveOperations(exec_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002561 current_subpass_);
John Zulaufbb890452021-12-14 11:30:18 -07002562 skip |= CurrentContext().ValidateStoreOperation(exec_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002563 func_name);
2564
John Zulauf355e49b2020-04-24 15:11:15 -06002565 const auto next_subpass = current_subpass_ + 1;
ziga-lunarg31a3e772022-03-22 11:48:46 +01002566 if (next_subpass >= subpass_contexts_.size()) {
2567 return skip;
2568 }
John Zulauf1507ee42020-05-18 11:33:09 -06002569 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002570 skip |=
John Zulaufbb890452021-12-14 11:30:18 -07002571 next_context.ValidateLayoutTransitions(exec_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002572 if (!skip) {
2573 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2574 // on a copy of the (empty) next context.
2575 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2576 AccessContext temp_context(next_context);
John Zulaufee984022022-04-13 16:39:50 -06002577 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kInvalidTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002578 skip |=
John Zulaufbb890452021-12-14 11:30:18 -07002579 temp_context.ValidateLoadOperation(exec_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002580 }
John Zulauf7635de32020-05-29 17:14:15 -06002581 return skip;
2582}
John Zulaufbb890452021-12-14 11:30:18 -07002583bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &exec_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002584 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002585 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07002586 skip |= CurrentContext().ValidateResolveOperations(exec_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002587 current_subpass_);
John Zulaufbb890452021-12-14 11:30:18 -07002588 skip |= CurrentContext().ValidateStoreOperation(exec_context, *rp_state_, render_area_, current_subpass_,
John Zulaufd0ec59f2021-03-13 14:25:08 -07002589
2590 attachment_views_, func_name);
John Zulaufbb890452021-12-14 11:30:18 -07002591 skip |= ValidateFinalSubpassLayoutTransitions(exec_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002592 return skip;
2593}
2594
John Zulauf64ffe552021-02-06 10:25:07 -07002595AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002596 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002597}
2598
John Zulaufbb890452021-12-14 11:30:18 -07002599bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &exec_context,
John Zulauf64ffe552021-02-06 10:25:07 -07002600 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002601 bool skip = false;
2602
John Zulauf7635de32020-05-29 17:14:15 -06002603 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2604 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2605 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2606 // to apply and only copy then, if this proves a hot spot.
2607 std::unique_ptr<AccessContext> proxy_for_current;
2608
John Zulauf355e49b2020-04-24 15:11:15 -06002609 // Validate the "finalLayout" transitions to external
2610 // Get them from where there we're hidding in the extra entry.
2611 const auto &final_transitions = rp_state_->subpass_transitions.back();
2612 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002613 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002614 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufbb890452021-12-14 11:30:18 -07002615 assert(trackback.source_subpass); // Transitions are given implicit transitions if the StateTracker is working correctly
2616 auto *context = trackback.source_subpass;
John Zulauf7635de32020-05-29 17:14:15 -06002617
2618 if (transition.prev_pass == current_subpass_) {
2619 if (!proxy_for_current) {
2620 // 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 -07002621 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002622 }
2623 context = proxy_for_current.get();
2624 }
2625
John Zulaufa0a98292020-09-18 09:30:10 -06002626 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2627 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002628 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002629 if (hazard.hazard) {
John Zulaufee984022022-04-13 16:39:50 -06002630 if (hazard.tag == kInvalidTag) {
2631 // Hazard vs. ILT
John Zulaufbb890452021-12-14 11:30:18 -07002632 skip |= exec_context.GetSyncState().LogError(
John Zulaufee984022022-04-13 16:39:50 -06002633 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
2634 "%s: Hazard %s vs. store/resolve operations in subpass %" PRIu32 " for attachment %" PRIu32
2635 " final image layout transition (old_layout: %s, new_layout: %s).",
2636 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2637 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout));
2638 } else {
John Zulaufbb890452021-12-14 11:30:18 -07002639 skip |= exec_context.GetSyncState().LogError(
John Zulaufee984022022-04-13 16:39:50 -06002640 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
2641 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2642 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2643 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2644 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulaufbb890452021-12-14 11:30:18 -07002645 exec_context.FormatUsage(hazard).c_str());
John Zulaufee984022022-04-13 16:39:50 -06002646 }
John Zulauf355e49b2020-04-24 15:11:15 -06002647 }
2648 }
2649 return skip;
2650}
2651
John Zulauf14940722021-04-12 15:19:02 -06002652void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002653 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002654 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002655}
2656
John Zulauf14940722021-04-12 15:19:02 -06002657void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002658 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2659 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002660
2661 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2662 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002663 const AttachmentViewGen &view_gen = attachment_views_[i];
2664 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002665
2666 const auto &ci = attachment_ci[i];
2667 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002668 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002669 const bool is_color = !(has_depth || has_stencil);
2670
2671 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002672 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2673 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2674 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2675 SyncOrdering::kColorAttachment, tag);
2676 }
John Zulauf1507ee42020-05-18 11:33:09 -06002677 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002678 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002679 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2680 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2681 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2682 SyncOrdering::kDepthStencilAttachment, tag);
2683 }
John Zulauf1507ee42020-05-18 11:33:09 -06002684 }
2685 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002686 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2687 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2688 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2689 SyncOrdering::kDepthStencilAttachment, tag);
2690 }
John Zulauf1507ee42020-05-18 11:33:09 -06002691 }
2692 }
2693 }
2694 }
2695}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002696AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2697 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2698 AttachmentViewGenVector view_gens;
2699 VkExtent3D extent = CastTo3D(render_area.extent);
2700 VkOffset3D offset = CastTo3D(render_area.offset);
2701 view_gens.reserve(attachment_views.size());
2702 for (const auto *view : attachment_views) {
2703 view_gens.emplace_back(view, offset, extent);
2704 }
2705 return view_gens;
2706}
John Zulauf64ffe552021-02-06 10:25:07 -07002707RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2708 VkQueueFlags queue_flags,
2709 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2710 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002711 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002712 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002713 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulaufbb890452021-12-14 11:30:18 -07002714 replay_context_ = std::make_shared<ReplayRenderpassContext>();
2715 auto &replay_subpass_contexts = replay_context_->subpass_contexts;
2716 replay_subpass_contexts.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002717 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002718 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulaufbb890452021-12-14 11:30:18 -07002719 replay_subpass_contexts.emplace_back(queue_flags, rp_state_->subpass_dependencies[pass], replay_subpass_contexts);
John Zulauf355e49b2020-04-24 15:11:15 -06002720 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002721 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002722}
John Zulauf41a9c7c2021-12-07 15:59:53 -07002723void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag barrier_tag, const ResourceUsageTag load_tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002724 assert(0 == current_subpass_);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002725 subpass_contexts_[current_subpass_].SetStartTag(barrier_tag);
2726 RecordLayoutTransitions(barrier_tag);
2727 RecordLoadOperations(load_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002728}
John Zulauf1507ee42020-05-18 11:33:09 -06002729
John Zulauf41a9c7c2021-12-07 15:59:53 -07002730void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag store_tag, const ResourceUsageTag barrier_tag,
2731 const ResourceUsageTag load_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002732 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulauf41a9c7c2021-12-07 15:59:53 -07002733 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
2734 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002735
ziga-lunarg31a3e772022-03-22 11:48:46 +01002736 if (current_subpass_ + 1 >= subpass_contexts_.size()) {
2737 return;
2738 }
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002739 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2740 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002741 current_subpass_++;
John Zulauf41a9c7c2021-12-07 15:59:53 -07002742 subpass_contexts_[current_subpass_].SetStartTag(barrier_tag);
2743 RecordLayoutTransitions(barrier_tag);
2744 RecordLoadOperations(load_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002745}
2746
John Zulauf41a9c7c2021-12-07 15:59:53 -07002747void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag store_tag,
2748 const ResourceUsageTag barrier_tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002749 // Add the resolve and store accesses
John Zulauf41a9c7c2021-12-07 15:59:53 -07002750 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
2751 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, store_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002752
John Zulauf355e49b2020-04-24 15:11:15 -06002753 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002754 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002755
2756 // Add the "finalLayout" transitions to external
2757 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002758 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2759 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2760 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002761 const auto &final_transitions = rp_state_->subpass_transitions.back();
2762 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002763 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002764 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufbb890452021-12-14 11:30:18 -07002765 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.source_subpass);
John Zulauf41a9c7c2021-12-07 15:59:53 -07002766 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), barrier_tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002767 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002768 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002769 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002770 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002771 }
2772}
2773
Jeremy Gebben40a22942020-12-22 14:22:06 -07002774SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002775 SyncExecScope result;
2776 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002777 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2778 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002779 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2780 return result;
2781}
2782
Jeremy Gebben40a22942020-12-22 14:22:06 -07002783SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002784 SyncExecScope result;
2785 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002786 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2787 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002788 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2789 return result;
2790}
2791
2792SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002793 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002794 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002795 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002796 dst_access_scope = 0;
2797}
2798
2799template <typename Barrier>
2800SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002801 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002802 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002803 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002804 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2805}
2806
2807SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002808 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2809 if (barrier) {
2810 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002811 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002812 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002813
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002814 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002815 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002816 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2817
2818 } else {
2819 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002820 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002821 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2822
2823 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002824 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002825 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2826 }
2827}
2828
2829template <typename Barrier>
2830SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2831 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2832 src_exec_scope = src.exec_scope;
2833 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2834
2835 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002836 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002837 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002838}
2839
John Zulaufb02c1eb2020-10-06 16:33:36 -06002840// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2841void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2842 for (const auto &barrier : barriers) {
2843 ApplyBarrier(barrier, layout_transition);
2844 }
2845}
2846
John Zulauf89311b42020-09-29 16:28:47 -06002847// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2848// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2849// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufbb890452021-12-14 11:30:18 -07002850void ResourceAccessState::ApplyBarriersImmediate(const std::vector<SyncBarrier> &barriers) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002851 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002852 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002853 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002854 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002855 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002856 }
John Zulaufbb890452021-12-14 11:30:18 -07002857 ApplyPendingBarriers(kInvalidTag); // There can't be any need for this tag
John Zulauf3d84f1b2020-03-09 13:33:25 -06002858}
John Zulauf9cb530d2019-09-30 14:14:10 -06002859HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2860 HazardResult hazard;
2861 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002862 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002863 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002864 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002865 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002866 }
2867 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002868 // Write operation:
2869 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2870 // If reads exists -- test only against them because either:
2871 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2872 // * 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
2873 // the current write happens after the reads, so just test the write against the reades
2874 // Otherwise test against last_write
2875 //
2876 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002877 if (last_reads.size()) {
2878 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002879 if (IsReadHazard(usage_stage, read_access)) {
2880 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2881 break;
2882 }
2883 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002884 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002885 // Write-After-Write check -- if we have a previous write to test against
2886 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002887 }
2888 }
2889 return hazard;
2890}
2891
John Zulauf4fa68462021-04-26 21:04:22 -06002892HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002893 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002894 return DetectHazard(usage_index, ordering);
2895}
2896
2897HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002898 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2899 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002900 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002901 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002902 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2903 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002904 if (IsRead(usage_bit)) {
2905 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2906 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2907 if (is_raw_hazard) {
2908 // NOTE: we know last_write is non-zero
2909 // See if the ordering rules save us from the simple RAW check above
2910 // First check to see if the current usage is covered by the ordering rules
2911 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2912 const bool usage_is_ordered =
2913 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2914 if (usage_is_ordered) {
2915 // Now see of the most recent write (or a subsequent read) are ordered
2916 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2917 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002918 }
2919 }
John Zulauf4285ee92020-09-23 10:20:52 -06002920 if (is_raw_hazard) {
2921 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2922 }
John Zulauf5c628d02021-05-04 15:46:36 -06002923 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2924 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2925 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002926 } else {
2927 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002928 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002929 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002930 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002931 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002932 if (usage_write_is_ordered) {
2933 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2934 ordered_stages = GetOrderedStages(ordering);
2935 }
2936 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2937 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002938 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002939 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2940 if (IsReadHazard(usage_stage, read_access)) {
2941 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2942 break;
2943 }
John Zulaufd14743a2020-07-03 09:42:39 -06002944 }
2945 }
John Zulauf2a344ca2021-09-09 17:07:19 -06002946 } else if (last_write.any() && !(last_write_is_ordered && usage_write_is_ordered)) {
2947 bool ilt_ilt_hazard = false;
2948 if ((usage_index == SYNC_IMAGE_LAYOUT_TRANSITION) && (usage_bit == last_write)) {
2949 // ILT after ILT is a special case where we check the 2nd access scope of the first ILT against the first access
2950 // scope of the second ILT, which has been passed (smuggled?) in the ordering barrier
2951 ilt_ilt_hazard = !(write_barriers & ordering.access_scope).any();
2952 }
2953 if (ilt_ilt_hazard || IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002954 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002955 }
John Zulauf69133422020-05-20 14:55:53 -06002956 }
2957 }
2958 return hazard;
2959}
2960
John Zulaufae842002021-04-15 18:20:55 -06002961HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2962 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002963 using Size = FirstAccesses::size_type;
2964 const auto &recorded_accesses = recorded_use.first_accesses_;
2965 Size count = recorded_accesses.size();
2966 if (count) {
2967 const auto &last_access = recorded_accesses.back();
2968 bool do_write_last = IsWrite(last_access.usage_index);
2969 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06002970
John Zulauf4fa68462021-04-26 21:04:22 -06002971 for (Size i = 0; i < count; ++count) {
2972 const auto &first = recorded_accesses[i];
2973 // Skip and quit logic
2974 if (first.tag < tag_range.begin) continue;
2975 if (first.tag >= tag_range.end) {
2976 do_write_last = false; // ignore last since we know it can't be in tag_range
2977 break;
2978 }
2979
2980 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2981 if (hazard.hazard) {
2982 hazard.AddRecordedAccess(first);
2983 break;
2984 }
2985 }
2986
2987 if (do_write_last && tag_range.includes(last_access.tag)) {
2988 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
2989 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
2990 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2991 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
2992 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
2993 // the barrier that applies them
2994 barrier |= recorded_use.first_write_layout_ordering_;
2995 }
2996 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
2997 // the active context
2998 if (recorded_use.first_read_stages_) {
2999 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
3000 // reads in the active context are not "most recent" as all recorded context operations are *after* them
3001 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
3002 // active context.
3003 barrier.exec_scope |= recorded_use.first_read_stages_;
3004 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
3005 barrier.access_scope |= FlagBit(last_access.usage_index);
3006 }
3007 hazard = DetectHazard(last_access.usage_index, barrier);
3008 if (hazard.hazard) {
3009 hazard.AddRecordedAccess(last_access);
3010 }
3011 }
John Zulaufae842002021-04-15 18:20:55 -06003012 }
3013 return hazard;
3014}
3015
John Zulauf2f952d22020-02-10 11:34:51 -07003016// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06003017HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07003018 HazardResult hazard;
3019 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003020 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
3021 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
3022 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07003023 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06003024 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06003025 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07003026 }
3027 } else {
John Zulauf14940722021-04-12 15:19:02 -06003028 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06003029 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07003030 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003031 // 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 -07003032 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003033 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003034 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07003035 break;
3036 }
3037 }
John Zulauf2f952d22020-02-10 11:34:51 -07003038 }
3039 }
3040 return hazard;
3041}
3042
John Zulaufae842002021-04-15 18:20:55 -06003043HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
3044 ResourceUsageTag start_tag) const {
3045 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06003046 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06003047 // Skip and quit logic
3048 if (first.tag < tag_range.begin) continue;
3049 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06003050
3051 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06003052 if (hazard.hazard) {
3053 hazard.AddRecordedAccess(first);
3054 break;
3055 }
John Zulaufae842002021-04-15 18:20:55 -06003056 }
3057 return hazard;
3058}
3059
Jeremy Gebben40a22942020-12-22 14:22:06 -07003060HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003061 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07003062 // Only supporting image layout transitions for now
3063 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3064 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06003065 // only test for WAW if there no intervening read operations.
3066 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07003067 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06003068 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07003069 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003070 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06003071 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07003072 break;
3073 }
3074 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003075 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3076 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3077 }
3078
3079 return hazard;
3080}
3081
Jeremy Gebben40a22942020-12-22 14:22:06 -07003082HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07003083 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06003084 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07003085 // Only supporting image layout transitions for now
3086 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3087 HazardResult hazard;
3088 // only test for WAW if there no intervening read operations.
3089 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3090
John Zulaufab7756b2020-12-29 16:10:16 -07003091 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003092 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3093 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003094 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003095 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003096 // The read is in the events first synchronization scope, so we use a barrier hazard check
3097 // If the read stage is not in the src sync scope
3098 // *AND* not execution chained with an existing sync barrier (that's the or)
3099 // then the barrier access is unsafe (R/W after R)
3100 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3101 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3102 break;
3103 }
3104 } else {
3105 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3106 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3107 }
3108 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003109 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003110 // 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 -06003111 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003112 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3113 // So do a normal barrier hazard check
3114 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3115 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3116 }
3117 } else {
3118 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003119 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3120 }
John Zulaufd14743a2020-07-03 09:42:39 -06003121 }
John Zulauf361fb532020-07-22 10:45:39 -06003122
John Zulauf0cb5be22020-01-23 12:18:22 -07003123 return hazard;
3124}
3125
John Zulauf5f13a792020-03-10 07:31:21 -06003126// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3127// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3128// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3129void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003130 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003131 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3132 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003133 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003134 } else if (other.write_tag == write_tag) {
3135 // 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 -06003136 // dependency chaining logic or any stage expansion)
3137 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003138 pending_write_barriers |= other.pending_write_barriers;
3139 pending_layout_transition |= other.pending_layout_transition;
3140 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003141 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003142
John Zulaufd14743a2020-07-03 09:42:39 -06003143 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003144 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003145 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003146 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003147 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003148 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003149 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003150 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3151 // but we should wait on profiling data for that.
3152 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003153 auto &my_read = last_reads[my_read_index];
3154 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003155 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003156 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003157 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003158 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003159 my_read.pending_dep_chain = other_read.pending_dep_chain;
3160 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3161 // May require tracking more than one access per stage.
3162 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003163 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003164 // Since I'm overwriting the fragement stage read, also update the input attachment info
3165 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003166 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003167 }
John Zulauf14940722021-04-12 15:19:02 -06003168 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003169 // The read tags match so merge the barriers
3170 my_read.barriers |= other_read.barriers;
3171 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003172 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003173
John Zulauf5f13a792020-03-10 07:31:21 -06003174 break;
3175 }
3176 }
3177 } else {
3178 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003179 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003180 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003181 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003182 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003183 }
John Zulauf5f13a792020-03-10 07:31:21 -06003184 }
3185 }
John Zulauf361fb532020-07-22 10:45:39 -06003186 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003187 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3188 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003189
3190 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3191 // of the copy and other into this using the update first logic.
3192 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3193 // of the other first_accesses... )
3194 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3195 FirstAccesses firsts(std::move(first_accesses_));
3196 first_accesses_.clear();
3197 first_read_stages_ = 0U;
3198 auto a = firsts.begin();
3199 auto a_end = firsts.end();
3200 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003201 // TODO: Determine whether some tag offset will be needed for PHASE II
3202 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003203 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3204 ++a;
3205 }
3206 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3207 }
3208 for (; a != a_end; ++a) {
3209 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3210 }
3211 }
John Zulauf5f13a792020-03-10 07:31:21 -06003212}
3213
John Zulauf14940722021-04-12 15:19:02 -06003214void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003215 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3216 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003217 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003218 // Mulitple outstanding reads may be of interest and do dependency chains independently
3219 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3220 const auto usage_stage = PipelineStageBit(usage_index);
3221 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003222 for (auto &read_access : last_reads) {
3223 if (read_access.stage == usage_stage) {
3224 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003225 break;
3226 }
3227 }
3228 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003229 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003230 last_read_stages |= usage_stage;
3231 }
John Zulauf4285ee92020-09-23 10:20:52 -06003232
3233 // 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 -07003234 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003235 // TODO Revisit re: multiple reads for a given stage
3236 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003237 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003238 } else {
3239 // Assume write
3240 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003241 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003242 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003243 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003244}
John Zulauf5f13a792020-03-10 07:31:21 -06003245
John Zulauf89311b42020-09-29 16:28:47 -06003246// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3247// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3248// We can overwrite them as *this* write is now after them.
3249//
3250// 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 -06003251void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003252 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003253 last_read_stages = 0;
3254 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003255 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003256
3257 write_barriers = 0;
3258 write_dependency_chain = 0;
3259 write_tag = tag;
3260 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003261}
3262
John Zulauf89311b42020-09-29 16:28:47 -06003263// Apply the memory barrier without updating the existing barriers. The execution barrier
3264// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3265// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3266// replace the current write barriers or add to them, so accumulate to pending as well.
3267void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3268 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3269 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003270 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3271 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3272 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3273 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003274 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003275 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003276 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003277 if (layout_transition) {
3278 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3279 }
John Zulaufa0a98292020-09-18 09:30:10 -06003280 }
John Zulauf89311b42020-09-29 16:28:47 -06003281 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3282 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003283
John Zulauf89311b42020-09-29 16:28:47 -06003284 if (!pending_layout_transition) {
3285 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3286 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003287 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003288 // 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 -07003289 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3290 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003291 }
3292 }
John Zulaufa0a98292020-09-18 09:30:10 -06003293 }
John Zulaufa0a98292020-09-18 09:30:10 -06003294}
3295
John Zulauf4a6105a2020-11-17 15:11:05 -07003296// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3297// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003298void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003299 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3300 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3301 // in order to know if it's in the excecution scope
3302 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3303 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3304 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003305 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003306 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003307 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003308 if (layout_transition) {
3309 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3310 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003311 }
3312 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3313 pending_layout_transition |= layout_transition;
3314
3315 if (!pending_layout_transition) {
3316 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3317 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003318 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003319 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3320 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3321 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3322 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3323 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3324 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3325 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003326 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 -07003327 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003328 }
3329 }
3330 }
3331}
John Zulauf14940722021-04-12 15:19:02 -06003332void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003333 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003334 // 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 -06003335 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003336 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003337 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3338 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003339 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003340 }
John Zulauf89311b42020-09-29 16:28:47 -06003341
3342 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003343 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003344 for (auto &read_access : last_reads) {
3345 read_access.barriers |= read_access.pending_dep_chain;
3346 read_execution_barriers |= read_access.barriers;
3347 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003348 }
3349
3350 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3351 write_dependency_chain |= pending_write_dep_chain;
3352 write_barriers |= pending_write_barriers;
3353 pending_write_dep_chain = 0;
3354 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003355}
3356
John Zulaufae842002021-04-15 18:20:55 -06003357bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3358 if (!first_accesses_.size()) return false;
3359 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3360 return tag_range.intersects(first_access_range);
3361}
3362
John Zulauf59e25072020-07-17 10:55:21 -06003363// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003364VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3365 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003366
John Zulaufab7756b2020-12-29 16:10:16 -07003367 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003368 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003369 barriers = read_access.barriers;
3370 break;
John Zulauf59e25072020-07-17 10:55:21 -06003371 }
3372 }
John Zulauf4285ee92020-09-23 10:20:52 -06003373
John Zulauf59e25072020-07-17 10:55:21 -06003374 return barriers;
3375}
3376
Jeremy Gebben40a22942020-12-22 14:22:06 -07003377inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003378 assert(IsRead(usage));
3379 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3380 // * the previous reads are not hazards, and thus last_write must be visible and available to
3381 // any reads that happen after.
3382 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3383 // 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 -07003384 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003385}
3386
Jeremy Gebben40a22942020-12-22 14:22:06 -07003387VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003388 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003389 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003390 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003391 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003392 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003393 // 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 -07003394 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003395 }
3396
3397 return ordered_stages;
3398}
3399
John Zulauf14940722021-04-12 15:19:02 -06003400void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003401 // Only record until we record a write.
3402 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003403 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003404 if (0 == (usage_stage & first_read_stages_)) {
3405 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003406 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003407 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003408 if (0 == (read_execution_barriers & usage_stage)) {
3409 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3410 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3411 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003412 }
3413 }
3414}
3415
John Zulauf4fa68462021-04-26 21:04:22 -06003416void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3417 // Only call this after recording an image layout transition
3418 assert(first_accesses_.size());
3419 if (first_accesses_.back().tag == tag) {
3420 // 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 +02003421 assert(first_accesses_.back().usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
John Zulauf4fa68462021-04-26 21:04:22 -06003422 first_write_layout_ordering_ = layout_ordering;
3423 }
3424}
3425
John Zulaufee984022022-04-13 16:39:50 -06003426void ResourceAccessState::ReadState::Set(VkPipelineStageFlags2KHR stage_, const SyncStageAccessFlags &access_,
3427 VkPipelineStageFlags2KHR barriers_, ResourceUsageTag tag_) {
3428 stage = stage_;
3429 access = access_;
3430 barriers = barriers_;
3431 tag = tag_;
3432 pending_dep_chain = 0; // If this is a new read, we aren't applying a barrier set.
3433}
3434
John Zulaufd1f85d42020-04-15 12:23:15 -06003435void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003436 auto *access_context = GetAccessContextNoInsert(command_buffer);
3437 if (access_context) {
3438 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003439 }
3440}
3441
John Zulaufd1f85d42020-04-15 12:23:15 -06003442void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3443 auto access_found = cb_access_state.find(command_buffer);
3444 if (access_found != cb_access_state.end()) {
3445 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003446 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003447 cb_access_state.erase(access_found);
3448 }
3449}
3450
John Zulauf9cb530d2019-09-30 14:14:10 -06003451bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3452 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3453 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003454 const auto *cb_context = GetAccessContext(commandBuffer);
3455 assert(cb_context);
3456 if (!cb_context) return skip;
3457 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003458
John Zulauf3d84f1b2020-03-09 13:33:25 -06003459 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003460 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3461 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003462
3463 for (uint32_t region = 0; region < regionCount; region++) {
3464 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003465 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003466 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003467 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003468 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003469 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003470 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003471 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003472 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003473 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003474 }
John Zulauf16adfc92020-04-08 10:28:33 -06003475 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003476 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003477 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003478 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003479 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003480 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003481 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003482 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003483 }
3484 }
3485 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003486 }
3487 return skip;
3488}
3489
3490void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3491 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003492 auto *cb_context = GetAccessContext(commandBuffer);
3493 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003494 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003495 auto *context = cb_context->GetCurrentAccessContext();
3496
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003497 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3498 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003499
3500 for (uint32_t region = 0; region < regionCount; region++) {
3501 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003502 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003503 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003504 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003505 }
John Zulauf16adfc92020-04-08 10:28:33 -06003506 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003507 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003508 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003509 }
3510 }
3511}
3512
John Zulauf4a6105a2020-11-17 15:11:05 -07003513void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3514 // Clear out events from the command buffer contexts
3515 for (auto &cb_context : cb_access_state) {
3516 cb_context.second->RecordDestroyEvent(event);
3517 }
3518}
3519
Tony-LunarGef035472021-11-02 10:23:33 -06003520bool SyncValidator::ValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos,
3521 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003522 bool skip = false;
3523 const auto *cb_context = GetAccessContext(commandBuffer);
3524 assert(cb_context);
3525 if (!cb_context) return skip;
3526 const auto *context = cb_context->GetCurrentAccessContext();
Tony-LunarGef035472021-11-02 10:23:33 -06003527 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003528
3529 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003530 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3531 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003532
3533 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3534 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3535 if (src_buffer) {
3536 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003537 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003538 if (hazard.hazard) {
3539 // TODO -- add tag information to log msg when useful.
3540 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003541 "%s(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003542 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003543 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003544 }
3545 }
3546 if (dst_buffer && !skip) {
3547 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003548 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003549 if (hazard.hazard) {
3550 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003551 "%s(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003552 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003553 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003554 }
3555 }
3556 if (skip) break;
3557 }
3558 return skip;
3559}
3560
Tony-LunarGef035472021-11-02 10:23:33 -06003561bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3562 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3563 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3564}
3565
3566bool SyncValidator::PreCallValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) const {
3567 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3568}
3569
3570void SyncValidator::RecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003571 auto *cb_context = GetAccessContext(commandBuffer);
3572 assert(cb_context);
Tony-LunarGef035472021-11-02 10:23:33 -06003573 const auto tag = cb_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003574 auto *context = cb_context->GetCurrentAccessContext();
3575
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003576 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3577 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003578
3579 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3580 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3581 if (src_buffer) {
3582 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003583 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003584 }
3585 if (dst_buffer) {
3586 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003587 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003588 }
3589 }
3590}
3591
Tony-LunarGef035472021-11-02 10:23:33 -06003592void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3593 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3594}
3595
3596void SyncValidator::PreCallRecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) {
3597 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3598}
3599
John Zulauf5c5e88d2019-12-26 11:22:02 -07003600bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3601 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3602 const VkImageCopy *pRegions) const {
3603 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003604 const auto *cb_access_context = GetAccessContext(commandBuffer);
3605 assert(cb_access_context);
3606 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003607
John Zulauf3d84f1b2020-03-09 13:33:25 -06003608 const auto *context = cb_access_context->GetCurrentAccessContext();
3609 assert(context);
3610 if (!context) return skip;
3611
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003612 auto src_image = Get<IMAGE_STATE>(srcImage);
3613 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003614 for (uint32_t region = 0; region < regionCount; region++) {
3615 const auto &copy_region = pRegions[region];
3616 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003617 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003618 copy_region.srcOffset, copy_region.extent);
3619 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003620 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003621 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003622 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003623 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003624 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003625 }
3626
3627 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003628 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003629 copy_region.dstOffset, copy_region.extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003630 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003631 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003632 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003633 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003634 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003635 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003636 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003637 }
3638 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003639
John Zulauf5c5e88d2019-12-26 11:22:02 -07003640 return skip;
3641}
3642
3643void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3644 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3645 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003646 auto *cb_access_context = GetAccessContext(commandBuffer);
3647 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003648 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003649 auto *context = cb_access_context->GetCurrentAccessContext();
3650 assert(context);
3651
Jeremy Gebben9f537102021-10-05 16:37:12 -06003652 auto src_image = Get<IMAGE_STATE>(srcImage);
3653 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003654
3655 for (uint32_t region = 0; region < regionCount; region++) {
3656 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003657 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003658 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003659 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003660 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003661 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003662 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003663 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003664 }
3665 }
3666}
3667
Tony-LunarGb61514a2021-11-02 12:36:51 -06003668bool SyncValidator::ValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo,
3669 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003670 bool skip = false;
3671 const auto *cb_access_context = GetAccessContext(commandBuffer);
3672 assert(cb_access_context);
3673 if (!cb_access_context) return skip;
3674
3675 const auto *context = cb_access_context->GetCurrentAccessContext();
3676 assert(context);
3677 if (!context) return skip;
3678
Tony-LunarGb61514a2021-11-02 12:36:51 -06003679 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003680 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3681 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003682
Jeff Leger178b1e52020-10-05 12:22:23 -04003683 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3684 const auto &copy_region = pCopyImageInfo->pRegions[region];
3685 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003686 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003687 copy_region.srcOffset, copy_region.extent);
3688 if (hazard.hazard) {
3689 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003690 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003691 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003692 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003693 }
3694 }
3695
3696 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003697 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003698 copy_region.dstOffset, copy_region.extent);
Jeff Leger178b1e52020-10-05 12:22:23 -04003699 if (hazard.hazard) {
3700 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003701 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003702 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003703 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003704 }
3705 if (skip) break;
3706 }
3707 }
3708
3709 return skip;
3710}
3711
Tony-LunarGb61514a2021-11-02 12:36:51 -06003712bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3713 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3714 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3715}
3716
3717bool SyncValidator::PreCallValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) const {
3718 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3719}
3720
3721void SyncValidator::RecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003722 auto *cb_access_context = GetAccessContext(commandBuffer);
3723 assert(cb_access_context);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003724 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003725 auto *context = cb_access_context->GetCurrentAccessContext();
3726 assert(context);
3727
Jeremy Gebben9f537102021-10-05 16:37:12 -06003728 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3729 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04003730
3731 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3732 const auto &copy_region = pCopyImageInfo->pRegions[region];
3733 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003734 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003735 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003736 }
3737 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003738 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003739 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003740 }
3741 }
3742}
3743
Tony-LunarGb61514a2021-11-02 12:36:51 -06003744void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3745 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3746}
3747
3748void SyncValidator::PreCallRecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) {
3749 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3750}
3751
John Zulauf9cb530d2019-09-30 14:14:10 -06003752bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3753 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3754 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3755 uint32_t bufferMemoryBarrierCount,
3756 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3757 uint32_t imageMemoryBarrierCount,
3758 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3759 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003760 const auto *cb_access_context = GetAccessContext(commandBuffer);
3761 assert(cb_access_context);
3762 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003763
John Zulauf36ef9282021-02-02 11:47:24 -07003764 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3765 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3766 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3767 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003768 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003769 return skip;
3770}
3771
3772void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3773 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3774 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3775 uint32_t bufferMemoryBarrierCount,
3776 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3777 uint32_t imageMemoryBarrierCount,
3778 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003779 auto *cb_access_context = GetAccessContext(commandBuffer);
3780 assert(cb_access_context);
3781 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003782
John Zulauf1bf30522021-09-03 15:39:06 -06003783 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(),
3784 srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount,
3785 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3786 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06003787}
3788
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003789bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3790 const VkDependencyInfoKHR *pDependencyInfo) const {
3791 bool skip = false;
3792 const auto *cb_access_context = GetAccessContext(commandBuffer);
3793 assert(cb_access_context);
3794 if (!cb_access_context) return skip;
3795
3796 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3797 skip = pipeline_barrier.Validate(*cb_access_context);
3798 return skip;
3799}
3800
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003801bool SyncValidator::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
3802 const VkDependencyInfo *pDependencyInfo) const {
3803 bool skip = false;
3804 const auto *cb_access_context = GetAccessContext(commandBuffer);
3805 assert(cb_access_context);
3806 if (!cb_access_context) return skip;
3807
3808 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3809 skip = pipeline_barrier.Validate(*cb_access_context);
3810 return skip;
3811}
3812
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003813void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3814 auto *cb_access_context = GetAccessContext(commandBuffer);
3815 assert(cb_access_context);
3816 if (!cb_access_context) return;
3817
John Zulauf1bf30522021-09-03 15:39:06 -06003818 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(),
3819 *pDependencyInfo);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003820}
3821
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003822void SyncValidator::PreCallRecordCmdPipelineBarrier2(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) {
3823 auto *cb_access_context = GetAccessContext(commandBuffer);
3824 assert(cb_access_context);
3825 if (!cb_access_context) return;
3826
3827 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(),
3828 *pDependencyInfo);
3829}
3830
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003831void SyncValidator::CreateDevice(const VkDeviceCreateInfo *pCreateInfo) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003832 // The state tracker sets up the device state
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003833 StateTracker::CreateDevice(pCreateInfo);
John Zulauf9cb530d2019-09-30 14:14:10 -06003834
John Zulauf5f13a792020-03-10 07:31:21 -06003835 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3836 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003837 // TODO: Find a good way to do this hooklessly.
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003838 SetCommandBufferResetCallback([this](VkCommandBuffer command_buffer) -> void { ResetCommandBufferCallback(command_buffer); });
3839 SetCommandBufferFreeCallback([this](VkCommandBuffer command_buffer) -> void { FreeCommandBufferCallback(command_buffer); });
John Zulauf9cb530d2019-09-30 14:14:10 -06003840}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003841
John Zulauf355e49b2020-04-24 15:11:15 -06003842bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003843 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003844 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003845 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003846 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003847 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003848 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003849 }
John Zulauf355e49b2020-04-24 15:11:15 -06003850 return skip;
3851}
3852
3853bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3854 VkSubpassContents contents) const {
3855 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003856 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003857 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003858 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003859 return skip;
3860}
3861
3862bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003863 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003864 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003865 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003866 return skip;
3867}
3868
3869bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3870 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003871 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003872 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003873 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003874 return skip;
3875}
3876
John Zulauf3d84f1b2020-03-09 13:33:25 -06003877void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3878 VkResult result) {
3879 // The state tracker sets up the command buffer state
3880 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3881
3882 // Create/initialize the structure that trackers accesses at the command buffer scope.
3883 auto cb_access_context = GetAccessContext(commandBuffer);
3884 assert(cb_access_context);
3885 cb_access_context->Reset();
3886}
3887
3888void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003889 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003890 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003891 if (cb_context) {
John Zulaufbb890452021-12-14 11:30:18 -07003892 cb_context->RecordSyncOp<SyncOpBeginRenderPass>(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003893 }
3894}
3895
3896void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3897 VkSubpassContents contents) {
3898 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003899 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003900 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003901 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003902}
3903
3904void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3905 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3906 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003907 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003908}
3909
3910void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3911 const VkRenderPassBeginInfo *pRenderPassBegin,
3912 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3913 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003914 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003915}
3916
Mike Schuchardt2df08912020-12-15 16:28:09 -08003917bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003918 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003919 bool skip = false;
3920
3921 auto cb_context = GetAccessContext(commandBuffer);
3922 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003923 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003924 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003925 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003926}
3927
3928bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3929 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003930 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003931 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003932 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003933 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3934 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003935 return skip;
3936}
3937
Mike Schuchardt2df08912020-12-15 16:28:09 -08003938bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3939 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003940 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003941 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003942 return skip;
3943}
3944
3945bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3946 const VkSubpassEndInfo *pSubpassEndInfo) const {
3947 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003948 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003949 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003950}
3951
3952void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003953 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003954 auto cb_context = GetAccessContext(commandBuffer);
3955 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003956 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003957
John Zulaufbb890452021-12-14 11:30:18 -07003958 cb_context->RecordSyncOp<SyncOpNextSubpass>(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003959}
3960
3961void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3962 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003963 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003964 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003965 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003966}
3967
3968void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3969 const VkSubpassEndInfo *pSubpassEndInfo) {
3970 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003971 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003972}
3973
3974void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3975 const VkSubpassEndInfo *pSubpassEndInfo) {
3976 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003977 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003978}
3979
sfricke-samsung85584a72021-09-30 21:43:38 -07003980bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3981 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003982 bool skip = false;
3983
3984 auto cb_context = GetAccessContext(commandBuffer);
3985 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003986 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003987
sfricke-samsung85584a72021-09-30 21:43:38 -07003988 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003989 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003990 return skip;
3991}
3992
3993bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3994 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003995 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003996 return skip;
3997}
3998
Mike Schuchardt2df08912020-12-15 16:28:09 -08003999bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004000 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07004001 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06004002 return skip;
4003}
4004
4005bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08004006 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06004007 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07004008 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06004009 return skip;
4010}
4011
sfricke-samsung85584a72021-09-30 21:43:38 -07004012void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06004013 // Resolve the all subpass contexts to the command buffer contexts
4014 auto cb_context = GetAccessContext(commandBuffer);
4015 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07004016 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06004017
John Zulaufbb890452021-12-14 11:30:18 -07004018 cb_context->RecordSyncOp<SyncOpEndRenderPass>(cmd, *this, pSubpassEndInfo);
John Zulaufe5da6e52020-03-18 15:32:18 -06004019}
John Zulauf3d84f1b2020-03-09 13:33:25 -06004020
John Zulauf33fc1d52020-07-17 11:01:10 -06004021// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
4022// updates to a resource which do not conflict at the byte level.
4023// TODO: Revisit this rule to see if it needs to be tighter or looser
4024// TODO: Add programatic control over suppression heuristics
4025bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
4026 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
4027}
4028
John Zulauf3d84f1b2020-03-09 13:33:25 -06004029void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06004030 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06004031 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004032}
4033
4034void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06004035 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06004036 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004037}
4038
4039void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07004040 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06004041 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06004042}
locke-lunarga19c71d2020-03-02 18:17:04 -07004043
sfricke-samsung71f04e32022-03-16 01:21:21 -05004044template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004045bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004046 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4047 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004048 bool skip = false;
4049 const auto *cb_access_context = GetAccessContext(commandBuffer);
4050 assert(cb_access_context);
4051 if (!cb_access_context) return skip;
4052
Tony Barbour845d29b2021-11-09 11:43:14 -07004053 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004054
locke-lunarga19c71d2020-03-02 18:17:04 -07004055 const auto *context = cb_access_context->GetCurrentAccessContext();
4056 assert(context);
4057 if (!context) return skip;
4058
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004059 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4060 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004061
4062 for (uint32_t region = 0; region < regionCount; region++) {
4063 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07004064 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07004065 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004066 if (src_buffer) {
4067 ResourceAccessRange src_range =
4068 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004069 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07004070 if (hazard.hazard) {
4071 // PHASE1 TODO -- add tag information to log msg when useful.
4072 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
4073 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4074 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004075 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004076 }
4077 }
4078
Jeremy Gebben40a22942020-12-22 14:22:06 -07004079 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07004080 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004081 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004082 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004083 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004084 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004085 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004086 }
4087 if (skip) break;
4088 }
4089 if (skip) break;
4090 }
4091 return skip;
4092}
4093
Jeff Leger178b1e52020-10-05 12:22:23 -04004094bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4095 VkImageLayout dstImageLayout, uint32_t regionCount,
4096 const VkBufferImageCopy *pRegions) const {
4097 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
Tony Barbour845d29b2021-11-09 11:43:14 -07004098 CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004099}
4100
4101bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4102 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4103 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4104 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004105 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4106}
4107
4108bool SyncValidator::PreCallValidateCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4109 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) const {
4110 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4111 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4112 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004113}
4114
sfricke-samsung71f04e32022-03-16 01:21:21 -05004115template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004116void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004117 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4118 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004119 auto *cb_access_context = GetAccessContext(commandBuffer);
4120 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004121
Jeff Leger178b1e52020-10-05 12:22:23 -04004122 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004123 auto *context = cb_access_context->GetCurrentAccessContext();
4124 assert(context);
4125
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004126 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4127 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004128
4129 for (uint32_t region = 0; region < regionCount; region++) {
4130 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07004131 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004132 if (src_buffer) {
4133 ResourceAccessRange src_range =
4134 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004135 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004136 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07004137 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004138 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004139 }
4140 }
4141}
4142
Jeff Leger178b1e52020-10-05 12:22:23 -04004143void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4144 VkImageLayout dstImageLayout, uint32_t regionCount,
4145 const VkBufferImageCopy *pRegions) {
4146 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
Tony Barbour845d29b2021-11-09 11:43:14 -07004147 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004148}
4149
4150void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4151 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4152 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4153 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4154 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004155 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4156}
4157
4158void SyncValidator::PreCallRecordCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4159 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) {
4160 StateTracker::PreCallRecordCmdCopyBufferToImage2(commandBuffer, pCopyBufferToImageInfo);
4161 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4162 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4163 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004164}
4165
sfricke-samsung71f04e32022-03-16 01:21:21 -05004166template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004167bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004168 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
4169 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004170 bool skip = false;
4171 const auto *cb_access_context = GetAccessContext(commandBuffer);
4172 assert(cb_access_context);
4173 if (!cb_access_context) return skip;
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004174 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004175
locke-lunarga19c71d2020-03-02 18:17:04 -07004176 const auto *context = cb_access_context->GetCurrentAccessContext();
4177 assert(context);
4178 if (!context) return skip;
4179
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004180 auto src_image = Get<IMAGE_STATE>(srcImage);
4181 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004182 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004183 for (uint32_t region = 0; region < regionCount; region++) {
4184 const auto &copy_region = pRegions[region];
4185 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004186 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004187 copy_region.imageOffset, copy_region.imageExtent);
4188 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004189 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004190 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004191 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004192 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004193 }
John Zulauf477700e2021-01-06 11:41:49 -07004194 if (dst_mem) {
4195 ResourceAccessRange dst_range =
4196 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004197 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004198 if (hazard.hazard) {
4199 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4200 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4201 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004202 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004203 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004204 }
4205 }
4206 if (skip) break;
4207 }
4208 return skip;
4209}
4210
Jeff Leger178b1e52020-10-05 12:22:23 -04004211bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4212 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4213 const VkBufferImageCopy *pRegions) const {
4214 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004215 CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004216}
4217
4218bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4219 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4220 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4221 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004222 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4223}
4224
4225bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4226 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) const {
4227 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4228 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4229 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004230}
4231
sfricke-samsung71f04e32022-03-16 01:21:21 -05004232template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004233void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004234 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004235 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004236 auto *cb_access_context = GetAccessContext(commandBuffer);
4237 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004238
Jeff Leger178b1e52020-10-05 12:22:23 -04004239 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004240 auto *context = cb_access_context->GetCurrentAccessContext();
4241 assert(context);
4242
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004243 auto src_image = Get<IMAGE_STATE>(srcImage);
Jeremy Gebben9f537102021-10-05 16:37:12 -06004244 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004245 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004246 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004247
4248 for (uint32_t region = 0; region < regionCount; region++) {
4249 const auto &copy_region = pRegions[region];
4250 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004251 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004252 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004253 if (dst_buffer) {
4254 ResourceAccessRange dst_range =
4255 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004256 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004257 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004258 }
4259 }
4260}
4261
Jeff Leger178b1e52020-10-05 12:22:23 -04004262void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4263 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4264 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004265 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004266}
4267
4268void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4269 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4270 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4271 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4272 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004273 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4274}
4275
4276void SyncValidator::PreCallRecordCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4277 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) {
4278 StateTracker::PreCallRecordCmdCopyImageToBuffer2(commandBuffer, pCopyImageToBufferInfo);
4279 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4280 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4281 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004282}
4283
4284template <typename RegionType>
4285bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4286 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4287 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004288 bool skip = false;
4289 const auto *cb_access_context = GetAccessContext(commandBuffer);
4290 assert(cb_access_context);
4291 if (!cb_access_context) return skip;
4292
4293 const auto *context = cb_access_context->GetCurrentAccessContext();
4294 assert(context);
4295 if (!context) return skip;
4296
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004297 auto src_image = Get<IMAGE_STATE>(srcImage);
4298 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004299
4300 for (uint32_t region = 0; region < regionCount; region++) {
4301 const auto &blit_region = pRegions[region];
4302 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004303 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4304 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4305 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4306 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4307 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4308 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004309 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004310 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004311 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004312 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004313 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004314 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004315 }
4316 }
4317
4318 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004319 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4320 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4321 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4322 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4323 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4324 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004325 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004326 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004327 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004328 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004329 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004330 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004331 }
4332 if (skip) break;
4333 }
4334 }
4335
4336 return skip;
4337}
4338
Jeff Leger178b1e52020-10-05 12:22:23 -04004339bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4340 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4341 const VkImageBlit *pRegions, VkFilter filter) const {
4342 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4343 "vkCmdBlitImage");
4344}
4345
4346bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4347 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4348 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4349 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4350 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4351}
4352
Tony-LunarG542ae912021-11-04 16:06:44 -06004353bool SyncValidator::PreCallValidateCmdBlitImage2(VkCommandBuffer commandBuffer,
4354 const VkBlitImageInfo2 *pBlitImageInfo) const {
4355 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4356 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4357 pBlitImageInfo->filter, "vkCmdBlitImage2");
4358}
4359
Jeff Leger178b1e52020-10-05 12:22:23 -04004360template <typename RegionType>
4361void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4362 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4363 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004364 auto *cb_access_context = GetAccessContext(commandBuffer);
4365 assert(cb_access_context);
4366 auto *context = cb_access_context->GetCurrentAccessContext();
4367 assert(context);
4368
Jeremy Gebben9f537102021-10-05 16:37:12 -06004369 auto src_image = Get<IMAGE_STATE>(srcImage);
4370 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004371
4372 for (uint32_t region = 0; region < regionCount; region++) {
4373 const auto &blit_region = pRegions[region];
4374 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004375 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4376 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4377 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4378 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4379 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4380 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004381 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004382 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004383 }
4384 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004385 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4386 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4387 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4388 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4389 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4390 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004391 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004392 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004393 }
4394 }
4395}
locke-lunarg36ba2592020-04-03 09:42:04 -06004396
Jeff Leger178b1e52020-10-05 12:22:23 -04004397void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4398 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4399 const VkImageBlit *pRegions, VkFilter filter) {
4400 auto *cb_access_context = GetAccessContext(commandBuffer);
4401 assert(cb_access_context);
4402 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4403 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4404 pRegions, filter);
4405 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4406}
4407
4408void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4409 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4410 auto *cb_access_context = GetAccessContext(commandBuffer);
4411 assert(cb_access_context);
4412 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4413 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4414 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4415 pBlitImageInfo->filter, tag);
4416}
4417
Tony-LunarG542ae912021-11-04 16:06:44 -06004418void SyncValidator::PreCallRecordCmdBlitImage2(VkCommandBuffer commandBuffer, const VkBlitImageInfo2 *pBlitImageInfo) {
4419 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4420 auto *cb_access_context = GetAccessContext(commandBuffer);
4421 assert(cb_access_context);
4422 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2);
4423 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4424 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4425 pBlitImageInfo->filter, tag);
4426}
4427
John Zulauffaea0ee2021-01-14 14:01:32 -07004428bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4429 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4430 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4431 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004432 bool skip = false;
4433 if (drawCount == 0) return skip;
4434
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004435 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004436 VkDeviceSize size = struct_size;
4437 if (drawCount == 1 || stride == size) {
4438 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004439 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004440 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4441 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004442 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004443 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004444 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004445 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004446 }
4447 } else {
4448 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004449 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004450 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4451 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004452 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004453 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4454 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004455 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004456 break;
4457 }
4458 }
4459 }
4460 return skip;
4461}
4462
John Zulauf14940722021-04-12 15:19:02 -06004463void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004464 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4465 uint32_t stride) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004466 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004467 VkDeviceSize size = struct_size;
4468 if (drawCount == 1 || stride == size) {
4469 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004470 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004471 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004472 } else {
4473 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004474 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004475 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4476 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004477 }
4478 }
4479}
4480
John Zulauffaea0ee2021-01-14 14:01:32 -07004481bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4482 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4483 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004484 bool skip = false;
4485
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004486 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004487 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004488 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4489 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004490 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004491 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004492 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004493 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004494 }
4495 return skip;
4496}
4497
John Zulauf14940722021-04-12 15:19:02 -06004498void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004499 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004500 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004501 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004502}
4503
locke-lunarg36ba2592020-04-03 09:42:04 -06004504bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004505 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004506 const auto *cb_access_context = GetAccessContext(commandBuffer);
4507 assert(cb_access_context);
4508 if (!cb_access_context) return skip;
4509
locke-lunarg61870c22020-06-09 14:51:50 -06004510 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004511 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004512}
4513
4514void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004515 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004516 auto *cb_access_context = GetAccessContext(commandBuffer);
4517 assert(cb_access_context);
4518 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004519
locke-lunarg61870c22020-06-09 14:51:50 -06004520 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004521}
locke-lunarge1a67022020-04-29 00:15:36 -06004522
4523bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004524 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004525 const auto *cb_access_context = GetAccessContext(commandBuffer);
4526 assert(cb_access_context);
4527 if (!cb_access_context) return skip;
4528
4529 const auto *context = cb_access_context->GetCurrentAccessContext();
4530 assert(context);
4531 if (!context) return skip;
4532
locke-lunarg61870c22020-06-09 14:51:50 -06004533 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004534 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4535 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004536 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004537}
4538
4539void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004540 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004541 auto *cb_access_context = GetAccessContext(commandBuffer);
4542 assert(cb_access_context);
4543 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4544 auto *context = cb_access_context->GetCurrentAccessContext();
4545 assert(context);
4546
locke-lunarg61870c22020-06-09 14:51:50 -06004547 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4548 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004549}
4550
4551bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4552 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004553 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004554 const auto *cb_access_context = GetAccessContext(commandBuffer);
4555 assert(cb_access_context);
4556 if (!cb_access_context) return skip;
4557
locke-lunarg61870c22020-06-09 14:51:50 -06004558 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4559 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4560 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004561 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004562}
4563
4564void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4565 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004566 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004567 auto *cb_access_context = GetAccessContext(commandBuffer);
4568 assert(cb_access_context);
4569 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004570
locke-lunarg61870c22020-06-09 14:51:50 -06004571 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4572 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4573 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004574}
4575
4576bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4577 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004578 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004579 const auto *cb_access_context = GetAccessContext(commandBuffer);
4580 assert(cb_access_context);
4581 if (!cb_access_context) return skip;
4582
locke-lunarg61870c22020-06-09 14:51:50 -06004583 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4584 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4585 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004586 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004587}
4588
4589void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4590 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004591 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004592 auto *cb_access_context = GetAccessContext(commandBuffer);
4593 assert(cb_access_context);
4594 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004595
locke-lunarg61870c22020-06-09 14:51:50 -06004596 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4597 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4598 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004599}
4600
4601bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4602 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004603 bool skip = false;
4604 if (drawCount == 0) return skip;
4605
locke-lunargff255f92020-05-13 18:53:52 -06004606 const auto *cb_access_context = GetAccessContext(commandBuffer);
4607 assert(cb_access_context);
4608 if (!cb_access_context) return skip;
4609
4610 const auto *context = cb_access_context->GetCurrentAccessContext();
4611 assert(context);
4612 if (!context) return skip;
4613
locke-lunarg61870c22020-06-09 14:51:50 -06004614 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4615 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004616 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4617 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004618
4619 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4620 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4621 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004622 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004623 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004624}
4625
4626void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4627 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004628 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004629 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004630 auto *cb_access_context = GetAccessContext(commandBuffer);
4631 assert(cb_access_context);
4632 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4633 auto *context = cb_access_context->GetCurrentAccessContext();
4634 assert(context);
4635
locke-lunarg61870c22020-06-09 14:51:50 -06004636 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4637 cb_access_context->RecordDrawSubpassAttachment(tag);
4638 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004639
4640 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4641 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4642 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004643 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004644}
4645
4646bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4647 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004648 bool skip = false;
4649 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004650 const auto *cb_access_context = GetAccessContext(commandBuffer);
4651 assert(cb_access_context);
4652 if (!cb_access_context) return skip;
4653
4654 const auto *context = cb_access_context->GetCurrentAccessContext();
4655 assert(context);
4656 if (!context) return skip;
4657
locke-lunarg61870c22020-06-09 14:51:50 -06004658 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4659 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004660 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4661 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004662
4663 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4664 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4665 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004666 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004667 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004668}
4669
4670void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4671 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004672 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004673 auto *cb_access_context = GetAccessContext(commandBuffer);
4674 assert(cb_access_context);
4675 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4676 auto *context = cb_access_context->GetCurrentAccessContext();
4677 assert(context);
4678
locke-lunarg61870c22020-06-09 14:51:50 -06004679 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4680 cb_access_context->RecordDrawSubpassAttachment(tag);
4681 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004682
4683 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4684 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4685 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004686 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004687}
4688
4689bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4690 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4691 uint32_t stride, const char *function) const {
4692 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004693 const auto *cb_access_context = GetAccessContext(commandBuffer);
4694 assert(cb_access_context);
4695 if (!cb_access_context) return skip;
4696
4697 const auto *context = cb_access_context->GetCurrentAccessContext();
4698 assert(context);
4699 if (!context) return skip;
4700
locke-lunarg61870c22020-06-09 14:51:50 -06004701 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4702 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004703 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4704 maxDrawCount, stride, function);
4705 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004706
4707 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4708 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4709 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004710 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004711 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004712}
4713
4714bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4715 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4716 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004717 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4718 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004719}
4720
sfricke-samsung85584a72021-09-30 21:43:38 -07004721void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4722 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4723 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004724 auto *cb_access_context = GetAccessContext(commandBuffer);
4725 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004726 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004727 auto *context = cb_access_context->GetCurrentAccessContext();
4728 assert(context);
4729
locke-lunarg61870c22020-06-09 14:51:50 -06004730 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4731 cb_access_context->RecordDrawSubpassAttachment(tag);
4732 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4733 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004734
4735 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4736 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4737 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004738 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004739}
4740
sfricke-samsung85584a72021-09-30 21:43:38 -07004741void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4742 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4743 uint32_t stride) {
4744 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4745 stride);
4746 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4747 CMD_DRAWINDIRECTCOUNT);
4748}
locke-lunarge1a67022020-04-29 00:15:36 -06004749bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4750 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4751 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004752 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4753 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004754}
4755
4756void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4757 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4758 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004759 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4760 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004761 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4762 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004763}
4764
4765bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4766 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4767 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004768 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4769 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004770}
4771
4772void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4773 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4774 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004775 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4776 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004777 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4778 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004779}
4780
4781bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4782 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4783 uint32_t stride, const char *function) const {
4784 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004785 const auto *cb_access_context = GetAccessContext(commandBuffer);
4786 assert(cb_access_context);
4787 if (!cb_access_context) return skip;
4788
4789 const auto *context = cb_access_context->GetCurrentAccessContext();
4790 assert(context);
4791 if (!context) return skip;
4792
locke-lunarg61870c22020-06-09 14:51:50 -06004793 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4794 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004795 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4796 offset, maxDrawCount, stride, function);
4797 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004798
4799 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4800 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4801 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004802 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004803 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004804}
4805
4806bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4807 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4808 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004809 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4810 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004811}
4812
sfricke-samsung85584a72021-09-30 21:43:38 -07004813void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4814 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4815 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004816 auto *cb_access_context = GetAccessContext(commandBuffer);
4817 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004818 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004819 auto *context = cb_access_context->GetCurrentAccessContext();
4820 assert(context);
4821
locke-lunarg61870c22020-06-09 14:51:50 -06004822 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4823 cb_access_context->RecordDrawSubpassAttachment(tag);
4824 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4825 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004826
4827 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4828 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004829 // We will update the index and vertex buffer in SubmitQueue in the future.
4830 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004831}
4832
sfricke-samsung85584a72021-09-30 21:43:38 -07004833void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4834 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4835 uint32_t maxDrawCount, uint32_t stride) {
4836 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4837 maxDrawCount, stride);
4838 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4839 CMD_DRAWINDEXEDINDIRECTCOUNT);
4840}
4841
locke-lunarge1a67022020-04-29 00:15:36 -06004842bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4843 VkDeviceSize offset, VkBuffer countBuffer,
4844 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4845 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004846 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4847 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004848}
4849
4850void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4851 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4852 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004853 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4854 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004855 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4856 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004857}
4858
4859bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4860 VkDeviceSize offset, VkBuffer countBuffer,
4861 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4862 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004863 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4864 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004865}
4866
4867void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4868 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4869 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004870 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4871 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004872 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4873 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004874}
4875
4876bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4877 const VkClearColorValue *pColor, uint32_t rangeCount,
4878 const VkImageSubresourceRange *pRanges) const {
4879 bool skip = false;
4880 const auto *cb_access_context = GetAccessContext(commandBuffer);
4881 assert(cb_access_context);
4882 if (!cb_access_context) return skip;
4883
4884 const auto *context = cb_access_context->GetCurrentAccessContext();
4885 assert(context);
4886 if (!context) return skip;
4887
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004888 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004889
4890 for (uint32_t index = 0; index < rangeCount; index++) {
4891 const auto &range = pRanges[index];
4892 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004893 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004894 if (hazard.hazard) {
4895 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004896 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004897 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004898 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004899 }
4900 }
4901 }
4902 return skip;
4903}
4904
4905void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4906 const VkClearColorValue *pColor, uint32_t rangeCount,
4907 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004908 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004909 auto *cb_access_context = GetAccessContext(commandBuffer);
4910 assert(cb_access_context);
4911 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4912 auto *context = cb_access_context->GetCurrentAccessContext();
4913 assert(context);
4914
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004915 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004916
4917 for (uint32_t index = 0; index < rangeCount; index++) {
4918 const auto &range = pRanges[index];
4919 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004920 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004921 }
4922 }
4923}
4924
4925bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4926 VkImageLayout imageLayout,
4927 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4928 const VkImageSubresourceRange *pRanges) const {
4929 bool skip = false;
4930 const auto *cb_access_context = GetAccessContext(commandBuffer);
4931 assert(cb_access_context);
4932 if (!cb_access_context) return skip;
4933
4934 const auto *context = cb_access_context->GetCurrentAccessContext();
4935 assert(context);
4936 if (!context) return skip;
4937
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004938 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004939
4940 for (uint32_t index = 0; index < rangeCount; index++) {
4941 const auto &range = pRanges[index];
4942 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004943 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004944 if (hazard.hazard) {
4945 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004946 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004947 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004948 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004949 }
4950 }
4951 }
4952 return skip;
4953}
4954
4955void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4956 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4957 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004958 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004959 auto *cb_access_context = GetAccessContext(commandBuffer);
4960 assert(cb_access_context);
4961 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4962 auto *context = cb_access_context->GetCurrentAccessContext();
4963 assert(context);
4964
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004965 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004966
4967 for (uint32_t index = 0; index < rangeCount; index++) {
4968 const auto &range = pRanges[index];
4969 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004970 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004971 }
4972 }
4973}
4974
4975bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4976 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4977 VkDeviceSize dstOffset, VkDeviceSize stride,
4978 VkQueryResultFlags flags) const {
4979 bool skip = false;
4980 const auto *cb_access_context = GetAccessContext(commandBuffer);
4981 assert(cb_access_context);
4982 if (!cb_access_context) return skip;
4983
4984 const auto *context = cb_access_context->GetCurrentAccessContext();
4985 assert(context);
4986 if (!context) return skip;
4987
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004988 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004989
4990 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004991 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004992 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004993 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004994 skip |=
4995 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4996 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004997 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004998 }
4999 }
locke-lunargff255f92020-05-13 18:53:52 -06005000
5001 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06005002 return skip;
5003}
5004
5005void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
5006 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5007 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005008 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
5009 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06005010 auto *cb_access_context = GetAccessContext(commandBuffer);
5011 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06005012 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06005013 auto *context = cb_access_context->GetCurrentAccessContext();
5014 assert(context);
5015
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005016 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005017
5018 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005019 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005020 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005021 }
locke-lunargff255f92020-05-13 18:53:52 -06005022
5023 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06005024}
5025
5026bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5027 VkDeviceSize size, uint32_t data) const {
5028 bool skip = false;
5029 const auto *cb_access_context = GetAccessContext(commandBuffer);
5030 assert(cb_access_context);
5031 if (!cb_access_context) return skip;
5032
5033 const auto *context = cb_access_context->GetCurrentAccessContext();
5034 assert(context);
5035 if (!context) return skip;
5036
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005037 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005038
5039 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005040 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005041 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005042 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005043 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005044 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005045 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005046 }
5047 }
5048 return skip;
5049}
5050
5051void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5052 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005053 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06005054 auto *cb_access_context = GetAccessContext(commandBuffer);
5055 assert(cb_access_context);
5056 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
5057 auto *context = cb_access_context->GetCurrentAccessContext();
5058 assert(context);
5059
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005060 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005061
5062 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005063 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005064 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005065 }
5066}
5067
5068bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5069 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5070 const VkImageResolve *pRegions) const {
5071 bool skip = false;
5072 const auto *cb_access_context = GetAccessContext(commandBuffer);
5073 assert(cb_access_context);
5074 if (!cb_access_context) return skip;
5075
5076 const auto *context = cb_access_context->GetCurrentAccessContext();
5077 assert(context);
5078 if (!context) return skip;
5079
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005080 auto src_image = Get<IMAGE_STATE>(srcImage);
5081 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005082
5083 for (uint32_t region = 0; region < regionCount; region++) {
5084 const auto &resolve_region = pRegions[region];
5085 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005086 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005087 resolve_region.srcOffset, resolve_region.extent);
5088 if (hazard.hazard) {
5089 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005090 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005091 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005092 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005093 }
5094 }
5095
5096 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005097 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005098 resolve_region.dstOffset, resolve_region.extent);
5099 if (hazard.hazard) {
5100 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005101 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005102 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005103 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005104 }
5105 if (skip) break;
5106 }
5107 }
5108
5109 return skip;
5110}
5111
5112void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5113 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5114 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005115 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5116 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005117 auto *cb_access_context = GetAccessContext(commandBuffer);
5118 assert(cb_access_context);
5119 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5120 auto *context = cb_access_context->GetCurrentAccessContext();
5121 assert(context);
5122
Jeremy Gebben9f537102021-10-05 16:37:12 -06005123 auto src_image = Get<IMAGE_STATE>(srcImage);
5124 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005125
5126 for (uint32_t region = 0; region < regionCount; region++) {
5127 const auto &resolve_region = pRegions[region];
5128 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005129 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005130 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005131 }
5132 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005133 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005134 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005135 }
5136 }
5137}
5138
Tony-LunarG562fc102021-11-12 13:58:35 -07005139bool SyncValidator::ValidateCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5140 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04005141 bool skip = false;
5142 const auto *cb_access_context = GetAccessContext(commandBuffer);
5143 assert(cb_access_context);
5144 if (!cb_access_context) return skip;
5145
5146 const auto *context = cb_access_context->GetCurrentAccessContext();
5147 assert(context);
5148 if (!context) return skip;
5149
Tony-LunarG562fc102021-11-12 13:58:35 -07005150 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005151 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5152 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005153
5154 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5155 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5156 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005157 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005158 resolve_region.srcOffset, resolve_region.extent);
5159 if (hazard.hazard) {
5160 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005161 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005162 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005163 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005164 }
5165 }
5166
5167 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005168 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005169 resolve_region.dstOffset, resolve_region.extent);
5170 if (hazard.hazard) {
5171 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005172 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005173 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005174 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005175 }
5176 if (skip) break;
5177 }
5178 }
5179
5180 return skip;
5181}
5182
Tony-LunarG562fc102021-11-12 13:58:35 -07005183bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5184 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5185 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5186}
5187
5188bool SyncValidator::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
5189 const VkResolveImageInfo2 *pResolveImageInfo) const {
5190 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5191}
5192
5193void SyncValidator::RecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5194 CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04005195 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5196 auto *cb_access_context = GetAccessContext(commandBuffer);
5197 assert(cb_access_context);
Tony-LunarG562fc102021-11-12 13:58:35 -07005198 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04005199 auto *context = cb_access_context->GetCurrentAccessContext();
5200 assert(context);
5201
Jeremy Gebben9f537102021-10-05 16:37:12 -06005202 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5203 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005204
5205 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5206 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5207 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005208 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005209 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005210 }
5211 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005212 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005213 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005214 }
5215 }
5216}
5217
Tony-LunarG562fc102021-11-12 13:58:35 -07005218void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5219 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5220 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5221}
5222
5223void SyncValidator::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2 *pResolveImageInfo) {
5224 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5225}
5226
locke-lunarge1a67022020-04-29 00:15:36 -06005227bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5228 VkDeviceSize dataSize, const void *pData) const {
5229 bool skip = false;
5230 const auto *cb_access_context = GetAccessContext(commandBuffer);
5231 assert(cb_access_context);
5232 if (!cb_access_context) return skip;
5233
5234 const auto *context = cb_access_context->GetCurrentAccessContext();
5235 assert(context);
5236 if (!context) return skip;
5237
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005238 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005239
5240 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005241 // VK_WHOLE_SIZE not allowed
5242 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005243 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005244 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005245 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005246 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005247 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005248 }
5249 }
5250 return skip;
5251}
5252
5253void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5254 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005255 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005256 auto *cb_access_context = GetAccessContext(commandBuffer);
5257 assert(cb_access_context);
5258 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5259 auto *context = cb_access_context->GetCurrentAccessContext();
5260 assert(context);
5261
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005262 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005263
5264 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005265 // VK_WHOLE_SIZE not allowed
5266 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005267 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005268 }
5269}
locke-lunargff255f92020-05-13 18:53:52 -06005270
5271bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5272 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5273 bool skip = false;
5274 const auto *cb_access_context = GetAccessContext(commandBuffer);
5275 assert(cb_access_context);
5276 if (!cb_access_context) return skip;
5277
5278 const auto *context = cb_access_context->GetCurrentAccessContext();
5279 assert(context);
5280 if (!context) return skip;
5281
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005282 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005283
5284 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005285 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005286 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005287 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005288 skip |=
5289 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5290 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005291 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005292 }
5293 }
5294 return skip;
5295}
5296
5297void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5298 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005299 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005300 auto *cb_access_context = GetAccessContext(commandBuffer);
5301 assert(cb_access_context);
5302 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5303 auto *context = cb_access_context->GetCurrentAccessContext();
5304 assert(context);
5305
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005306 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005307
5308 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005309 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005310 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005311 }
5312}
John Zulauf49beb112020-11-04 16:06:31 -07005313
5314bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5315 bool skip = false;
5316 const auto *cb_context = GetAccessContext(commandBuffer);
5317 assert(cb_context);
5318 if (!cb_context) return skip;
5319
John Zulauf36ef9282021-02-02 11:47:24 -07005320 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005321 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005322}
5323
5324void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5325 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5326 auto *cb_context = GetAccessContext(commandBuffer);
5327 assert(cb_context);
5328 if (!cb_context) return;
John Zulauf1bf30522021-09-03 15:39:06 -06005329 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005330}
5331
John Zulauf4edde622021-02-15 08:54:50 -07005332bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5333 const VkDependencyInfoKHR *pDependencyInfo) const {
5334 bool skip = false;
5335 const auto *cb_context = GetAccessContext(commandBuffer);
5336 assert(cb_context);
5337 if (!cb_context || !pDependencyInfo) return skip;
5338
5339 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5340 return set_event_op.Validate(*cb_context);
5341}
5342
Tony-LunarGc43525f2021-11-15 16:12:38 -07005343bool SyncValidator::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5344 const VkDependencyInfo *pDependencyInfo) const {
5345 bool skip = false;
5346 const auto *cb_context = GetAccessContext(commandBuffer);
5347 assert(cb_context);
5348 if (!cb_context || !pDependencyInfo) return skip;
5349
5350 SyncOpSetEvent set_event_op(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5351 return set_event_op.Validate(*cb_context);
5352}
5353
John Zulauf4edde622021-02-15 08:54:50 -07005354void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5355 const VkDependencyInfoKHR *pDependencyInfo) {
5356 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5357 auto *cb_context = GetAccessContext(commandBuffer);
5358 assert(cb_context);
5359 if (!cb_context || !pDependencyInfo) return;
5360
John Zulauf1bf30522021-09-03 15:39:06 -06005361 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
John Zulauf4edde622021-02-15 08:54:50 -07005362}
5363
Tony-LunarGc43525f2021-11-15 16:12:38 -07005364void SyncValidator::PostCallRecordCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5365 const VkDependencyInfo *pDependencyInfo) {
5366 StateTracker::PostCallRecordCmdSetEvent2(commandBuffer, event, pDependencyInfo);
5367 auto *cb_context = GetAccessContext(commandBuffer);
5368 assert(cb_context);
5369 if (!cb_context || !pDependencyInfo) return;
5370
5371 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5372}
5373
John Zulauf49beb112020-11-04 16:06:31 -07005374bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5375 VkPipelineStageFlags stageMask) const {
5376 bool skip = false;
5377 const auto *cb_context = GetAccessContext(commandBuffer);
5378 assert(cb_context);
5379 if (!cb_context) return skip;
5380
John Zulauf36ef9282021-02-02 11:47:24 -07005381 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005382 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005383}
5384
5385void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5386 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5387 auto *cb_context = GetAccessContext(commandBuffer);
5388 assert(cb_context);
5389 if (!cb_context) return;
5390
John Zulauf1bf30522021-09-03 15:39:06 -06005391 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005392}
5393
John Zulauf4edde622021-02-15 08:54:50 -07005394bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5395 VkPipelineStageFlags2KHR stageMask) const {
5396 bool skip = false;
5397 const auto *cb_context = GetAccessContext(commandBuffer);
5398 assert(cb_context);
5399 if (!cb_context) return skip;
5400
5401 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5402 return reset_event_op.Validate(*cb_context);
5403}
5404
Tony-LunarGa2662db2021-11-16 07:26:24 -07005405bool SyncValidator::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5406 VkPipelineStageFlags2 stageMask) const {
5407 bool skip = false;
5408 const auto *cb_context = GetAccessContext(commandBuffer);
5409 assert(cb_context);
5410 if (!cb_context) return skip;
5411
5412 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5413 return reset_event_op.Validate(*cb_context);
5414}
5415
John Zulauf4edde622021-02-15 08:54:50 -07005416void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5417 VkPipelineStageFlags2KHR stageMask) {
5418 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5419 auto *cb_context = GetAccessContext(commandBuffer);
5420 assert(cb_context);
5421 if (!cb_context) return;
5422
John Zulauf1bf30522021-09-03 15:39:06 -06005423 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4edde622021-02-15 08:54:50 -07005424}
5425
Tony-LunarGa2662db2021-11-16 07:26:24 -07005426void SyncValidator::PostCallRecordCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags2 stageMask) {
5427 StateTracker::PostCallRecordCmdResetEvent2(commandBuffer, event, stageMask);
5428 auto *cb_context = GetAccessContext(commandBuffer);
5429 assert(cb_context);
5430 if (!cb_context) return;
5431
5432 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5433}
5434
John Zulauf49beb112020-11-04 16:06:31 -07005435bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5436 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5437 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5438 uint32_t bufferMemoryBarrierCount,
5439 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5440 uint32_t imageMemoryBarrierCount,
5441 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5442 bool skip = false;
5443 const auto *cb_context = GetAccessContext(commandBuffer);
5444 assert(cb_context);
5445 if (!cb_context) return skip;
5446
John Zulauf36ef9282021-02-02 11:47:24 -07005447 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5448 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5449 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005450 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005451}
5452
5453void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5454 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5455 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5456 uint32_t bufferMemoryBarrierCount,
5457 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5458 uint32_t imageMemoryBarrierCount,
5459 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5460 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5461 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5462 imageMemoryBarrierCount, pImageMemoryBarriers);
5463
5464 auto *cb_context = GetAccessContext(commandBuffer);
5465 assert(cb_context);
5466 if (!cb_context) return;
5467
John Zulauf1bf30522021-09-03 15:39:06 -06005468 cb_context->RecordSyncOp<SyncOpWaitEvents>(
John Zulauf610e28c2021-08-03 17:46:23 -06005469 CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
John Zulauf1bf30522021-09-03 15:39:06 -06005470 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf4a6105a2020-11-17 15:11:05 -07005471}
5472
John Zulauf4edde622021-02-15 08:54:50 -07005473bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5474 const VkDependencyInfoKHR *pDependencyInfos) const {
5475 bool skip = false;
5476 const auto *cb_context = GetAccessContext(commandBuffer);
5477 assert(cb_context);
5478 if (!cb_context) return skip;
5479
5480 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5481 skip |= wait_events_op.Validate(*cb_context);
5482 return skip;
5483}
5484
5485void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5486 const VkDependencyInfoKHR *pDependencyInfos) {
5487 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5488
5489 auto *cb_context = GetAccessContext(commandBuffer);
5490 assert(cb_context);
5491 if (!cb_context) return;
5492
John Zulauf1bf30522021-09-03 15:39:06 -06005493 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5494 pDependencyInfos);
John Zulauf4edde622021-02-15 08:54:50 -07005495}
5496
Tony-LunarG1364cf52021-11-17 16:10:11 -07005497bool SyncValidator::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5498 const VkDependencyInfo *pDependencyInfos) const {
5499 bool skip = false;
5500 const auto *cb_context = GetAccessContext(commandBuffer);
5501 assert(cb_context);
5502 if (!cb_context) return skip;
5503
5504 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5505 skip |= wait_events_op.Validate(*cb_context);
5506 return skip;
5507}
5508
5509void SyncValidator::PostCallRecordCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5510 const VkDependencyInfo *pDependencyInfos) {
5511 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5512
5513 auto *cb_context = GetAccessContext(commandBuffer);
5514 assert(cb_context);
5515 if (!cb_context) return;
5516
5517 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5518 pDependencyInfos);
5519}
5520
John Zulauf4a6105a2020-11-17 15:11:05 -07005521void SyncEventState::ResetFirstScope() {
5522 for (const auto address_type : kAddressTypes) {
5523 first_scope[static_cast<size_t>(address_type)].clear();
5524 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005525 scope = SyncExecScope();
John Zulauf78b1f892021-09-20 15:02:09 -06005526 first_scope_set = false;
5527 first_scope_tag = 0;
John Zulauf4a6105a2020-11-17 15:11:05 -07005528}
5529
5530// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005531SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005532 IgnoreReason reason = NotIgnored;
5533
Tony-LunarG1364cf52021-11-17 16:10:11 -07005534 if ((CMD_WAITEVENTS2KHR == cmd || CMD_WAITEVENTS2 == cmd) && (CMD_SETEVENT == last_command)) {
John Zulauf4edde622021-02-15 08:54:50 -07005535 reason = SetVsWait2;
5536 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5537 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005538 } else if (unsynchronized_set) {
5539 reason = SetRace;
John Zulauf78b1f892021-09-20 15:02:09 -06005540 } else if (first_scope_set) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005541 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005542 if (missing_bits) reason = MissingStageBits;
5543 }
5544
5545 return reason;
5546}
5547
Jeremy Gebben40a22942020-12-22 14:22:06 -07005548bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005549 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5550 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5551 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005552}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005553
John Zulaufbb890452021-12-14 11:30:18 -07005554void SyncOpBase::SetReplayContext(uint32_t subpass, ReplayContextPtr &&replay) {
5555 subpass_ = subpass;
5556 replay_context_ = std::move(replay);
5557}
5558
5559const ReplayTrackbackBarriersAction *SyncOpBase::GetReplayTrackback() const {
5560 if (replay_context_) {
5561 assert(subpass_ < replay_context_->subpass_contexts.size());
5562 return &replay_context_->subpass_contexts[subpass_];
5563 }
5564 return nullptr;
5565}
5566
John Zulauf36ef9282021-02-02 11:47:24 -07005567SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5568 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5569 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005570 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5571 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5572 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005573 : SyncOpBase(cmd), barriers_(1) {
5574 auto &barrier_set = barriers_[0];
5575 barrier_set.dependency_flags = dependencyFlags;
5576 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5577 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005578 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005579 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5580 pMemoryBarriers);
5581 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5582 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5583 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5584 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005585}
5586
John Zulauf4edde622021-02-15 08:54:50 -07005587SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5588 const VkDependencyInfoKHR *dep_infos)
5589 : SyncOpBase(cmd), barriers_(event_count) {
5590 for (uint32_t i = 0; i < event_count; i++) {
5591 const auto &dep_info = dep_infos[i];
5592 auto &barrier_set = barriers_[i];
5593 barrier_set.dependency_flags = dep_info.dependencyFlags;
5594 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5595 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5596 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5597 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5598 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5599 dep_info.pMemoryBarriers);
5600 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5601 dep_info.pBufferMemoryBarriers);
5602 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5603 dep_info.pImageMemoryBarriers);
5604 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005605}
5606
John Zulauf36ef9282021-02-02 11:47:24 -07005607SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005608 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5609 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5610 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5611 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5612 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005613 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005614 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5615
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005616SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5617 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005618 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005619
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005620bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5621 bool skip = false;
5622 const auto *context = cb_context.GetCurrentAccessContext();
5623 assert(context);
5624 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005625 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5626
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005627 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005628 const auto &barrier_set = barriers_[0];
5629 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5630 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5631 const auto *image_state = image_barrier.image.get();
5632 if (!image_state) continue;
5633 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5634 if (hazard.hazard) {
5635 // PHASE1 TODO -- add tag information to log msg when useful.
5636 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005637 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005638 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5639 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5640 string_SyncHazard(hazard.hazard), image_barrier.index,
5641 sync_state.report_data->FormatHandle(image_handle).c_str(),
5642 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005643 }
5644 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005645 return skip;
5646}
5647
John Zulaufd5115702021-01-18 12:34:33 -07005648struct SyncOpPipelineBarrierFunctorFactory {
5649 using BarrierOpFunctor = PipelineBarrierOp;
5650 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5651 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5652 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5653 using BufferRange = ResourceAccessRange;
5654 using ImageRange = subresource_adapter::ImageRangeGenerator;
5655 using GlobalRange = ResourceAccessRange;
5656
5657 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5658 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5659 }
John Zulauf14940722021-04-12 15:19:02 -06005660 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005661 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5662 }
5663 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5664 return GlobalBarrierOpFunctor(barrier, false);
5665 }
5666
5667 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5668 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5669 const auto base_address = ResourceBaseAddress(buffer);
5670 return (range + base_address);
5671 }
John Zulauf110413c2021-03-20 05:38:38 -06005672 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005673 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005674
5675 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005676 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005677 return range_gen;
5678 }
5679 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5680};
5681
5682template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005683void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005684 AccessContext *context) {
5685 for (const auto &barrier : barriers) {
5686 const auto *state = barrier.GetState();
5687 if (state) {
5688 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5689 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5690 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5691 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5692 }
5693 }
5694}
5695
5696template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005697void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005698 AccessContext *access_context) {
5699 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5700 for (const auto &barrier : barriers) {
5701 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5702 }
5703 for (const auto address_type : kAddressTypes) {
5704 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5705 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5706 }
5707}
5708
John Zulauf8eda1562021-04-13 17:06:41 -06005709ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005710 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005711 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005712 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufbb890452021-12-14 11:30:18 -07005713 ReplayRecord(tag, access_context, events_context);
John Zulauf4fa68462021-04-26 21:04:22 -06005714 return tag;
5715}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005716
John Zulaufbb890452021-12-14 11:30:18 -07005717void SyncOpPipelineBarrier::ReplayRecord(const ResourceUsageTag tag, AccessContext *access_context,
5718 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005719 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005720 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5721 assert(barriers_.size() == 1);
5722 const auto &barrier_set = barriers_[0];
5723 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5724 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5725 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005726 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005727 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005728 } else {
5729 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005730 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005731 }
5732 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005733}
5734
John Zulauf8eda1562021-04-13 17:06:41 -06005735bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07005736 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005737 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5738 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005739 return false;
5740}
5741
John Zulauf4edde622021-02-15 08:54:50 -07005742void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5743 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5744 const VkMemoryBarrier *barriers) {
5745 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005746 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005747 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005748 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005749 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005750 }
5751 if (0 == memory_barrier_count) {
5752 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005753 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005754 }
John Zulauf4edde622021-02-15 08:54:50 -07005755 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005756}
5757
John Zulauf4edde622021-02-15 08:54:50 -07005758void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5759 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5760 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5761 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005762 for (uint32_t index = 0; index < barrier_count; index++) {
5763 const auto &barrier = barriers[index];
Jeremy Gebben9f537102021-10-05 16:37:12 -06005764 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005765 if (buffer) {
5766 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5767 const auto range = MakeRange(barrier.offset, barrier_size);
5768 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005769 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005770 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005771 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005772 }
5773 }
5774}
5775
John Zulauf4edde622021-02-15 08:54:50 -07005776void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005777 uint32_t memory_barrier_count, const VkMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005778 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005779 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005780 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005781 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5782 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5783 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005784 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005785 }
John Zulauf4edde622021-02-15 08:54:50 -07005786 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005787}
5788
John Zulauf4edde622021-02-15 08:54:50 -07005789void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5790 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005791 const VkBufferMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005792 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005793 for (uint32_t index = 0; index < barrier_count; index++) {
5794 const auto &barrier = barriers[index];
5795 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5796 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9f537102021-10-05 16:37:12 -06005797 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005798 if (buffer) {
5799 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5800 const auto range = MakeRange(barrier.offset, barrier_size);
5801 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005802 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005803 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005804 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005805 }
5806 }
5807}
5808
John Zulauf4edde622021-02-15 08:54:50 -07005809void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5810 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5811 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5812 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005813 for (uint32_t index = 0; index < barrier_count; index++) {
5814 const auto &barrier = barriers[index];
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005815 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005816 if (image) {
5817 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5818 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005819 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005820 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005821 image_memory_barriers.emplace_back();
5822 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005823 }
5824 }
5825}
John Zulaufd5115702021-01-18 12:34:33 -07005826
John Zulauf4edde622021-02-15 08:54:50 -07005827void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5828 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005829 const VkImageMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005830 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005831 for (uint32_t index = 0; index < barrier_count; index++) {
5832 const auto &barrier = barriers[index];
5833 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5834 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005835 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005836 if (image) {
5837 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5838 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005839 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005840 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005841 image_memory_barriers.emplace_back();
5842 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005843 }
5844 }
5845}
5846
John Zulauf36ef9282021-02-02 11:47:24 -07005847SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005848 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5849 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5850 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5851 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005852 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005853 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5854 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005855 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005856}
5857
John Zulauf4edde622021-02-15 08:54:50 -07005858SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5859 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5860 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5861 MakeEventsList(sync_state, eventCount, pEvents);
5862 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5863}
5864
John Zulauf610e28c2021-08-03 17:46:23 -06005865const char *const SyncOpWaitEvents::kIgnored = "Wait operation is ignored for this event.";
5866
John Zulaufd5115702021-01-18 12:34:33 -07005867bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005868 bool skip = false;
5869 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005870 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005871
John Zulauf610e28c2021-08-03 17:46:23 -06005872 // This is only interesting at record and not replay (Execute/Submit) time.
John Zulauf4edde622021-02-15 08:54:50 -07005873 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5874 const auto &barrier_set = barriers_[barrier_set_index];
5875 if (barrier_set.single_exec_scope) {
5876 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5877 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5878 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5879 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5880 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5881 } else {
5882 const auto &barriers = barrier_set.memory_barriers;
5883 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5884 const auto &barrier = barriers[barrier_index];
5885 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5886 const std::string vuid =
5887 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5888 skip =
5889 sync_state.LogInfo(command_buffer_handle, vuid,
5890 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5891 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5892 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5893 }
5894 }
5895 }
5896 }
John Zulaufd5115702021-01-18 12:34:33 -07005897 }
5898
John Zulauf610e28c2021-08-03 17:46:23 -06005899 // The rest is common to record time and replay time.
5900 skip |= DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5901 return skip;
5902}
5903
John Zulaufbb890452021-12-14 11:30:18 -07005904bool SyncOpWaitEvents::DoValidate(const CommandExecutionContext &exec_context, const ResourceUsageTag base_tag) const {
John Zulauf610e28c2021-08-03 17:46:23 -06005905 bool skip = false;
John Zulaufbb890452021-12-14 11:30:18 -07005906 const auto &sync_state = exec_context.GetSyncState();
John Zulauf610e28c2021-08-03 17:46:23 -06005907
Jeremy Gebben40a22942020-12-22 14:22:06 -07005908 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005909 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005910 bool events_not_found = false;
John Zulaufbb890452021-12-14 11:30:18 -07005911 const auto *events_context = exec_context.GetCurrentEventsContext();
John Zulauf669dfd52021-01-27 17:15:28 -07005912 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005913 size_t barrier_set_index = 0;
5914 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06005915 for (const auto &event : events_) {
5916 const auto *sync_event = events_context->Get(event.get());
5917 const auto &barrier_set = barriers_[barrier_set_index];
5918 if (!sync_event) {
5919 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5920 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5921 // new validation error... wait without previously submitted set event...
5922 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 -07005923 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06005924 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005925 }
John Zulauf610e28c2021-08-03 17:46:23 -06005926
5927 // For replay calls, don't revalidate "same command buffer" events
5928 if (sync_event->last_command_tag > base_tag) continue;
5929
John Zulauf78394fc2021-07-12 15:41:40 -06005930 const auto event_handle = sync_event->event->event();
5931 // TODO add "destroyed" checks
5932
John Zulauf78b1f892021-09-20 15:02:09 -06005933 if (sync_event->first_scope_set) {
5934 // Only accumulate barrier and event stages if there is a pending set in the current context
5935 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5936 event_stage_masks |= sync_event->scope.mask_param;
5937 }
5938
John Zulauf78394fc2021-07-12 15:41:40 -06005939 const auto &src_exec_scope = barrier_set.src_exec_scope;
John Zulauf78b1f892021-09-20 15:02:09 -06005940
John Zulauf78394fc2021-07-12 15:41:40 -06005941 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5942 if (ignore_reason) {
5943 switch (ignore_reason) {
5944 case SyncEventState::ResetWaitRace:
5945 case SyncEventState::Reset2WaitRace: {
5946 // Four permuations of Reset and Wait calls...
5947 const char *vuid =
5948 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5949 if (ignore_reason == SyncEventState::Reset2WaitRace) {
Tony-LunarG279601c2021-11-16 10:50:51 -07005950 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2-event-03831"
5951 : "VUID-vkCmdResetEvent2-event-03832";
John Zulauf78394fc2021-07-12 15:41:40 -06005952 }
5953 const char *const message =
5954 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5955 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5956 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
John Zulauf610e28c2021-08-03 17:46:23 -06005957 CommandTypeString(sync_event->last_command), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005958 break;
5959 }
5960 case SyncEventState::SetRace: {
5961 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5962 // this event
5963 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5964 const char *const message =
5965 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5966 const char *const reason = "First synchronization scope is undefined.";
5967 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5968 sync_state.report_data->FormatHandle(event_handle).c_str(),
John Zulauf610e28c2021-08-03 17:46:23 -06005969 CommandTypeString(sync_event->last_command), reason, kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005970 break;
5971 }
5972 case SyncEventState::MissingStageBits: {
5973 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5974 // Issue error message that event waited for is not in wait events scope
5975 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5976 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5977 ". Bits missing from srcStageMask %s. %s";
5978 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5979 sync_state.report_data->FormatHandle(event_handle).c_str(),
5980 sync_event->scope.mask_param, src_exec_scope.mask_param,
John Zulauf610e28c2021-08-03 17:46:23 -06005981 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005982 break;
5983 }
5984 case SyncEventState::SetVsWait2: {
Tony-LunarG279601c2021-11-16 10:50:51 -07005985 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2-pEvents-03837",
John Zulauf78394fc2021-07-12 15:41:40 -06005986 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5987 sync_state.report_data->FormatHandle(event_handle).c_str(),
5988 CommandTypeString(sync_event->last_command));
5989 break;
5990 }
5991 default:
5992 assert(ignore_reason == SyncEventState::NotIgnored);
5993 }
5994 } else if (barrier_set.image_memory_barriers.size()) {
5995 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
John Zulaufbb890452021-12-14 11:30:18 -07005996 const auto *context = exec_context.GetCurrentAccessContext();
John Zulauf78394fc2021-07-12 15:41:40 -06005997 assert(context);
5998 for (const auto &image_memory_barrier : image_memory_barriers) {
5999 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
6000 const auto *image_state = image_memory_barrier.image.get();
6001 if (!image_state) continue;
6002 const auto &subresource_range = image_memory_barrier.range;
6003 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
6004 const auto hazard =
6005 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
6006 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
6007 if (hazard.hazard) {
6008 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
6009 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
6010 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
6011 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
John Zulaufbb890452021-12-14 11:30:18 -07006012 exec_context.FormatUsage(hazard).c_str());
John Zulauf78394fc2021-07-12 15:41:40 -06006013 break;
6014 }
6015 }
6016 }
6017 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
6018 // 03839
6019 barrier_set_index += barrier_set_incr;
6020 }
John Zulaufd5115702021-01-18 12:34:33 -07006021
6022 // 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 -07006023 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 -07006024 if (extra_stage_bits) {
6025 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07006026 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
6027 const char *const vuid =
Tony-LunarG279601c2021-11-16 10:50:51 -07006028 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07006029 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07006030 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufbb890452021-12-14 11:30:18 -07006031 const auto handle = exec_context.Handle();
John Zulaufd5115702021-01-18 12:34:33 -07006032 if (events_not_found) {
John Zulaufbb890452021-12-14 11:30:18 -07006033 skip |= sync_state.LogInfo(handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07006034 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07006035 " vkCmdSetEvent may be in previously submitted command buffer.");
6036 } else {
John Zulaufbb890452021-12-14 11:30:18 -07006037 skip |= sync_state.LogError(handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07006038 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07006039 }
6040 }
6041 return skip;
6042}
6043
6044struct SyncOpWaitEventsFunctorFactory {
6045 using BarrierOpFunctor = WaitEventBarrierOp;
6046 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
6047 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
6048 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
6049 using BufferRange = EventSimpleRangeGenerator;
6050 using ImageRange = EventImageRangeGenerator;
6051 using GlobalRange = EventSimpleRangeGenerator;
6052
6053 // Need to restrict to only valid exec and access scope for this event
6054 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
6055 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07006056 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07006057 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
6058 return barrier;
6059 }
6060 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
6061 auto barrier = RestrictToEvent(barrier_arg);
6062 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
6063 }
John Zulauf14940722021-04-12 15:19:02 -06006064 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07006065 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
6066 }
6067 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
6068 auto barrier = RestrictToEvent(barrier_arg);
6069 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
6070 }
6071
6072 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
6073 const AccessAddressType address_type = GetAccessAddressType(buffer);
6074 const auto base_address = ResourceBaseAddress(buffer);
6075 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
6076 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
6077 return filtered_range_gen;
6078 }
John Zulauf110413c2021-03-20 05:38:38 -06006079 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07006080 if (!SimpleBinding(image)) return ImageRange();
6081 const auto address_type = GetAccessAddressType(image);
6082 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06006083 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07006084 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
6085
6086 return filtered_range_gen;
6087 }
6088 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
6089 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
6090 }
6091 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
6092 SyncEventState *sync_event;
6093};
6094
John Zulauf8eda1562021-04-13 17:06:41 -06006095ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006096 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07006097 auto *access_context = cb_context->GetCurrentAccessContext();
6098 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006099 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07006100 auto *events_context = cb_context->GetCurrentEventsContext();
6101 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006102 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07006103
John Zulaufbb890452021-12-14 11:30:18 -07006104 ReplayRecord(tag, access_context, events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006105 return tag;
6106}
6107
John Zulaufbb890452021-12-14 11:30:18 -07006108void SyncOpWaitEvents::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07006109 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
6110 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
6111 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
6112 access_context->ResolvePreviousAccesses();
6113
John Zulauf4edde622021-02-15 08:54:50 -07006114 size_t barrier_set_index = 0;
6115 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
6116 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07006117 for (auto &event_shared : events_) {
6118 if (!event_shared.get()) continue;
6119 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07006120
John Zulauf4edde622021-02-15 08:54:50 -07006121 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006122 sync_event->last_command_tag = tag;
John Zulaufd5115702021-01-18 12:34:33 -07006123
John Zulauf4edde622021-02-15 08:54:50 -07006124 const auto &barrier_set = barriers_[barrier_set_index];
6125 const auto &dst = barrier_set.dst_exec_scope;
6126 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07006127 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
6128 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
6129 // of the barriers is maintained.
6130 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07006131 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
6132 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
6133 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07006134
6135 // Apply the global barrier to the event itself (for race condition tracking)
6136 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
6137 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6138 sync_event->barriers |= dst.exec_scope;
6139 } else {
6140 // We ignored this wait, so we don't have any effective synchronization barriers for it.
6141 sync_event->barriers = 0U;
6142 }
John Zulauf4edde622021-02-15 08:54:50 -07006143 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07006144 }
6145
6146 // Apply the pending barriers
6147 ResolvePendingBarrierFunctor apply_pending_action(tag);
6148 access_context->ApplyToContext(apply_pending_action);
6149}
6150
John Zulauf8eda1562021-04-13 17:06:41 -06006151bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006152 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
6153 return DoValidate(*exec_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006154}
6155
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006156bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6157 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
6158 bool skip = false;
6159 const auto *cb_access_context = GetAccessContext(commandBuffer);
6160 assert(cb_access_context);
6161 if (!cb_access_context) return skip;
6162
6163 const auto *context = cb_access_context->GetCurrentAccessContext();
6164 assert(context);
6165 if (!context) return skip;
6166
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006167 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006168
6169 if (dst_buffer) {
6170 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6171 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
6172 if (hazard.hazard) {
6173 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
6174 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
6175 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06006176 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006177 }
6178 }
6179 return skip;
6180}
6181
John Zulauf669dfd52021-01-27 17:15:28 -07006182void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07006183 events_.reserve(event_count);
6184 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006185 events_.emplace_back(sync_state.Get<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07006186 }
6187}
John Zulauf6ce24372021-01-30 05:56:25 -07006188
John Zulauf36ef9282021-02-02 11:47:24 -07006189SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006190 VkPipelineStageFlags2KHR stageMask)
Jeremy Gebben9f537102021-10-05 16:37:12 -06006191 : SyncOpBase(cmd), event_(sync_state.Get<EVENT_STATE>(event)), exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006192
John Zulauf1bf30522021-09-03 15:39:06 -06006193bool SyncOpResetEvent::Validate(const CommandBufferAccessContext& cb_context) const {
6194 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6195}
6196
John Zulaufbb890452021-12-14 11:30:18 -07006197bool SyncOpResetEvent::DoValidate(const CommandExecutionContext &exec_context, const ResourceUsageTag base_tag) const {
6198 auto *events_context = exec_context.GetCurrentEventsContext();
John Zulauf6ce24372021-01-30 05:56:25 -07006199 assert(events_context);
6200 bool skip = false;
6201 if (!events_context) return skip;
6202
John Zulaufbb890452021-12-14 11:30:18 -07006203 const auto &sync_state = exec_context.GetSyncState();
John Zulauf6ce24372021-01-30 05:56:25 -07006204 const auto *sync_event = events_context->Get(event_);
6205 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6206
John Zulauf1bf30522021-09-03 15:39:06 -06006207 if (sync_event->last_command_tag > base_tag) return skip; // if we validated this in recording of the secondary, don't repeat
6208
John Zulauf6ce24372021-01-30 05:56:25 -07006209 const char *const set_wait =
6210 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6211 "hazards.";
6212 const char *message = set_wait; // Only one message this call.
6213 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
6214 const char *vuid = nullptr;
6215 switch (sync_event->last_command) {
6216 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006217 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006218 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006219 // Needs a barrier between set and reset
6220 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
6221 break;
John Zulauf4edde622021-02-15 08:54:50 -07006222 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006223 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006224 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07006225 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
6226 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
6227 break;
6228 }
6229 default:
6230 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07006231 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
6232 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07006233 break;
6234 }
6235 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006236 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
6237 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006238 CommandTypeString(sync_event->last_command));
6239 }
6240 }
6241 return skip;
6242}
6243
John Zulauf8eda1562021-04-13 17:06:41 -06006244ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
6245 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006246 auto *events_context = cb_context->GetCurrentEventsContext();
6247 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006248 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006249
6250 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06006251 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006252
6253 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07006254 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006255 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006256 sync_event->unsynchronized_set = CMD_NONE;
6257 sync_event->ResetFirstScope();
6258 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06006259
6260 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006261}
6262
John Zulauf8eda1562021-04-13 17:06:41 -06006263bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006264 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
6265 return DoValidate(*exec_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006266}
6267
John Zulaufbb890452021-12-14 11:30:18 -07006268void SyncOpResetEvent::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006269
John Zulauf36ef9282021-02-02 11:47:24 -07006270SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006271 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07006272 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006273 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006274 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
6275 dep_info_() {}
6276
6277SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
6278 const VkDependencyInfoKHR &dep_info)
6279 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006280 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006281 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
Tony-LunarG273f32f2021-09-28 08:56:30 -06006282 dep_info_(new safe_VkDependencyInfo(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006283
6284bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf610e28c2021-08-03 17:46:23 -06006285 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6286}
6287bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006288 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
6289 assert(exec_context);
6290 return DoValidate(*exec_context, base_tag);
John Zulauf610e28c2021-08-03 17:46:23 -06006291}
6292
John Zulaufbb890452021-12-14 11:30:18 -07006293bool SyncOpSetEvent::DoValidate(const CommandExecutionContext &exec_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006294 bool skip = false;
6295
John Zulaufbb890452021-12-14 11:30:18 -07006296 const auto &sync_state = exec_context.GetSyncState();
6297 auto *events_context = exec_context.GetCurrentEventsContext();
John Zulauf6ce24372021-01-30 05:56:25 -07006298 assert(events_context);
6299 if (!events_context) return skip;
6300
6301 const auto *sync_event = events_context->Get(event_);
6302 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6303
John Zulauf610e28c2021-08-03 17:46:23 -06006304 if (sync_event->last_command_tag >= base_tag) return skip; // for replay we don't want to revalidate internal "last commmand"
6305
John Zulauf6ce24372021-01-30 05:56:25 -07006306 const char *const reset_set =
6307 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6308 "hazards.";
6309 const char *const wait =
6310 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6311
6312 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006313 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006314 const char *message = nullptr;
6315 switch (sync_event->last_command) {
6316 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006317 case CMD_RESETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006318 case CMD_RESETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006319 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006320 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006321 message = reset_set;
6322 break;
6323 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006324 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006325 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006326 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006327 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006328 message = reset_set;
6329 break;
6330 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006331 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006332 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006333 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006334 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006335 message = wait;
6336 break;
6337 default:
6338 // The only other valid last command that wasn't one.
6339 assert(sync_event->last_command == CMD_NONE);
6340 break;
6341 }
John Zulauf4edde622021-02-15 08:54:50 -07006342 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006343 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006344 std::string vuid("SYNC-");
6345 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006346 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6347 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006348 CommandTypeString(sync_event->last_command));
6349 }
6350 }
6351
6352 return skip;
6353}
6354
John Zulauf8eda1562021-04-13 17:06:41 -06006355ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006356 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006357 auto *events_context = cb_context->GetCurrentEventsContext();
6358 auto *access_context = cb_context->GetCurrentAccessContext();
6359 assert(events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006360 if (access_context && events_context) {
John Zulaufbb890452021-12-14 11:30:18 -07006361 ReplayRecord(tag, access_context, events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006362 }
6363 return tag;
6364}
John Zulauf6ce24372021-01-30 05:56:25 -07006365
John Zulaufbb890452021-12-14 11:30:18 -07006366void SyncOpSetEvent::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006367 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf610e28c2021-08-03 17:46:23 -06006368 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006369
6370 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6371 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6372 // any issues caused by naive scope setting here.
6373
6374 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6375 // Given:
6376 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6377 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6378
6379 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6380 sync_event->unsynchronized_set = sync_event->last_command;
6381 sync_event->ResetFirstScope();
John Zulauf78b1f892021-09-20 15:02:09 -06006382 } else if (!sync_event->first_scope_set) {
John Zulauf6ce24372021-01-30 05:56:25 -07006383 // We only set the scope if there isn't one
6384 sync_event->scope = src_exec_scope_;
6385
6386 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6387 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6388 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6389 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6390 }
6391 };
6392 access_context->ForAll(set_scope);
6393 sync_event->unsynchronized_set = CMD_NONE;
John Zulauf78b1f892021-09-20 15:02:09 -06006394 sync_event->first_scope_set = true;
John Zulauf6ce24372021-01-30 05:56:25 -07006395 sync_event->first_scope_tag = tag;
6396 }
John Zulauf4edde622021-02-15 08:54:50 -07006397 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6398 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006399 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006400 sync_event->barriers = 0U;
6401}
John Zulauf64ffe552021-02-06 10:25:07 -07006402
6403SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6404 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006405 const VkSubpassBeginInfo *pSubpassBeginInfo)
6406 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006407 if (pRenderPassBegin) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006408 rp_state_ = sync_state.Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
John Zulauf64ffe552021-02-06 10:25:07 -07006409 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006410 auto fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07006411 if (fb_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006412 shared_attachments_ = sync_state.GetAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
John Zulauf64ffe552021-02-06 10:25:07 -07006413 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6414 // Note that this a safe to presist as long as shared_attachments is not cleared
6415 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006416 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006417 attachments_.emplace_back(attachment.get());
6418 }
6419 }
6420 if (pSubpassBeginInfo) {
6421 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6422 }
6423 }
6424}
6425
6426bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6427 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6428 bool skip = false;
6429
6430 assert(rp_state_.get());
6431 if (nullptr == rp_state_.get()) return skip;
6432 auto &rp_state = *rp_state_.get();
6433
6434 const uint32_t subpass = 0;
6435
6436 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6437 // hasn't happened yet)
6438 const std::vector<AccessContext> empty_context_vector;
6439 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6440 cb_context.GetCurrentAccessContext());
6441
6442 // Validate attachment operations
6443 if (attachments_.size() == 0) return skip;
6444 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006445
6446 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6447 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6448 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6449 // operations (though it's currently a messy approach)
6450 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6451 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006452
6453 // Validate load operations if there were no layout transition hazards
6454 if (!skip) {
John Zulaufee984022022-04-13 16:39:50 -06006455 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kInvalidTag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07006456 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006457 }
6458
6459 return skip;
6460}
6461
John Zulauf8eda1562021-04-13 17:06:41 -06006462ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006463 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6464 assert(rp_state_.get());
John Zulauf41a9c7c2021-12-07 15:59:53 -07006465 if (nullptr == rp_state_.get()) return cb_context->NextCommandTag(cmd_);
6466 return cb_context->RecordBeginRenderPass(cmd_, *rp_state_.get(), renderpass_begin_info_.renderArea, attachments_);
John Zulauf64ffe552021-02-06 10:25:07 -07006467}
6468
John Zulauf8eda1562021-04-13 17:06:41 -06006469bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006470 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006471 return false;
6472}
6473
John Zulaufbb890452021-12-14 11:30:18 -07006474void SyncOpBeginRenderPass::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context,
6475 SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006476
John Zulauf64ffe552021-02-06 10:25:07 -07006477SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006478 const VkSubpassEndInfo *pSubpassEndInfo)
6479 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006480 if (pSubpassBeginInfo) {
6481 subpass_begin_info_.initialize(pSubpassBeginInfo);
6482 }
6483 if (pSubpassEndInfo) {
6484 subpass_end_info_.initialize(pSubpassEndInfo);
6485 }
6486}
6487
6488bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6489 bool skip = false;
6490 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6491 if (!renderpass_context) return skip;
6492
6493 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6494 return skip;
6495}
6496
John Zulauf8eda1562021-04-13 17:06:41 -06006497ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006498 return cb_context->RecordNextSubpass(cmd_);
John Zulauf8eda1562021-04-13 17:06:41 -06006499}
6500
6501bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006502 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006503 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006504}
6505
sfricke-samsung85584a72021-09-30 21:43:38 -07006506SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6507 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006508 if (pSubpassEndInfo) {
6509 subpass_end_info_.initialize(pSubpassEndInfo);
6510 }
6511}
6512
John Zulaufbb890452021-12-14 11:30:18 -07006513void SyncOpNextSubpass::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6514}
John Zulauf8eda1562021-04-13 17:06:41 -06006515
John Zulauf64ffe552021-02-06 10:25:07 -07006516bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6517 bool skip = false;
6518 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6519
6520 if (!renderpass_context) return skip;
6521 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6522 return skip;
6523}
6524
John Zulauf8eda1562021-04-13 17:06:41 -06006525ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006526 return cb_context->RecordEndRenderPass(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006527}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006528
John Zulauf8eda1562021-04-13 17:06:41 -06006529bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulaufbb890452021-12-14 11:30:18 -07006530 ResourceUsageTag base_tag, CommandExecutionContext *exec_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006531 return false;
6532}
6533
John Zulaufbb890452021-12-14 11:30:18 -07006534void SyncOpEndRenderPass::ReplayRecord(ResourceUsageTag tag, AccessContext *access_context,
6535 SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006536
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006537void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6538 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6539 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6540 auto *cb_access_context = GetAccessContext(commandBuffer);
6541 assert(cb_access_context);
6542 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6543 auto *context = cb_access_context->GetCurrentAccessContext();
6544 assert(context);
6545
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006546 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006547
6548 if (dst_buffer) {
6549 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6550 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6551 }
6552}
John Zulaufd05c5842021-03-26 11:32:16 -06006553
John Zulaufae842002021-04-15 18:20:55 -06006554bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6555 const VkCommandBuffer *pCommandBuffers) const {
6556 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6557 const char *func_name = "vkCmdExecuteCommands";
6558 const auto *cb_context = GetAccessContext(commandBuffer);
6559 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006560
6561 // Heavyweight, but we need a proxy copy of the active command buffer access context
6562 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006563
6564 // Make working copies of the access and events contexts
John Zulaufae842002021-04-15 18:20:55 -06006565 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006566 proxy_cb_context.NextIndexedCommandTag(CMD_EXECUTECOMMANDS, cb_index);
6567
John Zulaufae842002021-04-15 18:20:55 -06006568 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6569 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006570
6571 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6572 assert(recorded_context);
6573 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6574
6575 // The barriers have already been applied in ValidatFirstUse
6576 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6577 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006578 }
6579
John Zulaufae842002021-04-15 18:20:55 -06006580 return skip;
6581}
6582
6583void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6584 const VkCommandBuffer *pCommandBuffers) {
6585 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006586 auto *cb_context = GetAccessContext(commandBuffer);
6587 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006588 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf41a9c7c2021-12-07 15:59:53 -07006589 cb_context->NextIndexedCommandTag(CMD_EXECUTECOMMANDS, cb_index);
John Zulauf4fa68462021-04-26 21:04:22 -06006590 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6591 if (!recorded_cb_context) continue;
6592 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6593 }
John Zulaufae842002021-04-15 18:20:55 -06006594}
6595
John Zulaufd0ec59f2021-03-13 14:25:08 -07006596AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6597 : view_(view), view_mask_(), gen_store_() {
6598 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6599 const IMAGE_STATE &image_state = *view_->image_state.get();
6600 const auto base_address = ResourceBaseAddress(image_state);
6601 const auto *encoder = image_state.fragment_encoder.get();
6602 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006603 // Get offset and extent for the view, accounting for possible depth slicing
6604 const VkOffset3D zero_offset = view->GetOffset();
6605 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006606 // Intentional copy
6607 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6608 view_mask_ = subres_range.aspectMask;
6609 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6610 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6611
6612 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6613 if (depth && (depth != view_mask_)) {
6614 subres_range.aspectMask = depth;
6615 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6616 }
6617 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6618 if (stencil && (stencil != view_mask_)) {
6619 subres_range.aspectMask = stencil;
6620 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6621 }
6622}
6623
6624const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6625 const ImageRangeGen *got = nullptr;
6626 switch (gen_type) {
6627 case kViewSubresource:
6628 got = &gen_store_[kViewSubresource];
6629 break;
6630 case kRenderArea:
6631 got = &gen_store_[kRenderArea];
6632 break;
6633 case kDepthOnlyRenderArea:
6634 got =
6635 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6636 break;
6637 case kStencilOnlyRenderArea:
6638 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6639 : &gen_store_[Gen::kStencilOnlyRenderArea];
6640 break;
6641 default:
6642 assert(got);
6643 }
6644 return got;
6645}
6646
6647AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6648 assert(IsValid());
6649 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6650 if (depth_op) {
6651 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6652 if (stencil_op) {
6653 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6654 return kRenderArea;
6655 }
6656 return kDepthOnlyRenderArea;
6657 }
6658 if (stencil_op) {
6659 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6660 return kStencilOnlyRenderArea;
6661 }
6662
6663 assert(depth_op || stencil_op);
6664 return kRenderArea;
6665}
6666
6667AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006668
6669void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6670 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6671 for (auto &event_pair : map_) {
6672 assert(event_pair.second); // Shouldn't be storing empty
6673 auto &sync_event = *event_pair.second;
6674 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6675 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6676 sync_event.barriers |= dst.exec_scope;
6677 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6678 }
6679 }
6680}
John Zulaufbb890452021-12-14 11:30:18 -07006681
6682ReplayTrackbackBarriersAction::ReplayTrackbackBarriersAction(VkQueueFlags queue_flags,
6683 const SubpassDependencyGraphNode &subpass_dep,
6684 const std::vector<ReplayTrackbackBarriersAction> &replay_contexts) {
6685 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
6686 trackback_barriers.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
6687 for (const auto &prev_dep : subpass_dep.prev) {
6688 const auto prev_pass = prev_dep.first->pass;
6689 const auto &prev_barriers = prev_dep.second;
6690 trackback_barriers.emplace_back(&replay_contexts[prev_pass], queue_flags, prev_barriers);
6691 }
6692 if (has_barrier_from_external) {
6693 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
6694 trackback_barriers.emplace_back(nullptr, queue_flags, subpass_dep.barrier_from_external);
6695 }
6696}
6697
6698void ReplayTrackbackBarriersAction::operator()(ResourceAccessState *access) const {
6699 if (trackback_barriers.size() == 1) {
6700 trackback_barriers[0](access);
6701 } else {
6702 ResourceAccessState resolved;
6703 for (const auto &trackback : trackback_barriers) {
6704 ResourceAccessState access_copy = *access;
6705 trackback(&access_copy);
6706 resolved.Resolve(access_copy);
6707 }
6708 *access = resolved;
6709 }
6710}
6711
6712ReplayTrackbackBarriersAction::TrackbackBarriers::TrackbackBarriers(
6713 const ReplayTrackbackBarriersAction *source_subpass_, VkQueueFlags queue_flags_,
6714 const std::vector<const VkSubpassDependency2 *> &subpass_dependencies_)
6715 : Base(source_subpass_, queue_flags_, subpass_dependencies_) {}
6716
6717void ReplayTrackbackBarriersAction::TrackbackBarriers::operator()(ResourceAccessState *access) const {
6718 if (source_subpass) {
6719 (*source_subpass)(access);
6720 }
6721 access->ApplyBarriersImmediate(barriers);
6722}