blob: 4d0ac531c1c0a7ffd8cf8b23700686b02502d888 [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 Zulauf3c2a0b32021-07-14 11:14:52 -0600233 out << ", reset_no: " << std::to_string(record.reset_count);
John Zulauf4fa68462021-04-26 21:04:22 -0600234 } else {
235 out << ", reset_no: " << std::to_string(reset_count_);
236 }
237 return out.str();
238}
239std::string CommandBufferAccessContext::FormatUsage(const ResourceFirstAccess &access) const {
240 std::stringstream out;
241 out << "(recorded_usage: " << string_UsageIndex(access.usage_index);
242 out << ", " << FormatUsage(access.tag) << ")";
243 return out.str();
244}
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700245
John Zulauffaea0ee2021-01-14 14:01:32 -0700246std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600247 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600248 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
249 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600250 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600251 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
252 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf4fa68462021-04-26 21:04:22 -0600253 out << "(";
254 if (!hazard.recorded_access.get()) {
255 // if we have a recorded usage the usage is reported from the recorded contexts point of view
256 out << "usage: " << usage_info.name << ", ";
257 }
258 out << "prior_usage: " << stage_access_name;
John Zulauf59e25072020-07-17 10:55:21 -0600259 if (IsHazardVsRead(hazard.hazard)) {
260 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700261 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600262 } else {
263 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
264 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
265 }
266
ziga-lunarg0f248902022-03-24 16:42:45 +0100267 if (tag < access_log_.size()) {
268 out << ", " << FormatUsage(tag) << ")";
269 }
John Zulauf1dae9192020-06-16 15:46:44 -0600270 return out.str();
271}
272
John Zulaufd14743a2020-07-03 09:42:39 -0600273// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
274// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
275// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700276static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700277static const SyncStageAccessFlags kColorAttachmentAccessScope =
278 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
279 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
280 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
281 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700282static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
283 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 -0700284static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
285 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
286 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
287 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700288static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700289static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600290
John Zulauf8e3c3e92021-01-06 11:19:36 -0700291ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700292 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700293 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
294 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
295 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
296
John Zulauf7635de32020-05-29 17:14:15 -0600297// 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 -0600298static const ResourceUsageTag kCurrentCommandTag(ResourceUsageRecord::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600299
Jeremy Gebben62c3bf42021-07-21 15:38:24 -0600300static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) { return bindable.GetFakeBaseAddress(); }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600301
locke-lunarg3c038002020-04-30 23:08:08 -0600302inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
303 if (size == VK_WHOLE_SIZE) {
304 return (whole_size - offset);
305 }
306 return size;
307}
308
John Zulauf3e86bf02020-09-12 10:47:57 -0600309static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
310 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
311}
312
John Zulauf16adfc92020-04-08 10:28:33 -0600313template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600314static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600315 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
316}
317
John Zulauf355e49b2020-04-24 15:11:15 -0600318static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600319
John Zulauf3e86bf02020-09-12 10:47:57 -0600320static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
321 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
322}
323
324static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
325 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
326}
327
John Zulauf4a6105a2020-11-17 15:11:05 -0700328// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
329//
John Zulauf10f1f522020-12-18 12:00:35 -0700330// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
331//
John Zulauf4a6105a2020-11-17 15:11:05 -0700332// Usage:
333// Constructor() -- initializes the generator to point to the begin of the space declared.
334// * -- the current range of the generator empty signfies end
335// ++ -- advance to the next non-empty range (or end)
336
337// A wrapper for a single range with the same semantics as the actual generators below
338template <typename KeyType>
339class SingleRangeGenerator {
340 public:
341 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700342 const KeyType &operator*() const { return current_; }
343 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700344 SingleRangeGenerator &operator++() {
345 current_ = KeyType(); // just one real range
346 return *this;
347 }
348
349 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
350
351 private:
352 SingleRangeGenerator() = default;
353 const KeyType range_;
354 KeyType current_;
355};
356
John Zulaufae842002021-04-15 18:20:55 -0600357// Generate the ranges that are the intersection of range and the entries in the RangeMap
358template <typename RangeMap, typename KeyType = typename RangeMap::key_type>
359class MapRangesRangeGenerator {
John Zulauf4a6105a2020-11-17 15:11:05 -0700360 public:
John Zulaufd5115702021-01-18 12:34:33 -0700361 // Default constructed is safe to dereference for "empty" test, but for no other operation.
John Zulaufae842002021-04-15 18:20:55 -0600362 MapRangesRangeGenerator() : range_(), map_(nullptr), map_pos_(), current_() {
John Zulaufd5115702021-01-18 12:34:33 -0700363 // Default construction for KeyType *must* be empty range
364 assert(current_.empty());
365 }
John Zulaufae842002021-04-15 18:20:55 -0600366 MapRangesRangeGenerator(const RangeMap &filter, const KeyType &range) : range_(range), map_(&filter), map_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700367 SeekBegin();
368 }
John Zulaufae842002021-04-15 18:20:55 -0600369 MapRangesRangeGenerator(const MapRangesRangeGenerator &from) = default;
John Zulaufd5115702021-01-18 12:34:33 -0700370
John Zulauf4a6105a2020-11-17 15:11:05 -0700371 const KeyType &operator*() const { return current_; }
372 const KeyType *operator->() const { return &current_; }
John Zulaufae842002021-04-15 18:20:55 -0600373 MapRangesRangeGenerator &operator++() {
374 ++map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700375 UpdateCurrent();
376 return *this;
377 }
378
John Zulaufae842002021-04-15 18:20:55 -0600379 bool operator==(const MapRangesRangeGenerator &other) const { return current_ == other.current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700380
John Zulaufae842002021-04-15 18:20:55 -0600381 protected:
John Zulauf4a6105a2020-11-17 15:11:05 -0700382 void UpdateCurrent() {
John Zulaufae842002021-04-15 18:20:55 -0600383 if (map_pos_ != map_->cend()) {
384 current_ = range_ & map_pos_->first;
John Zulauf4a6105a2020-11-17 15:11:05 -0700385 } else {
386 current_ = KeyType();
387 }
388 }
389 void SeekBegin() {
John Zulaufae842002021-04-15 18:20:55 -0600390 map_pos_ = map_->lower_bound(range_);
John Zulauf4a6105a2020-11-17 15:11:05 -0700391 UpdateCurrent();
392 }
John Zulaufae842002021-04-15 18:20:55 -0600393
394 // Adding this functionality here, to avoid gratuitous Base:: qualifiers in the derived class
395 // Note: Not exposed in this classes public interface to encourage using a consistent ++/empty generator semantic
396 template <typename Pred>
397 MapRangesRangeGenerator &PredicatedIncrement(Pred &pred) {
398 do {
399 ++map_pos_;
400 } while (map_pos_ != map_->cend() && map_pos_->first.intersects(range_) && !pred(map_pos_));
401 UpdateCurrent();
402 return *this;
403 }
404
John Zulauf4a6105a2020-11-17 15:11:05 -0700405 const KeyType range_;
John Zulaufae842002021-04-15 18:20:55 -0600406 const RangeMap *map_;
407 typename RangeMap::const_iterator map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700408 KeyType current_;
409};
John Zulaufd5115702021-01-18 12:34:33 -0700410using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulaufae842002021-04-15 18:20:55 -0600411using EventSimpleRangeGenerator = MapRangesRangeGenerator<SyncEventState::ScopeMap>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700412
John Zulaufae842002021-04-15 18:20:55 -0600413// Generate the ranges for entries meeting the predicate that are the intersection of range and the entries in the RangeMap
414template <typename RangeMap, typename Predicate, typename KeyType = typename RangeMap::key_type>
415class PredicatedMapRangesRangeGenerator : public MapRangesRangeGenerator<RangeMap, KeyType> {
416 public:
417 using Base = MapRangesRangeGenerator<RangeMap, KeyType>;
418 // Default constructed is safe to dereference for "empty" test, but for no other operation.
419 PredicatedMapRangesRangeGenerator() : Base(), pred_() {}
420 PredicatedMapRangesRangeGenerator(const RangeMap &filter, const KeyType &range, Predicate pred)
421 : Base(filter, range), pred_(pred) {}
422 PredicatedMapRangesRangeGenerator(const PredicatedMapRangesRangeGenerator &from) = default;
423
424 PredicatedMapRangesRangeGenerator &operator++() {
425 Base::PredicatedIncrement(pred_);
426 return *this;
427 }
428
429 protected:
430 Predicate pred_;
431};
John Zulauf4a6105a2020-11-17 15:11:05 -0700432
433// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulaufae842002021-04-15 18:20:55 -0600434// Templated to allow for different Range generators or map sources...
435template <typename RangeMap, typename RangeGen, typename KeyType = typename RangeMap::key_type>
John Zulauf4a6105a2020-11-17 15:11:05 -0700436class FilteredGeneratorGenerator {
437 public:
John Zulaufd5115702021-01-18 12:34:33 -0700438 // Default constructed is safe to dereference for "empty" test, but for no other operation.
439 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
440 // Default construction for KeyType *must* be empty range
441 assert(current_.empty());
442 }
John Zulaufae842002021-04-15 18:20:55 -0600443 FilteredGeneratorGenerator(const RangeMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700444 SeekBegin();
445 }
John Zulaufd5115702021-01-18 12:34:33 -0700446 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700447 const KeyType &operator*() const { return current_; }
448 const KeyType *operator->() const { return &current_; }
449 FilteredGeneratorGenerator &operator++() {
450 KeyType gen_range = GenRange();
451 KeyType filter_range = FilterRange();
452 current_ = KeyType();
453 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
454 if (gen_range.end > filter_range.end) {
455 // if the generated range is beyond the filter_range, advance the filter range
456 filter_range = AdvanceFilter();
457 } else {
458 gen_range = AdvanceGen();
459 }
460 current_ = gen_range & filter_range;
461 }
462 return *this;
463 }
464
465 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
466
467 private:
468 KeyType AdvanceFilter() {
469 ++filter_pos_;
470 auto filter_range = FilterRange();
471 if (filter_range.valid()) {
472 FastForwardGen(filter_range);
473 }
474 return filter_range;
475 }
476 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700477 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700478 auto gen_range = GenRange();
479 if (gen_range.valid()) {
480 FastForwardFilter(gen_range);
481 }
482 return gen_range;
483 }
484
485 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700486 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700487
488 KeyType FastForwardFilter(const KeyType &range) {
489 auto filter_range = FilterRange();
490 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700491 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700492 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
493 if (retry_count < kRetryLimit) {
494 ++filter_pos_;
495 filter_range = FilterRange();
496 retry_count++;
497 } else {
498 // Okay we've tried walking, do a seek.
499 filter_pos_ = filter_->lower_bound(range);
500 break;
501 }
502 }
503 return FilterRange();
504 }
505
506 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
507 // faster.
508 KeyType FastForwardGen(const KeyType &range) {
509 auto gen_range = GenRange();
510 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700511 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700512 gen_range = GenRange();
513 }
514 return gen_range;
515 }
516
517 void SeekBegin() {
518 auto gen_range = GenRange();
519 if (gen_range.empty()) {
520 current_ = KeyType();
521 filter_pos_ = filter_->cend();
522 } else {
523 filter_pos_ = filter_->lower_bound(gen_range);
524 current_ = gen_range & FilterRange();
525 }
526 }
527
John Zulaufae842002021-04-15 18:20:55 -0600528 const RangeMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700529 RangeGen gen_;
John Zulaufae842002021-04-15 18:20:55 -0600530 typename RangeMap::const_iterator filter_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700531 KeyType current_;
532};
533
534using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
535
John Zulauf5c5e88d2019-12-26 11:22:02 -0700536
John Zulauf3e86bf02020-09-12 10:47:57 -0600537ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
538 VkDeviceSize stride) {
539 VkDeviceSize range_start = offset + first_index * stride;
540 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600541 if (count == UINT32_MAX) {
542 range_size = buf_whole_size - range_start;
543 } else {
544 range_size = count * stride;
545 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600546 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600547}
548
locke-lunarg654e3692020-06-04 17:19:15 -0600549SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
550 VkShaderStageFlagBits stage_flag) {
551 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
552 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
553 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
554 }
555 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
556 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
557 assert(0);
558 }
559 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
560 return stage_access->second.uniform_read;
561 }
562
563 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
564 // Because if write hazard happens, read hazard might or might not happen.
565 // But if write hazard doesn't happen, read hazard is impossible to happen.
566 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700567 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600568 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700569 // TODO: sampled_read
570 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600571}
572
locke-lunarg37047832020-06-12 13:44:45 -0600573bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
574 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
575 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
576 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
577 ? true
578 : false;
579}
580
581bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
582 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
583 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
584 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
585 ? true
586 : false;
587}
588
John Zulauf355e49b2020-04-24 15:11:15 -0600589// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600590template <typename Action>
591static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
592 Action &action) {
593 // At this point the "apply over range" logic only supports a single memory binding
594 if (!SimpleBinding(image_state)) return;
595 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600596 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700597 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
598 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600599 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700600 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600601 }
602}
603
John Zulauf7635de32020-05-29 17:14:15 -0600604// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
605// Used by both validation and record operations
606//
607// The signature for Action() reflect the needs of both uses.
608template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700609void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
610 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600611 const auto &rp_ci = rp_state.createInfo;
612 const auto *attachment_ci = rp_ci.pAttachments;
613 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
614
615 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
616 const auto *color_attachments = subpass_ci.pColorAttachments;
617 const auto *color_resolve = subpass_ci.pResolveAttachments;
618 if (color_resolve && color_attachments) {
619 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
620 const auto &color_attach = color_attachments[i].attachment;
621 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
622 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
623 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700624 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
625 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600626 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700627 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
628 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600629 }
630 }
631 }
632
633 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700634 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600635 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
636 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
637 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
638 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
639 const auto src_ci = attachment_ci[src_at];
640 // The formats are required to match so we can pick either
641 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
642 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
643 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600644
645 // Figure out which aspects are actually touched during resolve operations
646 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700647 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600648 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600649 aspect_string = "depth/stencil";
650 } else if (resolve_depth) {
651 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700652 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600653 aspect_string = "depth";
654 } else if (resolve_stencil) {
655 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700656 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600657 aspect_string = "stencil";
658 }
659
John Zulaufd0ec59f2021-03-13 14:25:08 -0700660 if (aspect_string) {
661 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
662 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
663 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
664 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600665 }
666 }
667}
668
669// Action for validating resolve operations
670class ValidateResolveAction {
671 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700672 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700673 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600674 : render_pass_(render_pass),
675 subpass_(subpass),
676 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700677 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600678 func_name_(func_name),
679 skip_(false) {}
680 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 -0700681 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
682 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600683 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700684 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600685 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700686 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700687 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700688 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
689 " to resolve attachment %" PRIu32 ". Access info %s.",
690 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700691 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600692 }
693 }
694 // Providing a mechanism for the constructing caller to get the result of the validation
695 bool GetSkip() const { return skip_; }
696
697 private:
698 VkRenderPass render_pass_;
699 const uint32_t subpass_;
700 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700701 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600702 const char *func_name_;
703 bool skip_;
704};
705
706// Update action for resolve operations
707class UpdateStateResolveAction {
708 public:
John Zulauf14940722021-04-12 15:19:02 -0600709 UpdateStateResolveAction(AccessContext &context, ResourceUsageTag tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700710 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
711 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600712 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700713 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600714 }
715
716 private:
717 AccessContext &context_;
John Zulauf14940722021-04-12 15:19:02 -0600718 const ResourceUsageTag tag_;
John Zulauf7635de32020-05-29 17:14:15 -0600719};
720
John Zulauf59e25072020-07-17 10:55:21 -0600721void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
John Zulauf14940722021-04-12 15:19:02 -0600722 const SyncStageAccessFlags &prior_, const ResourceUsageTag tag_) {
John Zulauf4fa68462021-04-26 21:04:22 -0600723 access_state = layer_data::make_unique<const ResourceAccessState>(*access_state_);
John Zulauf59e25072020-07-17 10:55:21 -0600724 usage_index = usage_index_;
725 hazard = hazard_;
726 prior_access = prior_;
727 tag = tag_;
728}
729
John Zulauf4fa68462021-04-26 21:04:22 -0600730void HazardResult::AddRecordedAccess(const ResourceFirstAccess &first_access) {
731 recorded_access = layer_data::make_unique<const ResourceFirstAccess>(first_access);
732}
733
John Zulauf540266b2020-04-06 18:54:53 -0600734AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
735 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600736 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600737 Reset();
738 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700739 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
740 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600741 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600742 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600743 const auto prev_pass = prev_dep.first->pass;
744 const auto &prev_barriers = prev_dep.second;
745 assert(prev_dep.second.size());
746 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
747 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700748 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600749
750 async_.reserve(subpass_dep.async.size());
751 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700752 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600753 }
John Zulauf22aefed2021-03-11 18:14:35 -0700754 if (has_barrier_from_external) {
755 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
756 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
757 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600758 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600759 if (subpass_dep.barrier_to_external.size()) {
760 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600761 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700762}
763
John Zulauf5f13a792020-03-10 07:31:21 -0600764template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700765HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600766 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600767 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600768 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600769
770 HazardResult hazard;
771 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
772 hazard = detector.Detect(prev);
773 }
774 return hazard;
775}
776
John Zulauf4a6105a2020-11-17 15:11:05 -0700777template <typename Action>
778void AccessContext::ForAll(Action &&action) {
779 for (const auto address_type : kAddressTypes) {
780 auto &accesses = GetAccessStateMap(address_type);
781 for (const auto &access : accesses) {
782 action(address_type, access);
783 }
784 }
785}
786
John Zulauf3d84f1b2020-03-09 13:33:25 -0600787// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
788// the DAG of the contexts (for example subpasses)
789template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700790HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600791 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600792 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600793
John Zulauf1a224292020-06-30 14:52:13 -0600794 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600795 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
796 // so we'll check these first
797 for (const auto &async_context : async_) {
798 hazard = async_context->DetectAsyncHazard(type, detector, range);
799 if (hazard.hazard) return hazard;
800 }
John Zulauf5f13a792020-03-10 07:31:21 -0600801 }
802
John Zulauf1a224292020-06-30 14:52:13 -0600803 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600804
John Zulauf69133422020-05-20 14:55:53 -0600805 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600806 const auto the_end = accesses.cend(); // End is not invalidated
807 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600808 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600809
John Zulauf3cafbf72021-03-26 16:55:19 -0600810 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600811 // Cover any leading gap, or gap between entries
812 if (detect_prev) {
813 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
814 // Cover any leading gap, or gap between entries
815 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600816 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600817 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600818 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600819 if (hazard.hazard) return hazard;
820 }
John Zulauf69133422020-05-20 14:55:53 -0600821 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
822 gap.begin = pos->first.end;
823 }
824
825 hazard = detector.Detect(pos);
826 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600827 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600828 }
829
830 if (detect_prev) {
831 // Detect in the trailing empty as needed
832 gap.end = range.end;
833 if (gap.non_empty()) {
834 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600835 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600836 }
837
838 return hazard;
839}
840
841// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
842template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700843HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
844 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600845 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600846 auto pos = accesses.lower_bound(range);
847 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600848
John Zulauf3d84f1b2020-03-09 13:33:25 -0600849 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600850 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700851 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600852 if (hazard.hazard) break;
853 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600854 }
John Zulauf16adfc92020-04-08 10:28:33 -0600855
John Zulauf3d84f1b2020-03-09 13:33:25 -0600856 return hazard;
857}
858
John Zulaufb02c1eb2020-10-06 16:33:36 -0600859struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700860 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600861 void operator()(ResourceAccessState *access) const {
862 assert(access);
863 access->ApplyBarriers(barriers, true);
864 }
865 const std::vector<SyncBarrier> &barriers;
866};
867
John Zulauf22aefed2021-03-11 18:14:35 -0700868struct ApplyTrackbackStackAction {
869 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
870 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
871 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600872 void operator()(ResourceAccessState *access) const {
873 assert(access);
874 assert(!access->HasPendingState());
875 access->ApplyBarriers(barriers, false);
876 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700877 if (previous_barrier) {
878 assert(bool(*previous_barrier));
879 (*previous_barrier)(access);
880 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600881 }
882 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700883 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600884};
885
886// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
887// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
888// *different* map from dest.
889// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
890// range [first, last)
891template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600892static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
893 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600894 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600895 auto at = entry;
896 for (auto pos = first; pos != last; ++pos) {
897 // Every member of the input iterator range must fit within the remaining portion of entry
898 assert(at->first.includes(pos->first));
899 assert(at != dest->end());
900 // Trim up at to the same size as the entry to resolve
901 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600902 auto access = pos->second; // intentional copy
903 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600904 at->second.Resolve(access);
905 ++at; // Go to the remaining unused section of entry
906 }
907}
908
John Zulaufa0a98292020-09-18 09:30:10 -0600909static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
910 SyncBarrier merged = {};
911 for (const auto &barrier : barriers) {
912 merged.Merge(barrier);
913 }
914 return merged;
915}
916
John Zulaufb02c1eb2020-10-06 16:33:36 -0600917template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700918void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600919 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
920 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600921 if (!range.non_empty()) return;
922
John Zulauf355e49b2020-04-24 15:11:15 -0600923 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
924 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600925 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600926 if (current->pos_B->valid) {
927 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600928 auto access = src_pos->second; // intentional copy
929 barrier_action(&access);
930
John Zulauf16adfc92020-04-08 10:28:33 -0600931 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600932 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
933 trimmed->second.Resolve(access);
934 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600935 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600936 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600937 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600938 }
John Zulauf16adfc92020-04-08 10:28:33 -0600939 } else {
940 // we have to descend to fill this gap
941 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700942 ResourceAccessRange recurrence_range = current_range;
943 // The current context is empty for the current range, so recur to fill the gap.
944 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
945 // is not valid, to minimize that recurrence
946 if (current->pos_B.at_end()) {
947 // Do the remainder here....
948 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600949 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700950 // Recur only over the range until B becomes valid (within the limits of range).
951 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600952 }
John Zulauf22aefed2021-03-11 18:14:35 -0700953 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
954
John Zulauf355e49b2020-04-24 15:11:15 -0600955 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
956 // iterator of the outer while.
957
958 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
959 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
960 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700961 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 -0600962 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600963 current.seek(seek_to);
964 } else if (!current->pos_A->valid && infill_state) {
965 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
966 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
967 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600968 }
John Zulauf5f13a792020-03-10 07:31:21 -0600969 }
John Zulauf16adfc92020-04-08 10:28:33 -0600970 ++current;
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) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001853 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1854 pipe->create_info.graphics.pRasterizationState->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) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001990 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1991 pipe->create_info.graphics.pRasterizationState->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 Cesarioce9b4812020-12-17 08:55:28 -07002075 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002076
2077 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002078 const auto &binding_description = pipe->vertex_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 Cesarioce9b4812020-12-17 08:55:28 -07002105 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002106
2107 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002108 const auto &binding_description = pipe->vertex_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
2335 const auto &create_info = pipe->create_info.graphics;
2336 if (create_info.pRasterizationState && create_info.pRasterizationState->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.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002369 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002370 GetSubpassDepthStencilAttachmentIndex(pipe->create_info.graphics.pDepthStencilState, 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.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002378 if (!FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2379 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002380 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2381 depth_write = true;
2382 }
2383 // PHASE1 TODO: It needs to check if stencil is writable.
2384 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2385 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2386 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002387 if (!FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002388 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2389 stencil_write = true;
2390 }
2391
2392 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2393 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002394 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2395 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2396 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002397 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002398 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002399 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002400 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002401 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002402 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2403 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002404 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002405 }
2406 }
2407 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002408 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2409 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2410 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002411 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002412 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002413 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002414 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002415 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002416 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2417 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002418 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002419 }
locke-lunarg61870c22020-06-09 14:51:50 -06002420 }
2421 }
2422 return skip;
2423}
2424
John Zulauf14940722021-04-12 15:19:02 -06002425void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002426 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002427 if (!pipe) {
2428 return;
2429 }
2430
2431 const auto &create_info = pipe->create_info.graphics;
2432 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002433 return;
2434 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002435 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002436 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002437
John Zulauf1a224292020-06-30 14:52:13 -06002438 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002439 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002440 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2441 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002442 if (location >= subpass.colorAttachmentCount ||
2443 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002444 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002445 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002446 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2447 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2448 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2449 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002450 }
2451 }
locke-lunarg37047832020-06-12 13:44:45 -06002452
2453 // PHASE1 TODO: Add layout based read/vs. write selection.
2454 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002455 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002456 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002457 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2458 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2459 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002460 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002461 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2462 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002463
2464 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002465 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2466 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002467 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2468 depth_write = true;
2469 }
2470 // PHASE1 TODO: It needs to check if stencil is writable.
2471 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2472 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2473 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002474 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002475 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2476 stencil_write = true;
2477 }
2478
John Zulaufd0ec59f2021-03-13 14:25:08 -07002479 if (depth_write || stencil_write) {
2480 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2481 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2482 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2483 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002484 }
locke-lunarg61870c22020-06-09 14:51:50 -06002485 }
2486}
2487
John Zulauf64ffe552021-02-06 10:25:07 -07002488bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002489 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002490 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002491 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002492 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002493 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002494 func_name);
2495
John Zulauf355e49b2020-04-24 15:11:15 -06002496 const auto next_subpass = current_subpass_ + 1;
ziga-lunarg31a3e772022-03-22 11:48:46 +01002497 if (next_subpass >= subpass_contexts_.size()) {
2498 return skip;
2499 }
John Zulauf1507ee42020-05-18 11:33:09 -06002500 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002501 skip |=
2502 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002503 if (!skip) {
2504 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2505 // on a copy of the (empty) next context.
2506 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2507 AccessContext temp_context(next_context);
2508 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002509 skip |=
2510 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002511 }
John Zulauf7635de32020-05-29 17:14:15 -06002512 return skip;
2513}
John Zulauf64ffe552021-02-06 10:25:07 -07002514bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002515 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002516 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002517 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002518 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002519 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2520
2521 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002522 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002523 return skip;
2524}
2525
John Zulauf64ffe552021-02-06 10:25:07 -07002526AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002527 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002528}
2529
John Zulauf64ffe552021-02-06 10:25:07 -07002530bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2531 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002532 bool skip = false;
2533
John Zulauf7635de32020-05-29 17:14:15 -06002534 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2535 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2536 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2537 // to apply and only copy then, if this proves a hot spot.
2538 std::unique_ptr<AccessContext> proxy_for_current;
2539
John Zulauf355e49b2020-04-24 15:11:15 -06002540 // Validate the "finalLayout" transitions to external
2541 // Get them from where there we're hidding in the extra entry.
2542 const auto &final_transitions = rp_state_->subpass_transitions.back();
2543 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002544 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002545 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2546 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002547 auto *context = trackback.context;
2548
2549 if (transition.prev_pass == current_subpass_) {
2550 if (!proxy_for_current) {
2551 // 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 -07002552 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002553 }
2554 context = proxy_for_current.get();
2555 }
2556
John Zulaufa0a98292020-09-18 09:30:10 -06002557 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2558 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002559 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002560 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002561 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002562 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002563 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2564 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2565 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2566 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002567 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002568 }
2569 }
2570 return skip;
2571}
2572
John Zulauf14940722021-04-12 15:19:02 -06002573void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002574 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002575 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002576}
2577
John Zulauf14940722021-04-12 15:19:02 -06002578void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002579 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2580 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002581
2582 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2583 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002584 const AttachmentViewGen &view_gen = attachment_views_[i];
2585 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002586
2587 const auto &ci = attachment_ci[i];
2588 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002589 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002590 const bool is_color = !(has_depth || has_stencil);
2591
2592 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002593 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2594 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2595 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2596 SyncOrdering::kColorAttachment, tag);
2597 }
John Zulauf1507ee42020-05-18 11:33:09 -06002598 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002599 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002600 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2601 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2602 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2603 SyncOrdering::kDepthStencilAttachment, tag);
2604 }
John Zulauf1507ee42020-05-18 11:33:09 -06002605 }
2606 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002607 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2608 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2609 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2610 SyncOrdering::kDepthStencilAttachment, tag);
2611 }
John Zulauf1507ee42020-05-18 11:33:09 -06002612 }
2613 }
2614 }
2615 }
2616}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002617AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2618 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2619 AttachmentViewGenVector view_gens;
2620 VkExtent3D extent = CastTo3D(render_area.extent);
2621 VkOffset3D offset = CastTo3D(render_area.offset);
2622 view_gens.reserve(attachment_views.size());
2623 for (const auto *view : attachment_views) {
2624 view_gens.emplace_back(view, offset, extent);
2625 }
2626 return view_gens;
2627}
John Zulauf64ffe552021-02-06 10:25:07 -07002628RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2629 VkQueueFlags queue_flags,
2630 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2631 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002632 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002633 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002634 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002635 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002636 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002637 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002638 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002639}
John Zulauf14940722021-04-12 15:19:02 -06002640void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002641 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002642 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002643 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002644 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002645}
John Zulauf1507ee42020-05-18 11:33:09 -06002646
John Zulauf14940722021-04-12 15:19:02 -06002647void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag prev_subpass_tag, const ResourceUsageTag next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002648 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002649 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2650 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002651
ziga-lunarg31a3e772022-03-22 11:48:46 +01002652 if (current_subpass_ + 1 >= subpass_contexts_.size()) {
2653 return;
2654 }
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002655 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2656 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002657 current_subpass_++;
John Zulauffaea0ee2021-01-14 14:01:32 -07002658 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2659 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002660 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002661}
2662
John Zulauf14940722021-04-12 15:19:02 -06002663void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002664 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002665 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2666 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002667
John Zulauf355e49b2020-04-24 15:11:15 -06002668 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002669 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002670
2671 // Add the "finalLayout" transitions to external
2672 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002673 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2674 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2675 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002676 const auto &final_transitions = rp_state_->subpass_transitions.back();
2677 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002678 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002679 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002680 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002681 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002682 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002683 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002684 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002685 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002686 }
2687}
2688
Jeremy Gebben40a22942020-12-22 14:22:06 -07002689SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002690 SyncExecScope result;
2691 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002692 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2693 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002694 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2695 return result;
2696}
2697
Jeremy Gebben40a22942020-12-22 14:22:06 -07002698SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002699 SyncExecScope result;
2700 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002701 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2702 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002703 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2704 return result;
2705}
2706
2707SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002708 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002709 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002710 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002711 dst_access_scope = 0;
2712}
2713
2714template <typename Barrier>
2715SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002716 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002717 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002718 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002719 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2720}
2721
2722SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002723 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2724 if (barrier) {
2725 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002726 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002727 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002728
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002729 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002730 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002731 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2732
2733 } else {
2734 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002735 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002736 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2737
2738 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002739 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002740 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2741 }
2742}
2743
2744template <typename Barrier>
2745SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2746 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2747 src_exec_scope = src.exec_scope;
2748 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2749
2750 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002751 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002752 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002753}
2754
John Zulaufb02c1eb2020-10-06 16:33:36 -06002755// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2756void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2757 for (const auto &barrier : barriers) {
2758 ApplyBarrier(barrier, layout_transition);
2759 }
2760}
2761
John Zulauf89311b42020-09-29 16:28:47 -06002762// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2763// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2764// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002765void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002766 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002767 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002768 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002769 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002770 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002771 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002772 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002773}
John Zulauf9cb530d2019-09-30 14:14:10 -06002774HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2775 HazardResult hazard;
2776 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002777 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002778 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002779 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002780 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002781 }
2782 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002783 // Write operation:
2784 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2785 // If reads exists -- test only against them because either:
2786 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2787 // * 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
2788 // the current write happens after the reads, so just test the write against the reades
2789 // Otherwise test against last_write
2790 //
2791 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002792 if (last_reads.size()) {
2793 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002794 if (IsReadHazard(usage_stage, read_access)) {
2795 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2796 break;
2797 }
2798 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002799 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002800 // Write-After-Write check -- if we have a previous write to test against
2801 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002802 }
2803 }
2804 return hazard;
2805}
2806
John Zulauf4fa68462021-04-26 21:04:22 -06002807HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002808 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002809 return DetectHazard(usage_index, ordering);
2810}
2811
2812HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002813 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2814 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002815 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002816 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002817 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2818 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002819 if (IsRead(usage_bit)) {
2820 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2821 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2822 if (is_raw_hazard) {
2823 // NOTE: we know last_write is non-zero
2824 // See if the ordering rules save us from the simple RAW check above
2825 // First check to see if the current usage is covered by the ordering rules
2826 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2827 const bool usage_is_ordered =
2828 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2829 if (usage_is_ordered) {
2830 // Now see of the most recent write (or a subsequent read) are ordered
2831 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2832 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002833 }
2834 }
John Zulauf4285ee92020-09-23 10:20:52 -06002835 if (is_raw_hazard) {
2836 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2837 }
John Zulauf5c628d02021-05-04 15:46:36 -06002838 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2839 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2840 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002841 } else {
2842 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002843 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002844 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002845 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002846 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002847 if (usage_write_is_ordered) {
2848 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2849 ordered_stages = GetOrderedStages(ordering);
2850 }
2851 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2852 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002853 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002854 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2855 if (IsReadHazard(usage_stage, read_access)) {
2856 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2857 break;
2858 }
John Zulaufd14743a2020-07-03 09:42:39 -06002859 }
2860 }
John Zulauf2a344ca2021-09-09 17:07:19 -06002861 } else if (last_write.any() && !(last_write_is_ordered && usage_write_is_ordered)) {
2862 bool ilt_ilt_hazard = false;
2863 if ((usage_index == SYNC_IMAGE_LAYOUT_TRANSITION) && (usage_bit == last_write)) {
2864 // ILT after ILT is a special case where we check the 2nd access scope of the first ILT against the first access
2865 // scope of the second ILT, which has been passed (smuggled?) in the ordering barrier
2866 ilt_ilt_hazard = !(write_barriers & ordering.access_scope).any();
2867 }
2868 if (ilt_ilt_hazard || IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002869 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002870 }
John Zulauf69133422020-05-20 14:55:53 -06002871 }
2872 }
2873 return hazard;
2874}
2875
John Zulaufae842002021-04-15 18:20:55 -06002876HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2877 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002878 using Size = FirstAccesses::size_type;
2879 const auto &recorded_accesses = recorded_use.first_accesses_;
2880 Size count = recorded_accesses.size();
2881 if (count) {
2882 const auto &last_access = recorded_accesses.back();
2883 bool do_write_last = IsWrite(last_access.usage_index);
2884 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06002885
John Zulauf4fa68462021-04-26 21:04:22 -06002886 for (Size i = 0; i < count; ++count) {
2887 const auto &first = recorded_accesses[i];
2888 // Skip and quit logic
2889 if (first.tag < tag_range.begin) continue;
2890 if (first.tag >= tag_range.end) {
2891 do_write_last = false; // ignore last since we know it can't be in tag_range
2892 break;
2893 }
2894
2895 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2896 if (hazard.hazard) {
2897 hazard.AddRecordedAccess(first);
2898 break;
2899 }
2900 }
2901
2902 if (do_write_last && tag_range.includes(last_access.tag)) {
2903 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
2904 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
2905 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2906 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
2907 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
2908 // the barrier that applies them
2909 barrier |= recorded_use.first_write_layout_ordering_;
2910 }
2911 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
2912 // the active context
2913 if (recorded_use.first_read_stages_) {
2914 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
2915 // reads in the active context are not "most recent" as all recorded context operations are *after* them
2916 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
2917 // active context.
2918 barrier.exec_scope |= recorded_use.first_read_stages_;
2919 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
2920 barrier.access_scope |= FlagBit(last_access.usage_index);
2921 }
2922 hazard = DetectHazard(last_access.usage_index, barrier);
2923 if (hazard.hazard) {
2924 hazard.AddRecordedAccess(last_access);
2925 }
2926 }
John Zulaufae842002021-04-15 18:20:55 -06002927 }
2928 return hazard;
2929}
2930
John Zulauf2f952d22020-02-10 11:34:51 -07002931// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002932HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002933 HazardResult hazard;
2934 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002935 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2936 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2937 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002938 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002939 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002940 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002941 }
2942 } else {
John Zulauf14940722021-04-12 15:19:02 -06002943 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002944 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002945 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002946 // 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 -07002947 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002948 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002949 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002950 break;
2951 }
2952 }
John Zulauf2f952d22020-02-10 11:34:51 -07002953 }
2954 }
2955 return hazard;
2956}
2957
John Zulaufae842002021-04-15 18:20:55 -06002958HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2959 ResourceUsageTag start_tag) const {
2960 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002961 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06002962 // Skip and quit logic
2963 if (first.tag < tag_range.begin) continue;
2964 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06002965
2966 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06002967 if (hazard.hazard) {
2968 hazard.AddRecordedAccess(first);
2969 break;
2970 }
John Zulaufae842002021-04-15 18:20:55 -06002971 }
2972 return hazard;
2973}
2974
Jeremy Gebben40a22942020-12-22 14:22:06 -07002975HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002976 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002977 // Only supporting image layout transitions for now
2978 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2979 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002980 // only test for WAW if there no intervening read operations.
2981 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002982 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002983 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002984 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002985 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002986 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002987 break;
2988 }
2989 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002990 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2991 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2992 }
2993
2994 return hazard;
2995}
2996
Jeremy Gebben40a22942020-12-22 14:22:06 -07002997HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002998 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06002999 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07003000 // Only supporting image layout transitions for now
3001 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3002 HazardResult hazard;
3003 // only test for WAW if there no intervening read operations.
3004 // See DetectHazard(SyncStagetAccessIndex) above for more details.
3005
John Zulaufab7756b2020-12-29 16:10:16 -07003006 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003007 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
3008 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07003009 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06003010 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003011 // The read is in the events first synchronization scope, so we use a barrier hazard check
3012 // If the read stage is not in the src sync scope
3013 // *AND* not execution chained with an existing sync barrier (that's the or)
3014 // then the barrier access is unsafe (R/W after R)
3015 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3016 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3017 break;
3018 }
3019 } else {
3020 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3021 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3022 }
3023 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003024 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003025 // 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 -06003026 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003027 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3028 // So do a normal barrier hazard check
3029 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3030 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3031 }
3032 } else {
3033 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003034 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3035 }
John Zulaufd14743a2020-07-03 09:42:39 -06003036 }
John Zulauf361fb532020-07-22 10:45:39 -06003037
John Zulauf0cb5be22020-01-23 12:18:22 -07003038 return hazard;
3039}
3040
John Zulauf5f13a792020-03-10 07:31:21 -06003041// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3042// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3043// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3044void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003045 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003046 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3047 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003048 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003049 } else if (other.write_tag == write_tag) {
3050 // 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 -06003051 // dependency chaining logic or any stage expansion)
3052 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003053 pending_write_barriers |= other.pending_write_barriers;
3054 pending_layout_transition |= other.pending_layout_transition;
3055 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003056 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003057
John Zulaufd14743a2020-07-03 09:42:39 -06003058 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003059 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003060 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003061 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003062 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003063 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003064 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003065 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3066 // but we should wait on profiling data for that.
3067 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003068 auto &my_read = last_reads[my_read_index];
3069 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003070 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003071 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003072 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003073 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003074 my_read.pending_dep_chain = other_read.pending_dep_chain;
3075 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3076 // May require tracking more than one access per stage.
3077 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003078 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003079 // Since I'm overwriting the fragement stage read, also update the input attachment info
3080 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003081 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003082 }
John Zulauf14940722021-04-12 15:19:02 -06003083 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003084 // The read tags match so merge the barriers
3085 my_read.barriers |= other_read.barriers;
3086 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003087 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003088
John Zulauf5f13a792020-03-10 07:31:21 -06003089 break;
3090 }
3091 }
3092 } else {
3093 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003094 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003095 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003096 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003097 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003098 }
John Zulauf5f13a792020-03-10 07:31:21 -06003099 }
3100 }
John Zulauf361fb532020-07-22 10:45:39 -06003101 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003102 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3103 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003104
3105 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3106 // of the copy and other into this using the update first logic.
3107 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3108 // of the other first_accesses... )
3109 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3110 FirstAccesses firsts(std::move(first_accesses_));
3111 first_accesses_.clear();
3112 first_read_stages_ = 0U;
3113 auto a = firsts.begin();
3114 auto a_end = firsts.end();
3115 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003116 // TODO: Determine whether some tag offset will be needed for PHASE II
3117 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003118 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3119 ++a;
3120 }
3121 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3122 }
3123 for (; a != a_end; ++a) {
3124 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3125 }
3126 }
John Zulauf5f13a792020-03-10 07:31:21 -06003127}
3128
John Zulauf14940722021-04-12 15:19:02 -06003129void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003130 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3131 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003132 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003133 // Mulitple outstanding reads may be of interest and do dependency chains independently
3134 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3135 const auto usage_stage = PipelineStageBit(usage_index);
3136 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003137 for (auto &read_access : last_reads) {
3138 if (read_access.stage == usage_stage) {
3139 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003140 break;
3141 }
3142 }
3143 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003144 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003145 last_read_stages |= usage_stage;
3146 }
John Zulauf4285ee92020-09-23 10:20:52 -06003147
3148 // 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 -07003149 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003150 // TODO Revisit re: multiple reads for a given stage
3151 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003152 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003153 } else {
3154 // Assume write
3155 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003156 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003157 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003158 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003159}
John Zulauf5f13a792020-03-10 07:31:21 -06003160
John Zulauf89311b42020-09-29 16:28:47 -06003161// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3162// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3163// We can overwrite them as *this* write is now after them.
3164//
3165// 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 -06003166void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003167 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003168 last_read_stages = 0;
3169 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003170 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003171
3172 write_barriers = 0;
3173 write_dependency_chain = 0;
3174 write_tag = tag;
3175 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003176}
3177
John Zulauf89311b42020-09-29 16:28:47 -06003178// Apply the memory barrier without updating the existing barriers. The execution barrier
3179// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3180// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3181// replace the current write barriers or add to them, so accumulate to pending as well.
3182void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3183 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3184 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003185 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3186 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3187 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3188 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003189 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003190 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003191 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003192 if (layout_transition) {
3193 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3194 }
John Zulaufa0a98292020-09-18 09:30:10 -06003195 }
John Zulauf89311b42020-09-29 16:28:47 -06003196 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3197 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003198
John Zulauf89311b42020-09-29 16:28:47 -06003199 if (!pending_layout_transition) {
3200 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3201 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003202 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003203 // 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 -07003204 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3205 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003206 }
3207 }
John Zulaufa0a98292020-09-18 09:30:10 -06003208 }
John Zulaufa0a98292020-09-18 09:30:10 -06003209}
3210
John Zulauf4a6105a2020-11-17 15:11:05 -07003211// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3212// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003213void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003214 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3215 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3216 // in order to know if it's in the excecution scope
3217 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3218 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3219 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003220 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003221 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003222 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003223 if (layout_transition) {
3224 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3225 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003226 }
3227 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3228 pending_layout_transition |= layout_transition;
3229
3230 if (!pending_layout_transition) {
3231 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3232 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003233 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003234 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3235 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3236 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3237 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3238 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3239 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3240 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003241 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 -07003242 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003243 }
3244 }
3245 }
3246}
John Zulauf14940722021-04-12 15:19:02 -06003247void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003248 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003249 // 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 -06003250 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003251 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003252 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3253 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003254 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003255 }
John Zulauf89311b42020-09-29 16:28:47 -06003256
3257 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003258 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003259 for (auto &read_access : last_reads) {
3260 read_access.barriers |= read_access.pending_dep_chain;
3261 read_execution_barriers |= read_access.barriers;
3262 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003263 }
3264
3265 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3266 write_dependency_chain |= pending_write_dep_chain;
3267 write_barriers |= pending_write_barriers;
3268 pending_write_dep_chain = 0;
3269 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003270}
3271
John Zulaufae842002021-04-15 18:20:55 -06003272bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3273 if (!first_accesses_.size()) return false;
3274 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3275 return tag_range.intersects(first_access_range);
3276}
3277
John Zulauf59e25072020-07-17 10:55:21 -06003278// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003279VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3280 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003281
John Zulaufab7756b2020-12-29 16:10:16 -07003282 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003283 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003284 barriers = read_access.barriers;
3285 break;
John Zulauf59e25072020-07-17 10:55:21 -06003286 }
3287 }
John Zulauf4285ee92020-09-23 10:20:52 -06003288
John Zulauf59e25072020-07-17 10:55:21 -06003289 return barriers;
3290}
3291
Jeremy Gebben40a22942020-12-22 14:22:06 -07003292inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003293 assert(IsRead(usage));
3294 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3295 // * the previous reads are not hazards, and thus last_write must be visible and available to
3296 // any reads that happen after.
3297 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3298 // 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 -07003299 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003300}
3301
Jeremy Gebben40a22942020-12-22 14:22:06 -07003302VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003303 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003304 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003305 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003306 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003307 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003308 // 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 -07003309 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003310 }
3311
3312 return ordered_stages;
3313}
3314
John Zulauf14940722021-04-12 15:19:02 -06003315void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003316 // Only record until we record a write.
3317 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003318 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003319 if (0 == (usage_stage & first_read_stages_)) {
3320 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003321 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003322 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003323 if (0 == (read_execution_barriers & usage_stage)) {
3324 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3325 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3326 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003327 }
3328 }
3329}
3330
John Zulauf4fa68462021-04-26 21:04:22 -06003331void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3332 // Only call this after recording an image layout transition
3333 assert(first_accesses_.size());
3334 if (first_accesses_.back().tag == tag) {
3335 // 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 +02003336 assert(first_accesses_.back().usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
John Zulauf4fa68462021-04-26 21:04:22 -06003337 first_write_layout_ordering_ = layout_ordering;
3338 }
3339}
3340
John Zulaufd1f85d42020-04-15 12:23:15 -06003341void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003342 auto *access_context = GetAccessContextNoInsert(command_buffer);
3343 if (access_context) {
3344 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003345 }
3346}
3347
John Zulaufd1f85d42020-04-15 12:23:15 -06003348void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3349 auto access_found = cb_access_state.find(command_buffer);
3350 if (access_found != cb_access_state.end()) {
3351 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003352 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003353 cb_access_state.erase(access_found);
3354 }
3355}
3356
John Zulauf9cb530d2019-09-30 14:14:10 -06003357bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3358 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3359 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003360 const auto *cb_context = GetAccessContext(commandBuffer);
3361 assert(cb_context);
3362 if (!cb_context) return skip;
3363 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003364
John Zulauf3d84f1b2020-03-09 13:33:25 -06003365 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003366 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3367 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003368
3369 for (uint32_t region = 0; region < regionCount; region++) {
3370 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003371 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003372 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003373 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003374 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003375 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003376 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003377 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003378 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003379 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003380 }
John Zulauf16adfc92020-04-08 10:28:33 -06003381 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003382 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003383 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003384 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003385 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003386 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003387 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003388 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003389 }
3390 }
3391 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003392 }
3393 return skip;
3394}
3395
3396void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3397 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003398 auto *cb_context = GetAccessContext(commandBuffer);
3399 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003400 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003401 auto *context = cb_context->GetCurrentAccessContext();
3402
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003403 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3404 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003405
3406 for (uint32_t region = 0; region < regionCount; region++) {
3407 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003408 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003409 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003410 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003411 }
John Zulauf16adfc92020-04-08 10:28:33 -06003412 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003413 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003414 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003415 }
3416 }
3417}
3418
John Zulauf4a6105a2020-11-17 15:11:05 -07003419void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3420 // Clear out events from the command buffer contexts
3421 for (auto &cb_context : cb_access_state) {
3422 cb_context.second->RecordDestroyEvent(event);
3423 }
3424}
3425
Tony-LunarGef035472021-11-02 10:23:33 -06003426bool SyncValidator::ValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos,
3427 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003428 bool skip = false;
3429 const auto *cb_context = GetAccessContext(commandBuffer);
3430 assert(cb_context);
3431 if (!cb_context) return skip;
3432 const auto *context = cb_context->GetCurrentAccessContext();
Tony-LunarGef035472021-11-02 10:23:33 -06003433 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003434
3435 // If we have no previous accesses, we have no hazards
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003436 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3437 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003438
3439 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3440 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3441 if (src_buffer) {
3442 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003443 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003444 if (hazard.hazard) {
3445 // TODO -- add tag information to log msg when useful.
3446 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003447 "%s(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003448 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003449 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003450 }
3451 }
3452 if (dst_buffer && !skip) {
3453 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003454 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003455 if (hazard.hazard) {
3456 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
Tony-LunarGef035472021-11-02 10:23:33 -06003457 "%s(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003458 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003459 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003460 }
3461 }
3462 if (skip) break;
3463 }
3464 return skip;
3465}
3466
Tony-LunarGef035472021-11-02 10:23:33 -06003467bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3468 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3469 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3470}
3471
3472bool SyncValidator::PreCallValidateCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) const {
3473 return ValidateCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3474}
3475
3476void SyncValidator::RecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003477 auto *cb_context = GetAccessContext(commandBuffer);
3478 assert(cb_context);
Tony-LunarGef035472021-11-02 10:23:33 -06003479 const auto tag = cb_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003480 auto *context = cb_context->GetCurrentAccessContext();
3481
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003482 auto src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3483 auto dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
Jeff Leger178b1e52020-10-05 12:22:23 -04003484
3485 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3486 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3487 if (src_buffer) {
3488 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003489 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003490 }
3491 if (dst_buffer) {
3492 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003493 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003494 }
3495 }
3496}
3497
Tony-LunarGef035472021-11-02 10:23:33 -06003498void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3499 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2KHR);
3500}
3501
3502void SyncValidator::PreCallRecordCmdCopyBuffer2(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2 *pCopyBufferInfos) {
3503 RecordCmdCopyBuffer2(commandBuffer, pCopyBufferInfos, CMD_COPYBUFFER2);
3504}
3505
John Zulauf5c5e88d2019-12-26 11:22:02 -07003506bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3507 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3508 const VkImageCopy *pRegions) const {
3509 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003510 const auto *cb_access_context = GetAccessContext(commandBuffer);
3511 assert(cb_access_context);
3512 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003513
John Zulauf3d84f1b2020-03-09 13:33:25 -06003514 const auto *context = cb_access_context->GetCurrentAccessContext();
3515 assert(context);
3516 if (!context) return skip;
3517
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003518 auto src_image = Get<IMAGE_STATE>(srcImage);
3519 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003520 for (uint32_t region = 0; region < regionCount; region++) {
3521 const auto &copy_region = pRegions[region];
3522 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003523 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003524 copy_region.srcOffset, copy_region.extent);
3525 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003526 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003527 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003528 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003529 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003530 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003531 }
3532
3533 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003534 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003535 copy_region.dstOffset, copy_region.extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003536 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003537 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003538 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003539 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003540 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003541 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003542 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003543 }
3544 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003545
John Zulauf5c5e88d2019-12-26 11:22:02 -07003546 return skip;
3547}
3548
3549void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3550 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3551 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003552 auto *cb_access_context = GetAccessContext(commandBuffer);
3553 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003554 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003555 auto *context = cb_access_context->GetCurrentAccessContext();
3556 assert(context);
3557
Jeremy Gebben9f537102021-10-05 16:37:12 -06003558 auto src_image = Get<IMAGE_STATE>(srcImage);
3559 auto dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003560
3561 for (uint32_t region = 0; region < regionCount; region++) {
3562 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003563 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003564 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003565 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003566 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003567 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003568 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003569 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003570 }
3571 }
3572}
3573
Tony-LunarGb61514a2021-11-02 12:36:51 -06003574bool SyncValidator::ValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo,
3575 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04003576 bool skip = false;
3577 const auto *cb_access_context = GetAccessContext(commandBuffer);
3578 assert(cb_access_context);
3579 if (!cb_access_context) return skip;
3580
3581 const auto *context = cb_access_context->GetCurrentAccessContext();
3582 assert(context);
3583 if (!context) return skip;
3584
Tony-LunarGb61514a2021-11-02 12:36:51 -06003585 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003586 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3587 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003588
Jeff Leger178b1e52020-10-05 12:22:23 -04003589 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3590 const auto &copy_region = pCopyImageInfo->pRegions[region];
3591 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003592 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003593 copy_region.srcOffset, copy_region.extent);
3594 if (hazard.hazard) {
3595 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003596 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003597 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003598 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003599 }
3600 }
3601
3602 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003603 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
ziga-lunarg73746512022-03-23 23:08:17 +01003604 copy_region.dstOffset, copy_region.extent);
Jeff Leger178b1e52020-10-05 12:22:23 -04003605 if (hazard.hazard) {
3606 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
sfricke-samsung71f04e32022-03-16 01:21:21 -05003607 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04003608 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003609 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003610 }
3611 if (skip) break;
3612 }
3613 }
3614
3615 return skip;
3616}
3617
Tony-LunarGb61514a2021-11-02 12:36:51 -06003618bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3619 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3620 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3621}
3622
3623bool SyncValidator::PreCallValidateCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) const {
3624 return ValidateCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3625}
3626
3627void SyncValidator::RecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo, CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04003628 auto *cb_access_context = GetAccessContext(commandBuffer);
3629 assert(cb_access_context);
Tony-LunarGb61514a2021-11-02 12:36:51 -06003630 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003631 auto *context = cb_access_context->GetCurrentAccessContext();
3632 assert(context);
3633
Jeremy Gebben9f537102021-10-05 16:37:12 -06003634 auto src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3635 auto dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04003636
3637 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3638 const auto &copy_region = pCopyImageInfo->pRegions[region];
3639 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003640 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003641 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003642 }
3643 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003644 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
ziga-lunarg73746512022-03-23 23:08:17 +01003645 copy_region.dstSubresource, copy_region.dstOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003646 }
3647 }
3648}
3649
Tony-LunarGb61514a2021-11-02 12:36:51 -06003650void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3651 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2KHR);
3652}
3653
3654void SyncValidator::PreCallRecordCmdCopyImage2(VkCommandBuffer commandBuffer, const VkCopyImageInfo2 *pCopyImageInfo) {
3655 RecordCmdCopyImage2(commandBuffer, pCopyImageInfo, CMD_COPYIMAGE2);
3656}
3657
John Zulauf9cb530d2019-09-30 14:14:10 -06003658bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3659 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3660 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3661 uint32_t bufferMemoryBarrierCount,
3662 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3663 uint32_t imageMemoryBarrierCount,
3664 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3665 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003666 const auto *cb_access_context = GetAccessContext(commandBuffer);
3667 assert(cb_access_context);
3668 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003669
John Zulauf36ef9282021-02-02 11:47:24 -07003670 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3671 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3672 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3673 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003674 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003675 return skip;
3676}
3677
3678void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3679 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3680 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3681 uint32_t bufferMemoryBarrierCount,
3682 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3683 uint32_t imageMemoryBarrierCount,
3684 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003685 auto *cb_access_context = GetAccessContext(commandBuffer);
3686 assert(cb_access_context);
3687 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003688
John Zulauf1bf30522021-09-03 15:39:06 -06003689 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(),
3690 srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount,
3691 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
3692 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf9cb530d2019-09-30 14:14:10 -06003693}
3694
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003695bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3696 const VkDependencyInfoKHR *pDependencyInfo) const {
3697 bool skip = false;
3698 const auto *cb_access_context = GetAccessContext(commandBuffer);
3699 assert(cb_access_context);
3700 if (!cb_access_context) return skip;
3701
3702 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3703 skip = pipeline_barrier.Validate(*cb_access_context);
3704 return skip;
3705}
3706
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003707bool SyncValidator::PreCallValidateCmdPipelineBarrier2(VkCommandBuffer commandBuffer,
3708 const VkDependencyInfo *pDependencyInfo) const {
3709 bool skip = false;
3710 const auto *cb_access_context = GetAccessContext(commandBuffer);
3711 assert(cb_access_context);
3712 if (!cb_access_context) return skip;
3713
3714 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3715 skip = pipeline_barrier.Validate(*cb_access_context);
3716 return skip;
3717}
3718
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003719void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3720 auto *cb_access_context = GetAccessContext(commandBuffer);
3721 assert(cb_access_context);
3722 if (!cb_access_context) return;
3723
John Zulauf1bf30522021-09-03 15:39:06 -06003724 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(),
3725 *pDependencyInfo);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003726}
3727
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07003728void SyncValidator::PreCallRecordCmdPipelineBarrier2(VkCommandBuffer commandBuffer, const VkDependencyInfo *pDependencyInfo) {
3729 auto *cb_access_context = GetAccessContext(commandBuffer);
3730 assert(cb_access_context);
3731 if (!cb_access_context) return;
3732
3733 cb_access_context->RecordSyncOp<SyncOpPipelineBarrier>(CMD_PIPELINEBARRIER2, *this, cb_access_context->GetQueueFlags(),
3734 *pDependencyInfo);
3735}
3736
John Zulauf9cb530d2019-09-30 14:14:10 -06003737void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3738 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3739 // The state tracker sets up the device state
3740 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3741
John Zulauf5f13a792020-03-10 07:31:21 -06003742 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3743 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003744 // TODO: Find a good way to do this hooklessly.
3745 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3746 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3747 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3748
John Zulaufd1f85d42020-04-15 12:23:15 -06003749 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3750 sync_device_state->ResetCommandBufferCallback(command_buffer);
3751 });
3752 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3753 sync_device_state->FreeCommandBufferCallback(command_buffer);
3754 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003755}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003756
John Zulauf355e49b2020-04-24 15:11:15 -06003757bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003758 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003759 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003760 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003761 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003762 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003763 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003764 }
John Zulauf355e49b2020-04-24 15:11:15 -06003765 return skip;
3766}
3767
3768bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3769 VkSubpassContents contents) const {
3770 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003771 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003772 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003773 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003774 return skip;
3775}
3776
3777bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003778 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003779 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003780 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003781 return skip;
3782}
3783
3784bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3785 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003786 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003787 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003788 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003789 return skip;
3790}
3791
John Zulauf3d84f1b2020-03-09 13:33:25 -06003792void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3793 VkResult result) {
3794 // The state tracker sets up the command buffer state
3795 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3796
3797 // Create/initialize the structure that trackers accesses at the command buffer scope.
3798 auto cb_access_context = GetAccessContext(commandBuffer);
3799 assert(cb_access_context);
3800 cb_access_context->Reset();
3801}
3802
3803void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003804 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003805 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003806 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003807 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003808 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003809 }
3810}
3811
3812void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3813 VkSubpassContents contents) {
3814 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003815 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003816 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003817 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003818}
3819
3820void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3821 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3822 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003823 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003824}
3825
3826void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3827 const VkRenderPassBeginInfo *pRenderPassBegin,
3828 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3829 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003830 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003831}
3832
Mike Schuchardt2df08912020-12-15 16:28:09 -08003833bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003834 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003835 bool skip = false;
3836
3837 auto cb_context = GetAccessContext(commandBuffer);
3838 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003839 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003840 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003841 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003842}
3843
3844bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3845 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003846 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003847 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003848 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003849 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3850 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003851 return skip;
3852}
3853
Mike Schuchardt2df08912020-12-15 16:28:09 -08003854bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3855 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003856 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003857 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003858 return skip;
3859}
3860
3861bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3862 const VkSubpassEndInfo *pSubpassEndInfo) const {
3863 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003864 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003865 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003866}
3867
3868void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003869 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003870 auto cb_context = GetAccessContext(commandBuffer);
3871 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003872 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003873
sfricke-samsung85584a72021-09-30 21:43:38 -07003874 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003875 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003876}
3877
3878void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3879 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003880 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003881 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003882 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003883}
3884
3885void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3886 const VkSubpassEndInfo *pSubpassEndInfo) {
3887 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003888 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003889}
3890
3891void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3892 const VkSubpassEndInfo *pSubpassEndInfo) {
3893 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003894 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003895}
3896
sfricke-samsung85584a72021-09-30 21:43:38 -07003897bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3898 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003899 bool skip = false;
3900
3901 auto cb_context = GetAccessContext(commandBuffer);
3902 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003903 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003904
sfricke-samsung85584a72021-09-30 21:43:38 -07003905 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003906 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003907 return skip;
3908}
3909
3910bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3911 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003912 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003913 return skip;
3914}
3915
Mike Schuchardt2df08912020-12-15 16:28:09 -08003916bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003917 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003918 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003919 return skip;
3920}
3921
3922bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003923 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003924 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003925 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003926 return skip;
3927}
3928
sfricke-samsung85584a72021-09-30 21:43:38 -07003929void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003930 // Resolve the all subpass contexts to the command buffer contexts
3931 auto cb_context = GetAccessContext(commandBuffer);
3932 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003933 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003934
sfricke-samsung85584a72021-09-30 21:43:38 -07003935 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003936 sync_op.Record(cb_context);
3937 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003938}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003939
John Zulauf33fc1d52020-07-17 11:01:10 -06003940// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3941// updates to a resource which do not conflict at the byte level.
3942// TODO: Revisit this rule to see if it needs to be tighter or looser
3943// TODO: Add programatic control over suppression heuristics
3944bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3945 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3946}
3947
John Zulauf3d84f1b2020-03-09 13:33:25 -06003948void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003949 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003950 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003951}
3952
3953void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003954 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003955 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003956}
3957
3958void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003959 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003960 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003961}
locke-lunarga19c71d2020-03-02 18:17:04 -07003962
sfricke-samsung71f04e32022-03-16 01:21:21 -05003963template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04003964bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05003965 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
3966 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003967 bool skip = false;
3968 const auto *cb_access_context = GetAccessContext(commandBuffer);
3969 assert(cb_access_context);
3970 if (!cb_access_context) return skip;
3971
Tony Barbour845d29b2021-11-09 11:43:14 -07003972 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04003973
locke-lunarga19c71d2020-03-02 18:17:04 -07003974 const auto *context = cb_access_context->GetCurrentAccessContext();
3975 assert(context);
3976 if (!context) return skip;
3977
Jeremy Gebbenf4449392022-01-28 10:09:10 -07003978 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
3979 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003980
3981 for (uint32_t region = 0; region < regionCount; region++) {
3982 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003983 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003984 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003985 if (src_buffer) {
3986 ResourceAccessRange src_range =
3987 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003988 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003989 if (hazard.hazard) {
3990 // PHASE1 TODO -- add tag information to log msg when useful.
3991 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3992 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3993 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003994 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003995 }
3996 }
3997
Jeremy Gebben40a22942020-12-22 14:22:06 -07003998 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003999 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004000 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004001 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004002 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004003 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004004 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004005 }
4006 if (skip) break;
4007 }
4008 if (skip) break;
4009 }
4010 return skip;
4011}
4012
Jeff Leger178b1e52020-10-05 12:22:23 -04004013bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4014 VkImageLayout dstImageLayout, uint32_t regionCount,
4015 const VkBufferImageCopy *pRegions) const {
4016 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
Tony Barbour845d29b2021-11-09 11:43:14 -07004017 CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004018}
4019
4020bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4021 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
4022 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4023 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004024 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4025}
4026
4027bool SyncValidator::PreCallValidateCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4028 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) const {
4029 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4030 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4031 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004032}
4033
sfricke-samsung71f04e32022-03-16 01:21:21 -05004034template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004035void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004036 VkImageLayout dstImageLayout, uint32_t regionCount, const RegionType *pRegions,
4037 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004038 auto *cb_access_context = GetAccessContext(commandBuffer);
4039 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004040
Jeff Leger178b1e52020-10-05 12:22:23 -04004041 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004042 auto *context = cb_access_context->GetCurrentAccessContext();
4043 assert(context);
4044
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004045 auto src_buffer = Get<BUFFER_STATE>(srcBuffer);
4046 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004047
4048 for (uint32_t region = 0; region < regionCount; region++) {
4049 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07004050 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07004051 if (src_buffer) {
4052 ResourceAccessRange src_range =
4053 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004054 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004055 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07004056 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004057 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004058 }
4059 }
4060}
4061
Jeff Leger178b1e52020-10-05 12:22:23 -04004062void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4063 VkImageLayout dstImageLayout, uint32_t regionCount,
4064 const VkBufferImageCopy *pRegions) {
4065 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
Tony Barbour845d29b2021-11-09 11:43:14 -07004066 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, CMD_COPYBUFFERTOIMAGE);
Jeff Leger178b1e52020-10-05 12:22:23 -04004067}
4068
4069void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4070 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4071 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4072 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4073 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
Tony Barbour845d29b2021-11-09 11:43:14 -07004074 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2KHR);
4075}
4076
4077void SyncValidator::PreCallRecordCmdCopyBufferToImage2(VkCommandBuffer commandBuffer,
4078 const VkCopyBufferToImageInfo2 *pCopyBufferToImageInfo) {
4079 StateTracker::PreCallRecordCmdCopyBufferToImage2(commandBuffer, pCopyBufferToImageInfo);
4080 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4081 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4082 pCopyBufferToImageInfo->pRegions, CMD_COPYBUFFERTOIMAGE2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004083}
4084
sfricke-samsung71f04e32022-03-16 01:21:21 -05004085template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004086bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004087 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
4088 CMD_TYPE cmd_type) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004089 bool skip = false;
4090 const auto *cb_access_context = GetAccessContext(commandBuffer);
4091 assert(cb_access_context);
4092 if (!cb_access_context) return skip;
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004093 const char *func_name = CommandTypeString(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04004094
locke-lunarga19c71d2020-03-02 18:17:04 -07004095 const auto *context = cb_access_context->GetCurrentAccessContext();
4096 assert(context);
4097 if (!context) return skip;
4098
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004099 auto src_image = Get<IMAGE_STATE>(srcImage);
4100 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004101 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004102 for (uint32_t region = 0; region < regionCount; region++) {
4103 const auto &copy_region = pRegions[region];
4104 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004105 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004106 copy_region.imageOffset, copy_region.imageExtent);
4107 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004108 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004109 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004110 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004111 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004112 }
John Zulauf477700e2021-01-06 11:41:49 -07004113 if (dst_mem) {
4114 ResourceAccessRange dst_range =
4115 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004116 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004117 if (hazard.hazard) {
4118 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4119 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4120 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004121 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004122 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004123 }
4124 }
4125 if (skip) break;
4126 }
4127 return skip;
4128}
4129
Jeff Leger178b1e52020-10-05 12:22:23 -04004130bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4131 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4132 const VkBufferImageCopy *pRegions) const {
4133 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004134 CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004135}
4136
4137bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4138 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4139 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4140 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004141 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4142}
4143
4144bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4145 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) const {
4146 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4147 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4148 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004149}
4150
sfricke-samsung71f04e32022-03-16 01:21:21 -05004151template <typename RegionType>
Jeff Leger178b1e52020-10-05 12:22:23 -04004152void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
sfricke-samsung71f04e32022-03-16 01:21:21 -05004153 VkBuffer dstBuffer, uint32_t regionCount, const RegionType *pRegions,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004154 CMD_TYPE cmd_type) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004155 auto *cb_access_context = GetAccessContext(commandBuffer);
4156 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004157
Jeff Leger178b1e52020-10-05 12:22:23 -04004158 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004159 auto *context = cb_access_context->GetCurrentAccessContext();
4160 assert(context);
4161
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004162 auto src_image = Get<IMAGE_STATE>(srcImage);
Jeremy Gebben9f537102021-10-05 16:37:12 -06004163 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004164 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004165 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004166
4167 for (uint32_t region = 0; region < regionCount; region++) {
4168 const auto &copy_region = pRegions[region];
4169 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004170 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004171 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004172 if (dst_buffer) {
4173 ResourceAccessRange dst_range =
4174 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004175 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004176 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004177 }
4178 }
4179}
4180
Jeff Leger178b1e52020-10-05 12:22:23 -04004181void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4182 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4183 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004184 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, CMD_COPYIMAGETOBUFFER);
Jeff Leger178b1e52020-10-05 12:22:23 -04004185}
4186
4187void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4188 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4189 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4190 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4191 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
Tony-LunarGaf3632a2021-11-10 15:51:57 -07004192 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2KHR);
4193}
4194
4195void SyncValidator::PreCallRecordCmdCopyImageToBuffer2(VkCommandBuffer commandBuffer,
4196 const VkCopyImageToBufferInfo2 *pCopyImageToBufferInfo) {
4197 StateTracker::PreCallRecordCmdCopyImageToBuffer2(commandBuffer, pCopyImageToBufferInfo);
4198 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4199 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4200 pCopyImageToBufferInfo->pRegions, CMD_COPYIMAGETOBUFFER2);
Jeff Leger178b1e52020-10-05 12:22:23 -04004201}
4202
4203template <typename RegionType>
4204bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4205 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4206 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004207 bool skip = false;
4208 const auto *cb_access_context = GetAccessContext(commandBuffer);
4209 assert(cb_access_context);
4210 if (!cb_access_context) return skip;
4211
4212 const auto *context = cb_access_context->GetCurrentAccessContext();
4213 assert(context);
4214 if (!context) return skip;
4215
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004216 auto src_image = Get<IMAGE_STATE>(srcImage);
4217 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004218
4219 for (uint32_t region = 0; region < regionCount; region++) {
4220 const auto &blit_region = pRegions[region];
4221 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004222 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4223 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4224 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4225 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4226 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4227 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004228 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004229 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004230 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004231 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004232 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004233 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004234 }
4235 }
4236
4237 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004238 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4239 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4240 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4241 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4242 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4243 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004244 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004245 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004246 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004247 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004248 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004249 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004250 }
4251 if (skip) break;
4252 }
4253 }
4254
4255 return skip;
4256}
4257
Jeff Leger178b1e52020-10-05 12:22:23 -04004258bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4259 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4260 const VkImageBlit *pRegions, VkFilter filter) const {
4261 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4262 "vkCmdBlitImage");
4263}
4264
4265bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4266 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4267 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4268 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4269 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4270}
4271
Tony-LunarG542ae912021-11-04 16:06:44 -06004272bool SyncValidator::PreCallValidateCmdBlitImage2(VkCommandBuffer commandBuffer,
4273 const VkBlitImageInfo2 *pBlitImageInfo) const {
4274 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4275 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4276 pBlitImageInfo->filter, "vkCmdBlitImage2");
4277}
4278
Jeff Leger178b1e52020-10-05 12:22:23 -04004279template <typename RegionType>
4280void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4281 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4282 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004283 auto *cb_access_context = GetAccessContext(commandBuffer);
4284 assert(cb_access_context);
4285 auto *context = cb_access_context->GetCurrentAccessContext();
4286 assert(context);
4287
Jeremy Gebben9f537102021-10-05 16:37:12 -06004288 auto src_image = Get<IMAGE_STATE>(srcImage);
4289 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004290
4291 for (uint32_t region = 0; region < regionCount; region++) {
4292 const auto &blit_region = pRegions[region];
4293 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004294 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4295 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4296 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4297 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4298 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4299 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004300 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004301 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004302 }
4303 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004304 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4305 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4306 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4307 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4308 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4309 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004310 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004311 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004312 }
4313 }
4314}
locke-lunarg36ba2592020-04-03 09:42:04 -06004315
Jeff Leger178b1e52020-10-05 12:22:23 -04004316void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4317 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4318 const VkImageBlit *pRegions, VkFilter filter) {
4319 auto *cb_access_context = GetAccessContext(commandBuffer);
4320 assert(cb_access_context);
4321 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4322 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4323 pRegions, filter);
4324 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4325}
4326
4327void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4328 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4329 auto *cb_access_context = GetAccessContext(commandBuffer);
4330 assert(cb_access_context);
4331 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4332 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4333 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4334 pBlitImageInfo->filter, tag);
4335}
4336
Tony-LunarG542ae912021-11-04 16:06:44 -06004337void SyncValidator::PreCallRecordCmdBlitImage2(VkCommandBuffer commandBuffer, const VkBlitImageInfo2 *pBlitImageInfo) {
4338 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4339 auto *cb_access_context = GetAccessContext(commandBuffer);
4340 assert(cb_access_context);
4341 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2);
4342 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4343 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4344 pBlitImageInfo->filter, tag);
4345}
4346
John Zulauffaea0ee2021-01-14 14:01:32 -07004347bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4348 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4349 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4350 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004351 bool skip = false;
4352 if (drawCount == 0) return skip;
4353
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004354 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004355 VkDeviceSize size = struct_size;
4356 if (drawCount == 1 || stride == size) {
4357 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004358 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004359 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4360 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004361 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004362 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004363 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004364 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004365 }
4366 } else {
4367 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004368 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004369 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4370 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004371 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004372 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4373 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004374 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004375 break;
4376 }
4377 }
4378 }
4379 return skip;
4380}
4381
John Zulauf14940722021-04-12 15:19:02 -06004382void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004383 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4384 uint32_t stride) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004385 auto buf_state = Get<BUFFER_STATE>(buffer);
locke-lunargff255f92020-05-13 18:53:52 -06004386 VkDeviceSize size = struct_size;
4387 if (drawCount == 1 || stride == size) {
4388 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004389 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004390 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004391 } else {
4392 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004393 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004394 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4395 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004396 }
4397 }
4398}
4399
John Zulauffaea0ee2021-01-14 14:01:32 -07004400bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4401 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4402 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004403 bool skip = false;
4404
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004405 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004406 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004407 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4408 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004409 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004410 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004411 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004412 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004413 }
4414 return skip;
4415}
4416
John Zulauf14940722021-04-12 15:19:02 -06004417void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004418 auto count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004419 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004420 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004421}
4422
locke-lunarg36ba2592020-04-03 09:42:04 -06004423bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004424 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004425 const auto *cb_access_context = GetAccessContext(commandBuffer);
4426 assert(cb_access_context);
4427 if (!cb_access_context) return skip;
4428
locke-lunarg61870c22020-06-09 14:51:50 -06004429 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004430 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004431}
4432
4433void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004434 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004435 auto *cb_access_context = GetAccessContext(commandBuffer);
4436 assert(cb_access_context);
4437 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004438
locke-lunarg61870c22020-06-09 14:51:50 -06004439 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004440}
locke-lunarge1a67022020-04-29 00:15:36 -06004441
4442bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004443 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004444 const auto *cb_access_context = GetAccessContext(commandBuffer);
4445 assert(cb_access_context);
4446 if (!cb_access_context) return skip;
4447
4448 const auto *context = cb_access_context->GetCurrentAccessContext();
4449 assert(context);
4450 if (!context) return skip;
4451
locke-lunarg61870c22020-06-09 14:51:50 -06004452 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004453 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4454 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004455 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004456}
4457
4458void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004459 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004460 auto *cb_access_context = GetAccessContext(commandBuffer);
4461 assert(cb_access_context);
4462 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4463 auto *context = cb_access_context->GetCurrentAccessContext();
4464 assert(context);
4465
locke-lunarg61870c22020-06-09 14:51:50 -06004466 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4467 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004468}
4469
4470bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4471 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004472 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004473 const auto *cb_access_context = GetAccessContext(commandBuffer);
4474 assert(cb_access_context);
4475 if (!cb_access_context) return skip;
4476
locke-lunarg61870c22020-06-09 14:51:50 -06004477 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4478 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4479 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004480 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004481}
4482
4483void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4484 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004485 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004486 auto *cb_access_context = GetAccessContext(commandBuffer);
4487 assert(cb_access_context);
4488 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004489
locke-lunarg61870c22020-06-09 14:51:50 -06004490 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4491 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4492 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004493}
4494
4495bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4496 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004497 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004498 const auto *cb_access_context = GetAccessContext(commandBuffer);
4499 assert(cb_access_context);
4500 if (!cb_access_context) return skip;
4501
locke-lunarg61870c22020-06-09 14:51:50 -06004502 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4503 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4504 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004505 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004506}
4507
4508void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4509 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004510 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004511 auto *cb_access_context = GetAccessContext(commandBuffer);
4512 assert(cb_access_context);
4513 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004514
locke-lunarg61870c22020-06-09 14:51:50 -06004515 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4516 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4517 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004518}
4519
4520bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4521 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004522 bool skip = false;
4523 if (drawCount == 0) return skip;
4524
locke-lunargff255f92020-05-13 18:53:52 -06004525 const auto *cb_access_context = GetAccessContext(commandBuffer);
4526 assert(cb_access_context);
4527 if (!cb_access_context) return skip;
4528
4529 const auto *context = cb_access_context->GetCurrentAccessContext();
4530 assert(context);
4531 if (!context) return skip;
4532
locke-lunarg61870c22020-06-09 14:51:50 -06004533 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4534 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004535 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4536 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004537
4538 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4539 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4540 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004541 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004542 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004543}
4544
4545void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4546 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004547 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004548 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004549 auto *cb_access_context = GetAccessContext(commandBuffer);
4550 assert(cb_access_context);
4551 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4552 auto *context = cb_access_context->GetCurrentAccessContext();
4553 assert(context);
4554
locke-lunarg61870c22020-06-09 14:51:50 -06004555 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4556 cb_access_context->RecordDrawSubpassAttachment(tag);
4557 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004558
4559 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4560 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4561 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004562 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004563}
4564
4565bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4566 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004567 bool skip = false;
4568 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004569 const auto *cb_access_context = GetAccessContext(commandBuffer);
4570 assert(cb_access_context);
4571 if (!cb_access_context) return skip;
4572
4573 const auto *context = cb_access_context->GetCurrentAccessContext();
4574 assert(context);
4575 if (!context) return skip;
4576
locke-lunarg61870c22020-06-09 14:51:50 -06004577 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4578 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004579 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4580 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004581
4582 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4583 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4584 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004585 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004586 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004587}
4588
4589void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4590 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004591 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004592 auto *cb_access_context = GetAccessContext(commandBuffer);
4593 assert(cb_access_context);
4594 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4595 auto *context = cb_access_context->GetCurrentAccessContext();
4596 assert(context);
4597
locke-lunarg61870c22020-06-09 14:51:50 -06004598 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4599 cb_access_context->RecordDrawSubpassAttachment(tag);
4600 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004601
4602 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4603 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4604 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004605 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004606}
4607
4608bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4609 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4610 uint32_t stride, const char *function) const {
4611 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004612 const auto *cb_access_context = GetAccessContext(commandBuffer);
4613 assert(cb_access_context);
4614 if (!cb_access_context) return skip;
4615
4616 const auto *context = cb_access_context->GetCurrentAccessContext();
4617 assert(context);
4618 if (!context) return skip;
4619
locke-lunarg61870c22020-06-09 14:51:50 -06004620 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4621 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004622 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4623 maxDrawCount, stride, function);
4624 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004625
4626 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4627 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4628 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004629 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004630 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004631}
4632
4633bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4634 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4635 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004636 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4637 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004638}
4639
sfricke-samsung85584a72021-09-30 21:43:38 -07004640void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4641 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4642 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004643 auto *cb_access_context = GetAccessContext(commandBuffer);
4644 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004645 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004646 auto *context = cb_access_context->GetCurrentAccessContext();
4647 assert(context);
4648
locke-lunarg61870c22020-06-09 14:51:50 -06004649 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4650 cb_access_context->RecordDrawSubpassAttachment(tag);
4651 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4652 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004653
4654 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4655 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4656 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004657 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004658}
4659
sfricke-samsung85584a72021-09-30 21:43:38 -07004660void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4661 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4662 uint32_t stride) {
4663 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4664 stride);
4665 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4666 CMD_DRAWINDIRECTCOUNT);
4667}
locke-lunarge1a67022020-04-29 00:15:36 -06004668bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4669 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4670 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004671 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4672 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004673}
4674
4675void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4676 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4677 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004678 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4679 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004680 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4681 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004682}
4683
4684bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4685 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4686 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004687 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4688 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004689}
4690
4691void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4692 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4693 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004694 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4695 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004696 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4697 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004698}
4699
4700bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4701 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4702 uint32_t stride, const char *function) const {
4703 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004704 const auto *cb_access_context = GetAccessContext(commandBuffer);
4705 assert(cb_access_context);
4706 if (!cb_access_context) return skip;
4707
4708 const auto *context = cb_access_context->GetCurrentAccessContext();
4709 assert(context);
4710 if (!context) return skip;
4711
locke-lunarg61870c22020-06-09 14:51:50 -06004712 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4713 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004714 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4715 offset, maxDrawCount, stride, function);
4716 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004717
4718 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4719 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4720 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004721 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004722 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004723}
4724
4725bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4726 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4727 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004728 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4729 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004730}
4731
sfricke-samsung85584a72021-09-30 21:43:38 -07004732void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4733 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4734 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004735 auto *cb_access_context = GetAccessContext(commandBuffer);
4736 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004737 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004738 auto *context = cb_access_context->GetCurrentAccessContext();
4739 assert(context);
4740
locke-lunarg61870c22020-06-09 14:51:50 -06004741 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4742 cb_access_context->RecordDrawSubpassAttachment(tag);
4743 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4744 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004745
4746 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4747 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004748 // We will update the index and vertex buffer in SubmitQueue in the future.
4749 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004750}
4751
sfricke-samsung85584a72021-09-30 21:43:38 -07004752void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4753 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4754 uint32_t maxDrawCount, uint32_t stride) {
4755 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4756 maxDrawCount, stride);
4757 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4758 CMD_DRAWINDEXEDINDIRECTCOUNT);
4759}
4760
locke-lunarge1a67022020-04-29 00:15:36 -06004761bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4762 VkDeviceSize offset, VkBuffer countBuffer,
4763 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4764 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004765 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4766 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004767}
4768
4769void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4770 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4771 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004772 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4773 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004774 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4775 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004776}
4777
4778bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4779 VkDeviceSize offset, VkBuffer countBuffer,
4780 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4781 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004782 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4783 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004784}
4785
4786void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4787 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4788 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004789 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4790 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004791 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4792 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004793}
4794
4795bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4796 const VkClearColorValue *pColor, uint32_t rangeCount,
4797 const VkImageSubresourceRange *pRanges) const {
4798 bool skip = false;
4799 const auto *cb_access_context = GetAccessContext(commandBuffer);
4800 assert(cb_access_context);
4801 if (!cb_access_context) return skip;
4802
4803 const auto *context = cb_access_context->GetCurrentAccessContext();
4804 assert(context);
4805 if (!context) return skip;
4806
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004807 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004808
4809 for (uint32_t index = 0; index < rangeCount; index++) {
4810 const auto &range = pRanges[index];
4811 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004812 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004813 if (hazard.hazard) {
4814 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004815 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004816 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004817 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004818 }
4819 }
4820 }
4821 return skip;
4822}
4823
4824void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4825 const VkClearColorValue *pColor, uint32_t rangeCount,
4826 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004827 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004828 auto *cb_access_context = GetAccessContext(commandBuffer);
4829 assert(cb_access_context);
4830 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4831 auto *context = cb_access_context->GetCurrentAccessContext();
4832 assert(context);
4833
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004834 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004835
4836 for (uint32_t index = 0; index < rangeCount; index++) {
4837 const auto &range = pRanges[index];
4838 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004839 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004840 }
4841 }
4842}
4843
4844bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4845 VkImageLayout imageLayout,
4846 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4847 const VkImageSubresourceRange *pRanges) const {
4848 bool skip = false;
4849 const auto *cb_access_context = GetAccessContext(commandBuffer);
4850 assert(cb_access_context);
4851 if (!cb_access_context) return skip;
4852
4853 const auto *context = cb_access_context->GetCurrentAccessContext();
4854 assert(context);
4855 if (!context) return skip;
4856
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004857 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004858
4859 for (uint32_t index = 0; index < rangeCount; index++) {
4860 const auto &range = pRanges[index];
4861 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004862 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004863 if (hazard.hazard) {
4864 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004865 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004866 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004867 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004868 }
4869 }
4870 }
4871 return skip;
4872}
4873
4874void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4875 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4876 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004877 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004878 auto *cb_access_context = GetAccessContext(commandBuffer);
4879 assert(cb_access_context);
4880 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4881 auto *context = cb_access_context->GetCurrentAccessContext();
4882 assert(context);
4883
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004884 auto image_state = Get<IMAGE_STATE>(image);
locke-lunarge1a67022020-04-29 00:15:36 -06004885
4886 for (uint32_t index = 0; index < rangeCount; index++) {
4887 const auto &range = pRanges[index];
4888 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004889 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004890 }
4891 }
4892}
4893
4894bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4895 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4896 VkDeviceSize dstOffset, VkDeviceSize stride,
4897 VkQueryResultFlags flags) const {
4898 bool skip = false;
4899 const auto *cb_access_context = GetAccessContext(commandBuffer);
4900 assert(cb_access_context);
4901 if (!cb_access_context) return skip;
4902
4903 const auto *context = cb_access_context->GetCurrentAccessContext();
4904 assert(context);
4905 if (!context) return skip;
4906
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004907 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004908
4909 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004910 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004911 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004912 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004913 skip |=
4914 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4915 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004916 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004917 }
4918 }
locke-lunargff255f92020-05-13 18:53:52 -06004919
4920 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004921 return skip;
4922}
4923
4924void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4925 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4926 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004927 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4928 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004929 auto *cb_access_context = GetAccessContext(commandBuffer);
4930 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004931 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004932 auto *context = cb_access_context->GetCurrentAccessContext();
4933 assert(context);
4934
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004935 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004936
4937 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004938 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004939 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004940 }
locke-lunargff255f92020-05-13 18:53:52 -06004941
4942 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004943}
4944
4945bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4946 VkDeviceSize size, uint32_t data) const {
4947 bool skip = false;
4948 const auto *cb_access_context = GetAccessContext(commandBuffer);
4949 assert(cb_access_context);
4950 if (!cb_access_context) return skip;
4951
4952 const auto *context = cb_access_context->GetCurrentAccessContext();
4953 assert(context);
4954 if (!context) return skip;
4955
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004956 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004957
4958 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004959 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004960 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004961 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004962 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004963 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004964 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004965 }
4966 }
4967 return skip;
4968}
4969
4970void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4971 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004972 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004973 auto *cb_access_context = GetAccessContext(commandBuffer);
4974 assert(cb_access_context);
4975 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4976 auto *context = cb_access_context->GetCurrentAccessContext();
4977 assert(context);
4978
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004979 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06004980
4981 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004982 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004983 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004984 }
4985}
4986
4987bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4988 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4989 const VkImageResolve *pRegions) const {
4990 bool skip = false;
4991 const auto *cb_access_context = GetAccessContext(commandBuffer);
4992 assert(cb_access_context);
4993 if (!cb_access_context) return skip;
4994
4995 const auto *context = cb_access_context->GetCurrentAccessContext();
4996 assert(context);
4997 if (!context) return skip;
4998
Jeremy Gebbenf4449392022-01-28 10:09:10 -07004999 auto src_image = Get<IMAGE_STATE>(srcImage);
5000 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005001
5002 for (uint32_t region = 0; region < regionCount; region++) {
5003 const auto &resolve_region = pRegions[region];
5004 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005005 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005006 resolve_region.srcOffset, resolve_region.extent);
5007 if (hazard.hazard) {
5008 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005009 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005010 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).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 }
5014
5015 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005016 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06005017 resolve_region.dstOffset, resolve_region.extent);
5018 if (hazard.hazard) {
5019 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005020 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06005021 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07005022 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005023 }
5024 if (skip) break;
5025 }
5026 }
5027
5028 return skip;
5029}
5030
5031void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
5032 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
5033 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005034 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
5035 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06005036 auto *cb_access_context = GetAccessContext(commandBuffer);
5037 assert(cb_access_context);
5038 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
5039 auto *context = cb_access_context->GetCurrentAccessContext();
5040 assert(context);
5041
Jeremy Gebben9f537102021-10-05 16:37:12 -06005042 auto src_image = Get<IMAGE_STATE>(srcImage);
5043 auto dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarge1a67022020-04-29 00:15:36 -06005044
5045 for (uint32_t region = 0; region < regionCount; region++) {
5046 const auto &resolve_region = pRegions[region];
5047 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005048 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005049 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005050 }
5051 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005052 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005053 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005054 }
5055 }
5056}
5057
Tony-LunarG562fc102021-11-12 13:58:35 -07005058bool SyncValidator::ValidateCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5059 CMD_TYPE cmd_type) const {
Jeff Leger178b1e52020-10-05 12:22:23 -04005060 bool skip = false;
5061 const auto *cb_access_context = GetAccessContext(commandBuffer);
5062 assert(cb_access_context);
5063 if (!cb_access_context) return skip;
5064
5065 const auto *context = cb_access_context->GetCurrentAccessContext();
5066 assert(context);
5067 if (!context) return skip;
5068
Tony-LunarG562fc102021-11-12 13:58:35 -07005069 const char *func_name = CommandTypeString(cmd_type);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005070 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5071 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005072
5073 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5074 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5075 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005076 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005077 resolve_region.srcOffset, resolve_region.extent);
5078 if (hazard.hazard) {
5079 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005080 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005081 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).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 }
5085
5086 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005087 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04005088 resolve_region.dstOffset, resolve_region.extent);
5089 if (hazard.hazard) {
5090 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
Tony-LunarG562fc102021-11-12 13:58:35 -07005091 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
Jeff Leger178b1e52020-10-05 12:22:23 -04005092 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07005093 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04005094 }
5095 if (skip) break;
5096 }
5097 }
5098
5099 return skip;
5100}
5101
Tony-LunarG562fc102021-11-12 13:58:35 -07005102bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5103 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
5104 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5105}
5106
5107bool SyncValidator::PreCallValidateCmdResolveImage2(VkCommandBuffer commandBuffer,
5108 const VkResolveImageInfo2 *pResolveImageInfo) const {
5109 return ValidateCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5110}
5111
5112void SyncValidator::RecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2KHR *pResolveImageInfo,
5113 CMD_TYPE cmd_type) {
Jeff Leger178b1e52020-10-05 12:22:23 -04005114 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5115 auto *cb_access_context = GetAccessContext(commandBuffer);
5116 assert(cb_access_context);
Tony-LunarG562fc102021-11-12 13:58:35 -07005117 const auto tag = cb_access_context->NextCommandTag(cmd_type);
Jeff Leger178b1e52020-10-05 12:22:23 -04005118 auto *context = cb_access_context->GetCurrentAccessContext();
5119 assert(context);
5120
Jeremy Gebben9f537102021-10-05 16:37:12 -06005121 auto src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5122 auto dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
Jeff Leger178b1e52020-10-05 12:22:23 -04005123
5124 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5125 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5126 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005127 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005128 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005129 }
5130 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005131 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005132 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005133 }
5134 }
5135}
5136
Tony-LunarG562fc102021-11-12 13:58:35 -07005137void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5138 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5139 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2KHR);
5140}
5141
5142void SyncValidator::PreCallRecordCmdResolveImage2(VkCommandBuffer commandBuffer, const VkResolveImageInfo2 *pResolveImageInfo) {
5143 RecordCmdResolveImage2(commandBuffer, pResolveImageInfo, CMD_RESOLVEIMAGE2);
5144}
5145
locke-lunarge1a67022020-04-29 00:15:36 -06005146bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5147 VkDeviceSize dataSize, const void *pData) const {
5148 bool skip = false;
5149 const auto *cb_access_context = GetAccessContext(commandBuffer);
5150 assert(cb_access_context);
5151 if (!cb_access_context) return skip;
5152
5153 const auto *context = cb_access_context->GetCurrentAccessContext();
5154 assert(context);
5155 if (!context) return skip;
5156
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005157 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005158
5159 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005160 // VK_WHOLE_SIZE not allowed
5161 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005162 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005163 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005164 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005165 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005166 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005167 }
5168 }
5169 return skip;
5170}
5171
5172void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5173 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005174 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005175 auto *cb_access_context = GetAccessContext(commandBuffer);
5176 assert(cb_access_context);
5177 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5178 auto *context = cb_access_context->GetCurrentAccessContext();
5179 assert(context);
5180
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005181 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunarge1a67022020-04-29 00:15:36 -06005182
5183 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005184 // VK_WHOLE_SIZE not allowed
5185 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005186 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005187 }
5188}
locke-lunargff255f92020-05-13 18:53:52 -06005189
5190bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5191 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5192 bool skip = false;
5193 const auto *cb_access_context = GetAccessContext(commandBuffer);
5194 assert(cb_access_context);
5195 if (!cb_access_context) return skip;
5196
5197 const auto *context = cb_access_context->GetCurrentAccessContext();
5198 assert(context);
5199 if (!context) return skip;
5200
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005201 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005202
5203 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005204 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005205 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005206 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005207 skip |=
5208 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5209 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005210 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005211 }
5212 }
5213 return skip;
5214}
5215
5216void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5217 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005218 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005219 auto *cb_access_context = GetAccessContext(commandBuffer);
5220 assert(cb_access_context);
5221 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5222 auto *context = cb_access_context->GetCurrentAccessContext();
5223 assert(context);
5224
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005225 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
locke-lunargff255f92020-05-13 18:53:52 -06005226
5227 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005228 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005229 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005230 }
5231}
John Zulauf49beb112020-11-04 16:06:31 -07005232
5233bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5234 bool skip = false;
5235 const auto *cb_context = GetAccessContext(commandBuffer);
5236 assert(cb_context);
5237 if (!cb_context) return skip;
5238
John Zulauf36ef9282021-02-02 11:47:24 -07005239 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005240 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005241}
5242
5243void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5244 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5245 auto *cb_context = GetAccessContext(commandBuffer);
5246 assert(cb_context);
5247 if (!cb_context) return;
John Zulauf1bf30522021-09-03 15:39:06 -06005248 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005249}
5250
John Zulauf4edde622021-02-15 08:54:50 -07005251bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5252 const VkDependencyInfoKHR *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_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5259 return set_event_op.Validate(*cb_context);
5260}
5261
Tony-LunarGc43525f2021-11-15 16:12:38 -07005262bool SyncValidator::PreCallValidateCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5263 const VkDependencyInfo *pDependencyInfo) const {
5264 bool skip = false;
5265 const auto *cb_context = GetAccessContext(commandBuffer);
5266 assert(cb_context);
5267 if (!cb_context || !pDependencyInfo) return skip;
5268
5269 SyncOpSetEvent set_event_op(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5270 return set_event_op.Validate(*cb_context);
5271}
5272
John Zulauf4edde622021-02-15 08:54:50 -07005273void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5274 const VkDependencyInfoKHR *pDependencyInfo) {
5275 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5276 auto *cb_context = GetAccessContext(commandBuffer);
5277 assert(cb_context);
5278 if (!cb_context || !pDependencyInfo) return;
5279
John Zulauf1bf30522021-09-03 15:39:06 -06005280 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
John Zulauf4edde622021-02-15 08:54:50 -07005281}
5282
Tony-LunarGc43525f2021-11-15 16:12:38 -07005283void SyncValidator::PostCallRecordCmdSetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5284 const VkDependencyInfo *pDependencyInfo) {
5285 StateTracker::PostCallRecordCmdSetEvent2(commandBuffer, event, pDependencyInfo);
5286 auto *cb_context = GetAccessContext(commandBuffer);
5287 assert(cb_context);
5288 if (!cb_context || !pDependencyInfo) return;
5289
5290 cb_context->RecordSyncOp<SyncOpSetEvent>(CMD_SETEVENT2, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5291}
5292
John Zulauf49beb112020-11-04 16:06:31 -07005293bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5294 VkPipelineStageFlags stageMask) const {
5295 bool skip = false;
5296 const auto *cb_context = GetAccessContext(commandBuffer);
5297 assert(cb_context);
5298 if (!cb_context) return skip;
5299
John Zulauf36ef9282021-02-02 11:47:24 -07005300 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005301 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005302}
5303
5304void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5305 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5306 auto *cb_context = GetAccessContext(commandBuffer);
5307 assert(cb_context);
5308 if (!cb_context) return;
5309
John Zulauf1bf30522021-09-03 15:39:06 -06005310 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf49beb112020-11-04 16:06:31 -07005311}
5312
John Zulauf4edde622021-02-15 08:54:50 -07005313bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5314 VkPipelineStageFlags2KHR 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_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5321 return reset_event_op.Validate(*cb_context);
5322}
5323
Tony-LunarGa2662db2021-11-16 07:26:24 -07005324bool SyncValidator::PreCallValidateCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event,
5325 VkPipelineStageFlags2 stageMask) const {
5326 bool skip = false;
5327 const auto *cb_context = GetAccessContext(commandBuffer);
5328 assert(cb_context);
5329 if (!cb_context) return skip;
5330
5331 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5332 return reset_event_op.Validate(*cb_context);
5333}
5334
John Zulauf4edde622021-02-15 08:54:50 -07005335void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5336 VkPipelineStageFlags2KHR stageMask) {
5337 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5338 auto *cb_context = GetAccessContext(commandBuffer);
5339 assert(cb_context);
5340 if (!cb_context) return;
5341
John Zulauf1bf30522021-09-03 15:39:06 -06005342 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf4edde622021-02-15 08:54:50 -07005343}
5344
Tony-LunarGa2662db2021-11-16 07:26:24 -07005345void SyncValidator::PostCallRecordCmdResetEvent2(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags2 stageMask) {
5346 StateTracker::PostCallRecordCmdResetEvent2(commandBuffer, event, stageMask);
5347 auto *cb_context = GetAccessContext(commandBuffer);
5348 assert(cb_context);
5349 if (!cb_context) return;
5350
5351 cb_context->RecordSyncOp<SyncOpResetEvent>(CMD_RESETEVENT2, *this, cb_context->GetQueueFlags(), event, stageMask);
5352}
5353
John Zulauf49beb112020-11-04 16:06:31 -07005354bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5355 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5356 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5357 uint32_t bufferMemoryBarrierCount,
5358 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5359 uint32_t imageMemoryBarrierCount,
5360 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5361 bool skip = false;
5362 const auto *cb_context = GetAccessContext(commandBuffer);
5363 assert(cb_context);
5364 if (!cb_context) return skip;
5365
John Zulauf36ef9282021-02-02 11:47:24 -07005366 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5367 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5368 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005369 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005370}
5371
5372void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5373 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5374 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5375 uint32_t bufferMemoryBarrierCount,
5376 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5377 uint32_t imageMemoryBarrierCount,
5378 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5379 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5380 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5381 imageMemoryBarrierCount, pImageMemoryBarriers);
5382
5383 auto *cb_context = GetAccessContext(commandBuffer);
5384 assert(cb_context);
5385 if (!cb_context) return;
5386
John Zulauf1bf30522021-09-03 15:39:06 -06005387 cb_context->RecordSyncOp<SyncOpWaitEvents>(
John Zulauf610e28c2021-08-03 17:46:23 -06005388 CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
John Zulauf1bf30522021-09-03 15:39:06 -06005389 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf4a6105a2020-11-17 15:11:05 -07005390}
5391
John Zulauf4edde622021-02-15 08:54:50 -07005392bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5393 const VkDependencyInfoKHR *pDependencyInfos) const {
5394 bool skip = false;
5395 const auto *cb_context = GetAccessContext(commandBuffer);
5396 assert(cb_context);
5397 if (!cb_context) return skip;
5398
5399 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5400 skip |= wait_events_op.Validate(*cb_context);
5401 return skip;
5402}
5403
5404void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5405 const VkDependencyInfoKHR *pDependencyInfos) {
5406 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5407
5408 auto *cb_context = GetAccessContext(commandBuffer);
5409 assert(cb_context);
5410 if (!cb_context) return;
5411
John Zulauf1bf30522021-09-03 15:39:06 -06005412 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5413 pDependencyInfos);
John Zulauf4edde622021-02-15 08:54:50 -07005414}
5415
Tony-LunarG1364cf52021-11-17 16:10:11 -07005416bool SyncValidator::PreCallValidateCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5417 const VkDependencyInfo *pDependencyInfos) const {
5418 bool skip = false;
5419 const auto *cb_context = GetAccessContext(commandBuffer);
5420 assert(cb_context);
5421 if (!cb_context) return skip;
5422
5423 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5424 skip |= wait_events_op.Validate(*cb_context);
5425 return skip;
5426}
5427
5428void SyncValidator::PostCallRecordCmdWaitEvents2(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5429 const VkDependencyInfo *pDependencyInfos) {
5430 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5431
5432 auto *cb_context = GetAccessContext(commandBuffer);
5433 assert(cb_context);
5434 if (!cb_context) return;
5435
5436 cb_context->RecordSyncOp<SyncOpWaitEvents>(CMD_WAITEVENTS2, *this, cb_context->GetQueueFlags(), eventCount, pEvents,
5437 pDependencyInfos);
5438}
5439
John Zulauf4a6105a2020-11-17 15:11:05 -07005440void SyncEventState::ResetFirstScope() {
5441 for (const auto address_type : kAddressTypes) {
5442 first_scope[static_cast<size_t>(address_type)].clear();
5443 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005444 scope = SyncExecScope();
John Zulauf78b1f892021-09-20 15:02:09 -06005445 first_scope_set = false;
5446 first_scope_tag = 0;
John Zulauf4a6105a2020-11-17 15:11:05 -07005447}
5448
5449// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005450SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005451 IgnoreReason reason = NotIgnored;
5452
Tony-LunarG1364cf52021-11-17 16:10:11 -07005453 if ((CMD_WAITEVENTS2KHR == cmd || CMD_WAITEVENTS2 == cmd) && (CMD_SETEVENT == last_command)) {
John Zulauf4edde622021-02-15 08:54:50 -07005454 reason = SetVsWait2;
5455 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5456 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005457 } else if (unsynchronized_set) {
5458 reason = SetRace;
John Zulauf78b1f892021-09-20 15:02:09 -06005459 } else if (first_scope_set) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005460 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005461 if (missing_bits) reason = MissingStageBits;
5462 }
5463
5464 return reason;
5465}
5466
Jeremy Gebben40a22942020-12-22 14:22:06 -07005467bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005468 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5469 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5470 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005471}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005472
John Zulauf36ef9282021-02-02 11:47:24 -07005473SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5474 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5475 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005476 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5477 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5478 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005479 : SyncOpBase(cmd), barriers_(1) {
5480 auto &barrier_set = barriers_[0];
5481 barrier_set.dependency_flags = dependencyFlags;
5482 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5483 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005484 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005485 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5486 pMemoryBarriers);
5487 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5488 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5489 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5490 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005491}
5492
John Zulauf4edde622021-02-15 08:54:50 -07005493SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5494 const VkDependencyInfoKHR *dep_infos)
5495 : SyncOpBase(cmd), barriers_(event_count) {
5496 for (uint32_t i = 0; i < event_count; i++) {
5497 const auto &dep_info = dep_infos[i];
5498 auto &barrier_set = barriers_[i];
5499 barrier_set.dependency_flags = dep_info.dependencyFlags;
5500 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5501 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5502 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5503 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5504 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5505 dep_info.pMemoryBarriers);
5506 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5507 dep_info.pBufferMemoryBarriers);
5508 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5509 dep_info.pImageMemoryBarriers);
5510 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005511}
5512
John Zulauf36ef9282021-02-02 11:47:24 -07005513SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005514 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5515 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5516 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5517 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5518 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005519 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005520 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5521
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005522SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5523 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005524 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005525
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005526bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5527 bool skip = false;
5528 const auto *context = cb_context.GetCurrentAccessContext();
5529 assert(context);
5530 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005531 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5532
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005533 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005534 const auto &barrier_set = barriers_[0];
5535 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5536 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5537 const auto *image_state = image_barrier.image.get();
5538 if (!image_state) continue;
5539 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5540 if (hazard.hazard) {
5541 // PHASE1 TODO -- add tag information to log msg when useful.
5542 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005543 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005544 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5545 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5546 string_SyncHazard(hazard.hazard), image_barrier.index,
5547 sync_state.report_data->FormatHandle(image_handle).c_str(),
5548 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005549 }
5550 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005551 return skip;
5552}
5553
John Zulaufd5115702021-01-18 12:34:33 -07005554struct SyncOpPipelineBarrierFunctorFactory {
5555 using BarrierOpFunctor = PipelineBarrierOp;
5556 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5557 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5558 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5559 using BufferRange = ResourceAccessRange;
5560 using ImageRange = subresource_adapter::ImageRangeGenerator;
5561 using GlobalRange = ResourceAccessRange;
5562
5563 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5564 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5565 }
John Zulauf14940722021-04-12 15:19:02 -06005566 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005567 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5568 }
5569 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5570 return GlobalBarrierOpFunctor(barrier, false);
5571 }
5572
5573 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5574 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5575 const auto base_address = ResourceBaseAddress(buffer);
5576 return (range + base_address);
5577 }
John Zulauf110413c2021-03-20 05:38:38 -06005578 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005579 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005580
5581 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005582 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005583 return range_gen;
5584 }
5585 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5586};
5587
5588template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005589void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005590 AccessContext *context) {
5591 for (const auto &barrier : barriers) {
5592 const auto *state = barrier.GetState();
5593 if (state) {
5594 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5595 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5596 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5597 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5598 }
5599 }
5600}
5601
5602template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005603void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005604 AccessContext *access_context) {
5605 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5606 for (const auto &barrier : barriers) {
5607 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5608 }
5609 for (const auto address_type : kAddressTypes) {
5610 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5611 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5612 }
5613}
5614
John Zulauf8eda1562021-04-13 17:06:41 -06005615ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005616 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005617 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005618 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf4fa68462021-04-26 21:04:22 -06005619 DoRecord(tag, access_context, events_context);
5620 return tag;
5621}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005622
John Zulauf4fa68462021-04-26 21:04:22 -06005623void SyncOpPipelineBarrier::DoRecord(const ResourceUsageTag tag, AccessContext *access_context,
5624 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005625 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005626 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5627 assert(barriers_.size() == 1);
5628 const auto &barrier_set = barriers_[0];
5629 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5630 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5631 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005632 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005633 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005634 } else {
5635 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005636 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005637 }
5638 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005639}
5640
John Zulauf8eda1562021-04-13 17:06:41 -06005641bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06005642 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005643 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5644 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005645 return false;
5646}
5647
John Zulauf4edde622021-02-15 08:54:50 -07005648void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5649 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5650 const VkMemoryBarrier *barriers) {
5651 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005652 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005653 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005654 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005655 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005656 }
5657 if (0 == memory_barrier_count) {
5658 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005659 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005660 }
John Zulauf4edde622021-02-15 08:54:50 -07005661 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005662}
5663
John Zulauf4edde622021-02-15 08:54:50 -07005664void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5665 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5666 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5667 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005668 for (uint32_t index = 0; index < barrier_count; index++) {
5669 const auto &barrier = barriers[index];
Jeremy Gebben9f537102021-10-05 16:37:12 -06005670 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005671 if (buffer) {
5672 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5673 const auto range = MakeRange(barrier.offset, barrier_size);
5674 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005675 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005676 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005677 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005678 }
5679 }
5680}
5681
John Zulauf4edde622021-02-15 08:54:50 -07005682void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005683 uint32_t memory_barrier_count, const VkMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005684 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005685 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005686 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005687 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5688 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5689 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005690 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005691 }
John Zulauf4edde622021-02-15 08:54:50 -07005692 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005693}
5694
John Zulauf4edde622021-02-15 08:54:50 -07005695void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5696 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005697 const VkBufferMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005698 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005699 for (uint32_t index = 0; index < barrier_count; index++) {
5700 const auto &barrier = barriers[index];
5701 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5702 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9f537102021-10-05 16:37:12 -06005703 auto buffer = sync_state.Get<BUFFER_STATE>(barrier.buffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005704 if (buffer) {
5705 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5706 const auto range = MakeRange(barrier.offset, barrier_size);
5707 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005708 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005709 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005710 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005711 }
5712 }
5713}
5714
John Zulauf4edde622021-02-15 08:54:50 -07005715void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5716 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5717 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5718 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005719 for (uint32_t index = 0; index < barrier_count; index++) {
5720 const auto &barrier = barriers[index];
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005721 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005722 if (image) {
5723 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5724 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005725 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005726 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005727 image_memory_barriers.emplace_back();
5728 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005729 }
5730 }
5731}
John Zulaufd5115702021-01-18 12:34:33 -07005732
John Zulauf4edde622021-02-15 08:54:50 -07005733void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5734 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
Tony-LunarG3f6eceb2021-11-18 14:34:49 -07005735 const VkImageMemoryBarrier2 *barriers) {
John Zulauf4edde622021-02-15 08:54:50 -07005736 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005737 for (uint32_t index = 0; index < barrier_count; index++) {
5738 const auto &barrier = barriers[index];
5739 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5740 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07005741 auto image = sync_state.Get<IMAGE_STATE>(barrier.image);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005742 if (image) {
5743 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5744 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005745 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005746 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005747 image_memory_barriers.emplace_back();
5748 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005749 }
5750 }
5751}
5752
John Zulauf36ef9282021-02-02 11:47:24 -07005753SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005754 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5755 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5756 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5757 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005758 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005759 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5760 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005761 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005762}
5763
John Zulauf4edde622021-02-15 08:54:50 -07005764SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5765 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5766 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5767 MakeEventsList(sync_state, eventCount, pEvents);
5768 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5769}
5770
John Zulauf610e28c2021-08-03 17:46:23 -06005771const char *const SyncOpWaitEvents::kIgnored = "Wait operation is ignored for this event.";
5772
John Zulaufd5115702021-01-18 12:34:33 -07005773bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005774 bool skip = false;
5775 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005776 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005777
John Zulauf610e28c2021-08-03 17:46:23 -06005778 // This is only interesting at record and not replay (Execute/Submit) time.
John Zulauf4edde622021-02-15 08:54:50 -07005779 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5780 const auto &barrier_set = barriers_[barrier_set_index];
5781 if (barrier_set.single_exec_scope) {
5782 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5783 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5784 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5785 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5786 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5787 } else {
5788 const auto &barriers = barrier_set.memory_barriers;
5789 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5790 const auto &barrier = barriers[barrier_index];
5791 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5792 const std::string vuid =
5793 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5794 skip =
5795 sync_state.LogInfo(command_buffer_handle, vuid,
5796 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5797 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5798 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5799 }
5800 }
5801 }
5802 }
John Zulaufd5115702021-01-18 12:34:33 -07005803 }
5804
John Zulauf610e28c2021-08-03 17:46:23 -06005805 // The rest is common to record time and replay time.
5806 skip |= DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
5807 return skip;
5808}
5809
5810bool SyncOpWaitEvents::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
5811 bool skip = false;
5812 const auto &sync_state = cb_context.GetSyncState();
5813
Jeremy Gebben40a22942020-12-22 14:22:06 -07005814 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005815 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005816 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005817 const auto *events_context = cb_context.GetCurrentEventsContext();
5818 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005819 size_t barrier_set_index = 0;
5820 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
John Zulauf78394fc2021-07-12 15:41:40 -06005821 for (const auto &event : events_) {
5822 const auto *sync_event = events_context->Get(event.get());
5823 const auto &barrier_set = barriers_[barrier_set_index];
5824 if (!sync_event) {
5825 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5826 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5827 // new validation error... wait without previously submitted set event...
5828 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 -07005829 barrier_set_index += barrier_set_incr;
John Zulauf78394fc2021-07-12 15:41:40 -06005830 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005831 }
John Zulauf610e28c2021-08-03 17:46:23 -06005832
5833 // For replay calls, don't revalidate "same command buffer" events
5834 if (sync_event->last_command_tag > base_tag) continue;
5835
John Zulauf78394fc2021-07-12 15:41:40 -06005836 const auto event_handle = sync_event->event->event();
5837 // TODO add "destroyed" checks
5838
John Zulauf78b1f892021-09-20 15:02:09 -06005839 if (sync_event->first_scope_set) {
5840 // Only accumulate barrier and event stages if there is a pending set in the current context
5841 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5842 event_stage_masks |= sync_event->scope.mask_param;
5843 }
5844
John Zulauf78394fc2021-07-12 15:41:40 -06005845 const auto &src_exec_scope = barrier_set.src_exec_scope;
John Zulauf78b1f892021-09-20 15:02:09 -06005846
John Zulauf78394fc2021-07-12 15:41:40 -06005847 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5848 if (ignore_reason) {
5849 switch (ignore_reason) {
5850 case SyncEventState::ResetWaitRace:
5851 case SyncEventState::Reset2WaitRace: {
5852 // Four permuations of Reset and Wait calls...
5853 const char *vuid =
5854 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5855 if (ignore_reason == SyncEventState::Reset2WaitRace) {
Tony-LunarG279601c2021-11-16 10:50:51 -07005856 vuid = (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2-event-03831"
5857 : "VUID-vkCmdResetEvent2-event-03832";
John Zulauf78394fc2021-07-12 15:41:40 -06005858 }
5859 const char *const message =
5860 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5861 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5862 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
John Zulauf610e28c2021-08-03 17:46:23 -06005863 CommandTypeString(sync_event->last_command), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005864 break;
5865 }
5866 case SyncEventState::SetRace: {
5867 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5868 // this event
5869 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5870 const char *const message =
5871 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5872 const char *const reason = "First synchronization scope is undefined.";
5873 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5874 sync_state.report_data->FormatHandle(event_handle).c_str(),
John Zulauf610e28c2021-08-03 17:46:23 -06005875 CommandTypeString(sync_event->last_command), reason, kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005876 break;
5877 }
5878 case SyncEventState::MissingStageBits: {
5879 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5880 // Issue error message that event waited for is not in wait events scope
5881 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5882 const char *const message = "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5883 ". Bits missing from srcStageMask %s. %s";
5884 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5885 sync_state.report_data->FormatHandle(event_handle).c_str(),
5886 sync_event->scope.mask_param, src_exec_scope.mask_param,
John Zulauf610e28c2021-08-03 17:46:23 -06005887 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), kIgnored);
John Zulauf78394fc2021-07-12 15:41:40 -06005888 break;
5889 }
5890 case SyncEventState::SetVsWait2: {
Tony-LunarG279601c2021-11-16 10:50:51 -07005891 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2-pEvents-03837",
John Zulauf78394fc2021-07-12 15:41:40 -06005892 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5893 sync_state.report_data->FormatHandle(event_handle).c_str(),
5894 CommandTypeString(sync_event->last_command));
5895 break;
5896 }
5897 default:
5898 assert(ignore_reason == SyncEventState::NotIgnored);
5899 }
5900 } else if (barrier_set.image_memory_barriers.size()) {
5901 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5902 const auto *context = cb_context.GetCurrentAccessContext();
5903 assert(context);
5904 for (const auto &image_memory_barrier : image_memory_barriers) {
5905 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5906 const auto *image_state = image_memory_barrier.image.get();
5907 if (!image_state) continue;
5908 const auto &subresource_range = image_memory_barrier.range;
5909 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5910 const auto hazard =
5911 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5912 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5913 if (hazard.hazard) {
5914 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
5915 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5916 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
5917 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
5918 cb_context.FormatUsage(hazard).c_str());
5919 break;
5920 }
5921 }
5922 }
5923 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5924 // 03839
5925 barrier_set_index += barrier_set_incr;
5926 }
John Zulaufd5115702021-01-18 12:34:33 -07005927
5928 // 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 -07005929 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 -07005930 if (extra_stage_bits) {
5931 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005932 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5933 const char *const vuid =
Tony-LunarG279601c2021-11-16 10:50:51 -07005934 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005935 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005936 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulauf610e28c2021-08-03 17:46:23 -06005937 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005938 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005939 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005940 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005941 " vkCmdSetEvent may be in previously submitted command buffer.");
5942 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005943 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005944 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005945 }
5946 }
5947 return skip;
5948}
5949
5950struct SyncOpWaitEventsFunctorFactory {
5951 using BarrierOpFunctor = WaitEventBarrierOp;
5952 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5953 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5954 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5955 using BufferRange = EventSimpleRangeGenerator;
5956 using ImageRange = EventImageRangeGenerator;
5957 using GlobalRange = EventSimpleRangeGenerator;
5958
5959 // Need to restrict to only valid exec and access scope for this event
5960 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5961 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005962 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005963 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5964 return barrier;
5965 }
5966 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5967 auto barrier = RestrictToEvent(barrier_arg);
5968 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5969 }
John Zulauf14940722021-04-12 15:19:02 -06005970 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005971 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5972 }
5973 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5974 auto barrier = RestrictToEvent(barrier_arg);
5975 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5976 }
5977
5978 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5979 const AccessAddressType address_type = GetAccessAddressType(buffer);
5980 const auto base_address = ResourceBaseAddress(buffer);
5981 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5982 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5983 return filtered_range_gen;
5984 }
John Zulauf110413c2021-03-20 05:38:38 -06005985 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005986 if (!SimpleBinding(image)) return ImageRange();
5987 const auto address_type = GetAccessAddressType(image);
5988 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005989 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005990 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5991
5992 return filtered_range_gen;
5993 }
5994 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5995 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5996 }
5997 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5998 SyncEventState *sync_event;
5999};
6000
John Zulauf8eda1562021-04-13 17:06:41 -06006001ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006002 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07006003 auto *access_context = cb_context->GetCurrentAccessContext();
6004 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006005 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07006006 auto *events_context = cb_context->GetCurrentEventsContext();
6007 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006008 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07006009
John Zulauf610e28c2021-08-03 17:46:23 -06006010 DoRecord(tag, access_context, events_context);
6011 return tag;
6012}
6013
6014void SyncOpWaitEvents::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07006015 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
6016 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
6017 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
6018 access_context->ResolvePreviousAccesses();
6019
John Zulauf4edde622021-02-15 08:54:50 -07006020 size_t barrier_set_index = 0;
6021 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
6022 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07006023 for (auto &event_shared : events_) {
6024 if (!event_shared.get()) continue;
6025 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07006026
John Zulauf4edde622021-02-15 08:54:50 -07006027 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006028 sync_event->last_command_tag = tag;
John Zulaufd5115702021-01-18 12:34:33 -07006029
John Zulauf4edde622021-02-15 08:54:50 -07006030 const auto &barrier_set = barriers_[barrier_set_index];
6031 const auto &dst = barrier_set.dst_exec_scope;
6032 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07006033 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
6034 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
6035 // of the barriers is maintained.
6036 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07006037 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
6038 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
6039 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07006040
6041 // Apply the global barrier to the event itself (for race condition tracking)
6042 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
6043 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6044 sync_event->barriers |= dst.exec_scope;
6045 } else {
6046 // We ignored this wait, so we don't have any effective synchronization barriers for it.
6047 sync_event->barriers = 0U;
6048 }
John Zulauf4edde622021-02-15 08:54:50 -07006049 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07006050 }
6051
6052 // Apply the pending barriers
6053 ResolvePendingBarrierFunctor apply_pending_action(tag);
6054 access_context->ApplyToContext(apply_pending_action);
6055}
6056
John Zulauf8eda1562021-04-13 17:06:41 -06006057bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006058 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6059 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006060}
6061
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006062bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6063 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
6064 bool skip = false;
6065 const auto *cb_access_context = GetAccessContext(commandBuffer);
6066 assert(cb_access_context);
6067 if (!cb_access_context) return skip;
6068
6069 const auto *context = cb_access_context->GetCurrentAccessContext();
6070 assert(context);
6071 if (!context) return skip;
6072
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006073 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006074
6075 if (dst_buffer) {
6076 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6077 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
6078 if (hazard.hazard) {
6079 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
6080 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
6081 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06006082 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006083 }
6084 }
6085 return skip;
6086}
6087
John Zulauf669dfd52021-01-27 17:15:28 -07006088void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07006089 events_.reserve(event_count);
6090 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006091 events_.emplace_back(sync_state.Get<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07006092 }
6093}
John Zulauf6ce24372021-01-30 05:56:25 -07006094
John Zulauf36ef9282021-02-02 11:47:24 -07006095SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006096 VkPipelineStageFlags2KHR stageMask)
Jeremy Gebben9f537102021-10-05 16:37:12 -06006097 : SyncOpBase(cmd), event_(sync_state.Get<EVENT_STATE>(event)), exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006098
John Zulauf1bf30522021-09-03 15:39:06 -06006099bool SyncOpResetEvent::Validate(const CommandBufferAccessContext& cb_context) const {
6100 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6101}
6102
6103bool SyncOpResetEvent::DoValidate(const CommandBufferAccessContext & cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006104 auto *events_context = cb_context.GetCurrentEventsContext();
6105 assert(events_context);
6106 bool skip = false;
6107 if (!events_context) return skip;
6108
6109 const auto &sync_state = cb_context.GetSyncState();
6110 const auto *sync_event = events_context->Get(event_);
6111 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6112
John Zulauf1bf30522021-09-03 15:39:06 -06006113 if (sync_event->last_command_tag > base_tag) return skip; // if we validated this in recording of the secondary, don't repeat
6114
John Zulauf6ce24372021-01-30 05:56:25 -07006115 const char *const set_wait =
6116 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6117 "hazards.";
6118 const char *message = set_wait; // Only one message this call.
6119 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
6120 const char *vuid = nullptr;
6121 switch (sync_event->last_command) {
6122 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006123 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006124 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006125 // Needs a barrier between set and reset
6126 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
6127 break;
John Zulauf4edde622021-02-15 08:54:50 -07006128 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006129 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006130 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07006131 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
6132 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
6133 break;
6134 }
6135 default:
6136 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07006137 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
6138 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07006139 break;
6140 }
6141 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006142 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
6143 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006144 CommandTypeString(sync_event->last_command));
6145 }
6146 }
6147 return skip;
6148}
6149
John Zulauf8eda1562021-04-13 17:06:41 -06006150ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
6151 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006152 auto *events_context = cb_context->GetCurrentEventsContext();
6153 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006154 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006155
6156 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06006157 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006158
6159 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07006160 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006161 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006162 sync_event->unsynchronized_set = CMD_NONE;
6163 sync_event->ResetFirstScope();
6164 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06006165
6166 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006167}
6168
John Zulauf8eda1562021-04-13 17:06:41 -06006169bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006170 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf1bf30522021-09-03 15:39:06 -06006171 return DoValidate(*active_context, base_tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006172}
6173
John Zulauf4fa68462021-04-26 21:04:22 -06006174void SyncOpResetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006175
John Zulauf36ef9282021-02-02 11:47:24 -07006176SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07006177 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07006178 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006179 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006180 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
6181 dep_info_() {}
6182
6183SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
6184 const VkDependencyInfoKHR &dep_info)
6185 : SyncOpBase(cmd),
Jeremy Gebben9f537102021-10-05 16:37:12 -06006186 event_(sync_state.Get<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07006187 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
Tony-LunarG273f32f2021-09-28 08:56:30 -06006188 dep_info_(new safe_VkDependencyInfo(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07006189
6190bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf610e28c2021-08-03 17:46:23 -06006191 return DoValidate(cb_context, ResourceUsageRecord::kMaxIndex);
6192}
6193bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6194 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
6195 assert(active_context);
6196 return DoValidate(*active_context, base_tag);
6197}
6198
6199bool SyncOpSetEvent::DoValidate(const CommandBufferAccessContext &cb_context, const ResourceUsageTag base_tag) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006200 bool skip = false;
6201
6202 const auto &sync_state = cb_context.GetSyncState();
6203 auto *events_context = cb_context.GetCurrentEventsContext();
6204 assert(events_context);
6205 if (!events_context) return skip;
6206
6207 const auto *sync_event = events_context->Get(event_);
6208 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6209
John Zulauf610e28c2021-08-03 17:46:23 -06006210 if (sync_event->last_command_tag >= base_tag) return skip; // for replay we don't want to revalidate internal "last commmand"
6211
John Zulauf6ce24372021-01-30 05:56:25 -07006212 const char *const reset_set =
6213 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6214 "hazards.";
6215 const char *const wait =
6216 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6217
6218 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006219 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006220 const char *message = nullptr;
6221 switch (sync_event->last_command) {
6222 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006223 case CMD_RESETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006224 case CMD_RESETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006225 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006226 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006227 message = reset_set;
6228 break;
6229 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006230 case CMD_SETEVENT2KHR:
Tony-LunarG8d71c4f2022-01-27 15:25:53 -07006231 case CMD_SETEVENT2:
John Zulauf6ce24372021-01-30 05:56:25 -07006232 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006233 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006234 message = reset_set;
6235 break;
6236 case CMD_WAITEVENTS:
Tony-LunarG1364cf52021-11-17 16:10:11 -07006237 case CMD_WAITEVENTS2:
John Zulauf4edde622021-02-15 08:54:50 -07006238 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006239 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006240 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006241 message = wait;
6242 break;
6243 default:
6244 // The only other valid last command that wasn't one.
6245 assert(sync_event->last_command == CMD_NONE);
6246 break;
6247 }
John Zulauf4edde622021-02-15 08:54:50 -07006248 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006249 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006250 std::string vuid("SYNC-");
6251 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006252 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6253 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006254 CommandTypeString(sync_event->last_command));
6255 }
6256 }
6257
6258 return skip;
6259}
6260
John Zulauf8eda1562021-04-13 17:06:41 -06006261ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006262 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006263 auto *events_context = cb_context->GetCurrentEventsContext();
6264 auto *access_context = cb_context->GetCurrentAccessContext();
6265 assert(events_context);
John Zulauf610e28c2021-08-03 17:46:23 -06006266 if (access_context && events_context) {
6267 DoRecord(tag, access_context, events_context);
6268 }
6269 return tag;
6270}
John Zulauf6ce24372021-01-30 05:56:25 -07006271
John Zulauf610e28c2021-08-03 17:46:23 -06006272void SyncOpSetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07006273 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf610e28c2021-08-03 17:46:23 -06006274 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006275
6276 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6277 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6278 // any issues caused by naive scope setting here.
6279
6280 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6281 // Given:
6282 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6283 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6284
6285 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6286 sync_event->unsynchronized_set = sync_event->last_command;
6287 sync_event->ResetFirstScope();
John Zulauf78b1f892021-09-20 15:02:09 -06006288 } else if (!sync_event->first_scope_set) {
John Zulauf6ce24372021-01-30 05:56:25 -07006289 // We only set the scope if there isn't one
6290 sync_event->scope = src_exec_scope_;
6291
6292 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6293 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6294 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6295 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6296 }
6297 };
6298 access_context->ForAll(set_scope);
6299 sync_event->unsynchronized_set = CMD_NONE;
John Zulauf78b1f892021-09-20 15:02:09 -06006300 sync_event->first_scope_set = true;
John Zulauf6ce24372021-01-30 05:56:25 -07006301 sync_event->first_scope_tag = tag;
6302 }
John Zulauf4edde622021-02-15 08:54:50 -07006303 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6304 sync_event->last_command = cmd_;
John Zulauf610e28c2021-08-03 17:46:23 -06006305 sync_event->last_command_tag = tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006306 sync_event->barriers = 0U;
6307}
John Zulauf64ffe552021-02-06 10:25:07 -07006308
6309SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6310 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006311 const VkSubpassBeginInfo *pSubpassBeginInfo)
6312 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006313 if (pRenderPassBegin) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006314 rp_state_ = sync_state.Get<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
John Zulauf64ffe552021-02-06 10:25:07 -07006315 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006316 auto fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07006317 if (fb_state) {
Jeremy Gebben9f537102021-10-05 16:37:12 -06006318 shared_attachments_ = sync_state.GetAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
John Zulauf64ffe552021-02-06 10:25:07 -07006319 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6320 // Note that this a safe to presist as long as shared_attachments is not cleared
6321 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006322 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006323 attachments_.emplace_back(attachment.get());
6324 }
6325 }
6326 if (pSubpassBeginInfo) {
6327 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6328 }
6329 }
6330}
6331
6332bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6333 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6334 bool skip = false;
6335
6336 assert(rp_state_.get());
6337 if (nullptr == rp_state_.get()) return skip;
6338 auto &rp_state = *rp_state_.get();
6339
6340 const uint32_t subpass = 0;
6341
6342 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6343 // hasn't happened yet)
6344 const std::vector<AccessContext> empty_context_vector;
6345 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6346 cb_context.GetCurrentAccessContext());
6347
6348 // Validate attachment operations
6349 if (attachments_.size() == 0) return skip;
6350 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006351
6352 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6353 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6354 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6355 // operations (though it's currently a messy approach)
6356 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6357 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006358
6359 // Validate load operations if there were no layout transition hazards
6360 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07006361 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
6362 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006363 }
6364
6365 return skip;
6366}
6367
John Zulauf8eda1562021-04-13 17:06:41 -06006368ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
6369 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006370 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6371 assert(rp_state_.get());
John Zulauf8eda1562021-04-13 17:06:41 -06006372 if (nullptr == rp_state_.get()) return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006373 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006374
6375 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006376}
6377
John Zulauf8eda1562021-04-13 17:06:41 -06006378bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006379 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006380 return false;
6381}
6382
John Zulauf4fa68462021-04-26 21:04:22 -06006383void SyncOpBeginRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6384}
John Zulauf8eda1562021-04-13 17:06:41 -06006385
John Zulauf64ffe552021-02-06 10:25:07 -07006386SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006387 const VkSubpassEndInfo *pSubpassEndInfo)
6388 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006389 if (pSubpassBeginInfo) {
6390 subpass_begin_info_.initialize(pSubpassBeginInfo);
6391 }
6392 if (pSubpassEndInfo) {
6393 subpass_end_info_.initialize(pSubpassEndInfo);
6394 }
6395}
6396
6397bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6398 bool skip = false;
6399 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6400 if (!renderpass_context) return skip;
6401
6402 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6403 return skip;
6404}
6405
John Zulauf8eda1562021-04-13 17:06:41 -06006406ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006407 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006408 // TODO PHASE2 Need to fix renderpass tagging/segregation of barrier and access operations for QueueSubmit time validation
6409 auto prev_tag = cb_context->NextCommandTag(cmd_);
6410 auto next_tag = cb_context->NextSubcommandTag(cmd_);
6411
6412 cb_context->RecordNextSubpass(prev_tag, next_tag);
6413 // TODO PHASE2 This needs to be the tag of the barrier operations
6414 return prev_tag;
6415}
6416
6417bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006418 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006419 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006420}
6421
sfricke-samsung85584a72021-09-30 21:43:38 -07006422SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6423 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006424 if (pSubpassEndInfo) {
6425 subpass_end_info_.initialize(pSubpassEndInfo);
6426 }
6427}
6428
John Zulauf4fa68462021-04-26 21:04:22 -06006429void SyncOpNextSubpass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006430
John Zulauf64ffe552021-02-06 10:25:07 -07006431bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6432 bool skip = false;
6433 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6434
6435 if (!renderpass_context) return skip;
6436 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6437 return skip;
6438}
6439
John Zulauf8eda1562021-04-13 17:06:41 -06006440ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006441 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006442 const auto tag = cb_context->NextCommandTag(cmd_);
6443 cb_context->RecordEndRenderPass(tag);
6444 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006445}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006446
John Zulauf8eda1562021-04-13 17:06:41 -06006447bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
John Zulauf610e28c2021-08-03 17:46:23 -06006448 ResourceUsageTag base_tag, CommandBufferAccessContext *active_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06006449 return false;
6450}
6451
John Zulauf4fa68462021-04-26 21:04:22 -06006452void SyncOpEndRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006453
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006454void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6455 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6456 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6457 auto *cb_access_context = GetAccessContext(commandBuffer);
6458 assert(cb_access_context);
6459 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6460 auto *context = cb_access_context->GetCurrentAccessContext();
6461 assert(context);
6462
Jeremy Gebbenf4449392022-01-28 10:09:10 -07006463 auto dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006464
6465 if (dst_buffer) {
6466 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6467 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6468 }
6469}
John Zulaufd05c5842021-03-26 11:32:16 -06006470
John Zulaufae842002021-04-15 18:20:55 -06006471bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6472 const VkCommandBuffer *pCommandBuffers) const {
6473 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6474 const char *func_name = "vkCmdExecuteCommands";
6475 const auto *cb_context = GetAccessContext(commandBuffer);
6476 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006477
6478 // Heavyweight, but we need a proxy copy of the active command buffer access context
6479 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006480
6481 // Make working copies of the access and events contexts
John Zulauf4fa68462021-04-26 21:04:22 -06006482 proxy_cb_context.NextCommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006483
6484 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf4fa68462021-04-26 21:04:22 -06006485 proxy_cb_context.NextSubcommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006486 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6487 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006488
6489 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6490 assert(recorded_context);
6491 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6492
6493 // The barriers have already been applied in ValidatFirstUse
6494 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6495 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006496 }
6497
John Zulaufae842002021-04-15 18:20:55 -06006498 return skip;
6499}
6500
6501void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6502 const VkCommandBuffer *pCommandBuffers) {
6503 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006504 auto *cb_context = GetAccessContext(commandBuffer);
6505 assert(cb_context);
6506 cb_context->NextCommandTag(CMD_EXECUTECOMMANDS);
6507 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
6508 cb_context->NextSubcommandTag(CMD_EXECUTECOMMANDS);
6509 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6510 if (!recorded_cb_context) continue;
6511 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6512 }
John Zulaufae842002021-04-15 18:20:55 -06006513}
6514
John Zulaufd0ec59f2021-03-13 14:25:08 -07006515AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6516 : view_(view), view_mask_(), gen_store_() {
6517 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6518 const IMAGE_STATE &image_state = *view_->image_state.get();
6519 const auto base_address = ResourceBaseAddress(image_state);
6520 const auto *encoder = image_state.fragment_encoder.get();
6521 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006522 // Get offset and extent for the view, accounting for possible depth slicing
6523 const VkOffset3D zero_offset = view->GetOffset();
6524 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006525 // Intentional copy
6526 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6527 view_mask_ = subres_range.aspectMask;
6528 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6529 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6530
6531 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6532 if (depth && (depth != view_mask_)) {
6533 subres_range.aspectMask = depth;
6534 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6535 }
6536 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6537 if (stencil && (stencil != view_mask_)) {
6538 subres_range.aspectMask = stencil;
6539 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6540 }
6541}
6542
6543const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6544 const ImageRangeGen *got = nullptr;
6545 switch (gen_type) {
6546 case kViewSubresource:
6547 got = &gen_store_[kViewSubresource];
6548 break;
6549 case kRenderArea:
6550 got = &gen_store_[kRenderArea];
6551 break;
6552 case kDepthOnlyRenderArea:
6553 got =
6554 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6555 break;
6556 case kStencilOnlyRenderArea:
6557 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6558 : &gen_store_[Gen::kStencilOnlyRenderArea];
6559 break;
6560 default:
6561 assert(got);
6562 }
6563 return got;
6564}
6565
6566AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6567 assert(IsValid());
6568 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6569 if (depth_op) {
6570 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6571 if (stencil_op) {
6572 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6573 return kRenderArea;
6574 }
6575 return kDepthOnlyRenderArea;
6576 }
6577 if (stencil_op) {
6578 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6579 return kStencilOnlyRenderArea;
6580 }
6581
6582 assert(depth_op || stencil_op);
6583 return kRenderArea;
6584}
6585
6586AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006587
6588void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6589 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6590 for (auto &event_pair : map_) {
6591 assert(event_pair.second); // Shouldn't be storing empty
6592 auto &sync_event = *event_pair.second;
6593 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6594 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6595 sync_event.barriers |= dst.exec_scope;
6596 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6597 }
6598 }
6599}