blob: d8e8aa89f2dd5b9d441c682096017d12598df825 [file] [log] [blame]
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001/* Copyright (c) 2019-2022 The Khronos Group Inc.
2 * Copyright (c) 2019-2022 Valve Corporation
3 * Copyright (c) 2019-2022 LunarG, Inc.
John Zulauf9cb530d2019-09-30 14:14:10 -06004 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * Author: John Zulauf <jzulauf@lunarg.com>
John Zulaufab7756b2020-12-29 16:10:16 -070018 * Author: Locke Lin <locke@lunarg.com>
19 * Author: Jeremy Gebben <jeremyg@lunarg.com>
John Zulauf9cb530d2019-09-30 14:14:10 -060020 */
21
22#include <limits>
23#include <vector>
locke-lunarg296a3c92020-03-25 01:04:29 -060024#include <memory>
25#include <bitset>
John Zulauf9cb530d2019-09-30 14:14:10 -060026#include "synchronization_validation.h"
Jeremy Gebben5f585ae2021-02-02 09:03:06 -070027#include "sync_utils.h"
John Zulauf9cb530d2019-09-30 14:14:10 -060028
Jeremy Gebben6fbf8242021-06-21 09:14:46 -060029static bool SimpleBinding(const BINDABLE &bindable) { return !bindable.sparse && bindable.Binding(); }
John Zulauf264cce02021-02-05 14:40:47 -070030
John Zulauf29d00532021-03-04 13:28:54 -070031static bool SimpleBinding(const IMAGE_STATE &image_state) {
Jeremy Gebben62c3bf42021-07-21 15:38:24 -060032 bool simple =
Jeremy Gebben82e11d52021-07-26 09:19:37 -060033 SimpleBinding(static_cast<const BINDABLE &>(image_state)) || image_state.IsSwapchainImage() || image_state.bind_swapchain;
John Zulauf29d00532021-03-04 13:28:54 -070034
35 // If it's not simple we must have an encoder.
36 assert(!simple || image_state.fragment_encoder.get());
37 return simple;
38}
39
John Zulauf4fa68462021-04-26 21:04:22 -060040static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
41static const std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
John Zulauf43cc7462020-12-03 12:33:12 -070042 AccessAddressType::kLinear, AccessAddressType::kIdealized};
43
John Zulaufd5115702021-01-18 12:34:33 -070044static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070045static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
46 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
47}
John Zulaufd5115702021-01-18 12:34:33 -070048
John Zulauf9cb530d2019-09-30 14:14:10 -060049static const char *string_SyncHazardVUID(SyncHazard hazard) {
50 switch (hazard) {
51 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070052 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060053 break;
54 case SyncHazard::READ_AFTER_WRITE:
55 return "SYNC-HAZARD-READ_AFTER_WRITE";
56 break;
57 case SyncHazard::WRITE_AFTER_READ:
58 return "SYNC-HAZARD-WRITE_AFTER_READ";
59 break;
60 case SyncHazard::WRITE_AFTER_WRITE:
61 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
62 break;
John Zulauf2f952d22020-02-10 11:34:51 -070063 case SyncHazard::READ_RACING_WRITE:
64 return "SYNC-HAZARD-READ-RACING-WRITE";
65 break;
66 case SyncHazard::WRITE_RACING_WRITE:
67 return "SYNC-HAZARD-WRITE-RACING-WRITE";
68 break;
69 case SyncHazard::WRITE_RACING_READ:
70 return "SYNC-HAZARD-WRITE-RACING-READ";
71 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060072 default:
73 assert(0);
74 }
75 return "SYNC-HAZARD-INVALID";
76}
77
John Zulauf59e25072020-07-17 10:55:21 -060078static bool IsHazardVsRead(SyncHazard hazard) {
79 switch (hazard) {
80 case SyncHazard::NONE:
81 return false;
82 break;
83 case SyncHazard::READ_AFTER_WRITE:
84 return false;
85 break;
86 case SyncHazard::WRITE_AFTER_READ:
87 return true;
88 break;
89 case SyncHazard::WRITE_AFTER_WRITE:
90 return false;
91 break;
92 case SyncHazard::READ_RACING_WRITE:
93 return false;
94 break;
95 case SyncHazard::WRITE_RACING_WRITE:
96 return false;
97 break;
98 case SyncHazard::WRITE_RACING_READ:
99 return true;
100 break;
101 default:
102 assert(0);
103 }
104 return false;
105}
106
John Zulauf9cb530d2019-09-30 14:14:10 -0600107static const char *string_SyncHazard(SyncHazard hazard) {
108 switch (hazard) {
109 case SyncHazard::NONE:
110 return "NONR";
111 break;
112 case SyncHazard::READ_AFTER_WRITE:
113 return "READ_AFTER_WRITE";
114 break;
115 case SyncHazard::WRITE_AFTER_READ:
116 return "WRITE_AFTER_READ";
117 break;
118 case SyncHazard::WRITE_AFTER_WRITE:
119 return "WRITE_AFTER_WRITE";
120 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700121 case SyncHazard::READ_RACING_WRITE:
122 return "READ_RACING_WRITE";
123 break;
124 case SyncHazard::WRITE_RACING_WRITE:
125 return "WRITE_RACING_WRITE";
126 break;
127 case SyncHazard::WRITE_RACING_READ:
128 return "WRITE_RACING_READ";
129 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600130 default:
131 assert(0);
132 }
133 return "INVALID HAZARD";
134}
135
John Zulauf37ceaed2020-07-03 16:18:15 -0600136static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
137 // Return the info for the first bit found
138 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700139 for (size_t i = 0; i < flags.size(); i++) {
140 if (flags.test(i)) {
141 info = &syncStageAccessInfoByStageAccessIndex[i];
142 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600143 }
144 }
145 return info;
146}
147
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700148static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600149 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700150 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600151 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700152 } else {
153 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
154 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
155 if ((flags & info.stage_access_bit).any()) {
156 if (!out_str.empty()) {
157 out_str.append(sep);
158 }
159 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600160 }
John Zulauf59e25072020-07-17 10:55:21 -0600161 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700162 if (out_str.length() == 0) {
163 out_str.append("Unhandled SyncStageAccess");
164 }
John Zulauf59e25072020-07-17 10:55:21 -0600165 }
166 return out_str;
167}
168
John Zulauf14940722021-04-12 15:19:02 -0600169static std::string string_UsageTag(const ResourceUsageRecord &tag) {
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700170 std::stringstream out;
171
John Zulauffaea0ee2021-01-14 14:01:32 -0700172 out << "command: " << CommandTypeString(tag.command);
173 out << ", seq_no: " << tag.seq_num;
174 if (tag.sub_command != 0) {
175 out << ", subcmd: " << tag.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700176 }
177 return out.str();
178}
John Zulauf4fa68462021-04-26 21:04:22 -0600179static std::string string_UsageIndex(SyncStageAccessIndex usage_index) {
180 const char *stage_access_name = "INVALID_STAGE_ACCESS";
181 if (usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size())) {
182 stage_access_name = syncStageAccessInfoByStageAccessIndex[usage_index].name;
183 }
184 return std::string(stage_access_name);
185}
186
187struct NoopBarrierAction {
188 explicit NoopBarrierAction() {}
189 void operator()(ResourceAccessState *access) const {}
John Zulauf5c628d02021-05-04 15:46:36 -0600190 const bool layout_transition = false;
John Zulauf4fa68462021-04-26 21:04:22 -0600191};
192
193// NOTE: Make sure the proxy doesn't outlive from, as the proxy is pointing directly to access contexts owned by from.
194CommandBufferAccessContext::CommandBufferAccessContext(const CommandBufferAccessContext &from, AsProxyContext dummy)
195 : CommandBufferAccessContext(from.sync_state_) {
196 // Copy only the needed fields out of from for a temporary, proxy command buffer context
197 cb_state_ = from.cb_state_;
198 queue_flags_ = from.queue_flags_;
199 destroyed_ = from.destroyed_;
200 access_log_ = from.access_log_; // potentially large, but no choice given tagging lookup.
201 command_number_ = from.command_number_;
202 subcommand_number_ = from.subcommand_number_;
203 reset_count_ = from.reset_count_;
204
205 const auto *from_context = from.GetCurrentAccessContext();
206 assert(from_context);
207
208 // Construct a fully resolved single access context out of from
209 const NoopBarrierAction noop_barrier;
210 for (AccessAddressType address_type : kAddressTypes) {
211 from_context->ResolveAccessRange(address_type, kFullRange, noop_barrier,
212 &cb_access_context_.GetAccessStateMap(address_type), nullptr);
213 }
214 // The proxy has flatten the current render pass context (if any), but the async contexts are needed for hazard detection
215 cb_access_context_.ImportAsyncContexts(*from_context);
216
217 events_context_ = from.events_context_;
218
219 // We don't want to copy the full render_pass_context_ history just for the proxy.
220}
221
222std::string CommandBufferAccessContext::FormatUsage(const ResourceUsageTag tag) const {
223 std::stringstream out;
224 assert(tag < access_log_.size());
225 const auto &record = access_log_[tag];
226 out << string_UsageTag(record);
227 if (record.cb_state != cb_state_.get()) {
228 out << ", command_buffer: " << sync_state_->report_data->FormatHandle(record.cb_state->commandBuffer()).c_str();
229 if (record.cb_state->Destroyed()) {
230 out << " (destroyed)";
231 }
232
John Zulauf4fa68462021-04-26 21:04:22 -0600233 }
John Zulaufd142c9a2022-04-12 14:22:44 -0600234 out << ", reset_no: " << std::to_string(record.reset_count);
John Zulauf4fa68462021-04-26 21:04:22 -0600235 return out.str();
236}
237std::string CommandBufferAccessContext::FormatUsage(const ResourceFirstAccess &access) const {
238 std::stringstream out;
239 out << "(recorded_usage: " << string_UsageIndex(access.usage_index);
240 out << ", " << FormatUsage(access.tag) << ")";
241 return out.str();
242}
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700243
John Zulauffaea0ee2021-01-14 14:01:32 -0700244std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600245 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600246 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
247 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600248 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600249 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
250 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf4fa68462021-04-26 21:04:22 -0600251 out << "(";
252 if (!hazard.recorded_access.get()) {
253 // if we have a recorded usage the usage is reported from the recorded contexts point of view
254 out << "usage: " << usage_info.name << ", ";
255 }
256 out << "prior_usage: " << stage_access_name;
John Zulauf59e25072020-07-17 10:55:21 -0600257 if (IsHazardVsRead(hazard.hazard)) {
258 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700259 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600260 } else {
261 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
262 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
263 }
264
ziga-lunarg0f248902022-03-24 16:42:45 +0100265 if (tag < access_log_.size()) {
266 out << ", " << FormatUsage(tag) << ")";
267 }
John Zulauf1dae9192020-06-16 15:46:44 -0600268 return out.str();
269}
270
John Zulaufd14743a2020-07-03 09:42:39 -0600271// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
272// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
273// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700274static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700275static const SyncStageAccessFlags kColorAttachmentAccessScope =
276 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
277 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
278 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
279 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700280static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
281 VK_PIPELINE_STAGE_2_EARLY_FRAGMENT_TESTS_BIT_KHR | VK_PIPELINE_STAGE_2_LATE_FRAGMENT_TESTS_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700282static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
283 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
284 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
285 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700286static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700287static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600288
John Zulauf8e3c3e92021-01-06 11:19:36 -0700289ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700290 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700291 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
292 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
293 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
294
John Zulauf7635de32020-05-29 17:14:15 -0600295// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulauf14940722021-04-12 15:19:02 -0600296static const ResourceUsageTag kCurrentCommandTag(ResourceUsageRecord::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600297
Jeremy Gebben62c3bf42021-07-21 15:38:24 -0600298static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) { return bindable.GetFakeBaseAddress(); }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600299
locke-lunarg3c038002020-04-30 23:08:08 -0600300inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
301 if (size == VK_WHOLE_SIZE) {
302 return (whole_size - offset);
303 }
304 return size;
305}
306
John Zulauf3e86bf02020-09-12 10:47:57 -0600307static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
308 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
309}
310
John Zulauf16adfc92020-04-08 10:28:33 -0600311template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600312static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600313 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
314}
315
John Zulauf355e49b2020-04-24 15:11:15 -0600316static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600317
John Zulauf3e86bf02020-09-12 10:47:57 -0600318static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
319 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
320}
321
322static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
323 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
324}
325
John Zulauf4a6105a2020-11-17 15:11:05 -0700326// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
327//
John Zulauf10f1f522020-12-18 12:00:35 -0700328// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
329//
John Zulauf4a6105a2020-11-17 15:11:05 -0700330// Usage:
331// Constructor() -- initializes the generator to point to the begin of the space declared.
332// * -- the current range of the generator empty signfies end
333// ++ -- advance to the next non-empty range (or end)
334
335// A wrapper for a single range with the same semantics as the actual generators below
336template <typename KeyType>
337class SingleRangeGenerator {
338 public:
339 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700340 const KeyType &operator*() const { return current_; }
341 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700342 SingleRangeGenerator &operator++() {
343 current_ = KeyType(); // just one real range
344 return *this;
345 }
346
347 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
348
349 private:
350 SingleRangeGenerator() = default;
351 const KeyType range_;
352 KeyType current_;
353};
354
John Zulaufae842002021-04-15 18:20:55 -0600355// Generate the ranges that are the intersection of range and the entries in the RangeMap
356template <typename RangeMap, typename KeyType = typename RangeMap::key_type>
357class MapRangesRangeGenerator {
John Zulauf4a6105a2020-11-17 15:11:05 -0700358 public:
John Zulaufd5115702021-01-18 12:34:33 -0700359 // Default constructed is safe to dereference for "empty" test, but for no other operation.
John Zulaufae842002021-04-15 18:20:55 -0600360 MapRangesRangeGenerator() : range_(), map_(nullptr), map_pos_(), current_() {
John Zulaufd5115702021-01-18 12:34:33 -0700361 // Default construction for KeyType *must* be empty range
362 assert(current_.empty());
363 }
John Zulaufae842002021-04-15 18:20:55 -0600364 MapRangesRangeGenerator(const RangeMap &filter, const KeyType &range) : range_(range), map_(&filter), map_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700365 SeekBegin();
366 }
John Zulaufae842002021-04-15 18:20:55 -0600367 MapRangesRangeGenerator(const MapRangesRangeGenerator &from) = default;
John Zulaufd5115702021-01-18 12:34:33 -0700368
John Zulauf4a6105a2020-11-17 15:11:05 -0700369 const KeyType &operator*() const { return current_; }
370 const KeyType *operator->() const { return &current_; }
John Zulaufae842002021-04-15 18:20:55 -0600371 MapRangesRangeGenerator &operator++() {
372 ++map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700373 UpdateCurrent();
374 return *this;
375 }
376
John Zulaufae842002021-04-15 18:20:55 -0600377 bool operator==(const MapRangesRangeGenerator &other) const { return current_ == other.current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700378
John Zulaufae842002021-04-15 18:20:55 -0600379 protected:
John Zulauf4a6105a2020-11-17 15:11:05 -0700380 void UpdateCurrent() {
John Zulaufae842002021-04-15 18:20:55 -0600381 if (map_pos_ != map_->cend()) {
382 current_ = range_ & map_pos_->first;
John Zulauf4a6105a2020-11-17 15:11:05 -0700383 } else {
384 current_ = KeyType();
385 }
386 }
387 void SeekBegin() {
John Zulaufae842002021-04-15 18:20:55 -0600388 map_pos_ = map_->lower_bound(range_);
John Zulauf4a6105a2020-11-17 15:11:05 -0700389 UpdateCurrent();
390 }
John Zulaufae842002021-04-15 18:20:55 -0600391
392 // Adding this functionality here, to avoid gratuitous Base:: qualifiers in the derived class
393 // Note: Not exposed in this classes public interface to encourage using a consistent ++/empty generator semantic
394 template <typename Pred>
395 MapRangesRangeGenerator &PredicatedIncrement(Pred &pred) {
396 do {
397 ++map_pos_;
398 } while (map_pos_ != map_->cend() && map_pos_->first.intersects(range_) && !pred(map_pos_));
399 UpdateCurrent();
400 return *this;
401 }
402
John Zulauf4a6105a2020-11-17 15:11:05 -0700403 const KeyType range_;
John Zulaufae842002021-04-15 18:20:55 -0600404 const RangeMap *map_;
405 typename RangeMap::const_iterator map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700406 KeyType current_;
407};
John Zulaufd5115702021-01-18 12:34:33 -0700408using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulaufae842002021-04-15 18:20:55 -0600409using EventSimpleRangeGenerator = MapRangesRangeGenerator<SyncEventState::ScopeMap>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700410
John Zulaufae842002021-04-15 18:20:55 -0600411// Generate the ranges for entries meeting the predicate that are the intersection of range and the entries in the RangeMap
412template <typename RangeMap, typename Predicate, typename KeyType = typename RangeMap::key_type>
413class PredicatedMapRangesRangeGenerator : public MapRangesRangeGenerator<RangeMap, KeyType> {
414 public:
415 using Base = MapRangesRangeGenerator<RangeMap, KeyType>;
416 // Default constructed is safe to dereference for "empty" test, but for no other operation.
417 PredicatedMapRangesRangeGenerator() : Base(), pred_() {}
418 PredicatedMapRangesRangeGenerator(const RangeMap &filter, const KeyType &range, Predicate pred)
419 : Base(filter, range), pred_(pred) {}
420 PredicatedMapRangesRangeGenerator(const PredicatedMapRangesRangeGenerator &from) = default;
421
422 PredicatedMapRangesRangeGenerator &operator++() {
423 Base::PredicatedIncrement(pred_);
424 return *this;
425 }
426
427 protected:
428 Predicate pred_;
429};
John Zulauf4a6105a2020-11-17 15:11:05 -0700430
431// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulaufae842002021-04-15 18:20:55 -0600432// Templated to allow for different Range generators or map sources...
433template <typename RangeMap, typename RangeGen, typename KeyType = typename RangeMap::key_type>
John Zulauf4a6105a2020-11-17 15:11:05 -0700434class FilteredGeneratorGenerator {
435 public:
John Zulaufd5115702021-01-18 12:34:33 -0700436 // Default constructed is safe to dereference for "empty" test, but for no other operation.
437 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
438 // Default construction for KeyType *must* be empty range
439 assert(current_.empty());
440 }
John Zulaufae842002021-04-15 18:20:55 -0600441 FilteredGeneratorGenerator(const RangeMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700442 SeekBegin();
443 }
John Zulaufd5115702021-01-18 12:34:33 -0700444 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700445 const KeyType &operator*() const { return current_; }
446 const KeyType *operator->() const { return &current_; }
447 FilteredGeneratorGenerator &operator++() {
448 KeyType gen_range = GenRange();
449 KeyType filter_range = FilterRange();
450 current_ = KeyType();
451 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
452 if (gen_range.end > filter_range.end) {
453 // if the generated range is beyond the filter_range, advance the filter range
454 filter_range = AdvanceFilter();
455 } else {
456 gen_range = AdvanceGen();
457 }
458 current_ = gen_range & filter_range;
459 }
460 return *this;
461 }
462
463 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
464
465 private:
466 KeyType AdvanceFilter() {
467 ++filter_pos_;
468 auto filter_range = FilterRange();
469 if (filter_range.valid()) {
470 FastForwardGen(filter_range);
471 }
472 return filter_range;
473 }
474 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700475 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700476 auto gen_range = GenRange();
477 if (gen_range.valid()) {
478 FastForwardFilter(gen_range);
479 }
480 return gen_range;
481 }
482
483 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700484 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700485
486 KeyType FastForwardFilter(const KeyType &range) {
487 auto filter_range = FilterRange();
488 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700489 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700490 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
491 if (retry_count < kRetryLimit) {
492 ++filter_pos_;
493 filter_range = FilterRange();
494 retry_count++;
495 } else {
496 // Okay we've tried walking, do a seek.
497 filter_pos_ = filter_->lower_bound(range);
498 break;
499 }
500 }
501 return FilterRange();
502 }
503
504 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
505 // faster.
506 KeyType FastForwardGen(const KeyType &range) {
507 auto gen_range = GenRange();
508 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700509 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700510 gen_range = GenRange();
511 }
512 return gen_range;
513 }
514
515 void SeekBegin() {
516 auto gen_range = GenRange();
517 if (gen_range.empty()) {
518 current_ = KeyType();
519 filter_pos_ = filter_->cend();
520 } else {
521 filter_pos_ = filter_->lower_bound(gen_range);
522 current_ = gen_range & FilterRange();
523 }
524 }
525
John Zulaufae842002021-04-15 18:20:55 -0600526 const RangeMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700527 RangeGen gen_;
John Zulaufae842002021-04-15 18:20:55 -0600528 typename RangeMap::const_iterator filter_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700529 KeyType current_;
530};
531
532using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
533
John Zulauf5c5e88d2019-12-26 11:22:02 -0700534
John Zulauf3e86bf02020-09-12 10:47:57 -0600535ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
536 VkDeviceSize stride) {
537 VkDeviceSize range_start = offset + first_index * stride;
538 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600539 if (count == UINT32_MAX) {
540 range_size = buf_whole_size - range_start;
541 } else {
542 range_size = count * stride;
543 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600544 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600545}
546
locke-lunarg654e3692020-06-04 17:19:15 -0600547SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
548 VkShaderStageFlagBits stage_flag) {
549 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
550 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
551 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
552 }
553 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
554 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
555 assert(0);
556 }
557 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
558 return stage_access->second.uniform_read;
559 }
560
561 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
562 // Because if write hazard happens, read hazard might or might not happen.
563 // But if write hazard doesn't happen, read hazard is impossible to happen.
564 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700565 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600566 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700567 // TODO: sampled_read
568 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600569}
570
locke-lunarg37047832020-06-12 13:44:45 -0600571bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
572 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
573 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
574 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
575 ? true
576 : false;
577}
578
579bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
580 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
581 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
582 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
583 ? true
584 : false;
585}
586
John Zulauf355e49b2020-04-24 15:11:15 -0600587// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600588template <typename Action>
589static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
590 Action &action) {
591 // At this point the "apply over range" logic only supports a single memory binding
592 if (!SimpleBinding(image_state)) return;
593 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600594 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700595 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
596 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600597 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700598 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600599 }
600}
601
John Zulauf7635de32020-05-29 17:14:15 -0600602// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
603// Used by both validation and record operations
604//
605// The signature for Action() reflect the needs of both uses.
606template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700607void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
608 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600609 const auto &rp_ci = rp_state.createInfo;
610 const auto *attachment_ci = rp_ci.pAttachments;
611 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
612
613 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
614 const auto *color_attachments = subpass_ci.pColorAttachments;
615 const auto *color_resolve = subpass_ci.pResolveAttachments;
616 if (color_resolve && color_attachments) {
617 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
618 const auto &color_attach = color_attachments[i].attachment;
619 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
620 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
621 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700622 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
623 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600624 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700625 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
626 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600627 }
628 }
629 }
630
631 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700632 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600633 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
634 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
635 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
636 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
637 const auto src_ci = attachment_ci[src_at];
638 // The formats are required to match so we can pick either
639 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
640 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
641 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600642
643 // Figure out which aspects are actually touched during resolve operations
644 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700645 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600646 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600647 aspect_string = "depth/stencil";
648 } else if (resolve_depth) {
649 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700650 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600651 aspect_string = "depth";
652 } else if (resolve_stencil) {
653 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700654 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600655 aspect_string = "stencil";
656 }
657
John Zulaufd0ec59f2021-03-13 14:25:08 -0700658 if (aspect_string) {
659 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
660 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
661 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
662 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600663 }
664 }
665}
666
667// Action for validating resolve operations
668class ValidateResolveAction {
669 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700670 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700671 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600672 : render_pass_(render_pass),
673 subpass_(subpass),
674 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700675 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600676 func_name_(func_name),
677 skip_(false) {}
678 void operator()(const char *aspect_name, const char *attachment_name, uint32_t src_at, uint32_t dst_at,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700679 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
680 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600681 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700682 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600683 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700684 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700685 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700686 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
687 " to resolve attachment %" PRIu32 ". Access info %s.",
688 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700689 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600690 }
691 }
692 // Providing a mechanism for the constructing caller to get the result of the validation
693 bool GetSkip() const { return skip_; }
694
695 private:
696 VkRenderPass render_pass_;
697 const uint32_t subpass_;
698 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700699 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600700 const char *func_name_;
701 bool skip_;
702};
703
704// Update action for resolve operations
705class UpdateStateResolveAction {
706 public:
John Zulauf14940722021-04-12 15:19:02 -0600707 UpdateStateResolveAction(AccessContext &context, ResourceUsageTag tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700708 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
709 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600710 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700711 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600712 }
713
714 private:
715 AccessContext &context_;
John Zulauf14940722021-04-12 15:19:02 -0600716 const ResourceUsageTag tag_;
John Zulauf7635de32020-05-29 17:14:15 -0600717};
718
John Zulauf59e25072020-07-17 10:55:21 -0600719void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
John Zulauf14940722021-04-12 15:19:02 -0600720 const SyncStageAccessFlags &prior_, const ResourceUsageTag tag_) {
John Zulauf4fa68462021-04-26 21:04:22 -0600721 access_state = layer_data::make_unique<const ResourceAccessState>(*access_state_);
John Zulauf59e25072020-07-17 10:55:21 -0600722 usage_index = usage_index_;
723 hazard = hazard_;
724 prior_access = prior_;
725 tag = tag_;
726}
727
John Zulauf4fa68462021-04-26 21:04:22 -0600728void HazardResult::AddRecordedAccess(const ResourceFirstAccess &first_access) {
729 recorded_access = layer_data::make_unique<const ResourceFirstAccess>(first_access);
730}
731
John Zulauf540266b2020-04-06 18:54:53 -0600732AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
733 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600734 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600735 Reset();
736 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700737 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
738 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600739 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600740 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600741 const auto prev_pass = prev_dep.first->pass;
742 const auto &prev_barriers = prev_dep.second;
743 assert(prev_dep.second.size());
744 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
745 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700746 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600747
748 async_.reserve(subpass_dep.async.size());
749 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700750 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600751 }
John Zulauf22aefed2021-03-11 18:14:35 -0700752 if (has_barrier_from_external) {
753 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
754 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
755 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600756 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600757 if (subpass_dep.barrier_to_external.size()) {
758 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600759 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700760}
761
John Zulauf5f13a792020-03-10 07:31:21 -0600762template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700763HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600764 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600765 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600766 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600767
768 HazardResult hazard;
769 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
770 hazard = detector.Detect(prev);
771 }
772 return hazard;
773}
774
John Zulauf4a6105a2020-11-17 15:11:05 -0700775template <typename Action>
776void AccessContext::ForAll(Action &&action) {
777 for (const auto address_type : kAddressTypes) {
778 auto &accesses = GetAccessStateMap(address_type);
779 for (const auto &access : accesses) {
780 action(address_type, access);
781 }
782 }
783}
784
John Zulauf3d84f1b2020-03-09 13:33:25 -0600785// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
786// the DAG of the contexts (for example subpasses)
787template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700788HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600789 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600790 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600791
John Zulauf1a224292020-06-30 14:52:13 -0600792 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600793 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
794 // so we'll check these first
795 for (const auto &async_context : async_) {
796 hazard = async_context->DetectAsyncHazard(type, detector, range);
797 if (hazard.hazard) return hazard;
798 }
John Zulauf5f13a792020-03-10 07:31:21 -0600799 }
800
John Zulauf1a224292020-06-30 14:52:13 -0600801 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600802
John Zulauf69133422020-05-20 14:55:53 -0600803 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600804 const auto the_end = accesses.cend(); // End is not invalidated
805 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600806 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600807
John Zulauf3cafbf72021-03-26 16:55:19 -0600808 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600809 // Cover any leading gap, or gap between entries
810 if (detect_prev) {
811 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
812 // Cover any leading gap, or gap between entries
813 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600814 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600815 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600816 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600817 if (hazard.hazard) return hazard;
818 }
John Zulauf69133422020-05-20 14:55:53 -0600819 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
820 gap.begin = pos->first.end;
821 }
822
823 hazard = detector.Detect(pos);
824 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600825 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600826 }
827
828 if (detect_prev) {
829 // Detect in the trailing empty as needed
830 gap.end = range.end;
831 if (gap.non_empty()) {
832 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600833 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600834 }
835
836 return hazard;
837}
838
839// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
840template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700841HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
842 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600843 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600844 auto pos = accesses.lower_bound(range);
845 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600846
John Zulauf3d84f1b2020-03-09 13:33:25 -0600847 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600848 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700849 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600850 if (hazard.hazard) break;
851 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600852 }
John Zulauf16adfc92020-04-08 10:28:33 -0600853
John Zulauf3d84f1b2020-03-09 13:33:25 -0600854 return hazard;
855}
856
John Zulaufb02c1eb2020-10-06 16:33:36 -0600857struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700858 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600859 void operator()(ResourceAccessState *access) const {
860 assert(access);
861 access->ApplyBarriers(barriers, true);
862 }
863 const std::vector<SyncBarrier> &barriers;
864};
865
John Zulauf22aefed2021-03-11 18:14:35 -0700866struct ApplyTrackbackStackAction {
867 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
868 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
869 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600870 void operator()(ResourceAccessState *access) const {
871 assert(access);
872 assert(!access->HasPendingState());
873 access->ApplyBarriers(barriers, false);
874 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700875 if (previous_barrier) {
876 assert(bool(*previous_barrier));
877 (*previous_barrier)(access);
878 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600879 }
880 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700881 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600882};
883
884// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
885// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
886// *different* map from dest.
887// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
888// range [first, last)
889template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600890static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
891 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600892 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600893 auto at = entry;
894 for (auto pos = first; pos != last; ++pos) {
895 // Every member of the input iterator range must fit within the remaining portion of entry
896 assert(at->first.includes(pos->first));
897 assert(at != dest->end());
898 // Trim up at to the same size as the entry to resolve
899 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600900 auto access = pos->second; // intentional copy
901 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600902 at->second.Resolve(access);
903 ++at; // Go to the remaining unused section of entry
904 }
905}
906
John Zulaufa0a98292020-09-18 09:30:10 -0600907static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
908 SyncBarrier merged = {};
909 for (const auto &barrier : barriers) {
910 merged.Merge(barrier);
911 }
912 return merged;
913}
914
John Zulaufb02c1eb2020-10-06 16:33:36 -0600915template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700916void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600917 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
918 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600919 if (!range.non_empty()) return;
920
John Zulauf355e49b2020-04-24 15:11:15 -0600921 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
922 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600923 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600924 if (current->pos_B->valid) {
925 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600926 auto access = src_pos->second; // intentional copy
927 barrier_action(&access);
928
John Zulauf16adfc92020-04-08 10:28:33 -0600929 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600930 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
931 trimmed->second.Resolve(access);
932 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600933 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600934 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600935 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600936 }
John Zulauf16adfc92020-04-08 10:28:33 -0600937 } else {
938 // we have to descend to fill this gap
939 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700940 ResourceAccessRange recurrence_range = current_range;
941 // The current context is empty for the current range, so recur to fill the gap.
942 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
943 // is not valid, to minimize that recurrence
944 if (current->pos_B.at_end()) {
945 // Do the remainder here....
946 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600947 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700948 // Recur only over the range until B becomes valid (within the limits of range).
949 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600950 }
John Zulauf22aefed2021-03-11 18:14:35 -0700951 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
952
John Zulauf355e49b2020-04-24 15:11:15 -0600953 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
954 // iterator of the outer while.
955
956 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
957 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
958 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700959 const auto seek_to = recurrence_range.end - 1; // The subtraction is safe as range can't be empty (loop condition)
locke-lunarg88dbb542020-06-23 22:05:42 -0600960 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600961 current.seek(seek_to);
962 } else if (!current->pos_A->valid && infill_state) {
963 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
964 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
965 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600966 }
John Zulauf5f13a792020-03-10 07:31:21 -0600967 }
ziga-lunargf0e27ad2022-03-28 00:44:12 +0200968 if (current->range.non_empty()) {
969 ++current;
970 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600971 }
John Zulauf1a224292020-06-30 14:52:13 -0600972
973 // Infill if range goes passed both the current and resolve map prior contents
974 if (recur_to_infill && (current->range.end < range.end)) {
975 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700976 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600977 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600978}
979
John Zulauf22aefed2021-03-11 18:14:35 -0700980template <typename BarrierAction>
981void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
982 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
983 const BarrierAction &previous_barrier) const {
984 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
985 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
986}
987
John Zulauf43cc7462020-12-03 12:33:12 -0700988void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700989 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
990 const ResourceAccessStateFunction *previous_barrier) const {
991 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600992 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700993 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
994 ResourceAccessState state_copy;
995 if (previous_barrier) {
996 assert(bool(*previous_barrier));
997 state_copy = *infill_state;
998 (*previous_barrier)(&state_copy);
999 infill_state = &state_copy;
1000 }
1001 sparse_container::update_range_value(*descent_map, range, *infill_state,
1002 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -06001003 }
1004 } else {
1005 // Look for something to fill the gap further along.
1006 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -07001007 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001008 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001009 }
John Zulauf5f13a792020-03-10 07:31:21 -06001010 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001011}
1012
John Zulauf4a6105a2020-11-17 15:11:05 -07001013// Non-lazy import of all accesses, WaitEvents needs this.
1014void AccessContext::ResolvePreviousAccesses() {
1015 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -07001016 if (!prev_.size()) return; // If no previous contexts, nothing to do
1017
John Zulauf4a6105a2020-11-17 15:11:05 -07001018 for (const auto address_type : kAddressTypes) {
1019 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
1020 }
1021}
1022
John Zulauf43cc7462020-12-03 12:33:12 -07001023AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
1024 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -06001025}
1026
John Zulauf1507ee42020-05-18 11:33:09 -06001027static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001028 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1029 ? SYNC_ACCESS_INDEX_NONE
1030 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
1031 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001032 return stage_access;
1033}
1034static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001035 const auto stage_access =
1036 (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1037 ? SYNC_ACCESS_INDEX_NONE
1038 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
1039 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001040 return stage_access;
1041}
1042
John Zulauf7635de32020-05-29 17:14:15 -06001043// Caller must manage returned pointer
1044static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001045 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -06001046 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001047 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
1048 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -06001049 return proxy;
1050}
1051
John Zulaufb02c1eb2020-10-06 16:33:36 -06001052template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001053void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1054 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
1055 const ResourceAccessState *infill_state) const {
1056 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1057 if (!attachment_gen) return;
1058
1059 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1060 const AccessAddressType address_type = view_gen.GetAddressType();
1061 for (; range_gen->non_empty(); ++range_gen) {
1062 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001063 }
John Zulauf62f10592020-04-03 12:20:02 -06001064}
1065
John Zulauf7635de32020-05-29 17:14:15 -06001066// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -07001067bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001068 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001069 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001070 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001071 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
1072 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
1073 // those affects have not been recorded yet.
1074 //
1075 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1076 // to apply and only copy then, if this proves a hot spot.
1077 std::unique_ptr<AccessContext> proxy_for_prev;
1078 TrackBack proxy_track_back;
1079
John Zulauf355e49b2020-04-24 15:11:15 -06001080 const auto &transitions = rp_state.subpass_transitions[subpass];
1081 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001082 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1083
1084 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001085 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001086 if (prev_needs_proxy) {
1087 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001088 proxy_for_prev.reset(
1089 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -06001090 proxy_track_back = *track_back;
1091 proxy_track_back.context = proxy_for_prev.get();
1092 }
1093 track_back = &proxy_track_back;
1094 }
1095 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001096 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001097 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001098 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1099 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1100 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1101 string_VkImageLayout(transition.old_layout),
1102 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001103 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001104 }
1105 }
1106 return skip;
1107}
1108
John Zulauf64ffe552021-02-06 10:25:07 -07001109bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001110 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001111 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001112 bool skip = false;
1113 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001114
John Zulauf1507ee42020-05-18 11:33:09 -06001115 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1116 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001117 const auto &view_gen = attachment_views[i];
1118 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001119 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001120
1121 // Need check in the following way
1122 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1123 // vs. transition
1124 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1125 // for each aspect loaded.
1126
1127 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001128 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001129 const bool is_color = !(has_depth || has_stencil);
1130
1131 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001132 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001133
John Zulaufaff20662020-06-01 14:07:58 -06001134 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001135 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001136
John Zulaufb02c1eb2020-10-06 16:33:36 -06001137 bool checked_stencil = false;
John Zulauf57261402021-08-13 11:32:06 -06001138 if (is_color && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001139 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001140 aspect = "color";
1141 } else {
John Zulauf57261402021-08-13 11:32:06 -06001142 if (has_depth && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001143 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1144 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001145 aspect = "depth";
1146 }
John Zulauf57261402021-08-13 11:32:06 -06001147 if (!hazard.hazard && has_stencil && (stencil_load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001148 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1149 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001150 aspect = "stencil";
1151 checked_stencil = true;
1152 }
1153 }
1154
1155 if (hazard.hazard) {
1156 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001157 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001158 if (hazard.tag == kCurrentCommandTag) {
1159 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001160 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001161 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1162 " aspect %s during load with loadOp %s.",
1163 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1164 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001165 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001166 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001167 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001168 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001169 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001170 }
1171 }
1172 }
1173 }
1174 return skip;
1175}
1176
John Zulaufaff20662020-06-01 14:07:58 -06001177// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1178// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1179// store is part of the same Next/End operation.
1180// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001181bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001182 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001183 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001184 bool skip = false;
1185 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001186
1187 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1188 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001189 const AttachmentViewGen &view_gen = attachment_views[i];
1190 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001191 const auto &ci = attachment_ci[i];
1192
1193 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1194 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1195 // sake, we treat DONT_CARE as writing.
1196 const bool has_depth = FormatHasDepth(ci.format);
1197 const bool has_stencil = FormatHasStencil(ci.format);
1198 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001199 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001200 if (!has_stencil && !store_op_stores) continue;
1201
1202 HazardResult hazard;
1203 const char *aspect = nullptr;
1204 bool checked_stencil = false;
1205 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001206 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1207 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001208 aspect = "color";
1209 } else {
John Zulauf57261402021-08-13 11:32:06 -06001210 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001211 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001212 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1213 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001214 aspect = "depth";
1215 }
1216 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001217 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1218 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001219 aspect = "stencil";
1220 checked_stencil = true;
1221 }
1222 }
1223
1224 if (hazard.hazard) {
1225 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1226 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001227 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001228 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1229 " %s aspect during store with %s %s. Access info %s",
1230 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001231 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001232 }
1233 }
1234 }
1235 return skip;
1236}
1237
John Zulauf64ffe552021-02-06 10:25:07 -07001238bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001239 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1240 const char *func_name, uint32_t subpass) const {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001241 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001242 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001243 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001244}
1245
John Zulauf3d84f1b2020-03-09 13:33:25 -06001246class HazardDetector {
1247 SyncStageAccessIndex usage_index_;
1248
1249 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001250 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf14940722021-04-12 15:19:02 -06001251 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001252 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001253 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001254 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001255};
1256
John Zulauf69133422020-05-20 14:55:53 -06001257class HazardDetectorWithOrdering {
1258 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001259 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001260
1261 public:
1262 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001263 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001264 }
John Zulauf14940722021-04-12 15:19:02 -06001265 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001266 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001267 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001268 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001269};
1270
John Zulauf16adfc92020-04-08 10:28:33 -06001271HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001272 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001273 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001274 const auto base_address = ResourceBaseAddress(buffer);
1275 HazardDetector detector(usage_index);
1276 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001277}
1278
John Zulauf69133422020-05-20 14:55:53 -06001279template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001280HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1281 DetectOptions options) const {
1282 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1283 if (!attachment_gen) return HazardResult();
1284
1285 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1286 const auto address_type = view_gen.GetAddressType();
1287 for (; range_gen->non_empty(); ++range_gen) {
1288 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1289 if (hazard.hazard) return hazard;
1290 }
1291
1292 return HazardResult();
1293}
1294
1295template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001296HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1297 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1298 const VkExtent3D &extent, DetectOptions options) const {
1299 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001300 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001301 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1302 base_address);
1303 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001304 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001305 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001306 if (hazard.hazard) return hazard;
1307 }
1308 return HazardResult();
1309}
John Zulauf110413c2021-03-20 05:38:38 -06001310template <typename Detector>
1311HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1312 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1313 if (!SimpleBinding(image)) return HazardResult();
1314 const auto base_address = ResourceBaseAddress(image);
1315 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1316 const auto address_type = ImageAddressType(image);
1317 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001318 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1319 if (hazard.hazard) return hazard;
1320 }
1321 return HazardResult();
1322}
John Zulauf69133422020-05-20 14:55:53 -06001323
John Zulauf540266b2020-04-06 18:54:53 -06001324HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1325 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1326 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001327 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1328 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001329 HazardDetector detector(current_usage);
1330 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001331}
1332
1333HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001334 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001335 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001336 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001337}
1338
John Zulaufd0ec59f2021-03-13 14:25:08 -07001339HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1340 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1341 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1342 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1343}
1344
John Zulauf69133422020-05-20 14:55:53 -06001345HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001346 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001347 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001348 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001349 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001350}
1351
John Zulauf3d84f1b2020-03-09 13:33:25 -06001352class BarrierHazardDetector {
1353 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001354 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001355 SyncStageAccessFlags src_access_scope)
1356 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1357
John Zulauf5f13a792020-03-10 07:31:21 -06001358 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1359 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001360 }
John Zulauf14940722021-04-12 15:19:02 -06001361 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001362 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001363 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001364 }
1365
1366 private:
1367 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001368 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001369 SyncStageAccessFlags src_access_scope_;
1370};
1371
John Zulauf4a6105a2020-11-17 15:11:05 -07001372class EventBarrierHazardDetector {
1373 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001374 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001375 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
John Zulauf14940722021-04-12 15:19:02 -06001376 ResourceUsageTag scope_tag)
John Zulauf4a6105a2020-11-17 15:11:05 -07001377 : usage_index_(usage_index),
1378 src_exec_scope_(src_exec_scope),
1379 src_access_scope_(src_access_scope),
1380 event_scope_(event_scope),
1381 scope_pos_(event_scope.cbegin()),
1382 scope_end_(event_scope.cend()),
1383 scope_tag_(scope_tag) {}
1384
1385 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1386 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1387 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1388 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1389 if (scope_pos_ == scope_end_) return HazardResult();
1390 if (!scope_pos_->first.intersects(pos->first)) {
1391 event_scope_.lower_bound(pos->first);
1392 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1393 }
1394
1395 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1396 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1397 }
John Zulauf14940722021-04-12 15:19:02 -06001398 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07001399 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1400 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1401 }
1402
1403 private:
1404 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001405 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001406 SyncStageAccessFlags src_access_scope_;
1407 const SyncEventState::ScopeMap &event_scope_;
1408 SyncEventState::ScopeMap::const_iterator scope_pos_;
1409 SyncEventState::ScopeMap::const_iterator scope_end_;
John Zulauf14940722021-04-12 15:19:02 -06001410 const ResourceUsageTag scope_tag_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001411};
1412
Jeremy Gebben40a22942020-12-22 14:22:06 -07001413HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001414 const SyncStageAccessFlags &src_access_scope,
1415 const VkImageSubresourceRange &subresource_range,
1416 const SyncEventState &sync_event, DetectOptions options) const {
1417 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1418 // first access scope map to use, and there's no easy way to plumb it in below.
1419 const auto address_type = ImageAddressType(image);
1420 const auto &event_scope = sync_event.FirstScope(address_type);
1421
1422 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1423 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001424 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001425}
1426
John Zulaufd0ec59f2021-03-13 14:25:08 -07001427HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1428 DetectOptions options) const {
1429 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1430 barrier.src_access_scope);
1431 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1432}
1433
Jeremy Gebben40a22942020-12-22 14:22:06 -07001434HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001435 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001436 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001437 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001438 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001439 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001440}
1441
Jeremy Gebben40a22942020-12-22 14:22:06 -07001442HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001443 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001444 const VkImageMemoryBarrier &barrier) const {
1445 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1446 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1447 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1448}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001449HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001450 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001451 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001452}
John Zulauf355e49b2020-04-24 15:11:15 -06001453
John Zulauf9cb530d2019-09-30 14:14:10 -06001454template <typename Flags, typename Map>
1455SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1456 SyncStageAccessFlags scope = 0;
1457 for (const auto &bit_scope : map) {
1458 if (flag_mask < bit_scope.first) break;
1459
1460 if (flag_mask & bit_scope.first) {
1461 scope |= bit_scope.second;
1462 }
1463 }
1464 return scope;
1465}
1466
Jeremy Gebben40a22942020-12-22 14:22:06 -07001467SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001468 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1469}
1470
Jeremy Gebben40a22942020-12-22 14:22:06 -07001471SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1472 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001473}
1474
Jeremy Gebben40a22942020-12-22 14:22:06 -07001475// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1476SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001477 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1478 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1479 // of the union of all stage/access types for all the stages and the same unions for the access mask...
John Zulauf9cb530d2019-09-30 14:14:10 -06001480 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1481}
1482
1483template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001484void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001485 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1486 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001487 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001488 auto pos = accesses->lower_bound(range);
1489 if (pos == accesses->end() || !pos->first.intersects(range)) {
1490 // The range is empty, fill it with a default value.
1491 pos = action.Infill(accesses, pos, range);
1492 } else if (range.begin < pos->first.begin) {
1493 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001494 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001495 } else if (pos->first.begin < range.begin) {
1496 // Trim the beginning if needed
1497 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1498 ++pos;
1499 }
1500
1501 const auto the_end = accesses->end();
1502 while ((pos != the_end) && pos->first.intersects(range)) {
1503 if (pos->first.end > range.end) {
1504 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1505 }
1506
1507 pos = action(accesses, pos);
1508 if (pos == the_end) break;
1509
1510 auto next = pos;
1511 ++next;
1512 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1513 // Need to infill if next is disjoint
1514 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001515 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001516 next = action.Infill(accesses, next, new_range);
1517 }
1518 pos = next;
1519 }
1520}
John Zulaufd5115702021-01-18 12:34:33 -07001521
1522// Give a comparable interface for range generators and ranges
1523template <typename Action>
1524inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1525 assert(range);
1526 UpdateMemoryAccessState(accesses, *range, action);
1527}
1528
John Zulauf4a6105a2020-11-17 15:11:05 -07001529template <typename Action, typename RangeGen>
1530void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1531 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001532 RangeGen &range_gen = *range_gen_arg; // Non-const references must be * by style requirement but deref-ing * iterator is a pain
John Zulauf4a6105a2020-11-17 15:11:05 -07001533 for (; range_gen->non_empty(); ++range_gen) {
1534 UpdateMemoryAccessState(accesses, *range_gen, action);
1535 }
1536}
John Zulauf9cb530d2019-09-30 14:14:10 -06001537
John Zulaufd0ec59f2021-03-13 14:25:08 -07001538template <typename Action, typename RangeGen>
1539void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1540 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1541 for (; range_gen->non_empty(); ++range_gen) {
1542 UpdateMemoryAccessState(accesses, *range_gen, action);
1543 }
1544}
John Zulauf9cb530d2019-09-30 14:14:10 -06001545struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001546 using Iterator = ResourceAccessRangeMap::iterator;
1547 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001548 // this is only called on gaps, and never returns a gap.
1549 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001550 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001551 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001552 }
John Zulauf5f13a792020-03-10 07:31:21 -06001553
John Zulauf5c5e88d2019-12-26 11:22:02 -07001554 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001555 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001556 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001557 return pos;
1558 }
1559
John Zulauf43cc7462020-12-03 12:33:12 -07001560 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf14940722021-04-12 15:19:02 -06001561 SyncOrdering ordering_rule_, ResourceUsageTag tag_)
John Zulauf8e3c3e92021-01-06 11:19:36 -07001562 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001563 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001564 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001565 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001566 const SyncOrdering ordering_rule;
John Zulauf14940722021-04-12 15:19:02 -06001567 const ResourceUsageTag tag;
John Zulauf9cb530d2019-09-30 14:14:10 -06001568};
1569
John Zulauf4a6105a2020-11-17 15:11:05 -07001570// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001571struct PipelineBarrierOp {
1572 SyncBarrier barrier;
1573 bool layout_transition;
1574 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1575 : barrier(barrier_), layout_transition(layout_transition_) {}
1576 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001577 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001578 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1579};
John Zulauf4a6105a2020-11-17 15:11:05 -07001580// The barrier operation for wait events
1581struct WaitEventBarrierOp {
John Zulauf14940722021-04-12 15:19:02 -06001582 ResourceUsageTag scope_tag;
John Zulauf4a6105a2020-11-17 15:11:05 -07001583 SyncBarrier barrier;
1584 bool layout_transition;
John Zulauf14940722021-04-12 15:19:02 -06001585 WaitEventBarrierOp(const ResourceUsageTag scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1586 : scope_tag(scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
John Zulauf4a6105a2020-11-17 15:11:05 -07001587 WaitEventBarrierOp() = default;
John Zulauf14940722021-04-12 15:19:02 -06001588 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(scope_tag, barrier, layout_transition); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001589};
John Zulauf1e331ec2020-12-04 18:29:38 -07001590
John Zulauf4a6105a2020-11-17 15:11:05 -07001591// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1592// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1593// of a collection is known/present.
John Zulauf5c628d02021-05-04 15:46:36 -06001594template <typename BarrierOp, typename OpVector = std::vector<BarrierOp>>
John Zulauf89311b42020-09-29 16:28:47 -06001595class ApplyBarrierOpsFunctor {
1596 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001597 using Iterator = ResourceAccessRangeMap::iterator;
John Zulauf5c628d02021-05-04 15:46:36 -06001598 // Only called with a gap, and pos at the lower_bound(range)
1599 inline Iterator Infill(ResourceAccessRangeMap *accesses, const Iterator &pos, const ResourceAccessRange &range) const {
1600 if (!infill_default_) {
1601 return pos;
1602 }
1603 ResourceAccessState default_state;
1604 auto inserted = accesses->insert(pos, std::make_pair(range, default_state));
1605 return inserted;
1606 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001607
John Zulauf5c628d02021-05-04 15:46:36 -06001608 Iterator operator()(ResourceAccessRangeMap *accesses, const Iterator &pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001609 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001610 for (const auto &op : barrier_ops_) {
1611 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001612 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001613
John Zulauf89311b42020-09-29 16:28:47 -06001614 if (resolve_) {
1615 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1616 // another walk
1617 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001618 }
1619 return pos;
1620 }
1621
John Zulauf89311b42020-09-29 16:28:47 -06001622 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf5c628d02021-05-04 15:46:36 -06001623 ApplyBarrierOpsFunctor(bool resolve, typename OpVector::size_type size_hint, ResourceUsageTag tag)
1624 : resolve_(resolve), infill_default_(false), barrier_ops_(), tag_(tag) {
John Zulaufd5115702021-01-18 12:34:33 -07001625 barrier_ops_.reserve(size_hint);
1626 }
John Zulauf5c628d02021-05-04 15:46:36 -06001627 void EmplaceBack(const BarrierOp &op) {
1628 barrier_ops_.emplace_back(op);
1629 infill_default_ |= op.layout_transition;
1630 }
John Zulauf89311b42020-09-29 16:28:47 -06001631
1632 private:
1633 bool resolve_;
John Zulauf5c628d02021-05-04 15:46:36 -06001634 bool infill_default_;
1635 OpVector barrier_ops_;
John Zulauf14940722021-04-12 15:19:02 -06001636 const ResourceUsageTag tag_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001637};
1638
John Zulauf4a6105a2020-11-17 15:11:05 -07001639// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1640// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1641template <typename BarrierOp>
John Zulauf5c628d02021-05-04 15:46:36 -06001642class ApplyBarrierFunctor : public ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>> {
1643 using Base = ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>>;
1644
John Zulauf4a6105a2020-11-17 15:11:05 -07001645 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001646 ApplyBarrierFunctor(const BarrierOp &barrier_op) : Base(false, 1, kCurrentCommandTag) { Base::EmplaceBack(barrier_op); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001647};
1648
John Zulauf1e331ec2020-12-04 18:29:38 -07001649// This functor resolves the pendinging state.
John Zulauf5c628d02021-05-04 15:46:36 -06001650class ResolvePendingBarrierFunctor : public ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>> {
1651 using Base = ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>>;
1652
John Zulauf1e331ec2020-12-04 18:29:38 -07001653 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001654 ResolvePendingBarrierFunctor(ResourceUsageTag tag) : Base(true, 0, tag) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001655};
1656
John Zulauf8e3c3e92021-01-06 11:19:36 -07001657void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001658 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001659 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001660 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001661}
1662
John Zulauf8e3c3e92021-01-06 11:19:36 -07001663void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001664 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001665 if (!SimpleBinding(buffer)) return;
1666 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001667 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001668}
John Zulauf355e49b2020-04-24 15:11:15 -06001669
John Zulauf8e3c3e92021-01-06 11:19:36 -07001670void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001671 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1672 if (!SimpleBinding(image)) return;
1673 const auto base_address = ResourceBaseAddress(image);
1674 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1675 const auto address_type = ImageAddressType(image);
1676 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1677 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1678}
1679void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001680 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001681 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001682 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001683 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001684 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1685 base_address);
1686 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001687 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001688 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001689}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001690
1691void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
John Zulauf14940722021-04-12 15:19:02 -06001692 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001693 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1694 if (!gen) return;
1695 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1696 const auto address_type = view_gen.GetAddressType();
1697 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1698 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001699}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001700
John Zulauf8e3c3e92021-01-06 11:19:36 -07001701void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001702 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001703 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001704 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1705 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001706 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001707}
1708
John Zulaufd0ec59f2021-03-13 14:25:08 -07001709template <typename Action, typename RangeGen>
1710void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1711 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1712 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001713}
1714
1715template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001716void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1717 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1718 if (!gen) return;
1719 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001720}
1721
John Zulaufd0ec59f2021-03-13 14:25:08 -07001722void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1723 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001724 const ResourceUsageTag tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001725 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001726 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001727}
1728
John Zulaufd0ec59f2021-03-13 14:25:08 -07001729void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06001730 uint32_t subpass, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001731 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001732
1733 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1734 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001735 const auto &view_gen = attachment_views[i];
1736 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001737
1738 const auto &ci = attachment_ci[i];
1739 const bool has_depth = FormatHasDepth(ci.format);
1740 const bool has_stencil = FormatHasStencil(ci.format);
1741 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001742 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001743
1744 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001745 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1746 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001747 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001748 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001749 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1750 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001751 }
John Zulauf57261402021-08-13 11:32:06 -06001752 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001753 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001754 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1755 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001756 }
1757 }
1758 }
1759 }
1760}
1761
John Zulauf540266b2020-04-06 18:54:53 -06001762template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001763void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001764 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001765 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001766 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001767 }
1768}
1769
1770void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001771 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1772 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001773 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001774 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001775 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001776 }
1777 }
1778}
1779
John Zulauf4fa68462021-04-26 21:04:22 -06001780// Caller must ensure that lifespan of this is less than from
1781void AccessContext::ImportAsyncContexts(const AccessContext &from) { async_ = from.async_; }
1782
John Zulauf355e49b2020-04-24 15:11:15 -06001783// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001784HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1785 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001786
John Zulauf355e49b2020-04-24 15:11:15 -06001787 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001788 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001789
1790 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001791 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1792 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001793 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001794 if (!hazard.hazard) {
1795 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001796 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001797 }
John Zulaufa0a98292020-09-18 09:30:10 -06001798
John Zulauf355e49b2020-04-24 15:11:15 -06001799 return hazard;
1800}
1801
John Zulaufb02c1eb2020-10-06 16:33:36 -06001802void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001803 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001804 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001805 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001806 for (const auto &transition : transitions) {
1807 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001808 const auto &view_gen = attachment_views[transition.attachment];
1809 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001810
1811 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1812 assert(trackback);
1813
1814 // Import the attachments into the current context
1815 const auto *prev_context = trackback->context;
1816 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001817 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001818 auto &target_map = GetAccessStateMap(address_type);
1819 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001820 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1821 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001822 }
1823
John Zulauf86356ca2020-10-19 11:46:41 -06001824 // If there were no transitions skip this global map walk
1825 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001826 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001827 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001828 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001829}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001830
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001831void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulauf669dfd52021-01-27 17:15:28 -07001832 auto *events_context = GetCurrentEventsContext();
1833 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06001834 events_context->ApplyBarrier(src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001835}
1836
locke-lunarg61870c22020-06-09 14:51:50 -06001837bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1838 const char *func_name) const {
1839 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001840 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001841 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001842 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001843 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001844 return skip;
1845 }
1846
1847 using DescriptorClass = cvdescriptorset::DescriptorClass;
1848 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1849 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001850 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1851
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001852 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001853 const auto raster_state = pipe->RasterizationState();
1854 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001855 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001856 }
locke-lunarg61870c22020-06-09 14:51:50 -06001857 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001858 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001859 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001860 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001861 const auto descriptor_type = binding_it.GetType();
1862 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1863 auto array_idx = 0;
1864
1865 if (binding_it.IsVariableDescriptorCount()) {
1866 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1867 }
1868 SyncStageAccessIndex sync_index =
1869 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1870
1871 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1872 uint32_t index = i - index_range.start;
1873 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1874 switch (descriptor->GetClass()) {
1875 case DescriptorClass::ImageSampler:
1876 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001877 if (descriptor->Invalid()) {
1878 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001879 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07001880
1881 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
1882 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1883 const auto *img_view_state = image_descriptor->GetImageViewState();
1884 VkImageLayout image_layout = image_descriptor->GetImageLayout();
1885
John Zulauf361fb532020-07-22 10:45:39 -06001886 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001887 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1888 // Descriptors, so we do not have to worry about depth slicing here.
1889 // See: VUID 00343
1890 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001891 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001892 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001893
1894 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1895 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1896 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001897 // Input attachments are subject to raster ordering rules
1898 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001899 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001900 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001901 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001902 }
John Zulauf110413c2021-03-20 05:38:38 -06001903
John Zulauf33fc1d52020-07-17 11:01:10 -06001904 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001905 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001906 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001907 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1908 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001909 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001910 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1911 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1912 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001913 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1914 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001915 set_binding.first.binding, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001916 }
1917 break;
1918 }
1919 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07001920 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
1921 if (texel_descriptor->Invalid()) {
1922 continue;
1923 }
1924 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
1925 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001926 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001927 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001928 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001929 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001930 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001931 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1932 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001933 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1934 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1935 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001936 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001937 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001938 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001939 }
1940 break;
1941 }
1942 case DescriptorClass::GeneralBuffer: {
1943 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07001944 if (buffer_descriptor->Invalid()) {
1945 continue;
1946 }
1947 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06001948 const ResourceAccessRange range =
1949 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001950 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001951 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001952 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001953 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001954 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1955 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001956 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1957 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1958 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001959 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001960 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001961 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001962 }
1963 break;
1964 }
1965 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1966 default:
1967 break;
1968 }
1969 }
1970 }
1971 }
1972 return skip;
1973}
1974
1975void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
John Zulauf14940722021-04-12 15:19:02 -06001976 const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001977 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001978 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001979 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001980 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001981 return;
1982 }
1983
1984 using DescriptorClass = cvdescriptorset::DescriptorClass;
1985 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1986 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
locke-lunarg61870c22020-06-09 14:51:50 -06001987 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1988
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001989 for (const auto &stage_state : pipe->stage_state) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07001990 const auto raster_state = pipe->RasterizationState();
1991 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001992 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001993 }
locke-lunarg61870c22020-06-09 14:51:50 -06001994 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben4d51c552022-01-06 21:27:15 -07001995 const auto *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001996 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001997 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001998 const auto descriptor_type = binding_it.GetType();
1999 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2000 auto array_idx = 0;
2001
2002 if (binding_it.IsVariableDescriptorCount()) {
2003 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2004 }
2005 SyncStageAccessIndex sync_index =
2006 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2007
2008 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2009 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2010 switch (descriptor->GetClass()) {
2011 case DescriptorClass::ImageSampler:
2012 case DescriptorClass::Image: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002013 // NOTE: ImageSamplerDescriptor inherits from ImageDescriptor, so this cast works for both types.
2014 const auto *image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
2015 if (image_descriptor->Invalid()) {
2016 continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002017 }
Jeremy Gebbena08da232022-02-01 15:14:52 -07002018 const auto *img_view_state = image_descriptor->GetImageViewState();
Jeremy Gebben11a68a32021-07-29 11:59:22 -06002019 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
2020 // Descriptors, so we do not have to worry about depth slicing here.
2021 // See: VUID 00343
2022 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06002023 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002024 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06002025 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2026 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2027 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
2028 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002029 } else {
John Zulauf110413c2021-03-20 05:38:38 -06002030 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
2031 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002032 }
locke-lunarg61870c22020-06-09 14:51:50 -06002033 break;
2034 }
2035 case DescriptorClass::TexelBuffer: {
Jeremy Gebbena08da232022-02-01 15:14:52 -07002036 const auto *texel_descriptor = static_cast<const TexelDescriptor *>(descriptor);
2037 if (texel_descriptor->Invalid()) {
2038 continue;
2039 }
2040 const auto *buf_view_state = texel_descriptor->GetBufferViewState();
2041 const auto *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002042 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002043 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002044 break;
2045 }
2046 case DescriptorClass::GeneralBuffer: {
2047 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
Jeremy Gebbena08da232022-02-01 15:14:52 -07002048 if (buffer_descriptor->Invalid()) {
2049 continue;
2050 }
2051 const auto *buf_state = buffer_descriptor->GetBufferState();
John Zulauf3e86bf02020-09-12 10:47:57 -06002052 const ResourceAccessRange range =
2053 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002054 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002055 break;
2056 }
2057 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2058 default:
2059 break;
2060 }
2061 }
2062 }
2063 }
2064}
2065
2066bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2067 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002068 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002069 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002070 return skip;
2071 }
2072
2073 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2074 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002075 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002076
2077 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002078 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002079 if (binding_description.binding < binding_buffers_size) {
2080 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002081 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002082
locke-lunarg1ae57d62020-11-18 10:49:19 -07002083 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002084 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2085 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002086 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002087 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002088 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002089 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
2090 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2091 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002092 }
2093 }
2094 }
2095 return skip;
2096}
2097
John Zulauf14940722021-04-12 15:19:02 -06002098void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002099 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002100 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002101 return;
2102 }
2103 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2104 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002105 const auto &binding_descriptions_size = pipe->vertex_input_state->binding_descriptions.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002106
2107 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002108 const auto &binding_description = pipe->vertex_input_state->binding_descriptions[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002109 if (binding_description.binding < binding_buffers_size) {
2110 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002111 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002112
locke-lunarg1ae57d62020-11-18 10:49:19 -07002113 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002114 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2115 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002116 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2117 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002118 }
2119 }
2120}
2121
2122bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2123 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002124 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002125 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002126 }
locke-lunarg61870c22020-06-09 14:51:50 -06002127
locke-lunarg1ae57d62020-11-18 10:49:19 -07002128 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002129 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002130 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2131 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002132 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002133 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002134 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002135 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2136 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2137 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002138 }
2139
2140 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2141 // We will detect more accurate range in the future.
2142 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2143 return skip;
2144}
2145
John Zulauf14940722021-04-12 15:19:02 -06002146void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002147 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) return;
locke-lunarg61870c22020-06-09 14:51:50 -06002148
locke-lunarg1ae57d62020-11-18 10:49:19 -07002149 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002150 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002151 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2152 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002153 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002154
2155 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2156 // We will detect more accurate range in the future.
2157 RecordDrawVertex(UINT32_MAX, 0, tag);
2158}
2159
2160bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002161 bool skip = false;
2162 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002163 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002164 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002165}
2166
John Zulauf14940722021-04-12 15:19:02 -06002167void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002168 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002169 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002170 }
locke-lunarg61870c22020-06-09 14:51:50 -06002171}
2172
John Zulauf64ffe552021-02-06 10:25:07 -07002173void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2174 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06002175 const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002176 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002177 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002178 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002179 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002180 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002181}
2182
John Zulauf8eda1562021-04-13 17:06:41 -06002183void CommandBufferAccessContext::RecordNextSubpass(ResourceUsageTag prev_tag, ResourceUsageTag next_tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002184 assert(current_renderpass_context_);
John Zulauf64ffe552021-02-06 10:25:07 -07002185 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002186 current_context_ = &current_renderpass_context_->CurrentContext();
2187}
2188
John Zulauf8eda1562021-04-13 17:06:41 -06002189void CommandBufferAccessContext::RecordEndRenderPass(const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002190 assert(current_renderpass_context_);
2191 if (!current_renderpass_context_) return;
2192
John Zulauf8eda1562021-04-13 17:06:41 -06002193 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002194 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002195 current_renderpass_context_ = nullptr;
2196}
2197
John Zulauf4a6105a2020-11-17 15:11:05 -07002198void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2199 // Erase is okay with the key not being
Jeremy Gebbenf4449392022-01-28 10:09:10 -07002200 auto event_state = sync_state_->Get<EVENT_STATE>(event);
John Zulauf669dfd52021-01-27 17:15:28 -07002201 if (event_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06002202 GetCurrentEventsContext()->Destroy(event_state.get());
John Zulaufd5115702021-01-18 12:34:33 -07002203 }
2204}
2205
John Zulaufae842002021-04-15 18:20:55 -06002206// The is the recorded cb context
John Zulauf4fa68462021-04-26 21:04:22 -06002207bool CommandBufferAccessContext::ValidateFirstUse(CommandBufferAccessContext *proxy_context, const char *func_name,
2208 uint32_t index) const {
2209 assert(proxy_context);
2210 auto *events_context = proxy_context->GetCurrentEventsContext();
2211 auto *access_context = proxy_context->GetCurrentAccessContext();
2212 const ResourceUsageTag base_tag = proxy_context->GetTagLimit();
John Zulaufae842002021-04-15 18:20:55 -06002213 bool skip = false;
2214 ResourceUsageRange tag_range = {0, 0};
2215 const AccessContext *recorded_context = GetCurrentAccessContext();
2216 assert(recorded_context);
2217 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002218 auto log_msg = [this](const HazardResult &hazard, const CommandBufferAccessContext &active_context, const char *func_name,
John Zulaufae842002021-04-15 18:20:55 -06002219 uint32_t index) {
2220 const auto cb_handle = active_context.cb_state_->commandBuffer();
2221 const auto recorded_handle = cb_state_->commandBuffer();
John Zulauf4fa68462021-04-26 21:04:22 -06002222 const auto *report_data = sync_state_->report_data;
John Zulaufae842002021-04-15 18:20:55 -06002223 return sync_state_->LogError(cb_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf4fa68462021-04-26 21:04:22 -06002224 "%s: Hazard %s for entry %" PRIu32 ", %s, Recorded access info %s. Access info %s.", func_name,
2225 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(recorded_handle).c_str(),
2226 FormatUsage(*hazard.recorded_access).c_str(), active_context.FormatUsage(hazard).c_str());
John Zulaufae842002021-04-15 18:20:55 -06002227 };
2228 for (const auto &sync_op : sync_ops_) {
John Zulauf4fa68462021-04-26 21:04:22 -06002229 // we update the range to any include layout transition first use writes,
2230 // as they are stored along with the source scope (as effective barrier) when recorded
2231 tag_range.end = sync_op.tag + 1;
John Zulauf610e28c2021-08-03 17:46:23 -06002232 skip |= sync_op.sync_op->ReplayValidate(sync_op.tag, *this, base_tag, proxy_context);
John Zulauf4fa68462021-04-26 21:04:22 -06002233
John Zulaufae842002021-04-15 18:20:55 -06002234 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2235 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002236 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002237 }
2238 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
John Zulauf4fa68462021-04-26 21:04:22 -06002239 // Record the barrier into the proxy context.
2240 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2241 tag_range.begin = tag_range.end;
John Zulaufae842002021-04-15 18:20:55 -06002242 }
2243
2244 // and anything after the last syncop
John Zulaufae842002021-04-15 18:20:55 -06002245 tag_range.end = ResourceUsageRecord::kMaxIndex;
2246 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2247 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002248 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002249 }
2250
2251 return skip;
2252}
2253
John Zulauf4fa68462021-04-26 21:04:22 -06002254void CommandBufferAccessContext::RecordExecutedCommandBuffer(const CommandBufferAccessContext &recorded_cb_context, CMD_TYPE cmd) {
2255 auto *events_context = GetCurrentEventsContext();
2256 auto *access_context = GetCurrentAccessContext();
2257 const AccessContext *recorded_context = recorded_cb_context.GetCurrentAccessContext();
2258 assert(recorded_context);
2259
2260 // Just run through the barriers ignoring the usage from the recorded context, as Resolve will overwrite outdated state
2261 const ResourceUsageTag base_tag = GetTagLimit();
2262 for (const auto &sync_op : recorded_cb_context.sync_ops_) {
2263 // we update the range to any include layout transition first use writes,
2264 // as they are stored along with the source scope (as effective barrier) when recorded
2265 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2266 }
2267
2268 ResourceUsageRange tag_range = ImportRecordedAccessLog(recorded_cb_context);
2269 assert(base_tag == tag_range.begin); // to ensure the to offset calculation agree
2270 ResolveRecordedContext(*recorded_context, tag_range.begin);
2271}
2272
2273void CommandBufferAccessContext::ResolveRecordedContext(const AccessContext &recorded_context, ResourceUsageTag offset) {
2274 auto tag_offset = [offset](ResourceAccessState *access) { access->OffsetTag(offset); };
2275
2276 auto *access_context = GetCurrentAccessContext();
2277 for (auto address_type : kAddressTypes) {
2278 recorded_context.ResolveAccessRange(address_type, kFullRange, tag_offset, &access_context->GetAccessStateMap(address_type),
2279 nullptr, false);
2280 }
2281}
2282
2283ResourceUsageRange CommandBufferAccessContext::ImportRecordedAccessLog(const CommandBufferAccessContext &recorded_context) {
2284 // The execution references ensure lifespan for the referenced child CB's...
2285 ResourceUsageRange tag_range(GetTagLimit(), 0);
John Zulauf3c2a0b32021-07-14 11:14:52 -06002286 cbs_referenced_.emplace(recorded_context.cb_state_);
John Zulauf4fa68462021-04-26 21:04:22 -06002287 access_log_.insert(access_log_.end(), recorded_context.access_log_.cbegin(), recorded_context.access_log_.end());
2288 tag_range.end = access_log_.size();
2289 return tag_range;
2290}
2291
John Zulaufae842002021-04-15 18:20:55 -06002292class HazardDetectFirstUse {
2293 public:
2294 HazardDetectFirstUse(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range)
2295 : recorded_use_(recorded_use), tag_range_(tag_range) {}
2296 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
2297 return pos->second.DetectHazard(recorded_use_, tag_range_);
2298 }
2299 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
2300 return pos->second.DetectAsyncHazard(recorded_use_, tag_range_, start_tag);
2301 }
2302
2303 private:
2304 const ResourceAccessState &recorded_use_;
2305 const ResourceUsageRange &tag_range_;
2306};
2307
2308// This is called with the *recorded* command buffers access context, with the *active* access context pass in, againsts which
2309// hazards will be detected
2310HazardResult AccessContext::DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context) const {
2311 HazardResult hazard;
2312 for (const auto address_type : kAddressTypes) {
2313 const auto &recorded_access_map = GetAccessStateMap(address_type);
2314 for (const auto &recorded_access : recorded_access_map) {
2315 // Cull any entries not in the current tag range
2316 if (!recorded_access.second.FirstAccessInTagRange(tag_range)) continue;
2317 HazardDetectFirstUse detector(recorded_access.second, tag_range);
2318 hazard = access_context.DetectHazard(address_type, detector, recorded_access.first, DetectOptions::kDetectAll);
2319 if (hazard.hazard) break;
2320 }
2321 }
2322
2323 return hazard;
2324}
2325
John Zulauf64ffe552021-02-06 10:25:07 -07002326bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002327 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002328 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002329 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002330 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002331 if (!pipe) {
2332 return skip;
2333 }
2334
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002335 const auto raster_state = pipe->RasterizationState();
2336 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002337 return skip;
2338 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002339 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002340 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002341
John Zulauf1a224292020-06-30 14:52:13 -06002342 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002343 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002344 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2345 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002346 if (location >= subpass.colorAttachmentCount ||
2347 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002348 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002349 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002350 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2351 if (!view_gen.IsValid()) continue;
2352 HazardResult hazard =
2353 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2354 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002355 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002356 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002357 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002358 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002359 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002360 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002361 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002362 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002363 }
2364 }
2365 }
locke-lunarg37047832020-06-12 13:44:45 -06002366
2367 // PHASE1 TODO: Add layout based read/vs. write selection.
2368 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002369 const auto ds_state = pipe->DepthStencilState();
2370 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002371
2372 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2373 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2374 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002375 bool depth_write = false, stencil_write = false;
2376
2377 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002378 if (!FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable && ds_state->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002379 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2380 depth_write = true;
2381 }
2382 // PHASE1 TODO: It needs to check if stencil is writable.
2383 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2384 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2385 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002386 if (!FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002387 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2388 stencil_write = true;
2389 }
2390
2391 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2392 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002393 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2394 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2395 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002396 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002397 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002398 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002399 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002400 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002401 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2402 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002403 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002404 }
2405 }
2406 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002407 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2408 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2409 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002410 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002411 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002412 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002413 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002414 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002415 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2416 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002417 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002418 }
locke-lunarg61870c22020-06-09 14:51:50 -06002419 }
2420 }
2421 return skip;
2422}
2423
John Zulauf14940722021-04-12 15:19:02 -06002424void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002425 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002426 if (!pipe) {
2427 return;
2428 }
2429
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002430 const auto *raster_state = pipe->RasterizationState();
2431 if (raster_state && raster_state->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002432 return;
2433 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002434 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002435 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002436
John Zulauf1a224292020-06-30 14:52:13 -06002437 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002438 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002439 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2440 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002441 if (location >= subpass.colorAttachmentCount ||
2442 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002443 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002444 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002445 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2446 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2447 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2448 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002449 }
2450 }
locke-lunarg37047832020-06-12 13:44:45 -06002451
2452 // PHASE1 TODO: Add layout based read/vs. write selection.
2453 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002454 const auto *ds_state = pipe->DepthStencilState();
2455 const uint32_t depth_stencil_attachment = GetSubpassDepthStencilAttachmentIndex(ds_state, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002456 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2457 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2458 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002459 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002460 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2461 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002462
2463 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002464 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && ds_state->depthTestEnable &&
2465 ds_state->depthWriteEnable && IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
locke-lunarg37047832020-06-12 13:44:45 -06002466 depth_write = true;
2467 }
2468 // PHASE1 TODO: It needs to check if stencil is writable.
2469 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2470 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2471 // PHASE1 TODO: These validation should be in core_checks.
Nathaniel Cesario3fd4f762022-02-16 16:07:06 -07002472 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && ds_state->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002473 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2474 stencil_write = true;
2475 }
2476
John Zulaufd0ec59f2021-03-13 14:25:08 -07002477 if (depth_write || stencil_write) {
2478 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2479 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2480 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2481 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002482 }
locke-lunarg61870c22020-06-09 14:51:50 -06002483 }
2484}
2485
John Zulauf64ffe552021-02-06 10:25:07 -07002486bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002487 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002488 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002489 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002490 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002491 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002492 func_name);
2493
John Zulauf355e49b2020-04-24 15:11:15 -06002494 const auto next_subpass = current_subpass_ + 1;
ziga-lunarg31a3e772022-03-22 11:48:46 +01002495 if (next_subpass >= subpass_contexts_.size()) {
2496 return skip;
2497 }
John Zulauf1507ee42020-05-18 11:33:09 -06002498 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002499 skip |=
2500 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002501 if (!skip) {
2502 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2503 // on a copy of the (empty) next context.
2504 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2505 AccessContext temp_context(next_context);
2506 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002507 skip |=
2508 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002509 }
John Zulauf7635de32020-05-29 17:14:15 -06002510 return skip;
2511}
John Zulauf64ffe552021-02-06 10:25:07 -07002512bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002513 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002514 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002515 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002516 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002517 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2518
2519 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002520 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002521 return skip;
2522}
2523
John Zulauf64ffe552021-02-06 10:25:07 -07002524AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002525 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002526}
2527
John Zulauf64ffe552021-02-06 10:25:07 -07002528bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2529 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002530 bool skip = false;
2531
John Zulauf7635de32020-05-29 17:14:15 -06002532 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2533 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2534 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2535 // to apply and only copy then, if this proves a hot spot.
2536 std::unique_ptr<AccessContext> proxy_for_current;
2537
John Zulauf355e49b2020-04-24 15:11:15 -06002538 // Validate the "finalLayout" transitions to external
2539 // Get them from where there we're hidding in the extra entry.
2540 const auto &final_transitions = rp_state_->subpass_transitions.back();
2541 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002542 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002543 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2544 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002545 auto *context = trackback.context;
2546
2547 if (transition.prev_pass == current_subpass_) {
2548 if (!proxy_for_current) {
2549 // 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 -07002550 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002551 }
2552 context = proxy_for_current.get();
2553 }
2554
John Zulaufa0a98292020-09-18 09:30:10 -06002555 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2556 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002557 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002558 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002559 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002560 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002561 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2562 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2563 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2564 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002565 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002566 }
2567 }
2568 return skip;
2569}
2570
John Zulauf14940722021-04-12 15:19:02 -06002571void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002572 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002573 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002574}
2575
John Zulauf14940722021-04-12 15:19:02 -06002576void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002577 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2578 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002579
2580 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2581 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002582 const AttachmentViewGen &view_gen = attachment_views_[i];
2583 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002584
2585 const auto &ci = attachment_ci[i];
2586 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002587 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002588 const bool is_color = !(has_depth || has_stencil);
2589
2590 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002591 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2592 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2593 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2594 SyncOrdering::kColorAttachment, tag);
2595 }
John Zulauf1507ee42020-05-18 11:33:09 -06002596 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002597 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002598 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2599 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2600 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2601 SyncOrdering::kDepthStencilAttachment, tag);
2602 }
John Zulauf1507ee42020-05-18 11:33:09 -06002603 }
2604 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002605 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2606 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2607 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2608 SyncOrdering::kDepthStencilAttachment, tag);
2609 }
John Zulauf1507ee42020-05-18 11:33:09 -06002610 }
2611 }
2612 }
2613 }
2614}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002615AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2616 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2617 AttachmentViewGenVector view_gens;
2618 VkExtent3D extent = CastTo3D(render_area.extent);
2619 VkOffset3D offset = CastTo3D(render_area.offset);
2620 view_gens.reserve(attachment_views.size());
2621 for (const auto *view : attachment_views) {
2622 view_gens.emplace_back(view, offset, extent);
2623 }
2624 return view_gens;
2625}
John Zulauf64ffe552021-02-06 10:25:07 -07002626RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2627 VkQueueFlags queue_flags,
2628 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2629 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002630 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002631 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002632 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002633 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002634 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002635 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002636 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002637}
John Zulauf14940722021-04-12 15:19:02 -06002638void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002639 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002640 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002641 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002642 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002643}
John Zulauf1507ee42020-05-18 11:33:09 -06002644
John Zulauf14940722021-04-12 15:19:02 -06002645void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag prev_subpass_tag, const ResourceUsageTag next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002646 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002647 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2648 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002649
ziga-lunarg31a3e772022-03-22 11:48:46 +01002650 if (current_subpass_ + 1 >= subpass_contexts_.size()) {
2651 return;
2652 }
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002653 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2654 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002655 current_subpass_++;
John Zulauffaea0ee2021-01-14 14:01:32 -07002656 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2657 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002658 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002659}
2660
John Zulauf14940722021-04-12 15:19:02 -06002661void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002662 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002663 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2664 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002665
John Zulauf355e49b2020-04-24 15:11:15 -06002666 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002667 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002668
2669 // Add the "finalLayout" transitions to external
2670 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002671 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2672 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2673 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002674 const auto &final_transitions = rp_state_->subpass_transitions.back();
2675 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002676 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002677 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002678 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002679 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002680 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002681 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002682 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002683 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002684 }
2685}
2686
Jeremy Gebben40a22942020-12-22 14:22:06 -07002687SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002688 SyncExecScope result;
2689 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002690 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2691 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002692 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2693 return result;
2694}
2695
Jeremy Gebben40a22942020-12-22 14:22:06 -07002696SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002697 SyncExecScope result;
2698 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002699 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2700 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002701 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2702 return result;
2703}
2704
2705SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002706 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002707 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002708 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002709 dst_access_scope = 0;
2710}
2711
2712template <typename Barrier>
2713SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002714 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002715 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002716 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002717 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2718}
2719
2720SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002721 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2722 if (barrier) {
2723 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002724 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002725 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002726
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002727 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002728 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002729 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2730
2731 } else {
2732 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002733 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002734 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2735
2736 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002737 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002738 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2739 }
2740}
2741
2742template <typename Barrier>
2743SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2744 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2745 src_exec_scope = src.exec_scope;
2746 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2747
2748 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002749 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002750 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002751}
2752
John Zulaufb02c1eb2020-10-06 16:33:36 -06002753// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2754void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2755 for (const auto &barrier : barriers) {
2756 ApplyBarrier(barrier, layout_transition);
2757 }
2758}
2759
John Zulauf89311b42020-09-29 16:28:47 -06002760// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2761// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2762// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002763void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002764 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002765 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002766 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002767 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002768 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002769 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002770 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002771}
John Zulauf9cb530d2019-09-30 14:14:10 -06002772HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2773 HazardResult hazard;
2774 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002775 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002776 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002777 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002778 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002779 }
2780 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002781 // Write operation:
2782 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2783 // If reads exists -- test only against them because either:
2784 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2785 // * 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
2786 // the current write happens after the reads, so just test the write against the reades
2787 // Otherwise test against last_write
2788 //
2789 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002790 if (last_reads.size()) {
2791 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002792 if (IsReadHazard(usage_stage, read_access)) {
2793 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2794 break;
2795 }
2796 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002797 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002798 // Write-After-Write check -- if we have a previous write to test against
2799 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002800 }
2801 }
2802 return hazard;
2803}
2804
John Zulauf4fa68462021-04-26 21:04:22 -06002805HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002806 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002807 return DetectHazard(usage_index, ordering);
2808}
2809
2810HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002811 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2812 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002813 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002814 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002815 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2816 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002817 if (IsRead(usage_bit)) {
2818 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2819 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2820 if (is_raw_hazard) {
2821 // NOTE: we know last_write is non-zero
2822 // See if the ordering rules save us from the simple RAW check above
2823 // First check to see if the current usage is covered by the ordering rules
2824 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2825 const bool usage_is_ordered =
2826 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2827 if (usage_is_ordered) {
2828 // Now see of the most recent write (or a subsequent read) are ordered
2829 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2830 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002831 }
2832 }
John Zulauf4285ee92020-09-23 10:20:52 -06002833 if (is_raw_hazard) {
2834 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2835 }
John Zulauf5c628d02021-05-04 15:46:36 -06002836 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2837 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2838 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002839 } else {
2840 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002841 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002842 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002843 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002844 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002845 if (usage_write_is_ordered) {
2846 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2847 ordered_stages = GetOrderedStages(ordering);
2848 }
2849 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2850 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002851 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002852 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2853 if (IsReadHazard(usage_stage, read_access)) {
2854 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2855 break;
2856 }
John Zulaufd14743a2020-07-03 09:42:39 -06002857 }
2858 }
John Zulauf2a344ca2021-09-09 17:07:19 -06002859 } else if (last_write.any() && !(last_write_is_ordered && usage_write_is_ordered)) {
2860 bool ilt_ilt_hazard = false;
2861 if ((usage_index == SYNC_IMAGE_LAYOUT_TRANSITION) && (usage_bit == last_write)) {
2862 // ILT after ILT is a special case where we check the 2nd access scope of the first ILT against the first access
2863 // scope of the second ILT, which has been passed (smuggled?) in the ordering barrier
2864 ilt_ilt_hazard = !(write_barriers & ordering.access_scope).any();
2865 }
2866 if (ilt_ilt_hazard || IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002867 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002868 }
John Zulauf69133422020-05-20 14:55:53 -06002869 }
2870 }
2871 return hazard;
2872}
2873
John Zulaufae842002021-04-15 18:20:55 -06002874HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2875 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002876 using Size = FirstAccesses::size_type;
2877 const auto &recorded_accesses = recorded_use.first_accesses_;
2878 Size count = recorded_accesses.size();
2879 if (count) {
2880 const auto &last_access = recorded_accesses.back();
2881 bool do_write_last = IsWrite(last_access.usage_index);
2882 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06002883
John Zulauf4fa68462021-04-26 21:04:22 -06002884 for (Size i = 0; i < count; ++count) {
2885 const auto &first = recorded_accesses[i];
2886 // Skip and quit logic
2887 if (first.tag < tag_range.begin) continue;
2888 if (first.tag >= tag_range.end) {
2889 do_write_last = false; // ignore last since we know it can't be in tag_range
2890 break;
2891 }
2892
2893 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2894 if (hazard.hazard) {
2895 hazard.AddRecordedAccess(first);
2896 break;
2897 }
2898 }
2899
2900 if (do_write_last && tag_range.includes(last_access.tag)) {
2901 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
2902 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
2903 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2904 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
2905 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
2906 // the barrier that applies them
2907 barrier |= recorded_use.first_write_layout_ordering_;
2908 }
2909 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
2910 // the active context
2911 if (recorded_use.first_read_stages_) {
2912 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
2913 // reads in the active context are not "most recent" as all recorded context operations are *after* them
2914 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
2915 // active context.
2916 barrier.exec_scope |= recorded_use.first_read_stages_;
2917 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
2918 barrier.access_scope |= FlagBit(last_access.usage_index);
2919 }
2920 hazard = DetectHazard(last_access.usage_index, barrier);
2921 if (hazard.hazard) {
2922 hazard.AddRecordedAccess(last_access);
2923 }
2924 }
John Zulaufae842002021-04-15 18:20:55 -06002925 }
2926 return hazard;
2927}
2928
John Zulauf2f952d22020-02-10 11:34:51 -07002929// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002930HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002931 HazardResult hazard;
2932 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002933 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2934 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2935 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002936 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002937 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002938 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002939 }
2940 } else {
John Zulauf14940722021-04-12 15:19:02 -06002941 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002942 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002943 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002944 // 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 -07002945 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002946 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002947 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002948 break;
2949 }
2950 }
John Zulauf2f952d22020-02-10 11:34:51 -07002951 }
2952 }
2953 return hazard;
2954}
2955
John Zulaufae842002021-04-15 18:20:55 -06002956HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2957 ResourceUsageTag start_tag) const {
2958 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002959 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06002960 // Skip and quit logic
2961 if (first.tag < tag_range.begin) continue;
2962 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06002963
2964 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06002965 if (hazard.hazard) {
2966 hazard.AddRecordedAccess(first);
2967 break;
2968 }
John Zulaufae842002021-04-15 18:20:55 -06002969 }
2970 return hazard;
2971}
2972
Jeremy Gebben40a22942020-12-22 14:22:06 -07002973HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002974 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002975 // Only supporting image layout transitions for now
2976 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2977 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002978 // only test for WAW if there no intervening read operations.
2979 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002980 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002981 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002982 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002983 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002984 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002985 break;
2986 }
2987 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002988 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2989 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2990 }
2991
2992 return hazard;
2993}
2994
Jeremy Gebben40a22942020-12-22 14:22:06 -07002995HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002996 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06002997 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002998 // Only supporting image layout transitions for now
2999 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3000 HazardResult hazard;
3001 // only test for WAW if there no intervening read operations.
3002 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3003
John Zulaufab7756b2020-12-29 16:10:16 -07003004 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003005 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3006 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003007 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003008 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003009 // The read is in the events first synchronization scope, so we use a barrier hazard check
3010 // If the read stage is not in the src sync scope
3011 // *AND* not execution chained with an existing sync barrier (that's the or)
3012 // then the barrier access is unsafe (R/W after R)
3013 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3014 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3015 break;
3016 }
3017 } else {
3018 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3019 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3020 }
3021 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003022 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003023 // 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 -06003024 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003025 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3026 // So do a normal barrier hazard check
3027 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3028 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3029 }
3030 } else {
3031 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003032 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3033 }
John Zulaufd14743a2020-07-03 09:42:39 -06003034 }
John Zulauf361fb532020-07-22 10:45:39 -06003035
John Zulauf0cb5be22020-01-23 12:18:22 -07003036 return hazard;
3037}
3038
John Zulauf5f13a792020-03-10 07:31:21 -06003039// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3040// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3041// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3042void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003043 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003044 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3045 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003046 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003047 } else if (other.write_tag == write_tag) {
3048 // 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 -06003049 // dependency chaining logic or any stage expansion)
3050 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003051 pending_write_barriers |= other.pending_write_barriers;
3052 pending_layout_transition |= other.pending_layout_transition;
3053 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003054 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003055
John Zulaufd14743a2020-07-03 09:42:39 -06003056 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003057 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003058 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003059 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003060 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003061 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003062 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003063 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3064 // but we should wait on profiling data for that.
3065 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003066 auto &my_read = last_reads[my_read_index];
3067 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003068 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003069 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003070 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003071 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003072 my_read.pending_dep_chain = other_read.pending_dep_chain;
3073 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3074 // May require tracking more than one access per stage.
3075 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003076 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003077 // Since I'm overwriting the fragement stage read, also update the input attachment info
3078 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003079 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003080 }
John Zulauf14940722021-04-12 15:19:02 -06003081 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003082 // The read tags match so merge the barriers
3083 my_read.barriers |= other_read.barriers;
3084 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003085 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003086
John Zulauf5f13a792020-03-10 07:31:21 -06003087 break;
3088 }
3089 }
3090 } else {
3091 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003092 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003093 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003094 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003095 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003096 }
John Zulauf5f13a792020-03-10 07:31:21 -06003097 }
3098 }
John Zulauf361fb532020-07-22 10:45:39 -06003099 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003100 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3101 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003102
3103 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3104 // of the copy and other into this using the update first logic.
3105 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3106 // of the other first_accesses... )
3107 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3108 FirstAccesses firsts(std::move(first_accesses_));
3109 first_accesses_.clear();
3110 first_read_stages_ = 0U;
3111 auto a = firsts.begin();
3112 auto a_end = firsts.end();
3113 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003114 // TODO: Determine whether some tag offset will be needed for PHASE II
3115 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003116 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3117 ++a;
3118 }
3119 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3120 }
3121 for (; a != a_end; ++a) {
3122 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3123 }
3124 }
John Zulauf5f13a792020-03-10 07:31:21 -06003125}
3126
John Zulauf14940722021-04-12 15:19:02 -06003127void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003128 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3129 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003130 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003131 // Mulitple outstanding reads may be of interest and do dependency chains independently
3132 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3133 const auto usage_stage = PipelineStageBit(usage_index);
3134 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003135 for (auto &read_access : last_reads) {
3136 if (read_access.stage == usage_stage) {
3137 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003138 break;
3139 }
3140 }
3141 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003142 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003143 last_read_stages |= usage_stage;
3144 }
John Zulauf4285ee92020-09-23 10:20:52 -06003145
3146 // 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 -07003147 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003148 // TODO Revisit re: multiple reads for a given stage
3149 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003150 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003151 } else {
3152 // Assume write
3153 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003154 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003155 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003156 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003157}
John Zulauf5f13a792020-03-10 07:31:21 -06003158
John Zulauf89311b42020-09-29 16:28:47 -06003159// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3160// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3161// We can overwrite them as *this* write is now after them.
3162//
3163// 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 -06003164void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003165 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003166 last_read_stages = 0;
3167 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003168 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003169
3170 write_barriers = 0;
3171 write_dependency_chain = 0;
3172 write_tag = tag;
3173 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003174}
3175
John Zulauf89311b42020-09-29 16:28:47 -06003176// Apply the memory barrier without updating the existing barriers. The execution barrier
3177// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3178// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3179// replace the current write barriers or add to them, so accumulate to pending as well.
3180void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3181 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3182 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003183 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3184 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3185 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3186 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003187 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003188 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003189 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003190 if (layout_transition) {
3191 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3192 }
John Zulaufa0a98292020-09-18 09:30:10 -06003193 }
John Zulauf89311b42020-09-29 16:28:47 -06003194 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3195 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003196
John Zulauf89311b42020-09-29 16:28:47 -06003197 if (!pending_layout_transition) {
3198 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3199 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003200 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003201 // 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 -07003202 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3203 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003204 }
3205 }
John Zulaufa0a98292020-09-18 09:30:10 -06003206 }
John Zulaufa0a98292020-09-18 09:30:10 -06003207}
3208
John Zulauf4a6105a2020-11-17 15:11:05 -07003209// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3210// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003211void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003212 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3213 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3214 // in order to know if it's in the excecution scope
3215 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3216 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3217 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003218 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003219 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003220 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003221 if (layout_transition) {
3222 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3223 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003224 }
3225 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3226 pending_layout_transition |= layout_transition;
3227
3228 if (!pending_layout_transition) {
3229 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3230 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003231 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003232 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3233 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3234 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3235 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3236 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3237 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3238 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003239 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 -07003240 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003241 }
3242 }
3243 }
3244}
John Zulauf14940722021-04-12 15:19:02 -06003245void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003246 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003247 // 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 -06003248 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003249 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003250 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3251 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003252 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003253 }
John Zulauf89311b42020-09-29 16:28:47 -06003254
3255 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003256 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003257 for (auto &read_access : last_reads) {
3258 read_access.barriers |= read_access.pending_dep_chain;
3259 read_execution_barriers |= read_access.barriers;
3260 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003261 }
3262
3263 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3264 write_dependency_chain |= pending_write_dep_chain;
3265 write_barriers |= pending_write_barriers;
3266 pending_write_dep_chain = 0;
3267 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003268}
3269
John Zulaufae842002021-04-15 18:20:55 -06003270bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3271 if (!first_accesses_.size()) return false;
3272 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3273 return tag_range.intersects(first_access_range);
3274}
3275
John Zulauf59e25072020-07-17 10:55:21 -06003276// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003277VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3278 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003279
John Zulaufab7756b2020-12-29 16:10:16 -07003280 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003281 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003282 barriers = read_access.barriers;
3283 break;
John Zulauf59e25072020-07-17 10:55:21 -06003284 }
3285 }
John Zulauf4285ee92020-09-23 10:20:52 -06003286
John Zulauf59e25072020-07-17 10:55:21 -06003287 return barriers;
3288}
3289
Jeremy Gebben40a22942020-12-22 14:22:06 -07003290inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003291 assert(IsRead(usage));
3292 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3293 // * the previous reads are not hazards, and thus last_write must be visible and available to
3294 // any reads that happen after.
3295 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3296 // 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 -07003297 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003298}
3299
Jeremy Gebben40a22942020-12-22 14:22:06 -07003300VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003301 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003302 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003303 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003304 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003305 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003306 // 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 -07003307 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003308 }
3309
3310 return ordered_stages;
3311}
3312
John Zulauf14940722021-04-12 15:19:02 -06003313void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003314 // Only record until we record a write.
3315 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003316 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003317 if (0 == (usage_stage & first_read_stages_)) {
3318 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003319 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003320 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003321 if (0 == (read_execution_barriers & usage_stage)) {
3322 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3323 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3324 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003325 }
3326 }
3327}
3328
John Zulauf4fa68462021-04-26 21:04:22 -06003329void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3330 // Only call this after recording an image layout transition
3331 assert(first_accesses_.size());
3332 if (first_accesses_.back().tag == tag) {
3333 // 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 +02003334 assert(first_accesses_.back().usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
John Zulauf4fa68462021-04-26 21:04:22 -06003335 first_write_layout_ordering_ = layout_ordering;
3336 }
3337}
3338
John Zulaufd1f85d42020-04-15 12:23:15 -06003339void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003340 auto *access_context = GetAccessContextNoInsert(command_buffer);
3341 if (access_context) {
3342 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003343 }
3344}
3345
John Zulaufd1f85d42020-04-15 12:23:15 -06003346void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3347 auto access_found = cb_access_state.find(command_buffer);
3348 if (access_found != cb_access_state.end()) {
3349 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003350 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003351 cb_access_state.erase(access_found);
3352 }
3353}
3354
John Zulauf9cb530d2019-09-30 14:14:10 -06003355bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3356 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3357 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003358 const auto *cb_context = GetAccessContext(commandBuffer);
3359 assert(cb_context);
3360 if (!cb_context) return skip;
3361 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003362
John Zulauf3d84f1b2020-03-09 13:33:25 -06003363 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003364 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3365 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003366
3367 for (uint32_t region = 0; region < regionCount; region++) {
3368 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003369 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003370 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003371 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003372 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003373 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003374 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003375 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003376 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003377 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003378 }
John Zulauf16adfc92020-04-08 10:28:33 -06003379 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003380 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003381 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003382 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003383 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003384 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003385 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003386 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003387 }
3388 }
3389 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003390 }
3391 return skip;
3392}
3393
3394void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3395 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003396 auto *cb_context = GetAccessContext(commandBuffer);
3397 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003398 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003399 auto *context = cb_context->GetCurrentAccessContext();
3400
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003401 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3402 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003403
3404 for (uint32_t region = 0; region < regionCount; region++) {
3405 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003406 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003407 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003408 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003409 }
John Zulauf16adfc92020-04-08 10:28:33 -06003410 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003411 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003412 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003413 }
3414 }
3415}
3416
John Zulauf4a6105a2020-11-17 15:11:05 -07003417void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3418 // Clear out events from the command buffer contexts
3419 for (auto &cb_context : cb_access_state) {
3420 cb_context.second->RecordDestroyEvent(event);
3421 }
3422}
3423
Tony-LunarGef035472021-11-02 10:23:33 -06003424bool SyncValidator::ValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos,
3425 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003426 bool skip = false;
3427 const auto *cb_context = GetAccessContext(commandBuffer);
3428 assert(cb_context);
3429 if (!cb_context) return skip;
3430 const auto *context = cb_context->GetCurrentAccessContext();
Tony-LunarGef035472021-11-02 10:23:33 -06003431 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003432
3433 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003434 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3435 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003436
3437 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3438 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3439 if (src_buffer) {
3440 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003441 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003442 if (hazard.hazard) {
3443 // TODO -- add tag information to log msg when useful.
3444 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003445 "%s(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003446 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003447 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003448 }
3449 }
3450 if (dst_buffer && !skip) {
3451 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003452 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003453 if (hazard.hazard) {
3454 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003455 "%s(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003456 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003457 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003458 }
3459 }
3460 if (skip) break;
3461 }
3462 return skip;
3463}
3464
Tony-LunarGef035472021-11-02 10:23:33 -06003465bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3466 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3467 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3468}
3469
3470bool SyncValidator::PreCallValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) const {
3471 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3472}
3473
3474void SyncValidator::RecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003475 auto *cb_context = GetAccessContext(commandBuffer);
3476 assert(cb_context);
Tony-LunarGef035472021-11-02 10:23:33 -06003477 const auto tag = cb_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003478 auto *context = cb_context->GetCurrentAccessContext();
3479
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003480 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3481 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003482
3483 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3484 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3485 if (src_buffer) {
3486 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003487 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003488 }
3489 if (dst_buffer) {
3490 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003491 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003492 }
3493 }
3494}
3495
Tony-LunarGef035472021-11-02 10:23:33 -06003496void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3497 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3498}
3499
3500void SyncValidator::PreCallRecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) {
3501 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3502}
3503
John Zulauf5c5e88d2019-12-26 11:22:02 -07003504bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3505 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3506 const VkImageCopy *pRegions) const {
3507 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003508 const auto *cb_access_context = GetAccessContext(commandBuffer);
3509 assert(cb_access_context);
3510 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003511
John Zulauf3d84f1b2020-03-09 13:33:25 -06003512 const auto *context = cb_access_context->GetCurrentAccessContext();
3513 assert(context);
3514 if (!context) return skip;
3515
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003516 auto src_image = Get<IMAGE_STATE>(srcImage);
3517 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003518 for (uint32_t region = 0; region < regionCount; region++) {
3519 const auto &copy_region = pRegions[region];
3520 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003521 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003522 copy_region.srcOffset, copy_region.extent);
3523 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003524 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003525 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003526 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003527 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003528 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003529 }
3530
3531 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003532 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003533 copy_region.dstOffset, copy_region.extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003534 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003535 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003536 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003537 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003538 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003539 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003540 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003541 }
3542 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003543
John Zulauf5c5e88d2019-12-26 11:22:02 -07003544 return skip;
3545}
3546
3547void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3548 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3549 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003550 auto *cb_access_context = GetAccessContext(commandBuffer);
3551 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003552 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003553 auto *context = cb_access_context->GetCurrentAccessContext();
3554 assert(context);
3555
Jeremy Gebben9f537102021-10-05 16:37:12 -06003556 auto src_image = Get<IMAGE_STATE>(srcImage);
3557 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003558
3559 for (uint32_t region = 0; region < regionCount; region++) {
3560 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003561 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003562 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003563 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003564 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003565 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003566 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003567 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003568 }
3569 }
3570}
3571
Tony-LunarGb61514a2021-11-02 12:36:51 -06003572bool SyncValidator::ValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo,
3573 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003574 bool skip = false;
3575 const auto *cb_access_context = GetAccessContext(commandBuffer);
3576 assert(cb_access_context);
3577 if (!cb_access_context) return skip;
3578
3579 const auto *context = cb_access_context->GetCurrentAccessContext();
3580 assert(context);
3581 if (!context) return skip;
3582
Tony-LunarGb61514a2021-11-02 12:36:51 -06003583 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003584 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3585 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003586
Jeff Leger178b1e52020-10-05 12:22:23 -04003587 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3588 const auto &copy_region = pCopyImageInfo->pRegions[region];
3589 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003590 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003591 copy_region.srcOffset, copy_region.extent);
3592 if (hazard.hazard) {
3593 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003594 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003595 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003596 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003597 }
3598 }
3599
3600 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003601 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003602 copy_region.dstOffset, copy_region.extent);
Jeff Leger178b1e52020-10-05 12:22:23 -04003603 if (hazard.hazard) {
3604 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003605 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003606 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003607 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003608 }
3609 if (skip) break;
3610 }
3611 }
3612
3613 return skip;
3614}
3615
Tony-LunarGb61514a2021-11-02 12:36:51 -06003616bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3617 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3618 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3619}
3620
3621bool SyncValidator::PreCallValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) const {
3622 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3623}
3624
3625void SyncValidator::RecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003626 auto *cb_access_context = GetAccessContext(commandBuffer);
3627 assert(cb_access_context);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003628 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003629 auto *context = cb_access_context->GetCurrentAccessContext();
3630 assert(context);
3631
Jeremy Gebben9f537102021-10-05 16:37:12 -06003632 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3633 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04003634
3635 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3636 const auto &copy_region = pCopyImageInfo->pRegions[region];
3637 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003638 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003639 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003640 }
3641 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003642 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003643 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003644 }
3645 }
3646}
3647
Tony-LunarGb61514a2021-11-02 12:36:51 -06003648void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3649 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3650}
3651
3652void SyncValidator::PreCallRecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) {
3653 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3654}
3655
John Zulauf9cb530d2019-09-30 14:14:10 -06003656bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3657 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3658 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3659 uint32_t bufferMemoryBarrierCount,
3660 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3661 uint32_t imageMemoryBarrierCount,
3662 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3663 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003664 const auto *cb_access_context = GetAccessContext(commandBuffer);
3665 assert(cb_access_context);
3666 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003667
John Zulauf36ef9282021-02-02 11:47:24 -07003668 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3669 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3670 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3671 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003672 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003673 return skip;
3674}
3675
3676void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3677 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3678 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3679 uint32_t bufferMemoryBarrierCount,
3680 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3681 uint32_t imageMemoryBarrierCount,
3682 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003683 auto *cb_access_context = GetAccessContext(commandBuffer);
3684 assert(cb_access_context);
3685 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003686
John Zulauf1bf30522021-09-03 15:39:06 -06003687 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(),
3688 srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount,
3689 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3690 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06003691}
3692
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003693bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3694 const VkDependencyInfoKHR *pDependencyInfo) const {
3695 bool skip = false;
3696 const auto *cb_access_context = GetAccessContext(commandBuffer);
3697 assert(cb_access_context);
3698 if (!cb_access_context) return skip;
3699
3700 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3701 skip = pipeline_barrier.Validate(*cb_access_context);
3702 return skip;
3703}
3704
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003705bool SyncValidator::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
3706 const VkDependencyInfo *pDependencyInfo) const {
3707 bool skip = false;
3708 const auto *cb_access_context = GetAccessContext(commandBuffer);
3709 assert(cb_access_context);
3710 if (!cb_access_context) return skip;
3711
3712 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3713 skip = pipeline_barrier.Validate(*cb_access_context);
3714 return skip;
3715}
3716
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003717void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3718 auto *cb_access_context = GetAccessContext(commandBuffer);
3719 assert(cb_access_context);
3720 if (!cb_access_context) return;
3721
John Zulauf1bf30522021-09-03 15:39:06 -06003722 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(),
3723 *pDependencyInfo);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003724}
3725
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003726void SyncValidator::PreCallRecordCmdPipelineBarrier2(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) {
3727 auto *cb_access_context = GetAccessContext(commandBuffer);
3728 assert(cb_access_context);
3729 if (!cb_access_context) return;
3730
3731 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(),
3732 *pDependencyInfo);
3733}
3734
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003735void SyncValidator::CreateDevice(const VkDeviceCreateInfo *pCreateInfo) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003736 // The state tracker sets up the device state
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003737 StateTracker::CreateDevice(pCreateInfo);
John Zulauf9cb530d2019-09-30 14:14:10 -06003738
John Zulauf5f13a792020-03-10 07:31:21 -06003739 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3740 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003741 // TODO: Find a good way to do this hooklessly.
Jeremy Gebben36a3b832022-03-23 10:54:18 -06003742 SetCommandBufferResetCallback([this](VkCommandBuffer command_buffer) -> void { ResetCommandBufferCallback(command_buffer); });
3743 SetCommandBufferFreeCallback([this](VkCommandBuffer command_buffer) -> void { FreeCommandBufferCallback(command_buffer); });
John Zulauf9cb530d2019-09-30 14:14:10 -06003744}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003745
John Zulauf355e49b2020-04-24 15:11:15 -06003746bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003747 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003748 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003749 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003750 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003751 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003752 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003753 }
John Zulauf355e49b2020-04-24 15:11:15 -06003754 return skip;
3755}
3756
3757bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3758 VkSubpassContents contents) const {
3759 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003760 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003761 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003762 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003763 return skip;
3764}
3765
3766bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003767 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003768 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003769 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003770 return skip;
3771}
3772
3773bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3774 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003775 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003776 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003777 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003778 return skip;
3779}
3780
John Zulauf3d84f1b2020-03-09 13:33:25 -06003781void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3782 VkResult result) {
3783 // The state tracker sets up the command buffer state
3784 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3785
3786 // Create/initialize the structure that trackers accesses at the command buffer scope.
3787 auto cb_access_context = GetAccessContext(commandBuffer);
3788 assert(cb_access_context);
3789 cb_access_context->Reset();
3790}
3791
3792void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003793 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003794 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003795 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003796 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003797 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003798 }
3799}
3800
3801void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3802 VkSubpassContents contents) {
3803 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003804 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003805 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003806 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003807}
3808
3809void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3810 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3811 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003812 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003813}
3814
3815void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3816 const VkRenderPassBeginInfo *pRenderPassBegin,
3817 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3818 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003819 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003820}
3821
Mike Schuchardt2df08912020-12-15 16:28:09 -08003822bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003823 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003824 bool skip = false;
3825
3826 auto cb_context = GetAccessContext(commandBuffer);
3827 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003828 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003829 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003830 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003831}
3832
3833bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3834 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003835 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003836 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003837 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003838 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3839 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003840 return skip;
3841}
3842
Mike Schuchardt2df08912020-12-15 16:28:09 -08003843bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3844 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003845 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003846 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003847 return skip;
3848}
3849
3850bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3851 const VkSubpassEndInfo *pSubpassEndInfo) const {
3852 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003853 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003854 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003855}
3856
3857void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003858 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003859 auto cb_context = GetAccessContext(commandBuffer);
3860 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003861 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003862
sfricke-samsung85584a72021-09-30 21:43:38 -07003863 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003864 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003865}
3866
3867void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3868 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003869 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003870 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003871 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003872}
3873
3874void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3875 const VkSubpassEndInfo *pSubpassEndInfo) {
3876 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003877 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003878}
3879
3880void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3881 const VkSubpassEndInfo *pSubpassEndInfo) {
3882 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003883 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003884}
3885
sfricke-samsung85584a72021-09-30 21:43:38 -07003886bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3887 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003888 bool skip = false;
3889
3890 auto cb_context = GetAccessContext(commandBuffer);
3891 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003892 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003893
sfricke-samsung85584a72021-09-30 21:43:38 -07003894 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003895 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003896 return skip;
3897}
3898
3899bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3900 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003901 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003902 return skip;
3903}
3904
Mike Schuchardt2df08912020-12-15 16:28:09 -08003905bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003906 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003907 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003908 return skip;
3909}
3910
3911bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003912 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003913 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003914 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003915 return skip;
3916}
3917
sfricke-samsung85584a72021-09-30 21:43:38 -07003918void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003919 // Resolve the all subpass contexts to the command buffer contexts
3920 auto cb_context = GetAccessContext(commandBuffer);
3921 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003922 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003923
sfricke-samsung85584a72021-09-30 21:43:38 -07003924 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003925 sync_op.Record(cb_context);
3926 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003927}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003928
John Zulauf33fc1d52020-07-17 11:01:10 -06003929// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3930// updates to a resource which do not conflict at the byte level.
3931// TODO: Revisit this rule to see if it needs to be tighter or looser
3932// TODO: Add programatic control over suppression heuristics
3933bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3934 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3935}
3936
John Zulauf3d84f1b2020-03-09 13:33:25 -06003937void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003938 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003939 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003940}
3941
3942void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003943 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003944 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003945}
3946
3947void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003948 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003949 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003950}
locke-lunarga19c71d2020-03-02 18:17:04 -07003951
sfricke-samsung71f04e32022-03-16 01:21:21 -05003952template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04003953bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05003954 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
3955 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003956 bool skip = false;
3957 const auto *cb_access_context = GetAccessContext(commandBuffer);
3958 assert(cb_access_context);
3959 if (!cb_access_context) return skip;
3960
Tony Barbour845d29b2021-11-09 11:43:14 -07003961 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003962
locke-lunarga19c71d2020-03-02 18:17:04 -07003963 const auto *context = cb_access_context->GetCurrentAccessContext();
3964 assert(context);
3965 if (!context) return skip;
3966
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003967 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3968 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003969
3970 for (uint32_t region = 0; region < regionCount; region++) {
3971 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003972 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003973 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003974 if (src_buffer) {
3975 ResourceAccessRange src_range =
3976 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003977 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003978 if (hazard.hazard) {
3979 // PHASE1 TODO -- add tag information to log msg when useful.
3980 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3981 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3982 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003983 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003984 }
3985 }
3986
Jeremy Gebben40a22942020-12-22 14:22:06 -07003987 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003988 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003989 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003990 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003991 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003992 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003993 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003994 }
3995 if (skip) break;
3996 }
3997 if (skip) break;
3998 }
3999 return skip;
4000}
4001
Jeff Leger178b1e52020-10-05 12:22:23 -04004002bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4003 VkImageLayout dstImageLayout, uint32_t regionCount,
4004 const VkBufferImageCopy *pRegions) const {
4005 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
Tony Barbour845d29b2021-11-09 11:43:14 -07004006 CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004007}
4008
4009bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4010 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4011 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4012 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004013 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4014}
4015
4016bool SyncValidator::PreCallValidateCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4017 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) const {
4018 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4019 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4020 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004021}
4022
sfricke-samsung71f04e32022-03-16 01:21:21 -05004023template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004024void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004025 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4026 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004027 auto *cb_access_context = GetAccessContext(commandBuffer);
4028 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004029
Jeff Leger178b1e52020-10-05 12:22:23 -04004030 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004031 auto *context = cb_access_context->GetCurrentAccessContext();
4032 assert(context);
4033
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004034 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4035 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004036
4037 for (uint32_t region = 0; region < regionCount; region++) {
4038 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07004039 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004040 if (src_buffer) {
4041 ResourceAccessRange src_range =
4042 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004043 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004044 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07004045 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004046 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004047 }
4048 }
4049}
4050
Jeff Leger178b1e52020-10-05 12:22:23 -04004051void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4052 VkImageLayout dstImageLayout, uint32_t regionCount,
4053 const VkBufferImageCopy *pRegions) {
4054 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
Tony Barbour845d29b2021-11-09 11:43:14 -07004055 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004056}
4057
4058void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4059 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4060 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4061 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4062 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004063 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4064}
4065
4066void SyncValidator::PreCallRecordCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4067 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) {
4068 StateTracker::PreCallRecordCmdCopyBufferToImage2(commandBuffer, pCopyBufferToImageInfo);
4069 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4070 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4071 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004072}
4073
sfricke-samsung71f04e32022-03-16 01:21:21 -05004074template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004075bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004076 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
4077 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004078 bool skip = false;
4079 const auto *cb_access_context = GetAccessContext(commandBuffer);
4080 assert(cb_access_context);
4081 if (!cb_access_context) return skip;
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004082 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004083
locke-lunarga19c71d2020-03-02 18:17:04 -07004084 const auto *context = cb_access_context->GetCurrentAccessContext();
4085 assert(context);
4086 if (!context) return skip;
4087
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004088 auto src_image = Get<IMAGE_STATE>(srcImage);
4089 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004090 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004091 for (uint32_t region = 0; region < regionCount; region++) {
4092 const auto &copy_region = pRegions[region];
4093 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004094 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004095 copy_region.imageOffset, copy_region.imageExtent);
4096 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004097 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004098 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004099 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004100 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004101 }
John Zulauf477700e2021-01-06 11:41:49 -07004102 if (dst_mem) {
4103 ResourceAccessRange dst_range =
4104 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004105 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004106 if (hazard.hazard) {
4107 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4108 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4109 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004110 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004111 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004112 }
4113 }
4114 if (skip) break;
4115 }
4116 return skip;
4117}
4118
Jeff Leger178b1e52020-10-05 12:22:23 -04004119bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4120 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4121 const VkBufferImageCopy *pRegions) const {
4122 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004123 CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004124}
4125
4126bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4127 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4128 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4129 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004130 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4131}
4132
4133bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4134 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) const {
4135 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4136 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4137 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004138}
4139
sfricke-samsung71f04e32022-03-16 01:21:21 -05004140template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004141void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004142 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004143 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004144 auto *cb_access_context = GetAccessContext(commandBuffer);
4145 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004146
Jeff Leger178b1e52020-10-05 12:22:23 -04004147 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004148 auto *context = cb_access_context->GetCurrentAccessContext();
4149 assert(context);
4150
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004151 auto src_image = Get<IMAGE_STATE>(srcImage);
Jeremy Gebben9f537102021-10-05 16:37:12 -06004152 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004153 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004154 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004155
4156 for (uint32_t region = 0; region < regionCount; region++) {
4157 const auto &copy_region = pRegions[region];
4158 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004159 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004160 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004161 if (dst_buffer) {
4162 ResourceAccessRange dst_range =
4163 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004164 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004165 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004166 }
4167 }
4168}
4169
Jeff Leger178b1e52020-10-05 12:22:23 -04004170void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4171 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4172 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004173 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004174}
4175
4176void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4177 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4178 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4179 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4180 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004181 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4182}
4183
4184void SyncValidator::PreCallRecordCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4185 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) {
4186 StateTracker::PreCallRecordCmdCopyImageToBuffer2(commandBuffer, pCopyImageToBufferInfo);
4187 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4188 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4189 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004190}
4191
4192template <typename RegionType>
4193bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4194 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4195 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004196 bool skip = false;
4197 const auto *cb_access_context = GetAccessContext(commandBuffer);
4198 assert(cb_access_context);
4199 if (!cb_access_context) return skip;
4200
4201 const auto *context = cb_access_context->GetCurrentAccessContext();
4202 assert(context);
4203 if (!context) return skip;
4204
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004205 auto src_image = Get<IMAGE_STATE>(srcImage);
4206 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004207
4208 for (uint32_t region = 0; region < regionCount; region++) {
4209 const auto &blit_region = pRegions[region];
4210 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004211 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4212 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4213 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4214 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4215 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4216 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004217 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004218 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004219 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004220 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004221 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004222 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004223 }
4224 }
4225
4226 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004227 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4228 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4229 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4230 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4231 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4232 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004233 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004234 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004235 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004236 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004237 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004238 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004239 }
4240 if (skip) break;
4241 }
4242 }
4243
4244 return skip;
4245}
4246
Jeff Leger178b1e52020-10-05 12:22:23 -04004247bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4248 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4249 const VkImageBlit *pRegions, VkFilter filter) const {
4250 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4251 "vkCmdBlitImage");
4252}
4253
4254bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4255 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4256 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4257 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4258 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4259}
4260
Tony-LunarG542ae912021-11-04 16:06:44 -06004261bool SyncValidator::PreCallValidateCmdBlitImage2(VkCommandBuffer commandBuffer,
4262 const VkBlitImageInfo2 *pBlitImageInfo) const {
4263 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4264 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4265 pBlitImageInfo->filter, "vkCmdBlitImage2");
4266}
4267
Jeff Leger178b1e52020-10-05 12:22:23 -04004268template <typename RegionType>
4269void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4270 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4271 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004272 auto *cb_access_context = GetAccessContext(commandBuffer);
4273 assert(cb_access_context);
4274 auto *context = cb_access_context->GetCurrentAccessContext();
4275 assert(context);
4276
Jeremy Gebben9f537102021-10-05 16:37:12 -06004277 auto src_image = Get<IMAGE_STATE>(srcImage);
4278 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004279
4280 for (uint32_t region = 0; region < regionCount; region++) {
4281 const auto &blit_region = pRegions[region];
4282 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004283 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4284 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4285 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4286 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4287 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4288 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004289 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004290 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004291 }
4292 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004293 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4294 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4295 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4296 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4297 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4298 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004299 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004300 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004301 }
4302 }
4303}
locke-lunarg36ba2592020-04-03 09:42:04 -06004304
Jeff Leger178b1e52020-10-05 12:22:23 -04004305void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4306 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4307 const VkImageBlit *pRegions, VkFilter filter) {
4308 auto *cb_access_context = GetAccessContext(commandBuffer);
4309 assert(cb_access_context);
4310 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4311 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4312 pRegions, filter);
4313 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4314}
4315
4316void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4317 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4318 auto *cb_access_context = GetAccessContext(commandBuffer);
4319 assert(cb_access_context);
4320 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4321 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4322 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4323 pBlitImageInfo->filter, tag);
4324}
4325
Tony-LunarG542ae912021-11-04 16:06:44 -06004326void SyncValidator::PreCallRecordCmdBlitImage2(VkCommandBuffer commandBuffer, const VkBlitImageInfo2 *pBlitImageInfo) {
4327 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4328 auto *cb_access_context = GetAccessContext(commandBuffer);
4329 assert(cb_access_context);
4330 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2);
4331 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4332 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4333 pBlitImageInfo->filter, tag);
4334}
4335
John Zulauffaea0ee2021-01-14 14:01:32 -07004336bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4337 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4338 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4339 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004340 bool skip = false;
4341 if (drawCount == 0) return skip;
4342
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004343 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004344 VkDeviceSize size = struct_size;
4345 if (drawCount == 1 || stride == size) {
4346 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004347 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004348 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4349 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004350 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004351 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004352 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004353 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004354 }
4355 } else {
4356 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004357 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004358 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4359 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004360 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004361 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4362 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004363 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004364 break;
4365 }
4366 }
4367 }
4368 return skip;
4369}
4370
John Zulauf14940722021-04-12 15:19:02 -06004371void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004372 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4373 uint32_t stride) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004374 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004375 VkDeviceSize size = struct_size;
4376 if (drawCount == 1 || stride == size) {
4377 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004378 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004379 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004380 } else {
4381 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004382 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004383 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4384 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004385 }
4386 }
4387}
4388
John Zulauffaea0ee2021-01-14 14:01:32 -07004389bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4390 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4391 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004392 bool skip = false;
4393
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004394 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004395 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004396 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4397 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004398 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004399 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004400 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004401 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004402 }
4403 return skip;
4404}
4405
John Zulauf14940722021-04-12 15:19:02 -06004406void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004407 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004408 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004409 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004410}
4411
locke-lunarg36ba2592020-04-03 09:42:04 -06004412bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004413 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004414 const auto *cb_access_context = GetAccessContext(commandBuffer);
4415 assert(cb_access_context);
4416 if (!cb_access_context) return skip;
4417
locke-lunarg61870c22020-06-09 14:51:50 -06004418 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004419 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004420}
4421
4422void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004423 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004424 auto *cb_access_context = GetAccessContext(commandBuffer);
4425 assert(cb_access_context);
4426 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004427
locke-lunarg61870c22020-06-09 14:51:50 -06004428 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004429}
locke-lunarge1a67022020-04-29 00:15:36 -06004430
4431bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004432 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004433 const auto *cb_access_context = GetAccessContext(commandBuffer);
4434 assert(cb_access_context);
4435 if (!cb_access_context) return skip;
4436
4437 const auto *context = cb_access_context->GetCurrentAccessContext();
4438 assert(context);
4439 if (!context) return skip;
4440
locke-lunarg61870c22020-06-09 14:51:50 -06004441 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004442 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4443 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004444 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004445}
4446
4447void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004448 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004449 auto *cb_access_context = GetAccessContext(commandBuffer);
4450 assert(cb_access_context);
4451 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4452 auto *context = cb_access_context->GetCurrentAccessContext();
4453 assert(context);
4454
locke-lunarg61870c22020-06-09 14:51:50 -06004455 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4456 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004457}
4458
4459bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4460 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004461 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004462 const auto *cb_access_context = GetAccessContext(commandBuffer);
4463 assert(cb_access_context);
4464 if (!cb_access_context) return skip;
4465
locke-lunarg61870c22020-06-09 14:51:50 -06004466 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4467 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4468 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004469 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004470}
4471
4472void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4473 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004474 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004475 auto *cb_access_context = GetAccessContext(commandBuffer);
4476 assert(cb_access_context);
4477 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004478
locke-lunarg61870c22020-06-09 14:51:50 -06004479 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4480 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4481 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004482}
4483
4484bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4485 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004486 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004487 const auto *cb_access_context = GetAccessContext(commandBuffer);
4488 assert(cb_access_context);
4489 if (!cb_access_context) return skip;
4490
locke-lunarg61870c22020-06-09 14:51:50 -06004491 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4492 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4493 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004494 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004495}
4496
4497void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4498 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004499 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004500 auto *cb_access_context = GetAccessContext(commandBuffer);
4501 assert(cb_access_context);
4502 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004503
locke-lunarg61870c22020-06-09 14:51:50 -06004504 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4505 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4506 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004507}
4508
4509bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4510 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004511 bool skip = false;
4512 if (drawCount == 0) return skip;
4513
locke-lunargff255f92020-05-13 18:53:52 -06004514 const auto *cb_access_context = GetAccessContext(commandBuffer);
4515 assert(cb_access_context);
4516 if (!cb_access_context) return skip;
4517
4518 const auto *context = cb_access_context->GetCurrentAccessContext();
4519 assert(context);
4520 if (!context) return skip;
4521
locke-lunarg61870c22020-06-09 14:51:50 -06004522 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4523 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004524 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4525 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004526
4527 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4528 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4529 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004530 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004531 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004532}
4533
4534void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4535 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004536 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004537 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004538 auto *cb_access_context = GetAccessContext(commandBuffer);
4539 assert(cb_access_context);
4540 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4541 auto *context = cb_access_context->GetCurrentAccessContext();
4542 assert(context);
4543
locke-lunarg61870c22020-06-09 14:51:50 -06004544 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4545 cb_access_context->RecordDrawSubpassAttachment(tag);
4546 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004547
4548 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4549 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4550 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004551 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004552}
4553
4554bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4555 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004556 bool skip = false;
4557 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004558 const auto *cb_access_context = GetAccessContext(commandBuffer);
4559 assert(cb_access_context);
4560 if (!cb_access_context) return skip;
4561
4562 const auto *context = cb_access_context->GetCurrentAccessContext();
4563 assert(context);
4564 if (!context) return skip;
4565
locke-lunarg61870c22020-06-09 14:51:50 -06004566 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4567 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004568 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4569 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004570
4571 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4572 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4573 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004574 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004575 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004576}
4577
4578void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4579 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004580 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004581 auto *cb_access_context = GetAccessContext(commandBuffer);
4582 assert(cb_access_context);
4583 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4584 auto *context = cb_access_context->GetCurrentAccessContext();
4585 assert(context);
4586
locke-lunarg61870c22020-06-09 14:51:50 -06004587 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4588 cb_access_context->RecordDrawSubpassAttachment(tag);
4589 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004590
4591 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4592 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4593 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004594 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004595}
4596
4597bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4598 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4599 uint32_t stride, const char *function) const {
4600 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004601 const auto *cb_access_context = GetAccessContext(commandBuffer);
4602 assert(cb_access_context);
4603 if (!cb_access_context) return skip;
4604
4605 const auto *context = cb_access_context->GetCurrentAccessContext();
4606 assert(context);
4607 if (!context) return skip;
4608
locke-lunarg61870c22020-06-09 14:51:50 -06004609 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4610 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004611 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4612 maxDrawCount, stride, function);
4613 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004614
4615 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4616 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4617 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004618 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004619 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004620}
4621
4622bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4623 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4624 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004625 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4626 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004627}
4628
sfricke-samsung85584a72021-09-30 21:43:38 -07004629void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4630 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4631 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004632 auto *cb_access_context = GetAccessContext(commandBuffer);
4633 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004634 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004635 auto *context = cb_access_context->GetCurrentAccessContext();
4636 assert(context);
4637
locke-lunarg61870c22020-06-09 14:51:50 -06004638 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4639 cb_access_context->RecordDrawSubpassAttachment(tag);
4640 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4641 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004642
4643 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4644 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4645 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004646 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004647}
4648
sfricke-samsung85584a72021-09-30 21:43:38 -07004649void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4650 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4651 uint32_t stride) {
4652 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4653 stride);
4654 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4655 CMD_DRAWINDIRECTCOUNT);
4656}
locke-lunarge1a67022020-04-29 00:15:36 -06004657bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4658 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4659 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004660 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4661 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004662}
4663
4664void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4665 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4666 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004667 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4668 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004669 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4670 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004671}
4672
4673bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4674 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4675 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004676 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4677 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004678}
4679
4680void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4681 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4682 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004683 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4684 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004685 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4686 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004687}
4688
4689bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(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(VkDrawIndexedIndirectCommand), buffer,
4704 offset, 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 index and vertex buffer. It might cause some false positive.
4708 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4709 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004710 skip |= cb_access_context->ValidateDrawVertexIndex(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::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4715 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4716 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004717 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4718 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004719}
4720
sfricke-samsung85584a72021-09-30 21:43:38 -07004721void SyncValidator::RecordCmdDrawIndexedIndirectCount(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(VkDrawIndexedIndirectCommand), 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 index and vertex buffer. It might cause some false positive.
4736 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004737 // We will update the index and vertex buffer in SubmitQueue in the future.
4738 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004739}
4740
sfricke-samsung85584a72021-09-30 21:43:38 -07004741void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4742 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4743 uint32_t maxDrawCount, uint32_t stride) {
4744 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4745 maxDrawCount, stride);
4746 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4747 CMD_DRAWINDEXEDINDIRECTCOUNT);
4748}
4749
locke-lunarge1a67022020-04-29 00:15:36 -06004750bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4751 VkDeviceSize offset, VkBuffer countBuffer,
4752 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4753 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004754 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4755 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004756}
4757
4758void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4759 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4760 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004761 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4762 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004763 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4764 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004765}
4766
4767bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4768 VkDeviceSize offset, VkBuffer countBuffer,
4769 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4770 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004771 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4772 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004773}
4774
4775void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4776 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4777 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004778 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4779 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004780 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4781 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004782}
4783
4784bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4785 const VkClearColorValue *pColor, uint32_t rangeCount,
4786 const VkImageSubresourceRange *pRanges) const {
4787 bool skip = false;
4788 const auto *cb_access_context = GetAccessContext(commandBuffer);
4789 assert(cb_access_context);
4790 if (!cb_access_context) return skip;
4791
4792 const auto *context = cb_access_context->GetCurrentAccessContext();
4793 assert(context);
4794 if (!context) return skip;
4795
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004796 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004797
4798 for (uint32_t index = 0; index < rangeCount; index++) {
4799 const auto &range = pRanges[index];
4800 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004801 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004802 if (hazard.hazard) {
4803 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004804 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004805 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004806 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004807 }
4808 }
4809 }
4810 return skip;
4811}
4812
4813void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4814 const VkClearColorValue *pColor, uint32_t rangeCount,
4815 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004816 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004817 auto *cb_access_context = GetAccessContext(commandBuffer);
4818 assert(cb_access_context);
4819 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4820 auto *context = cb_access_context->GetCurrentAccessContext();
4821 assert(context);
4822
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004823 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004824
4825 for (uint32_t index = 0; index < rangeCount; index++) {
4826 const auto &range = pRanges[index];
4827 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004828 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004829 }
4830 }
4831}
4832
4833bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4834 VkImageLayout imageLayout,
4835 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4836 const VkImageSubresourceRange *pRanges) const {
4837 bool skip = false;
4838 const auto *cb_access_context = GetAccessContext(commandBuffer);
4839 assert(cb_access_context);
4840 if (!cb_access_context) return skip;
4841
4842 const auto *context = cb_access_context->GetCurrentAccessContext();
4843 assert(context);
4844 if (!context) return skip;
4845
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004846 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004847
4848 for (uint32_t index = 0; index < rangeCount; index++) {
4849 const auto &range = pRanges[index];
4850 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004851 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004852 if (hazard.hazard) {
4853 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004854 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004855 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004856 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004857 }
4858 }
4859 }
4860 return skip;
4861}
4862
4863void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4864 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4865 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004866 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004867 auto *cb_access_context = GetAccessContext(commandBuffer);
4868 assert(cb_access_context);
4869 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4870 auto *context = cb_access_context->GetCurrentAccessContext();
4871 assert(context);
4872
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004873 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004874
4875 for (uint32_t index = 0; index < rangeCount; index++) {
4876 const auto &range = pRanges[index];
4877 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004878 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004879 }
4880 }
4881}
4882
4883bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4884 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4885 VkDeviceSize dstOffset, VkDeviceSize stride,
4886 VkQueryResultFlags flags) const {
4887 bool skip = false;
4888 const auto *cb_access_context = GetAccessContext(commandBuffer);
4889 assert(cb_access_context);
4890 if (!cb_access_context) return skip;
4891
4892 const auto *context = cb_access_context->GetCurrentAccessContext();
4893 assert(context);
4894 if (!context) return skip;
4895
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004896 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004897
4898 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004899 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004900 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004901 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004902 skip |=
4903 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4904 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004905 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004906 }
4907 }
locke-lunargff255f92020-05-13 18:53:52 -06004908
4909 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004910 return skip;
4911}
4912
4913void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4914 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4915 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004916 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4917 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004918 auto *cb_access_context = GetAccessContext(commandBuffer);
4919 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004920 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004921 auto *context = cb_access_context->GetCurrentAccessContext();
4922 assert(context);
4923
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004924 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004925
4926 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004927 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004928 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004929 }
locke-lunargff255f92020-05-13 18:53:52 -06004930
4931 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004932}
4933
4934bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4935 VkDeviceSize size, uint32_t data) const {
4936 bool skip = false;
4937 const auto *cb_access_context = GetAccessContext(commandBuffer);
4938 assert(cb_access_context);
4939 if (!cb_access_context) return skip;
4940
4941 const auto *context = cb_access_context->GetCurrentAccessContext();
4942 assert(context);
4943 if (!context) return skip;
4944
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004945 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004946
4947 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004948 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004949 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004950 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004951 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004952 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004953 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004954 }
4955 }
4956 return skip;
4957}
4958
4959void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4960 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004961 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004962 auto *cb_access_context = GetAccessContext(commandBuffer);
4963 assert(cb_access_context);
4964 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4965 auto *context = cb_access_context->GetCurrentAccessContext();
4966 assert(context);
4967
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004968 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004969
4970 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004971 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004972 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004973 }
4974}
4975
4976bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4977 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4978 const VkImageResolve *pRegions) 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 src_image = Get<IMAGE_STATE>(srcImage);
4989 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06004990
4991 for (uint32_t region = 0; region < regionCount; region++) {
4992 const auto &resolve_region = pRegions[region];
4993 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004994 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004995 resolve_region.srcOffset, resolve_region.extent);
4996 if (hazard.hazard) {
4997 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004998 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004999 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005000 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005001 }
5002 }
5003
5004 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005005 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005006 resolve_region.dstOffset, resolve_region.extent);
5007 if (hazard.hazard) {
5008 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005009 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005010 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005011 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005012 }
5013 if (skip) break;
5014 }
5015 }
5016
5017 return skip;
5018}
5019
5020void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5021 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5022 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005023 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5024 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005025 auto *cb_access_context = GetAccessContext(commandBuffer);
5026 assert(cb_access_context);
5027 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5028 auto *context = cb_access_context->GetCurrentAccessContext();
5029 assert(context);
5030
Jeremy Gebben9f537102021-10-05 16:37:12 -06005031 auto src_image = Get<IMAGE_STATE>(srcImage);
5032 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005033
5034 for (uint32_t region = 0; region < regionCount; region++) {
5035 const auto &resolve_region = pRegions[region];
5036 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005037 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005038 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005039 }
5040 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005041 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005042 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005043 }
5044 }
5045}
5046
Tony-LunarG562fc102021-11-12 13:58:35 -07005047bool SyncValidator::ValidateCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5048 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04005049 bool skip = false;
5050 const auto *cb_access_context = GetAccessContext(commandBuffer);
5051 assert(cb_access_context);
5052 if (!cb_access_context) return skip;
5053
5054 const auto *context = cb_access_context->GetCurrentAccessContext();
5055 assert(context);
5056 if (!context) return skip;
5057
Tony-LunarG562fc102021-11-12 13:58:35 -07005058 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005059 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5060 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005061
5062 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5063 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5064 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005065 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005066 resolve_region.srcOffset, resolve_region.extent);
5067 if (hazard.hazard) {
5068 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005069 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005070 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005071 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005072 }
5073 }
5074
5075 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005076 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005077 resolve_region.dstOffset, resolve_region.extent);
5078 if (hazard.hazard) {
5079 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005080 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005081 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005082 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005083 }
5084 if (skip) break;
5085 }
5086 }
5087
5088 return skip;
5089}
5090
Tony-LunarG562fc102021-11-12 13:58:35 -07005091bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5092 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5093 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5094}
5095
5096bool SyncValidator::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
5097 const VkResolveImageInfo2 *pResolveImageInfo) const {
5098 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5099}
5100
5101void SyncValidator::RecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5102 CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04005103 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5104 auto *cb_access_context = GetAccessContext(commandBuffer);
5105 assert(cb_access_context);
Tony-LunarG562fc102021-11-12 13:58:35 -07005106 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04005107 auto *context = cb_access_context->GetCurrentAccessContext();
5108 assert(context);
5109
Jeremy Gebben9f537102021-10-05 16:37:12 -06005110 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5111 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005112
5113 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5114 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5115 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005116 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005117 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005118 }
5119 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005120 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005121 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005122 }
5123 }
5124}
5125
Tony-LunarG562fc102021-11-12 13:58:35 -07005126void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5127 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5128 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5129}
5130
5131void SyncValidator::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2 *pResolveImageInfo) {
5132 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5133}
5134
locke-lunarge1a67022020-04-29 00:15:36 -06005135bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5136 VkDeviceSize dataSize, const void *pData) const {
5137 bool skip = false;
5138 const auto *cb_access_context = GetAccessContext(commandBuffer);
5139 assert(cb_access_context);
5140 if (!cb_access_context) return skip;
5141
5142 const auto *context = cb_access_context->GetCurrentAccessContext();
5143 assert(context);
5144 if (!context) return skip;
5145
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005146 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005147
5148 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005149 // VK_WHOLE_SIZE not allowed
5150 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005151 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005152 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005153 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005154 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005155 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005156 }
5157 }
5158 return skip;
5159}
5160
5161void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5162 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005163 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005164 auto *cb_access_context = GetAccessContext(commandBuffer);
5165 assert(cb_access_context);
5166 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5167 auto *context = cb_access_context->GetCurrentAccessContext();
5168 assert(context);
5169
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005170 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005171
5172 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005173 // VK_WHOLE_SIZE not allowed
5174 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005175 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005176 }
5177}
locke-lunargff255f92020-05-13 18:53:52 -06005178
5179bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5180 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5181 bool skip = false;
5182 const auto *cb_access_context = GetAccessContext(commandBuffer);
5183 assert(cb_access_context);
5184 if (!cb_access_context) return skip;
5185
5186 const auto *context = cb_access_context->GetCurrentAccessContext();
5187 assert(context);
5188 if (!context) return skip;
5189
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005190 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005191
5192 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005193 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005194 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005195 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005196 skip |=
5197 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5198 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005199 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005200 }
5201 }
5202 return skip;
5203}
5204
5205void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5206 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005207 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005208 auto *cb_access_context = GetAccessContext(commandBuffer);
5209 assert(cb_access_context);
5210 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5211 auto *context = cb_access_context->GetCurrentAccessContext();
5212 assert(context);
5213
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005214 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005215
5216 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005217 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005218 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005219 }
5220}
John Zulauf49beb112020-11-04 16:06:31 -07005221
5222bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5223 bool skip = false;
5224 const auto *cb_context = GetAccessContext(commandBuffer);
5225 assert(cb_context);
5226 if (!cb_context) return skip;
5227
John Zulauf36ef9282021-02-02 11:47:24 -07005228 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005229 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005230}
5231
5232void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5233 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5234 auto *cb_context = GetAccessContext(commandBuffer);
5235 assert(cb_context);
5236 if (!cb_context) return;
John Zulauf1bf30522021-09-03 15:39:06 -06005237 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005238}
5239
John Zulauf4edde622021-02-15 08:54:50 -07005240bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5241 const VkDependencyInfoKHR *pDependencyInfo) const {
5242 bool skip = false;
5243 const auto *cb_context = GetAccessContext(commandBuffer);
5244 assert(cb_context);
5245 if (!cb_context || !pDependencyInfo) return skip;
5246
5247 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5248 return set_event_op.Validate(*cb_context);
5249}
5250
Tony-LunarGc43525f2021-11-15 16:12:38 -07005251bool SyncValidator::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5252 const VkDependencyInfo *pDependencyInfo) const {
5253 bool skip = false;
5254 const auto *cb_context = GetAccessContext(commandBuffer);
5255 assert(cb_context);
5256 if (!cb_context || !pDependencyInfo) return skip;
5257
5258 SyncOpSetEvent set_event_op(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5259 return set_event_op.Validate(*cb_context);
5260}
5261
John Zulauf4edde622021-02-15 08:54:50 -07005262void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5263 const VkDependencyInfoKHR *pDependencyInfo) {
5264 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5265 auto *cb_context = GetAccessContext(commandBuffer);
5266 assert(cb_context);
5267 if (!cb_context || !pDependencyInfo) return;
5268
John Zulauf1bf30522021-09-03 15:39:06 -06005269 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
John Zulauf4edde622021-02-15 08:54:50 -07005270}
5271
Tony-LunarGc43525f2021-11-15 16:12:38 -07005272void SyncValidator::PostCallRecordCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5273 const VkDependencyInfo *pDependencyInfo) {
5274 StateTracker::PostCallRecordCmdSetEvent2(commandBuffer, event, pDependencyInfo);
5275 auto *cb_context = GetAccessContext(commandBuffer);
5276 assert(cb_context);
5277 if (!cb_context || !pDependencyInfo) return;
5278
5279 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5280}
5281
John Zulauf49beb112020-11-04 16:06:31 -07005282bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5283 VkPipelineStageFlags stageMask) const {
5284 bool skip = false;
5285 const auto *cb_context = GetAccessContext(commandBuffer);
5286 assert(cb_context);
5287 if (!cb_context) return skip;
5288
John Zulauf36ef9282021-02-02 11:47:24 -07005289 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005290 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005291}
5292
5293void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5294 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5295 auto *cb_context = GetAccessContext(commandBuffer);
5296 assert(cb_context);
5297 if (!cb_context) return;
5298
John Zulauf1bf30522021-09-03 15:39:06 -06005299 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005300}
5301
John Zulauf4edde622021-02-15 08:54:50 -07005302bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5303 VkPipelineStageFlags2KHR stageMask) const {
5304 bool skip = false;
5305 const auto *cb_context = GetAccessContext(commandBuffer);
5306 assert(cb_context);
5307 if (!cb_context) return skip;
5308
5309 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5310 return reset_event_op.Validate(*cb_context);
5311}
5312
Tony-LunarGa2662db2021-11-16 07:26:24 -07005313bool SyncValidator::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5314 VkPipelineStageFlags2 stageMask) const {
5315 bool skip = false;
5316 const auto *cb_context = GetAccessContext(commandBuffer);
5317 assert(cb_context);
5318 if (!cb_context) return skip;
5319
5320 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5321 return reset_event_op.Validate(*cb_context);
5322}
5323
John Zulauf4edde622021-02-15 08:54:50 -07005324void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5325 VkPipelineStageFlags2KHR stageMask) {
5326 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5327 auto *cb_context = GetAccessContext(commandBuffer);
5328 assert(cb_context);
5329 if (!cb_context) return;
5330
John Zulauf1bf30522021-09-03 15:39:06 -06005331 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4edde622021-02-15 08:54:50 -07005332}
5333
Tony-LunarGa2662db2021-11-16 07:26:24 -07005334void SyncValidator::PostCallRecordCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags2 stageMask) {
5335 StateTracker::PostCallRecordCmdResetEvent2(commandBuffer, event, stageMask);
5336 auto *cb_context = GetAccessContext(commandBuffer);
5337 assert(cb_context);
5338 if (!cb_context) return;
5339
5340 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5341}
5342
John Zulauf49beb112020-11-04 16:06:31 -07005343bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5344 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5345 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5346 uint32_t bufferMemoryBarrierCount,
5347 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5348 uint32_t imageMemoryBarrierCount,
5349 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5350 bool skip = false;
5351 const auto *cb_context = GetAccessContext(commandBuffer);
5352 assert(cb_context);
5353 if (!cb_context) return skip;
5354
John Zulauf36ef9282021-02-02 11:47:24 -07005355 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5356 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5357 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005358 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005359}
5360
5361void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5362 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5363 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5364 uint32_t bufferMemoryBarrierCount,
5365 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5366 uint32_t imageMemoryBarrierCount,
5367 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5368 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5369 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5370 imageMemoryBarrierCount, pImageMemoryBarriers);
5371
5372 auto *cb_context = GetAccessContext(commandBuffer);
5373 assert(cb_context);
5374 if (!cb_context) return;
5375
John Zulauf1bf30522021-09-03 15:39:06 -06005376 cb_context->RecordSyncOp<SyncOpWaitEvents>(
John Zulauf610e28c2021-08-03 17:46:23 -06005377 CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
John Zulauf1bf30522021-09-03 15:39:06 -06005378 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf4a6105a2020-11-17 15:11:05 -07005379}
5380
John Zulauf4edde622021-02-15 08:54:50 -07005381bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5382 const VkDependencyInfoKHR *pDependencyInfos) const {
5383 bool skip = false;
5384 const auto *cb_context = GetAccessContext(commandBuffer);
5385 assert(cb_context);
5386 if (!cb_context) return skip;
5387
5388 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5389 skip |= wait_events_op.Validate(*cb_context);
5390 return skip;
5391}
5392
5393void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5394 const VkDependencyInfoKHR *pDependencyInfos) {
5395 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5396
5397 auto *cb_context = GetAccessContext(commandBuffer);
5398 assert(cb_context);
5399 if (!cb_context) return;
5400
John Zulauf1bf30522021-09-03 15:39:06 -06005401 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5402 pDependencyInfos);
John Zulauf4edde622021-02-15 08:54:50 -07005403}
5404
Tony-LunarG1364cf52021-11-17 16:10:11 -07005405bool SyncValidator::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5406 const VkDependencyInfo *pDependencyInfos) const {
5407 bool skip = false;
5408 const auto *cb_context = GetAccessContext(commandBuffer);
5409 assert(cb_context);
5410 if (!cb_context) return skip;
5411
5412 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5413 skip |= wait_events_op.Validate(*cb_context);
5414 return skip;
5415}
5416
5417void SyncValidator::PostCallRecordCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5418 const VkDependencyInfo *pDependencyInfos) {
5419 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5420
5421 auto *cb_context = GetAccessContext(commandBuffer);
5422 assert(cb_context);
5423 if (!cb_context) return;
5424
5425 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5426 pDependencyInfos);
5427}
5428
John Zulauf4a6105a2020-11-17 15:11:05 -07005429void SyncEventState::ResetFirstScope() {
5430 for (const auto address_type : kAddressTypes) {
5431 first_scope[static_cast<size_t>(address_type)].clear();
5432 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005433 scope = SyncExecScope();
John Zulauf78b1f892021-09-20 15:02:09 -06005434 first_scope_set = false;
5435 first_scope_tag = 0;
John Zulauf4a6105a2020-11-17 15:11:05 -07005436}
5437
5438// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005439SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005440 IgnoreReason reason = NotIgnored;
5441
Tony-LunarG1364cf52021-11-17 16:10:11 -07005442 if ((CMD_WAITEVENTS2KHR == cmd || CMD_WAITEVENTS2 == cmd) && (CMD_SETEVENT == last_command)) {
John Zulauf4edde622021-02-15 08:54:50 -07005443 reason = SetVsWait2;
5444 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5445 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005446 } else if (unsynchronized_set) {
5447 reason = SetRace;
John Zulauf78b1f892021-09-20 15:02:09 -06005448 } else if (first_scope_set) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005449 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005450 if (missing_bits) reason = MissingStageBits;
5451 }
5452
5453 return reason;
5454}
5455
Jeremy Gebben40a22942020-12-22 14:22:06 -07005456bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005457 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5458 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5459 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005460}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005461
John Zulauf36ef9282021-02-02 11:47:24 -07005462SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5463 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5464 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005465 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5466 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5467 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005468 : SyncOpBase(cmd), barriers_(1) {
5469 auto &barrier_set = barriers_[0];
5470 barrier_set.dependency_flags = dependencyFlags;
5471 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5472 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005473 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005474 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5475 pMemoryBarriers);
5476 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5477 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5478 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5479 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005480}
5481
John Zulauf4edde622021-02-15 08:54:50 -07005482SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5483 const VkDependencyInfoKHR *dep_infos)
5484 : SyncOpBase(cmd), barriers_(event_count) {
5485 for (uint32_t i = 0; i < event_count; i++) {
5486 const auto &dep_info = dep_infos[i];
5487 auto &barrier_set = barriers_[i];
5488 barrier_set.dependency_flags = dep_info.dependencyFlags;
5489 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5490 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5491 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5492 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5493 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5494 dep_info.pMemoryBarriers);
5495 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5496 dep_info.pBufferMemoryBarriers);
5497 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5498 dep_info.pImageMemoryBarriers);
5499 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005500}
5501
John Zulauf36ef9282021-02-02 11:47:24 -07005502SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005503 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5504 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5505 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5506 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5507 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005508 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005509 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5510
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005511SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5512 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005513 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005514
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005515bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5516 bool skip = false;
5517 const auto *context = cb_context.GetCurrentAccessContext();
5518 assert(context);
5519 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005520 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5521
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005522 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005523 const auto &barrier_set = barriers_[0];
5524 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5525 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5526 const auto *image_state = image_barrier.image.get();
5527 if (!image_state) continue;
5528 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5529 if (hazard.hazard) {
5530 // PHASE1 TODO -- add tag information to log msg when useful.
5531 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005532 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005533 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5534 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5535 string_SyncHazard(hazard.hazard), image_barrier.index,
5536 sync_state.report_data->FormatHandle(image_handle).c_str(),
5537 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005538 }
5539 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005540 return skip;
5541}
5542
John Zulaufd5115702021-01-18 12:34:33 -07005543struct SyncOpPipelineBarrierFunctorFactory {
5544 using BarrierOpFunctor = PipelineBarrierOp;
5545 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5546 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5547 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5548 using BufferRange = ResourceAccessRange;
5549 using ImageRange = subresource_adapter::ImageRangeGenerator;
5550 using GlobalRange = ResourceAccessRange;
5551
5552 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5553 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5554 }
John Zulauf14940722021-04-12 15:19:02 -06005555 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005556 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5557 }
5558 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5559 return GlobalBarrierOpFunctor(barrier, false);
5560 }
5561
5562 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5563 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5564 const auto base_address = ResourceBaseAddress(buffer);
5565 return (range + base_address);
5566 }
John Zulauf110413c2021-03-20 05:38:38 -06005567 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005568 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005569
5570 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005571 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005572 return range_gen;
5573 }
5574 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5575};
5576
5577template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005578void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005579 AccessContext *context) {
5580 for (const auto &barrier : barriers) {
5581 const auto *state = barrier.GetState();
5582 if (state) {
5583 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5584 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5585 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5586 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5587 }
5588 }
5589}
5590
5591template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005592void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005593 AccessContext *access_context) {
5594 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5595 for (const auto &barrier : barriers) {
5596 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5597 }
5598 for (const auto address_type : kAddressTypes) {
5599 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5600 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5601 }
5602}
5603
John Zulauf8eda1562021-04-13 17:06:41 -06005604ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005605 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005606 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005607 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf4fa68462021-04-26 21:04:22 -06005608 DoRecord(tag, access_context, events_context);
5609 return tag;
5610}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005611
John Zulauf4fa68462021-04-26 21:04:22 -06005612void SyncOpPipelineBarrier::DoRecord(const ResourceUsageTag tag, AccessContext *access_context,
5613 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005614 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005615 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5616 assert(barriers_.size() == 1);
5617 const auto &barrier_set = barriers_[0];
5618 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5619 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5620 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005621 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005622 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005623 } else {
5624 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005625 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005626 }
5627 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005628}
5629
John Zulauf8eda1562021-04-13 17:06:41 -06005630bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005631 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005632 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5633 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005634 return false;
5635}
5636
John Zulauf4edde622021-02-15 08:54:50 -07005637void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5638 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5639 const VkMemoryBarrier *barriers) {
5640 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005641 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005642 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005643 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005644 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005645 }
5646 if (0 == memory_barrier_count) {
5647 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005648 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005649 }
John Zulauf4edde622021-02-15 08:54:50 -07005650 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005651}
5652
John Zulauf4edde622021-02-15 08:54:50 -07005653void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5654 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5655 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5656 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005657 for (uint32_t index = 0; index < barrier_count; index++) {
5658 const auto &barrier = barriers[index];
Jeremy Gebben9f537102021-10-05 16:37:12 -06005659 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005660 if (buffer) {
5661 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5662 const auto range = MakeRange(barrier.offset, barrier_size);
5663 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005664 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005665 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005666 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005667 }
5668 }
5669}
5670
John Zulauf4edde622021-02-15 08:54:50 -07005671void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005672 uint32_t memory_barrier_count, const VkMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005673 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005674 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005675 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005676 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5677 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5678 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005679 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005680 }
John Zulauf4edde622021-02-15 08:54:50 -07005681 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005682}
5683
John Zulauf4edde622021-02-15 08:54:50 -07005684void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5685 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005686 const VkBufferMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005687 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005688 for (uint32_t index = 0; index < barrier_count; index++) {
5689 const auto &barrier = barriers[index];
5690 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5691 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9f537102021-10-05 16:37:12 -06005692 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005693 if (buffer) {
5694 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5695 const auto range = MakeRange(barrier.offset, barrier_size);
5696 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005697 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005698 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005699 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005700 }
5701 }
5702}
5703
John Zulauf4edde622021-02-15 08:54:50 -07005704void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5705 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5706 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5707 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005708 for (uint32_t index = 0; index < barrier_count; index++) {
5709 const auto &barrier = barriers[index];
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005710 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005711 if (image) {
5712 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5713 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005714 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005715 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005716 image_memory_barriers.emplace_back();
5717 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005718 }
5719 }
5720}
John Zulaufd5115702021-01-18 12:34:33 -07005721
John Zulauf4edde622021-02-15 08:54:50 -07005722void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5723 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005724 const VkImageMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005725 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005726 for (uint32_t index = 0; index < barrier_count; index++) {
5727 const auto &barrier = barriers[index];
5728 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5729 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005730 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005731 if (image) {
5732 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5733 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005734 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005735 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005736 image_memory_barriers.emplace_back();
5737 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005738 }
5739 }
5740}
5741
John Zulauf36ef9282021-02-02 11:47:24 -07005742SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005743 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5744 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5745 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5746 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005747 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005748 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5749 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005750 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005751}
5752
John Zulauf4edde622021-02-15 08:54:50 -07005753SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5754 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5755 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5756 MakeEventsList(sync_state, eventCount, pEvents);
5757 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5758}
5759
John Zulauf610e28c2021-08-03 17:46:23 -06005760const char *const SyncOpWaitEvents::kIgnored = "Wait operation is ignored for this event.";
5761
John Zulaufd5115702021-01-18 12:34:33 -07005762bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005763 bool skip = false;
5764 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005765 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005766
John Zulauf610e28c2021-08-03 17:46:23 -06005767 // This is only interesting at record and not replay (Execute/Submit) time.
John Zulauf4edde622021-02-15 08:54:50 -07005768 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5769 const auto &barrier_set = barriers_[barrier_set_index];
5770 if (barrier_set.single_exec_scope) {
5771 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5772 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5773 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5774 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5775 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5776 } else {
5777 const auto &barriers = barrier_set.memory_barriers;
5778 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5779 const auto &barrier = barriers[barrier_index];
5780 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5781 const std::string vuid =
5782 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5783 skip =
5784 sync_state.LogInfo(command_buffer_handle, vuid,
5785 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5786 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5787 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5788 }
5789 }
5790 }
5791 }
John Zulaufd5115702021-01-18 12:34:33 -07005792 }
5793
John Zulauf610e28c2021-08-03 17:46:23 -06005794 // The rest is common to record time and replay time.
5795 skip |= DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5796 return skip;
5797}
5798
5799bool SyncOpWaitEvents::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
5800 bool skip = false;
5801 const auto &sync_state = cb_context.GetSyncState();
5802
Jeremy Gebben40a22942020-12-22 14:22:06 -07005803 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005804 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005805 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005806 const auto *events_context = cb_context.GetCurrentEventsContext();
5807 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005808 size_t barrier_set_index = 0;
5809 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06005810 for (const auto &event : events_) {
5811 const auto *sync_event = events_context->Get(event.get());
5812 const auto &barrier_set = barriers_[barrier_set_index];
5813 if (!sync_event) {
5814 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5815 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5816 // new validation error... wait without previously submitted set event...
5817 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 -07005818 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06005819 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005820 }
John Zulauf610e28c2021-08-03 17:46:23 -06005821
5822 // For replay calls, don't revalidate "same command buffer" events
5823 if (sync_event->last_command_tag > base_tag) continue;
5824
John Zulauf78394fc2021-07-12 15:41:40 -06005825 const auto event_handle = sync_event->event->event();
5826 // TODO add "destroyed" checks
5827
John Zulauf78b1f892021-09-20 15:02:09 -06005828 if (sync_event->first_scope_set) {
5829 // Only accumulate barrier and event stages if there is a pending set in the current context
5830 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5831 event_stage_masks |= sync_event->scope.mask_param;
5832 }
5833
John Zulauf78394fc2021-07-12 15:41:40 -06005834 const auto &src_exec_scope = barrier_set.src_exec_scope;
John Zulauf78b1f892021-09-20 15:02:09 -06005835
John Zulauf78394fc2021-07-12 15:41:40 -06005836 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5837 if (ignore_reason) {
5838 switch (ignore_reason) {
5839 case SyncEventState::ResetWaitRace:
5840 case SyncEventState::Reset2WaitRace: {
5841 // Four permuations of Reset and Wait calls...
5842 const char *vuid =
5843 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5844 if (ignore_reason == SyncEventState::Reset2WaitRace) {
Tony-LunarG279601c2021-11-16 10:50:51 -07005845 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2-event-03831"
5846 : "VUID-vkCmdResetEvent2-event-03832";
John Zulauf78394fc2021-07-12 15:41:40 -06005847 }
5848 const char *const message =
5849 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5850 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5851 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
John Zulauf610e28c2021-08-03 17:46:23 -06005852 CommandTypeString(sync_event->last_command), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005853 break;
5854 }
5855 case SyncEventState::SetRace: {
5856 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5857 // this event
5858 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5859 const char *const message =
5860 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5861 const char *const reason = "First synchronization scope is undefined.";
5862 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5863 sync_state.report_data->FormatHandle(event_handle).c_str(),
John Zulauf610e28c2021-08-03 17:46:23 -06005864 CommandTypeString(sync_event->last_command), reason, kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005865 break;
5866 }
5867 case SyncEventState::MissingStageBits: {
5868 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5869 // Issue error message that event waited for is not in wait events scope
5870 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5871 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5872 ". Bits missing from srcStageMask %s. %s";
5873 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5874 sync_state.report_data->FormatHandle(event_handle).c_str(),
5875 sync_event->scope.mask_param, src_exec_scope.mask_param,
John Zulauf610e28c2021-08-03 17:46:23 -06005876 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005877 break;
5878 }
5879 case SyncEventState::SetVsWait2: {
Tony-LunarG279601c2021-11-16 10:50:51 -07005880 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2-pEvents-03837",
John Zulauf78394fc2021-07-12 15:41:40 -06005881 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5882 sync_state.report_data->FormatHandle(event_handle).c_str(),
5883 CommandTypeString(sync_event->last_command));
5884 break;
5885 }
5886 default:
5887 assert(ignore_reason == SyncEventState::NotIgnored);
5888 }
5889 } else if (barrier_set.image_memory_barriers.size()) {
5890 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5891 const auto *context = cb_context.GetCurrentAccessContext();
5892 assert(context);
5893 for (const auto &image_memory_barrier : image_memory_barriers) {
5894 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5895 const auto *image_state = image_memory_barrier.image.get();
5896 if (!image_state) continue;
5897 const auto &subresource_range = image_memory_barrier.range;
5898 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5899 const auto hazard =
5900 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5901 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5902 if (hazard.hazard) {
5903 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
5904 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5905 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5906 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
5907 cb_context.FormatUsage(hazard).c_str());
5908 break;
5909 }
5910 }
5911 }
5912 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5913 // 03839
5914 barrier_set_index += barrier_set_incr;
5915 }
John Zulaufd5115702021-01-18 12:34:33 -07005916
5917 // 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 -07005918 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 -07005919 if (extra_stage_bits) {
5920 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005921 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5922 const char *const vuid =
Tony-LunarG279601c2021-11-16 10:50:51 -07005923 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005924 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005925 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulauf610e28c2021-08-03 17:46:23 -06005926 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005927 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005928 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005929 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005930 " vkCmdSetEvent may be in previously submitted command buffer.");
5931 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005932 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005933 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005934 }
5935 }
5936 return skip;
5937}
5938
5939struct SyncOpWaitEventsFunctorFactory {
5940 using BarrierOpFunctor = WaitEventBarrierOp;
5941 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5942 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5943 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5944 using BufferRange = EventSimpleRangeGenerator;
5945 using ImageRange = EventImageRangeGenerator;
5946 using GlobalRange = EventSimpleRangeGenerator;
5947
5948 // Need to restrict to only valid exec and access scope for this event
5949 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5950 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005951 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005952 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5953 return barrier;
5954 }
5955 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5956 auto barrier = RestrictToEvent(barrier_arg);
5957 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5958 }
John Zulauf14940722021-04-12 15:19:02 -06005959 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005960 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5961 }
5962 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5963 auto barrier = RestrictToEvent(barrier_arg);
5964 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5965 }
5966
5967 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5968 const AccessAddressType address_type = GetAccessAddressType(buffer);
5969 const auto base_address = ResourceBaseAddress(buffer);
5970 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5971 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5972 return filtered_range_gen;
5973 }
John Zulauf110413c2021-03-20 05:38:38 -06005974 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005975 if (!SimpleBinding(image)) return ImageRange();
5976 const auto address_type = GetAccessAddressType(image);
5977 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005978 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005979 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5980
5981 return filtered_range_gen;
5982 }
5983 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5984 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5985 }
5986 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5987 SyncEventState *sync_event;
5988};
5989
John Zulauf8eda1562021-04-13 17:06:41 -06005990ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07005991 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005992 auto *access_context = cb_context->GetCurrentAccessContext();
5993 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005994 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07005995 auto *events_context = cb_context->GetCurrentEventsContext();
5996 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005997 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07005998
John Zulauf610e28c2021-08-03 17:46:23 -06005999 DoRecord(tag, access_context, events_context);
6000 return tag;
6001}
6002
6003void SyncOpWaitEvents::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07006004 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
6005 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
6006 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
6007 access_context->ResolvePreviousAccesses();
6008
John Zulauf4edde622021-02-15 08:54:50 -07006009 size_t barrier_set_index = 0;
6010 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
6011 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07006012 for (auto &event_shared : events_) {
6013 if (!event_shared.get()) continue;
6014 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07006015
John Zulauf4edde622021-02-15 08:54:50 -07006016 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006017 sync_event->last_command_tag = tag;
John Zulaufd5115702021-01-18 12:34:33 -07006018
John Zulauf4edde622021-02-15 08:54:50 -07006019 const auto &barrier_set = barriers_[barrier_set_index];
6020 const auto &dst = barrier_set.dst_exec_scope;
6021 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07006022 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
6023 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
6024 // of the barriers is maintained.
6025 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07006026 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
6027 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
6028 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07006029
6030 // Apply the global barrier to the event itself (for race condition tracking)
6031 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
6032 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6033 sync_event->barriers |= dst.exec_scope;
6034 } else {
6035 // We ignored this wait, so we don't have any effective synchronization barriers for it.
6036 sync_event->barriers = 0U;
6037 }
John Zulauf4edde622021-02-15 08:54:50 -07006038 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07006039 }
6040
6041 // Apply the pending barriers
6042 ResolvePendingBarrierFunctor apply_pending_action(tag);
6043 access_context->ApplyToContext(apply_pending_action);
6044}
6045
John Zulauf8eda1562021-04-13 17:06:41 -06006046bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006047 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6048 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006049}
6050
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006051bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6052 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
6053 bool skip = false;
6054 const auto *cb_access_context = GetAccessContext(commandBuffer);
6055 assert(cb_access_context);
6056 if (!cb_access_context) return skip;
6057
6058 const auto *context = cb_access_context->GetCurrentAccessContext();
6059 assert(context);
6060 if (!context) return skip;
6061
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006062 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006063
6064 if (dst_buffer) {
6065 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6066 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
6067 if (hazard.hazard) {
6068 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
6069 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
6070 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06006071 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006072 }
6073 }
6074 return skip;
6075}
6076
John Zulauf669dfd52021-01-27 17:15:28 -07006077void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07006078 events_.reserve(event_count);
6079 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006080 events_.emplace_back(sync_state.Get<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07006081 }
6082}
John Zulauf6ce24372021-01-30 05:56:25 -07006083
John Zulauf36ef9282021-02-02 11:47:24 -07006084SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006085 VkPipelineStageFlags2KHR stageMask)
Jeremy Gebben9f537102021-10-05 16:37:12 -06006086 : SyncOpBase(cmd), event_(sync_state.Get<EVENT_STATE>(event)), exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006087
John Zulauf1bf30522021-09-03 15:39:06 -06006088bool SyncOpResetEvent::Validate(const CommandBufferAccessContext& cb_context) const {
6089 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6090}
6091
6092bool SyncOpResetEvent::DoValidate(const CommandBufferAccessContext & cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006093 auto *events_context = cb_context.GetCurrentEventsContext();
6094 assert(events_context);
6095 bool skip = false;
6096 if (!events_context) return skip;
6097
6098 const auto &sync_state = cb_context.GetSyncState();
6099 const auto *sync_event = events_context->Get(event_);
6100 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6101
John Zulauf1bf30522021-09-03 15:39:06 -06006102 if (sync_event->last_command_tag > base_tag) return skip; // if we validated this in recording of the secondary, don't repeat
6103
John Zulauf6ce24372021-01-30 05:56:25 -07006104 const char *const set_wait =
6105 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6106 "hazards.";
6107 const char *message = set_wait; // Only one message this call.
6108 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
6109 const char *vuid = nullptr;
6110 switch (sync_event->last_command) {
6111 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006112 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006113 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006114 // Needs a barrier between set and reset
6115 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
6116 break;
John Zulauf4edde622021-02-15 08:54:50 -07006117 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006118 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006119 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07006120 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
6121 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
6122 break;
6123 }
6124 default:
6125 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07006126 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
6127 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07006128 break;
6129 }
6130 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006131 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
6132 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006133 CommandTypeString(sync_event->last_command));
6134 }
6135 }
6136 return skip;
6137}
6138
John Zulauf8eda1562021-04-13 17:06:41 -06006139ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
6140 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006141 auto *events_context = cb_context->GetCurrentEventsContext();
6142 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006143 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006144
6145 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06006146 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006147
6148 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07006149 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006150 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006151 sync_event->unsynchronized_set = CMD_NONE;
6152 sync_event->ResetFirstScope();
6153 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06006154
6155 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006156}
6157
John Zulauf8eda1562021-04-13 17:06:41 -06006158bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006159 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf1bf30522021-09-03 15:39:06 -06006160 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006161}
6162
John Zulauf4fa68462021-04-26 21:04:22 -06006163void SyncOpResetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006164
John Zulauf36ef9282021-02-02 11:47:24 -07006165SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006166 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07006167 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006168 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006169 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
6170 dep_info_() {}
6171
6172SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
6173 const VkDependencyInfoKHR &dep_info)
6174 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006175 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006176 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
Tony-LunarG273f32f2021-09-28 08:56:30 -06006177 dep_info_(new safe_VkDependencyInfo(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006178
6179bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf610e28c2021-08-03 17:46:23 -06006180 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6181}
6182bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6183 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6184 assert(active_context);
6185 return DoValidate(*active_context, base_tag);
6186}
6187
6188bool SyncOpSetEvent::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006189 bool skip = false;
6190
6191 const auto &sync_state = cb_context.GetSyncState();
6192 auto *events_context = cb_context.GetCurrentEventsContext();
6193 assert(events_context);
6194 if (!events_context) return skip;
6195
6196 const auto *sync_event = events_context->Get(event_);
6197 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6198
John Zulauf610e28c2021-08-03 17:46:23 -06006199 if (sync_event->last_command_tag >= base_tag) return skip; // for replay we don't want to revalidate internal "last commmand"
6200
John Zulauf6ce24372021-01-30 05:56:25 -07006201 const char *const reset_set =
6202 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6203 "hazards.";
6204 const char *const wait =
6205 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6206
6207 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006208 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006209 const char *message = nullptr;
6210 switch (sync_event->last_command) {
6211 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006212 case CMD_RESETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006213 case CMD_RESETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006214 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006215 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006216 message = reset_set;
6217 break;
6218 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006219 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006220 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006221 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006222 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006223 message = reset_set;
6224 break;
6225 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006226 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006227 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006228 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006229 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006230 message = wait;
6231 break;
6232 default:
6233 // The only other valid last command that wasn't one.
6234 assert(sync_event->last_command == CMD_NONE);
6235 break;
6236 }
John Zulauf4edde622021-02-15 08:54:50 -07006237 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006238 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006239 std::string vuid("SYNC-");
6240 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006241 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6242 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006243 CommandTypeString(sync_event->last_command));
6244 }
6245 }
6246
6247 return skip;
6248}
6249
John Zulauf8eda1562021-04-13 17:06:41 -06006250ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006251 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006252 auto *events_context = cb_context->GetCurrentEventsContext();
6253 auto *access_context = cb_context->GetCurrentAccessContext();
6254 assert(events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006255 if (access_context && events_context) {
6256 DoRecord(tag, access_context, events_context);
6257 }
6258 return tag;
6259}
John Zulauf6ce24372021-01-30 05:56:25 -07006260
John Zulauf610e28c2021-08-03 17:46:23 -06006261void SyncOpSetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006262 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf610e28c2021-08-03 17:46:23 -06006263 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006264
6265 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6266 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6267 // any issues caused by naive scope setting here.
6268
6269 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6270 // Given:
6271 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6272 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6273
6274 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6275 sync_event->unsynchronized_set = sync_event->last_command;
6276 sync_event->ResetFirstScope();
John Zulauf78b1f892021-09-20 15:02:09 -06006277 } else if (!sync_event->first_scope_set) {
John Zulauf6ce24372021-01-30 05:56:25 -07006278 // We only set the scope if there isn't one
6279 sync_event->scope = src_exec_scope_;
6280
6281 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6282 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6283 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6284 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6285 }
6286 };
6287 access_context->ForAll(set_scope);
6288 sync_event->unsynchronized_set = CMD_NONE;
John Zulauf78b1f892021-09-20 15:02:09 -06006289 sync_event->first_scope_set = true;
John Zulauf6ce24372021-01-30 05:56:25 -07006290 sync_event->first_scope_tag = tag;
6291 }
John Zulauf4edde622021-02-15 08:54:50 -07006292 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6293 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006294 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006295 sync_event->barriers = 0U;
6296}
John Zulauf64ffe552021-02-06 10:25:07 -07006297
6298SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6299 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006300 const VkSubpassBeginInfo *pSubpassBeginInfo)
6301 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006302 if (pRenderPassBegin) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006303 rp_state_ = sync_state.Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
John Zulauf64ffe552021-02-06 10:25:07 -07006304 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006305 auto fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07006306 if (fb_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006307 shared_attachments_ = sync_state.GetAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
John Zulauf64ffe552021-02-06 10:25:07 -07006308 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6309 // Note that this a safe to presist as long as shared_attachments is not cleared
6310 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006311 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006312 attachments_.emplace_back(attachment.get());
6313 }
6314 }
6315 if (pSubpassBeginInfo) {
6316 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6317 }
6318 }
6319}
6320
6321bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6322 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6323 bool skip = false;
6324
6325 assert(rp_state_.get());
6326 if (nullptr == rp_state_.get()) return skip;
6327 auto &rp_state = *rp_state_.get();
6328
6329 const uint32_t subpass = 0;
6330
6331 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6332 // hasn't happened yet)
6333 const std::vector<AccessContext> empty_context_vector;
6334 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6335 cb_context.GetCurrentAccessContext());
6336
6337 // Validate attachment operations
6338 if (attachments_.size() == 0) return skip;
6339 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006340
6341 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6342 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6343 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6344 // operations (though it's currently a messy approach)
6345 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6346 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006347
6348 // Validate load operations if there were no layout transition hazards
6349 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07006350 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
6351 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006352 }
6353
6354 return skip;
6355}
6356
John Zulauf8eda1562021-04-13 17:06:41 -06006357ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
6358 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006359 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6360 assert(rp_state_.get());
John Zulauf8eda1562021-04-13 17:06:41 -06006361 if (nullptr == rp_state_.get()) return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006362 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006363
6364 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006365}
6366
John Zulauf8eda1562021-04-13 17:06:41 -06006367bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006368 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006369 return false;
6370}
6371
John Zulauf4fa68462021-04-26 21:04:22 -06006372void SyncOpBeginRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6373}
John Zulauf8eda1562021-04-13 17:06:41 -06006374
John Zulauf64ffe552021-02-06 10:25:07 -07006375SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006376 const VkSubpassEndInfo *pSubpassEndInfo)
6377 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006378 if (pSubpassBeginInfo) {
6379 subpass_begin_info_.initialize(pSubpassBeginInfo);
6380 }
6381 if (pSubpassEndInfo) {
6382 subpass_end_info_.initialize(pSubpassEndInfo);
6383 }
6384}
6385
6386bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6387 bool skip = false;
6388 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6389 if (!renderpass_context) return skip;
6390
6391 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6392 return skip;
6393}
6394
John Zulauf8eda1562021-04-13 17:06:41 -06006395ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006396 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006397 // TODO PHASE2 Need to fix renderpass tagging/segregation of barrier and access operations for QueueSubmit time validation
6398 auto prev_tag = cb_context->NextCommandTag(cmd_);
6399 auto next_tag = cb_context->NextSubcommandTag(cmd_);
6400
6401 cb_context->RecordNextSubpass(prev_tag, next_tag);
6402 // TODO PHASE2 This needs to be the tag of the barrier operations
6403 return prev_tag;
6404}
6405
6406bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006407 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006408 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006409}
6410
sfricke-samsung85584a72021-09-30 21:43:38 -07006411SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6412 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006413 if (pSubpassEndInfo) {
6414 subpass_end_info_.initialize(pSubpassEndInfo);
6415 }
6416}
6417
John Zulauf4fa68462021-04-26 21:04:22 -06006418void SyncOpNextSubpass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006419
John Zulauf64ffe552021-02-06 10:25:07 -07006420bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6421 bool skip = false;
6422 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6423
6424 if (!renderpass_context) return skip;
6425 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6426 return skip;
6427}
6428
John Zulauf8eda1562021-04-13 17:06:41 -06006429ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006430 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006431 const auto tag = cb_context->NextCommandTag(cmd_);
6432 cb_context->RecordEndRenderPass(tag);
6433 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006434}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006435
John Zulauf8eda1562021-04-13 17:06:41 -06006436bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006437 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006438 return false;
6439}
6440
John Zulauf4fa68462021-04-26 21:04:22 -06006441void SyncOpEndRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006442
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006443void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6444 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6445 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6446 auto *cb_access_context = GetAccessContext(commandBuffer);
6447 assert(cb_access_context);
6448 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6449 auto *context = cb_access_context->GetCurrentAccessContext();
6450 assert(context);
6451
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006452 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006453
6454 if (dst_buffer) {
6455 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6456 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6457 }
6458}
John Zulaufd05c5842021-03-26 11:32:16 -06006459
John Zulaufae842002021-04-15 18:20:55 -06006460bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6461 const VkCommandBuffer *pCommandBuffers) const {
6462 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6463 const char *func_name = "vkCmdExecuteCommands";
6464 const auto *cb_context = GetAccessContext(commandBuffer);
6465 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006466
6467 // Heavyweight, but we need a proxy copy of the active command buffer access context
6468 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006469
6470 // Make working copies of the access and events contexts
John Zulauf4fa68462021-04-26 21:04:22 -06006471 proxy_cb_context.NextCommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006472
6473 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf4fa68462021-04-26 21:04:22 -06006474 proxy_cb_context.NextSubcommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006475 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6476 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006477
6478 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6479 assert(recorded_context);
6480 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6481
6482 // The barriers have already been applied in ValidatFirstUse
6483 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6484 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006485 }
6486
John Zulaufae842002021-04-15 18:20:55 -06006487 return skip;
6488}
6489
6490void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6491 const VkCommandBuffer *pCommandBuffers) {
6492 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006493 auto *cb_context = GetAccessContext(commandBuffer);
6494 assert(cb_context);
6495 cb_context->NextCommandTag(CMD_EXECUTECOMMANDS);
6496 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
6497 cb_context->NextSubcommandTag(CMD_EXECUTECOMMANDS);
6498 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6499 if (!recorded_cb_context) continue;
6500 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6501 }
John Zulaufae842002021-04-15 18:20:55 -06006502}
6503
John Zulaufd0ec59f2021-03-13 14:25:08 -07006504AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6505 : view_(view), view_mask_(), gen_store_() {
6506 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6507 const IMAGE_STATE &image_state = *view_->image_state.get();
6508 const auto base_address = ResourceBaseAddress(image_state);
6509 const auto *encoder = image_state.fragment_encoder.get();
6510 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006511 // Get offset and extent for the view, accounting for possible depth slicing
6512 const VkOffset3D zero_offset = view->GetOffset();
6513 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006514 // Intentional copy
6515 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6516 view_mask_ = subres_range.aspectMask;
6517 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6518 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6519
6520 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6521 if (depth && (depth != view_mask_)) {
6522 subres_range.aspectMask = depth;
6523 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6524 }
6525 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6526 if (stencil && (stencil != view_mask_)) {
6527 subres_range.aspectMask = stencil;
6528 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6529 }
6530}
6531
6532const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6533 const ImageRangeGen *got = nullptr;
6534 switch (gen_type) {
6535 case kViewSubresource:
6536 got = &gen_store_[kViewSubresource];
6537 break;
6538 case kRenderArea:
6539 got = &gen_store_[kRenderArea];
6540 break;
6541 case kDepthOnlyRenderArea:
6542 got =
6543 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6544 break;
6545 case kStencilOnlyRenderArea:
6546 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6547 : &gen_store_[Gen::kStencilOnlyRenderArea];
6548 break;
6549 default:
6550 assert(got);
6551 }
6552 return got;
6553}
6554
6555AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6556 assert(IsValid());
6557 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6558 if (depth_op) {
6559 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6560 if (stencil_op) {
6561 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6562 return kRenderArea;
6563 }
6564 return kDepthOnlyRenderArea;
6565 }
6566 if (stencil_op) {
6567 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6568 return kStencilOnlyRenderArea;
6569 }
6570
6571 assert(depth_op || stencil_op);
6572 return kRenderArea;
6573}
6574
6575AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006576
6577void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6578 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6579 for (auto &event_pair : map_) {
6580 assert(event_pair.second); // Shouldn't be storing empty
6581 auto &sync_event = *event_pair.second;
6582 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6583 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6584 sync_event.barriers |= dst.exec_scope;
6585 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6586 }
6587 }
6588}