blob: d1c44d76afd4ed25080f8e074a34882b4e149b9e [file] [log] [blame]
John Zulaufab7756b2020-12-29 16:10:16 -07001/* Copyright (c) 2019-2021 The Khronos Group Inc.
2 * Copyright (c) 2019-2021 Valve Corporation
3 * Copyright (c) 2019-2021 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
233 const auto found_it = cb_execution_reference_.find(record.cb_state);
234 assert(found_it != cb_execution_reference_.end());
235 if (found_it != cb_execution_reference_.end()) {
236 out << ", reset_no: " << std::to_string(found_it->second.reset_count);
237 }
238 } else {
239 out << ", reset_no: " << std::to_string(reset_count_);
240 }
241 return out.str();
242}
243std::string CommandBufferAccessContext::FormatUsage(const ResourceFirstAccess &access) const {
244 std::stringstream out;
245 out << "(recorded_usage: " << string_UsageIndex(access.usage_index);
246 out << ", " << FormatUsage(access.tag) << ")";
247 return out.str();
248}
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700249
John Zulauffaea0ee2021-01-14 14:01:32 -0700250std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600251 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600252 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
253 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600254 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600255 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
256 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf4fa68462021-04-26 21:04:22 -0600257 out << "(";
258 if (!hazard.recorded_access.get()) {
259 // if we have a recorded usage the usage is reported from the recorded contexts point of view
260 out << "usage: " << usage_info.name << ", ";
261 }
262 out << "prior_usage: " << stage_access_name;
John Zulauf59e25072020-07-17 10:55:21 -0600263 if (IsHazardVsRead(hazard.hazard)) {
264 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700265 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600266 } else {
267 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
268 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
269 }
270
John Zulauf14940722021-04-12 15:19:02 -0600271 assert(tag < access_log_.size());
John Zulauf4fa68462021-04-26 21:04:22 -0600272 out << ", " << FormatUsage(tag) << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600273 return out.str();
274}
275
John Zulaufd14743a2020-07-03 09:42:39 -0600276// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
277// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
278// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700279static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700280static const SyncStageAccessFlags kColorAttachmentAccessScope =
281 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
282 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
283 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
284 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700285static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
286 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 -0700287static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
288 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
289 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
290 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700291static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700292static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600293
John Zulauf8e3c3e92021-01-06 11:19:36 -0700294ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700295 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700296 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
297 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
298 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
299
John Zulauf7635de32020-05-29 17:14:15 -0600300// 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 -0600301static const ResourceUsageTag kCurrentCommandTag(ResourceUsageRecord::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600302
Jeremy Gebben62c3bf42021-07-21 15:38:24 -0600303static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) { return bindable.GetFakeBaseAddress(); }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600304
locke-lunarg3c038002020-04-30 23:08:08 -0600305inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
306 if (size == VK_WHOLE_SIZE) {
307 return (whole_size - offset);
308 }
309 return size;
310}
311
John Zulauf3e86bf02020-09-12 10:47:57 -0600312static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
313 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
314}
315
John Zulauf16adfc92020-04-08 10:28:33 -0600316template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600317static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600318 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
319}
320
John Zulauf355e49b2020-04-24 15:11:15 -0600321static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600322
John Zulauf3e86bf02020-09-12 10:47:57 -0600323static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
324 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
325}
326
327static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
328 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
329}
330
John Zulauf4a6105a2020-11-17 15:11:05 -0700331// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
332//
John Zulauf10f1f522020-12-18 12:00:35 -0700333// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
334//
John Zulauf4a6105a2020-11-17 15:11:05 -0700335// Usage:
336// Constructor() -- initializes the generator to point to the begin of the space declared.
337// * -- the current range of the generator empty signfies end
338// ++ -- advance to the next non-empty range (or end)
339
340// A wrapper for a single range with the same semantics as the actual generators below
341template <typename KeyType>
342class SingleRangeGenerator {
343 public:
344 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700345 const KeyType &operator*() const { return current_; }
346 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700347 SingleRangeGenerator &operator++() {
348 current_ = KeyType(); // just one real range
349 return *this;
350 }
351
352 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
353
354 private:
355 SingleRangeGenerator() = default;
356 const KeyType range_;
357 KeyType current_;
358};
359
John Zulaufae842002021-04-15 18:20:55 -0600360// Generate the ranges that are the intersection of range and the entries in the RangeMap
361template <typename RangeMap, typename KeyType = typename RangeMap::key_type>
362class MapRangesRangeGenerator {
John Zulauf4a6105a2020-11-17 15:11:05 -0700363 public:
John Zulaufd5115702021-01-18 12:34:33 -0700364 // Default constructed is safe to dereference for "empty" test, but for no other operation.
John Zulaufae842002021-04-15 18:20:55 -0600365 MapRangesRangeGenerator() : range_(), map_(nullptr), map_pos_(), current_() {
John Zulaufd5115702021-01-18 12:34:33 -0700366 // Default construction for KeyType *must* be empty range
367 assert(current_.empty());
368 }
John Zulaufae842002021-04-15 18:20:55 -0600369 MapRangesRangeGenerator(const RangeMap &filter, const KeyType &range) : range_(range), map_(&filter), map_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700370 SeekBegin();
371 }
John Zulaufae842002021-04-15 18:20:55 -0600372 MapRangesRangeGenerator(const MapRangesRangeGenerator &from) = default;
John Zulaufd5115702021-01-18 12:34:33 -0700373
John Zulauf4a6105a2020-11-17 15:11:05 -0700374 const KeyType &operator*() const { return current_; }
375 const KeyType *operator->() const { return &current_; }
John Zulaufae842002021-04-15 18:20:55 -0600376 MapRangesRangeGenerator &operator++() {
377 ++map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700378 UpdateCurrent();
379 return *this;
380 }
381
John Zulaufae842002021-04-15 18:20:55 -0600382 bool operator==(const MapRangesRangeGenerator &other) const { return current_ == other.current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700383
John Zulaufae842002021-04-15 18:20:55 -0600384 protected:
John Zulauf4a6105a2020-11-17 15:11:05 -0700385 void UpdateCurrent() {
John Zulaufae842002021-04-15 18:20:55 -0600386 if (map_pos_ != map_->cend()) {
387 current_ = range_ & map_pos_->first;
John Zulauf4a6105a2020-11-17 15:11:05 -0700388 } else {
389 current_ = KeyType();
390 }
391 }
392 void SeekBegin() {
John Zulaufae842002021-04-15 18:20:55 -0600393 map_pos_ = map_->lower_bound(range_);
John Zulauf4a6105a2020-11-17 15:11:05 -0700394 UpdateCurrent();
395 }
John Zulaufae842002021-04-15 18:20:55 -0600396
397 // Adding this functionality here, to avoid gratuitous Base:: qualifiers in the derived class
398 // Note: Not exposed in this classes public interface to encourage using a consistent ++/empty generator semantic
399 template <typename Pred>
400 MapRangesRangeGenerator &PredicatedIncrement(Pred &pred) {
401 do {
402 ++map_pos_;
403 } while (map_pos_ != map_->cend() && map_pos_->first.intersects(range_) && !pred(map_pos_));
404 UpdateCurrent();
405 return *this;
406 }
407
John Zulauf4a6105a2020-11-17 15:11:05 -0700408 const KeyType range_;
John Zulaufae842002021-04-15 18:20:55 -0600409 const RangeMap *map_;
410 typename RangeMap::const_iterator map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700411 KeyType current_;
412};
John Zulaufd5115702021-01-18 12:34:33 -0700413using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulaufae842002021-04-15 18:20:55 -0600414using EventSimpleRangeGenerator = MapRangesRangeGenerator<SyncEventState::ScopeMap>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700415
John Zulaufae842002021-04-15 18:20:55 -0600416// Generate the ranges for entries meeting the predicate that are the intersection of range and the entries in the RangeMap
417template <typename RangeMap, typename Predicate, typename KeyType = typename RangeMap::key_type>
418class PredicatedMapRangesRangeGenerator : public MapRangesRangeGenerator<RangeMap, KeyType> {
419 public:
420 using Base = MapRangesRangeGenerator<RangeMap, KeyType>;
421 // Default constructed is safe to dereference for "empty" test, but for no other operation.
422 PredicatedMapRangesRangeGenerator() : Base(), pred_() {}
423 PredicatedMapRangesRangeGenerator(const RangeMap &filter, const KeyType &range, Predicate pred)
424 : Base(filter, range), pred_(pred) {}
425 PredicatedMapRangesRangeGenerator(const PredicatedMapRangesRangeGenerator &from) = default;
426
427 PredicatedMapRangesRangeGenerator &operator++() {
428 Base::PredicatedIncrement(pred_);
429 return *this;
430 }
431
432 protected:
433 Predicate pred_;
434};
John Zulauf4a6105a2020-11-17 15:11:05 -0700435
436// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulaufae842002021-04-15 18:20:55 -0600437// Templated to allow for different Range generators or map sources...
438template <typename RangeMap, typename RangeGen, typename KeyType = typename RangeMap::key_type>
John Zulauf4a6105a2020-11-17 15:11:05 -0700439class FilteredGeneratorGenerator {
440 public:
John Zulaufd5115702021-01-18 12:34:33 -0700441 // Default constructed is safe to dereference for "empty" test, but for no other operation.
442 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
443 // Default construction for KeyType *must* be empty range
444 assert(current_.empty());
445 }
John Zulaufae842002021-04-15 18:20:55 -0600446 FilteredGeneratorGenerator(const RangeMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700447 SeekBegin();
448 }
John Zulaufd5115702021-01-18 12:34:33 -0700449 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700450 const KeyType &operator*() const { return current_; }
451 const KeyType *operator->() const { return &current_; }
452 FilteredGeneratorGenerator &operator++() {
453 KeyType gen_range = GenRange();
454 KeyType filter_range = FilterRange();
455 current_ = KeyType();
456 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
457 if (gen_range.end > filter_range.end) {
458 // if the generated range is beyond the filter_range, advance the filter range
459 filter_range = AdvanceFilter();
460 } else {
461 gen_range = AdvanceGen();
462 }
463 current_ = gen_range & filter_range;
464 }
465 return *this;
466 }
467
468 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
469
470 private:
471 KeyType AdvanceFilter() {
472 ++filter_pos_;
473 auto filter_range = FilterRange();
474 if (filter_range.valid()) {
475 FastForwardGen(filter_range);
476 }
477 return filter_range;
478 }
479 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700480 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700481 auto gen_range = GenRange();
482 if (gen_range.valid()) {
483 FastForwardFilter(gen_range);
484 }
485 return gen_range;
486 }
487
488 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700489 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700490
491 KeyType FastForwardFilter(const KeyType &range) {
492 auto filter_range = FilterRange();
493 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700494 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700495 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
496 if (retry_count < kRetryLimit) {
497 ++filter_pos_;
498 filter_range = FilterRange();
499 retry_count++;
500 } else {
501 // Okay we've tried walking, do a seek.
502 filter_pos_ = filter_->lower_bound(range);
503 break;
504 }
505 }
506 return FilterRange();
507 }
508
509 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
510 // faster.
511 KeyType FastForwardGen(const KeyType &range) {
512 auto gen_range = GenRange();
513 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700514 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700515 gen_range = GenRange();
516 }
517 return gen_range;
518 }
519
520 void SeekBegin() {
521 auto gen_range = GenRange();
522 if (gen_range.empty()) {
523 current_ = KeyType();
524 filter_pos_ = filter_->cend();
525 } else {
526 filter_pos_ = filter_->lower_bound(gen_range);
527 current_ = gen_range & FilterRange();
528 }
529 }
530
John Zulaufae842002021-04-15 18:20:55 -0600531 const RangeMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700532 RangeGen gen_;
John Zulaufae842002021-04-15 18:20:55 -0600533 typename RangeMap::const_iterator filter_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700534 KeyType current_;
535};
536
537using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
538
John Zulauf5c5e88d2019-12-26 11:22:02 -0700539
John Zulauf3e86bf02020-09-12 10:47:57 -0600540ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
541 VkDeviceSize stride) {
542 VkDeviceSize range_start = offset + first_index * stride;
543 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600544 if (count == UINT32_MAX) {
545 range_size = buf_whole_size - range_start;
546 } else {
547 range_size = count * stride;
548 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600549 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600550}
551
locke-lunarg654e3692020-06-04 17:19:15 -0600552SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
553 VkShaderStageFlagBits stage_flag) {
554 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
555 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
556 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
557 }
558 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
559 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
560 assert(0);
561 }
562 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
563 return stage_access->second.uniform_read;
564 }
565
566 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
567 // Because if write hazard happens, read hazard might or might not happen.
568 // But if write hazard doesn't happen, read hazard is impossible to happen.
569 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700570 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600571 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700572 // TODO: sampled_read
573 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600574}
575
locke-lunarg37047832020-06-12 13:44:45 -0600576bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
577 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
578 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
579 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
580 ? true
581 : false;
582}
583
584bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
585 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
586 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
587 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
588 ? true
589 : false;
590}
591
John Zulauf355e49b2020-04-24 15:11:15 -0600592// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600593template <typename Action>
594static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
595 Action &action) {
596 // At this point the "apply over range" logic only supports a single memory binding
597 if (!SimpleBinding(image_state)) return;
598 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600599 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700600 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
601 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600602 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700603 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600604 }
605}
606
John Zulauf7635de32020-05-29 17:14:15 -0600607// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
608// Used by both validation and record operations
609//
610// The signature for Action() reflect the needs of both uses.
611template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700612void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
613 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600614 const auto &rp_ci = rp_state.createInfo;
615 const auto *attachment_ci = rp_ci.pAttachments;
616 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
617
618 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
619 const auto *color_attachments = subpass_ci.pColorAttachments;
620 const auto *color_resolve = subpass_ci.pResolveAttachments;
621 if (color_resolve && color_attachments) {
622 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
623 const auto &color_attach = color_attachments[i].attachment;
624 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
625 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
626 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700627 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
628 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600629 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700630 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
631 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600632 }
633 }
634 }
635
636 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700637 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600638 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
639 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
640 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
641 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
642 const auto src_ci = attachment_ci[src_at];
643 // The formats are required to match so we can pick either
644 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
645 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
646 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600647
648 // Figure out which aspects are actually touched during resolve operations
649 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700650 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600651 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600652 aspect_string = "depth/stencil";
653 } else if (resolve_depth) {
654 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700655 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600656 aspect_string = "depth";
657 } else if (resolve_stencil) {
658 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700659 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600660 aspect_string = "stencil";
661 }
662
John Zulaufd0ec59f2021-03-13 14:25:08 -0700663 if (aspect_string) {
664 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
665 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
666 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
667 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600668 }
669 }
670}
671
672// Action for validating resolve operations
673class ValidateResolveAction {
674 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700675 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700676 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600677 : render_pass_(render_pass),
678 subpass_(subpass),
679 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700680 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600681 func_name_(func_name),
682 skip_(false) {}
683 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 -0700684 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
685 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600686 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700687 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600688 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700689 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700690 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700691 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
692 " to resolve attachment %" PRIu32 ". Access info %s.",
693 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700694 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600695 }
696 }
697 // Providing a mechanism for the constructing caller to get the result of the validation
698 bool GetSkip() const { return skip_; }
699
700 private:
701 VkRenderPass render_pass_;
702 const uint32_t subpass_;
703 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700704 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600705 const char *func_name_;
706 bool skip_;
707};
708
709// Update action for resolve operations
710class UpdateStateResolveAction {
711 public:
John Zulauf14940722021-04-12 15:19:02 -0600712 UpdateStateResolveAction(AccessContext &context, ResourceUsageTag tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700713 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
714 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600715 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700716 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600717 }
718
719 private:
720 AccessContext &context_;
John Zulauf14940722021-04-12 15:19:02 -0600721 const ResourceUsageTag tag_;
John Zulauf7635de32020-05-29 17:14:15 -0600722};
723
John Zulauf59e25072020-07-17 10:55:21 -0600724void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
John Zulauf14940722021-04-12 15:19:02 -0600725 const SyncStageAccessFlags &prior_, const ResourceUsageTag tag_) {
John Zulauf4fa68462021-04-26 21:04:22 -0600726 access_state = layer_data::make_unique<const ResourceAccessState>(*access_state_);
John Zulauf59e25072020-07-17 10:55:21 -0600727 usage_index = usage_index_;
728 hazard = hazard_;
729 prior_access = prior_;
730 tag = tag_;
731}
732
John Zulauf4fa68462021-04-26 21:04:22 -0600733void HazardResult::AddRecordedAccess(const ResourceFirstAccess &first_access) {
734 recorded_access = layer_data::make_unique<const ResourceFirstAccess>(first_access);
735}
736
John Zulauf540266b2020-04-06 18:54:53 -0600737AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
738 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600739 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600740 Reset();
741 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700742 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
743 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600744 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600745 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600746 const auto prev_pass = prev_dep.first->pass;
747 const auto &prev_barriers = prev_dep.second;
748 assert(prev_dep.second.size());
749 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
750 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700751 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600752
753 async_.reserve(subpass_dep.async.size());
754 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700755 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600756 }
John Zulauf22aefed2021-03-11 18:14:35 -0700757 if (has_barrier_from_external) {
758 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
759 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
760 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600761 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600762 if (subpass_dep.barrier_to_external.size()) {
763 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600764 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700765}
766
John Zulauf5f13a792020-03-10 07:31:21 -0600767template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700768HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600769 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600770 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600771 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600772
773 HazardResult hazard;
774 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
775 hazard = detector.Detect(prev);
776 }
777 return hazard;
778}
779
John Zulauf4a6105a2020-11-17 15:11:05 -0700780template <typename Action>
781void AccessContext::ForAll(Action &&action) {
782 for (const auto address_type : kAddressTypes) {
783 auto &accesses = GetAccessStateMap(address_type);
784 for (const auto &access : accesses) {
785 action(address_type, access);
786 }
787 }
788}
789
John Zulauf3d84f1b2020-03-09 13:33:25 -0600790// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
791// the DAG of the contexts (for example subpasses)
792template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700793HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600794 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600795 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600796
John Zulauf1a224292020-06-30 14:52:13 -0600797 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600798 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
799 // so we'll check these first
800 for (const auto &async_context : async_) {
801 hazard = async_context->DetectAsyncHazard(type, detector, range);
802 if (hazard.hazard) return hazard;
803 }
John Zulauf5f13a792020-03-10 07:31:21 -0600804 }
805
John Zulauf1a224292020-06-30 14:52:13 -0600806 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600807
John Zulauf69133422020-05-20 14:55:53 -0600808 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600809 const auto the_end = accesses.cend(); // End is not invalidated
810 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600811 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600812
John Zulauf3cafbf72021-03-26 16:55:19 -0600813 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600814 // Cover any leading gap, or gap between entries
815 if (detect_prev) {
816 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
817 // Cover any leading gap, or gap between entries
818 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600819 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600820 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600821 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600822 if (hazard.hazard) return hazard;
823 }
John Zulauf69133422020-05-20 14:55:53 -0600824 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
825 gap.begin = pos->first.end;
826 }
827
828 hazard = detector.Detect(pos);
829 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600830 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600831 }
832
833 if (detect_prev) {
834 // Detect in the trailing empty as needed
835 gap.end = range.end;
836 if (gap.non_empty()) {
837 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600838 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600839 }
840
841 return hazard;
842}
843
844// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
845template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700846HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
847 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600848 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600849 auto pos = accesses.lower_bound(range);
850 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600851
John Zulauf3d84f1b2020-03-09 13:33:25 -0600852 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600853 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700854 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600855 if (hazard.hazard) break;
856 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600857 }
John Zulauf16adfc92020-04-08 10:28:33 -0600858
John Zulauf3d84f1b2020-03-09 13:33:25 -0600859 return hazard;
860}
861
John Zulaufb02c1eb2020-10-06 16:33:36 -0600862struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700863 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600864 void operator()(ResourceAccessState *access) const {
865 assert(access);
866 access->ApplyBarriers(barriers, true);
867 }
868 const std::vector<SyncBarrier> &barriers;
869};
870
John Zulauf22aefed2021-03-11 18:14:35 -0700871struct ApplyTrackbackStackAction {
872 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
873 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
874 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600875 void operator()(ResourceAccessState *access) const {
876 assert(access);
877 assert(!access->HasPendingState());
878 access->ApplyBarriers(barriers, false);
879 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700880 if (previous_barrier) {
881 assert(bool(*previous_barrier));
882 (*previous_barrier)(access);
883 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600884 }
885 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700886 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600887};
888
889// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
890// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
891// *different* map from dest.
892// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
893// range [first, last)
894template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600895static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
896 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600897 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600898 auto at = entry;
899 for (auto pos = first; pos != last; ++pos) {
900 // Every member of the input iterator range must fit within the remaining portion of entry
901 assert(at->first.includes(pos->first));
902 assert(at != dest->end());
903 // Trim up at to the same size as the entry to resolve
904 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600905 auto access = pos->second; // intentional copy
906 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600907 at->second.Resolve(access);
908 ++at; // Go to the remaining unused section of entry
909 }
910}
911
John Zulaufa0a98292020-09-18 09:30:10 -0600912static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
913 SyncBarrier merged = {};
914 for (const auto &barrier : barriers) {
915 merged.Merge(barrier);
916 }
917 return merged;
918}
919
John Zulaufb02c1eb2020-10-06 16:33:36 -0600920template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700921void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600922 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
923 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600924 if (!range.non_empty()) return;
925
John Zulauf355e49b2020-04-24 15:11:15 -0600926 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
927 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600928 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600929 if (current->pos_B->valid) {
930 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600931 auto access = src_pos->second; // intentional copy
932 barrier_action(&access);
933
John Zulauf16adfc92020-04-08 10:28:33 -0600934 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600935 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
936 trimmed->second.Resolve(access);
937 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600938 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600939 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600940 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600941 }
John Zulauf16adfc92020-04-08 10:28:33 -0600942 } else {
943 // we have to descend to fill this gap
944 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700945 ResourceAccessRange recurrence_range = current_range;
946 // The current context is empty for the current range, so recur to fill the gap.
947 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
948 // is not valid, to minimize that recurrence
949 if (current->pos_B.at_end()) {
950 // Do the remainder here....
951 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600952 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700953 // Recur only over the range until B becomes valid (within the limits of range).
954 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600955 }
John Zulauf22aefed2021-03-11 18:14:35 -0700956 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
957
John Zulauf355e49b2020-04-24 15:11:15 -0600958 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
959 // iterator of the outer while.
960
961 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
962 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
963 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700964 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 -0600965 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600966 current.seek(seek_to);
967 } else if (!current->pos_A->valid && infill_state) {
968 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
969 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
970 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600971 }
John Zulauf5f13a792020-03-10 07:31:21 -0600972 }
John Zulauf16adfc92020-04-08 10:28:33 -0600973 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600974 }
John Zulauf1a224292020-06-30 14:52:13 -0600975
976 // Infill if range goes passed both the current and resolve map prior contents
977 if (recur_to_infill && (current->range.end < range.end)) {
978 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700979 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600980 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600981}
982
John Zulauf22aefed2021-03-11 18:14:35 -0700983template <typename BarrierAction>
984void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
985 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
986 const BarrierAction &previous_barrier) const {
987 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
988 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
989}
990
John Zulauf43cc7462020-12-03 12:33:12 -0700991void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700992 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
993 const ResourceAccessStateFunction *previous_barrier) const {
994 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600995 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700996 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
997 ResourceAccessState state_copy;
998 if (previous_barrier) {
999 assert(bool(*previous_barrier));
1000 state_copy = *infill_state;
1001 (*previous_barrier)(&state_copy);
1002 infill_state = &state_copy;
1003 }
1004 sparse_container::update_range_value(*descent_map, range, *infill_state,
1005 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -06001006 }
1007 } else {
1008 // Look for something to fill the gap further along.
1009 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -07001010 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001011 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001012 }
John Zulauf5f13a792020-03-10 07:31:21 -06001013 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06001014}
1015
John Zulauf4a6105a2020-11-17 15:11:05 -07001016// Non-lazy import of all accesses, WaitEvents needs this.
1017void AccessContext::ResolvePreviousAccesses() {
1018 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -07001019 if (!prev_.size()) return; // If no previous contexts, nothing to do
1020
John Zulauf4a6105a2020-11-17 15:11:05 -07001021 for (const auto address_type : kAddressTypes) {
1022 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
1023 }
1024}
1025
John Zulauf43cc7462020-12-03 12:33:12 -07001026AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
1027 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -06001028}
1029
John Zulauf1507ee42020-05-18 11:33:09 -06001030static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001031 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1032 ? SYNC_ACCESS_INDEX_NONE
1033 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
1034 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001035 return stage_access;
1036}
1037static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -06001038 const auto stage_access =
1039 (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
1040 ? SYNC_ACCESS_INDEX_NONE
1041 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
1042 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -06001043 return stage_access;
1044}
1045
John Zulauf7635de32020-05-29 17:14:15 -06001046// Caller must manage returned pointer
1047static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001048 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -06001049 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001050 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
1051 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -06001052 return proxy;
1053}
1054
John Zulaufb02c1eb2020-10-06 16:33:36 -06001055template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001056void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1057 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
1058 const ResourceAccessState *infill_state) const {
1059 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1060 if (!attachment_gen) return;
1061
1062 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1063 const AccessAddressType address_type = view_gen.GetAddressType();
1064 for (; range_gen->non_empty(); ++range_gen) {
1065 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001066 }
John Zulauf62f10592020-04-03 12:20:02 -06001067}
1068
John Zulauf7635de32020-05-29 17:14:15 -06001069// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -07001070bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001071 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001072 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06001073 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -06001074 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
1075 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
1076 // those affects have not been recorded yet.
1077 //
1078 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1079 // to apply and only copy then, if this proves a hot spot.
1080 std::unique_ptr<AccessContext> proxy_for_prev;
1081 TrackBack proxy_track_back;
1082
John Zulauf355e49b2020-04-24 15:11:15 -06001083 const auto &transitions = rp_state.subpass_transitions[subpass];
1084 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001085 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1086
1087 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001088 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001089 if (prev_needs_proxy) {
1090 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001091 proxy_for_prev.reset(
1092 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -06001093 proxy_track_back = *track_back;
1094 proxy_track_back.context = proxy_for_prev.get();
1095 }
1096 track_back = &proxy_track_back;
1097 }
1098 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001099 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001100 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001101 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1102 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1103 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1104 string_VkImageLayout(transition.old_layout),
1105 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001106 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001107 }
1108 }
1109 return skip;
1110}
1111
John Zulauf64ffe552021-02-06 10:25:07 -07001112bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001113 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001114 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001115 bool skip = false;
1116 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001117
John Zulauf1507ee42020-05-18 11:33:09 -06001118 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1119 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001120 const auto &view_gen = attachment_views[i];
1121 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001122 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001123
1124 // Need check in the following way
1125 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1126 // vs. transition
1127 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1128 // for each aspect loaded.
1129
1130 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001131 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001132 const bool is_color = !(has_depth || has_stencil);
1133
1134 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001135 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001136
John Zulaufaff20662020-06-01 14:07:58 -06001137 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001138 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001139
John Zulaufb02c1eb2020-10-06 16:33:36 -06001140 bool checked_stencil = false;
John Zulauf57261402021-08-13 11:32:06 -06001141 if (is_color && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001142 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001143 aspect = "color";
1144 } else {
John Zulauf57261402021-08-13 11:32:06 -06001145 if (has_depth && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001146 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1147 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001148 aspect = "depth";
1149 }
John Zulauf57261402021-08-13 11:32:06 -06001150 if (!hazard.hazard && has_stencil && (stencil_load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001151 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1152 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001153 aspect = "stencil";
1154 checked_stencil = true;
1155 }
1156 }
1157
1158 if (hazard.hazard) {
1159 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001160 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001161 if (hazard.tag == kCurrentCommandTag) {
1162 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001163 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001164 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1165 " aspect %s during load with loadOp %s.",
1166 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1167 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001168 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001169 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001170 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001171 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001172 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001173 }
1174 }
1175 }
1176 }
1177 return skip;
1178}
1179
John Zulaufaff20662020-06-01 14:07:58 -06001180// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1181// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1182// store is part of the same Next/End operation.
1183// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001184bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001185 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001186 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001187 bool skip = false;
1188 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001189
1190 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1191 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001192 const AttachmentViewGen &view_gen = attachment_views[i];
1193 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001194 const auto &ci = attachment_ci[i];
1195
1196 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1197 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1198 // sake, we treat DONT_CARE as writing.
1199 const bool has_depth = FormatHasDepth(ci.format);
1200 const bool has_stencil = FormatHasStencil(ci.format);
1201 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001202 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001203 if (!has_stencil && !store_op_stores) continue;
1204
1205 HazardResult hazard;
1206 const char *aspect = nullptr;
1207 bool checked_stencil = false;
1208 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001209 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1210 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001211 aspect = "color";
1212 } else {
John Zulauf57261402021-08-13 11:32:06 -06001213 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001214 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001215 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1216 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001217 aspect = "depth";
1218 }
1219 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001220 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1221 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001222 aspect = "stencil";
1223 checked_stencil = true;
1224 }
1225 }
1226
1227 if (hazard.hazard) {
1228 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1229 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001230 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001231 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1232 " %s aspect during store with %s %s. Access info %s",
1233 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001234 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001235 }
1236 }
1237 }
1238 return skip;
1239}
1240
John Zulauf64ffe552021-02-06 10:25:07 -07001241bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001242 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1243 const char *func_name, uint32_t subpass) const {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001244 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001245 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001246 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001247}
1248
John Zulauf3d84f1b2020-03-09 13:33:25 -06001249class HazardDetector {
1250 SyncStageAccessIndex usage_index_;
1251
1252 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001253 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf14940722021-04-12 15:19:02 -06001254 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001255 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001256 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001257 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001258};
1259
John Zulauf69133422020-05-20 14:55:53 -06001260class HazardDetectorWithOrdering {
1261 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001262 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001263
1264 public:
1265 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001266 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001267 }
John Zulauf14940722021-04-12 15:19:02 -06001268 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001269 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001270 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001271 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001272};
1273
John Zulauf16adfc92020-04-08 10:28:33 -06001274HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001275 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001276 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001277 const auto base_address = ResourceBaseAddress(buffer);
1278 HazardDetector detector(usage_index);
1279 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001280}
1281
John Zulauf69133422020-05-20 14:55:53 -06001282template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001283HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1284 DetectOptions options) const {
1285 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1286 if (!attachment_gen) return HazardResult();
1287
1288 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1289 const auto address_type = view_gen.GetAddressType();
1290 for (; range_gen->non_empty(); ++range_gen) {
1291 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1292 if (hazard.hazard) return hazard;
1293 }
1294
1295 return HazardResult();
1296}
1297
1298template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001299HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1300 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1301 const VkExtent3D &extent, DetectOptions options) const {
1302 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001303 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001304 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1305 base_address);
1306 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001307 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001308 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001309 if (hazard.hazard) return hazard;
1310 }
1311 return HazardResult();
1312}
John Zulauf110413c2021-03-20 05:38:38 -06001313template <typename Detector>
1314HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1315 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1316 if (!SimpleBinding(image)) return HazardResult();
1317 const auto base_address = ResourceBaseAddress(image);
1318 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1319 const auto address_type = ImageAddressType(image);
1320 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001321 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1322 if (hazard.hazard) return hazard;
1323 }
1324 return HazardResult();
1325}
John Zulauf69133422020-05-20 14:55:53 -06001326
John Zulauf540266b2020-04-06 18:54:53 -06001327HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1328 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1329 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001330 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1331 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001332 HazardDetector detector(current_usage);
1333 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001334}
1335
1336HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001337 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001338 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001339 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001340}
1341
John Zulaufd0ec59f2021-03-13 14:25:08 -07001342HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1343 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1344 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1345 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1346}
1347
John Zulauf69133422020-05-20 14:55:53 -06001348HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001349 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001350 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001351 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001352 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001353}
1354
John Zulauf3d84f1b2020-03-09 13:33:25 -06001355class BarrierHazardDetector {
1356 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001357 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001358 SyncStageAccessFlags src_access_scope)
1359 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1360
John Zulauf5f13a792020-03-10 07:31:21 -06001361 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1362 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001363 }
John Zulauf14940722021-04-12 15:19:02 -06001364 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001365 // 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 -07001366 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001367 }
1368
1369 private:
1370 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001371 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001372 SyncStageAccessFlags src_access_scope_;
1373};
1374
John Zulauf4a6105a2020-11-17 15:11:05 -07001375class EventBarrierHazardDetector {
1376 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001377 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001378 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
John Zulauf14940722021-04-12 15:19:02 -06001379 ResourceUsageTag scope_tag)
John Zulauf4a6105a2020-11-17 15:11:05 -07001380 : usage_index_(usage_index),
1381 src_exec_scope_(src_exec_scope),
1382 src_access_scope_(src_access_scope),
1383 event_scope_(event_scope),
1384 scope_pos_(event_scope.cbegin()),
1385 scope_end_(event_scope.cend()),
1386 scope_tag_(scope_tag) {}
1387
1388 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1389 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1390 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1391 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1392 if (scope_pos_ == scope_end_) return HazardResult();
1393 if (!scope_pos_->first.intersects(pos->first)) {
1394 event_scope_.lower_bound(pos->first);
1395 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1396 }
1397
1398 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1399 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1400 }
John Zulauf14940722021-04-12 15:19:02 -06001401 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07001402 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1403 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1404 }
1405
1406 private:
1407 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001408 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001409 SyncStageAccessFlags src_access_scope_;
1410 const SyncEventState::ScopeMap &event_scope_;
1411 SyncEventState::ScopeMap::const_iterator scope_pos_;
1412 SyncEventState::ScopeMap::const_iterator scope_end_;
John Zulauf14940722021-04-12 15:19:02 -06001413 const ResourceUsageTag scope_tag_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001414};
1415
Jeremy Gebben40a22942020-12-22 14:22:06 -07001416HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001417 const SyncStageAccessFlags &src_access_scope,
1418 const VkImageSubresourceRange &subresource_range,
1419 const SyncEventState &sync_event, DetectOptions options) const {
1420 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1421 // first access scope map to use, and there's no easy way to plumb it in below.
1422 const auto address_type = ImageAddressType(image);
1423 const auto &event_scope = sync_event.FirstScope(address_type);
1424
1425 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1426 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001427 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001428}
1429
John Zulaufd0ec59f2021-03-13 14:25:08 -07001430HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1431 DetectOptions options) const {
1432 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1433 barrier.src_access_scope);
1434 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1435}
1436
Jeremy Gebben40a22942020-12-22 14:22:06 -07001437HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001438 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001439 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001440 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001441 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001442 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001443}
1444
Jeremy Gebben40a22942020-12-22 14:22:06 -07001445HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001446 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001447 const VkImageMemoryBarrier &barrier) const {
1448 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1449 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1450 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1451}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001452HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001453 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001454 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001455}
John Zulauf355e49b2020-04-24 15:11:15 -06001456
John Zulauf9cb530d2019-09-30 14:14:10 -06001457template <typename Flags, typename Map>
1458SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1459 SyncStageAccessFlags scope = 0;
1460 for (const auto &bit_scope : map) {
1461 if (flag_mask < bit_scope.first) break;
1462
1463 if (flag_mask & bit_scope.first) {
1464 scope |= bit_scope.second;
1465 }
1466 }
1467 return scope;
1468}
1469
Jeremy Gebben40a22942020-12-22 14:22:06 -07001470SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001471 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1472}
1473
Jeremy Gebben40a22942020-12-22 14:22:06 -07001474SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1475 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001476}
1477
Jeremy Gebben40a22942020-12-22 14:22:06 -07001478// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1479SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001480 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1481 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1482 // 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 -06001483 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1484}
1485
1486template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001487void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001488 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1489 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001490 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001491 auto pos = accesses->lower_bound(range);
1492 if (pos == accesses->end() || !pos->first.intersects(range)) {
1493 // The range is empty, fill it with a default value.
1494 pos = action.Infill(accesses, pos, range);
1495 } else if (range.begin < pos->first.begin) {
1496 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001497 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001498 } else if (pos->first.begin < range.begin) {
1499 // Trim the beginning if needed
1500 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1501 ++pos;
1502 }
1503
1504 const auto the_end = accesses->end();
1505 while ((pos != the_end) && pos->first.intersects(range)) {
1506 if (pos->first.end > range.end) {
1507 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1508 }
1509
1510 pos = action(accesses, pos);
1511 if (pos == the_end) break;
1512
1513 auto next = pos;
1514 ++next;
1515 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1516 // Need to infill if next is disjoint
1517 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001518 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001519 next = action.Infill(accesses, next, new_range);
1520 }
1521 pos = next;
1522 }
1523}
John Zulaufd5115702021-01-18 12:34:33 -07001524
1525// Give a comparable interface for range generators and ranges
1526template <typename Action>
1527inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1528 assert(range);
1529 UpdateMemoryAccessState(accesses, *range, action);
1530}
1531
John Zulauf4a6105a2020-11-17 15:11:05 -07001532template <typename Action, typename RangeGen>
1533void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1534 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001535 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 -07001536 for (; range_gen->non_empty(); ++range_gen) {
1537 UpdateMemoryAccessState(accesses, *range_gen, action);
1538 }
1539}
John Zulauf9cb530d2019-09-30 14:14:10 -06001540
John Zulaufd0ec59f2021-03-13 14:25:08 -07001541template <typename Action, typename RangeGen>
1542void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1543 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1544 for (; range_gen->non_empty(); ++range_gen) {
1545 UpdateMemoryAccessState(accesses, *range_gen, action);
1546 }
1547}
John Zulauf9cb530d2019-09-30 14:14:10 -06001548struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001549 using Iterator = ResourceAccessRangeMap::iterator;
1550 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001551 // this is only called on gaps, and never returns a gap.
1552 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001553 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001554 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001555 }
John Zulauf5f13a792020-03-10 07:31:21 -06001556
John Zulauf5c5e88d2019-12-26 11:22:02 -07001557 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001558 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001559 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001560 return pos;
1561 }
1562
John Zulauf43cc7462020-12-03 12:33:12 -07001563 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf14940722021-04-12 15:19:02 -06001564 SyncOrdering ordering_rule_, ResourceUsageTag tag_)
John Zulauf8e3c3e92021-01-06 11:19:36 -07001565 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001566 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001567 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001568 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001569 const SyncOrdering ordering_rule;
John Zulauf14940722021-04-12 15:19:02 -06001570 const ResourceUsageTag tag;
John Zulauf9cb530d2019-09-30 14:14:10 -06001571};
1572
John Zulauf4a6105a2020-11-17 15:11:05 -07001573// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001574struct PipelineBarrierOp {
1575 SyncBarrier barrier;
1576 bool layout_transition;
1577 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1578 : barrier(barrier_), layout_transition(layout_transition_) {}
1579 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001580 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001581 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1582};
John Zulauf4a6105a2020-11-17 15:11:05 -07001583// The barrier operation for wait events
1584struct WaitEventBarrierOp {
John Zulauf14940722021-04-12 15:19:02 -06001585 ResourceUsageTag scope_tag;
John Zulauf4a6105a2020-11-17 15:11:05 -07001586 SyncBarrier barrier;
1587 bool layout_transition;
John Zulauf14940722021-04-12 15:19:02 -06001588 WaitEventBarrierOp(const ResourceUsageTag scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1589 : scope_tag(scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
John Zulauf4a6105a2020-11-17 15:11:05 -07001590 WaitEventBarrierOp() = default;
John Zulauf14940722021-04-12 15:19:02 -06001591 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(scope_tag, barrier, layout_transition); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001592};
John Zulauf1e331ec2020-12-04 18:29:38 -07001593
John Zulauf4a6105a2020-11-17 15:11:05 -07001594// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1595// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1596// of a collection is known/present.
John Zulauf5c628d02021-05-04 15:46:36 -06001597template <typename BarrierOp, typename OpVector = std::vector<BarrierOp>>
John Zulauf89311b42020-09-29 16:28:47 -06001598class ApplyBarrierOpsFunctor {
1599 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001600 using Iterator = ResourceAccessRangeMap::iterator;
John Zulauf5c628d02021-05-04 15:46:36 -06001601 // Only called with a gap, and pos at the lower_bound(range)
1602 inline Iterator Infill(ResourceAccessRangeMap *accesses, const Iterator &pos, const ResourceAccessRange &range) const {
1603 if (!infill_default_) {
1604 return pos;
1605 }
1606 ResourceAccessState default_state;
1607 auto inserted = accesses->insert(pos, std::make_pair(range, default_state));
1608 return inserted;
1609 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001610
John Zulauf5c628d02021-05-04 15:46:36 -06001611 Iterator operator()(ResourceAccessRangeMap *accesses, const Iterator &pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001612 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001613 for (const auto &op : barrier_ops_) {
1614 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001615 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001616
John Zulauf89311b42020-09-29 16:28:47 -06001617 if (resolve_) {
1618 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1619 // another walk
1620 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001621 }
1622 return pos;
1623 }
1624
John Zulauf89311b42020-09-29 16:28:47 -06001625 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf5c628d02021-05-04 15:46:36 -06001626 ApplyBarrierOpsFunctor(bool resolve, typename OpVector::size_type size_hint, ResourceUsageTag tag)
1627 : resolve_(resolve), infill_default_(false), barrier_ops_(), tag_(tag) {
John Zulaufd5115702021-01-18 12:34:33 -07001628 barrier_ops_.reserve(size_hint);
1629 }
John Zulauf5c628d02021-05-04 15:46:36 -06001630 void EmplaceBack(const BarrierOp &op) {
1631 barrier_ops_.emplace_back(op);
1632 infill_default_ |= op.layout_transition;
1633 }
John Zulauf89311b42020-09-29 16:28:47 -06001634
1635 private:
1636 bool resolve_;
John Zulauf5c628d02021-05-04 15:46:36 -06001637 bool infill_default_;
1638 OpVector barrier_ops_;
John Zulauf14940722021-04-12 15:19:02 -06001639 const ResourceUsageTag tag_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001640};
1641
John Zulauf4a6105a2020-11-17 15:11:05 -07001642// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1643// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1644template <typename BarrierOp>
John Zulauf5c628d02021-05-04 15:46:36 -06001645class ApplyBarrierFunctor : public ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>> {
1646 using Base = ApplyBarrierOpsFunctor<BarrierOp, small_vector<BarrierOp, 1>>;
1647
John Zulauf4a6105a2020-11-17 15:11:05 -07001648 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001649 ApplyBarrierFunctor(const BarrierOp &barrier_op) : Base(false, 1, kCurrentCommandTag) { Base::EmplaceBack(barrier_op); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001650};
1651
John Zulauf1e331ec2020-12-04 18:29:38 -07001652// This functor resolves the pendinging state.
John Zulauf5c628d02021-05-04 15:46:36 -06001653class ResolvePendingBarrierFunctor : public ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>> {
1654 using Base = ApplyBarrierOpsFunctor<NoopBarrierAction, small_vector<NoopBarrierAction, 1>>;
1655
John Zulauf1e331ec2020-12-04 18:29:38 -07001656 public:
John Zulauf5c628d02021-05-04 15:46:36 -06001657 ResolvePendingBarrierFunctor(ResourceUsageTag tag) : Base(true, 0, tag) {}
John Zulauf9cb530d2019-09-30 14:14:10 -06001658};
1659
John Zulauf8e3c3e92021-01-06 11:19:36 -07001660void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001661 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001662 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001663 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001664}
1665
John Zulauf8e3c3e92021-01-06 11:19:36 -07001666void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001667 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001668 if (!SimpleBinding(buffer)) return;
1669 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001670 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001671}
John Zulauf355e49b2020-04-24 15:11:15 -06001672
John Zulauf8e3c3e92021-01-06 11:19:36 -07001673void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001674 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1675 if (!SimpleBinding(image)) return;
1676 const auto base_address = ResourceBaseAddress(image);
1677 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1678 const auto address_type = ImageAddressType(image);
1679 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1680 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1681}
1682void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001683 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001684 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001685 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001686 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001687 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1688 base_address);
1689 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001690 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001691 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001692}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001693
1694void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
John Zulauf14940722021-04-12 15:19:02 -06001695 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001696 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1697 if (!gen) return;
1698 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1699 const auto address_type = view_gen.GetAddressType();
1700 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1701 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001702}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001703
John Zulauf8e3c3e92021-01-06 11:19:36 -07001704void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001705 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001706 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001707 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1708 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001709 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001710}
1711
John Zulaufd0ec59f2021-03-13 14:25:08 -07001712template <typename Action, typename RangeGen>
1713void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1714 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1715 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001716}
1717
1718template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001719void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1720 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1721 if (!gen) return;
1722 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001723}
1724
John Zulaufd0ec59f2021-03-13 14:25:08 -07001725void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1726 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001727 const ResourceUsageTag tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001728 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001729 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001730}
1731
John Zulaufd0ec59f2021-03-13 14:25:08 -07001732void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06001733 uint32_t subpass, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001734 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001735
1736 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1737 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001738 const auto &view_gen = attachment_views[i];
1739 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001740
1741 const auto &ci = attachment_ci[i];
1742 const bool has_depth = FormatHasDepth(ci.format);
1743 const bool has_stencil = FormatHasStencil(ci.format);
1744 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001745 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001746
1747 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001748 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1749 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001750 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001751 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001752 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1753 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001754 }
John Zulauf57261402021-08-13 11:32:06 -06001755 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001756 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001757 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1758 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001759 }
1760 }
1761 }
1762 }
1763}
1764
John Zulauf540266b2020-04-06 18:54:53 -06001765template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001766void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001767 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001768 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001769 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001770 }
1771}
1772
1773void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001774 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1775 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001776 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001777 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001778 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001779 }
1780 }
1781}
1782
John Zulauf4fa68462021-04-26 21:04:22 -06001783// Caller must ensure that lifespan of this is less than from
1784void AccessContext::ImportAsyncContexts(const AccessContext &from) { async_ = from.async_; }
1785
John Zulauf355e49b2020-04-24 15:11:15 -06001786// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001787HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1788 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001789
John Zulauf355e49b2020-04-24 15:11:15 -06001790 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001791 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001792
1793 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001794 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1795 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001796 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001797 if (!hazard.hazard) {
1798 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001799 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001800 }
John Zulaufa0a98292020-09-18 09:30:10 -06001801
John Zulauf355e49b2020-04-24 15:11:15 -06001802 return hazard;
1803}
1804
John Zulaufb02c1eb2020-10-06 16:33:36 -06001805void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001806 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001807 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001808 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001809 for (const auto &transition : transitions) {
1810 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001811 const auto &view_gen = attachment_views[transition.attachment];
1812 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001813
1814 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1815 assert(trackback);
1816
1817 // Import the attachments into the current context
1818 const auto *prev_context = trackback->context;
1819 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001820 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001821 auto &target_map = GetAccessStateMap(address_type);
1822 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001823 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1824 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001825 }
1826
John Zulauf86356ca2020-10-19 11:46:41 -06001827 // If there were no transitions skip this global map walk
1828 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001829 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001830 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001831 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001832}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001833
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001834void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulauf669dfd52021-01-27 17:15:28 -07001835 auto *events_context = GetCurrentEventsContext();
1836 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06001837 events_context->ApplyBarrier(src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001838}
1839
locke-lunarg61870c22020-06-09 14:51:50 -06001840bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1841 const char *func_name) const {
1842 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001843 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001844 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001845 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001846 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001847 return skip;
1848 }
1849
1850 using DescriptorClass = cvdescriptorset::DescriptorClass;
1851 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1852 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1853 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1854 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1855
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001856 for (const auto &stage_state : pipe->stage_state) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001857 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1858 pipe->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001859 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001860 }
locke-lunarg61870c22020-06-09 14:51:50 -06001861 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001862 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set;
locke-lunarg61870c22020-06-09 14:51:50 -06001863 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001864 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001865 const auto descriptor_type = binding_it.GetType();
1866 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1867 auto array_idx = 0;
1868
1869 if (binding_it.IsVariableDescriptorCount()) {
1870 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1871 }
1872 SyncStageAccessIndex sync_index =
1873 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1874
1875 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1876 uint32_t index = i - index_range.start;
1877 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1878 switch (descriptor->GetClass()) {
1879 case DescriptorClass::ImageSampler:
1880 case DescriptorClass::Image: {
1881 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001882 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001883 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001884 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1885 img_view_state = image_sampler_descriptor->GetImageViewState();
1886 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001887 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001888 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1889 img_view_state = image_descriptor->GetImageViewState();
1890 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001891 }
1892 if (!img_view_state) continue;
John Zulauf361fb532020-07-22 10:45:39 -06001893 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001894 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1895 // Descriptors, so we do not have to worry about depth slicing here.
1896 // See: VUID 00343
1897 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001898 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001899 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001900
1901 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1902 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1903 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001904 // Input attachments are subject to raster ordering rules
1905 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001906 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001907 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001908 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001909 }
John Zulauf110413c2021-03-20 05:38:38 -06001910
John Zulauf33fc1d52020-07-17 11:01:10 -06001911 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001912 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001913 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001914 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1915 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001916 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001917 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1918 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1919 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001920 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1921 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001922 set_binding.first.binding, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001923 }
1924 break;
1925 }
1926 case DescriptorClass::TexelBuffer: {
1927 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1928 if (!buf_view_state) continue;
1929 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001930 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001931 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001932 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001933 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001934 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001935 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1936 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001937 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1938 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1939 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001940 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001941 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001942 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001943 }
1944 break;
1945 }
1946 case DescriptorClass::GeneralBuffer: {
1947 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1948 auto buf_state = buffer_descriptor->GetBufferState();
1949 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001950 const ResourceAccessRange range =
1951 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001952 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001953 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001954 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001955 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001956 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1957 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001958 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1959 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1960 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001961 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001962 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001963 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001964 }
1965 break;
1966 }
1967 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1968 default:
1969 break;
1970 }
1971 }
1972 }
1973 }
1974 return skip;
1975}
1976
1977void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
John Zulauf14940722021-04-12 15:19:02 -06001978 const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001979 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001980 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001981 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001982 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001983 return;
1984 }
1985
1986 using DescriptorClass = cvdescriptorset::DescriptorClass;
1987 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1988 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1989 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1990 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1991
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001992 for (const auto &stage_state : pipe->stage_state) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001993 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1994 pipe->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001995 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001996 }
locke-lunarg61870c22020-06-09 14:51:50 -06001997 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001998 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set;
locke-lunarg61870c22020-06-09 14:51:50 -06001999 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06002000 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06002001 const auto descriptor_type = binding_it.GetType();
2002 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
2003 auto array_idx = 0;
2004
2005 if (binding_it.IsVariableDescriptorCount()) {
2006 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
2007 }
2008 SyncStageAccessIndex sync_index =
2009 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
2010
2011 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
2012 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
2013 switch (descriptor->GetClass()) {
2014 case DescriptorClass::ImageSampler:
2015 case DescriptorClass::Image: {
2016 const IMAGE_VIEW_STATE *img_view_state = nullptr;
2017 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
2018 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
2019 } else {
2020 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
2021 }
2022 if (!img_view_state) continue;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06002023 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
2024 // Descriptors, so we do not have to worry about depth slicing here.
2025 // See: VUID 00343
2026 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06002027 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002028 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06002029 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
2030 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
2031 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
2032 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002033 } else {
John Zulauf110413c2021-03-20 05:38:38 -06002034 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
2035 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002036 }
locke-lunarg61870c22020-06-09 14:51:50 -06002037 break;
2038 }
2039 case DescriptorClass::TexelBuffer: {
2040 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
2041 if (!buf_view_state) continue;
2042 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002043 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07002044 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002045 break;
2046 }
2047 case DescriptorClass::GeneralBuffer: {
2048 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
2049 auto buf_state = buffer_descriptor->GetBufferState();
2050 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06002051 const ResourceAccessRange range =
2052 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07002053 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002054 break;
2055 }
2056 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
2057 default:
2058 break;
2059 }
2060 }
2061 }
2062 }
2063}
2064
2065bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
2066 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002067 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002068 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002069 return skip;
2070 }
2071
2072 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2073 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002074 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002075
2076 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002077 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002078 if (binding_description.binding < binding_buffers_size) {
2079 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002080 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002081
locke-lunarg1ae57d62020-11-18 10:49:19 -07002082 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002083 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2084 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002085 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002086 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002087 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002088 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
2089 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2090 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002091 }
2092 }
2093 }
2094 return skip;
2095}
2096
John Zulauf14940722021-04-12 15:19:02 -06002097void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002098 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002099 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002100 return;
2101 }
2102 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2103 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002104 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002105
2106 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002107 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002108 if (binding_description.binding < binding_buffers_size) {
2109 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002110 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002111
locke-lunarg1ae57d62020-11-18 10:49:19 -07002112 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002113 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2114 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002115 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2116 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002117 }
2118 }
2119}
2120
2121bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2122 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002123 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002124 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002125 }
locke-lunarg61870c22020-06-09 14:51:50 -06002126
locke-lunarg1ae57d62020-11-18 10:49:19 -07002127 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002128 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002129 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2130 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002131 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002132 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002133 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002134 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2135 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2136 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002137 }
2138
2139 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2140 // We will detect more accurate range in the future.
2141 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2142 return skip;
2143}
2144
John Zulauf14940722021-04-12 15:19:02 -06002145void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002146 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 -06002147
locke-lunarg1ae57d62020-11-18 10:49:19 -07002148 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002149 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002150 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2151 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002152 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002153
2154 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2155 // We will detect more accurate range in the future.
2156 RecordDrawVertex(UINT32_MAX, 0, tag);
2157}
2158
2159bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002160 bool skip = false;
2161 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002162 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002163 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002164}
2165
John Zulauf14940722021-04-12 15:19:02 -06002166void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002167 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002168 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002169 }
locke-lunarg61870c22020-06-09 14:51:50 -06002170}
2171
John Zulauf64ffe552021-02-06 10:25:07 -07002172void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2173 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06002174 const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002175 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002176 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002177 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002178 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002179 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002180}
2181
John Zulauf8eda1562021-04-13 17:06:41 -06002182void CommandBufferAccessContext::RecordNextSubpass(ResourceUsageTag prev_tag, ResourceUsageTag next_tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002183 assert(current_renderpass_context_);
John Zulauf64ffe552021-02-06 10:25:07 -07002184 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002185 current_context_ = &current_renderpass_context_->CurrentContext();
2186}
2187
John Zulauf8eda1562021-04-13 17:06:41 -06002188void CommandBufferAccessContext::RecordEndRenderPass(const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002189 assert(current_renderpass_context_);
2190 if (!current_renderpass_context_) return;
2191
John Zulauf8eda1562021-04-13 17:06:41 -06002192 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002193 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002194 current_renderpass_context_ = nullptr;
2195}
2196
John Zulauf4a6105a2020-11-17 15:11:05 -07002197void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2198 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002199 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2200 if (event_state) {
2201 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002202 }
2203}
2204
John Zulaufae842002021-04-15 18:20:55 -06002205// The is the recorded cb context
John Zulauf4fa68462021-04-26 21:04:22 -06002206bool CommandBufferAccessContext::ValidateFirstUse(CommandBufferAccessContext *proxy_context, const char *func_name,
2207 uint32_t index) const {
2208 assert(proxy_context);
2209 auto *events_context = proxy_context->GetCurrentEventsContext();
2210 auto *access_context = proxy_context->GetCurrentAccessContext();
2211 const ResourceUsageTag base_tag = proxy_context->GetTagLimit();
John Zulaufae842002021-04-15 18:20:55 -06002212 bool skip = false;
2213 ResourceUsageRange tag_range = {0, 0};
2214 const AccessContext *recorded_context = GetCurrentAccessContext();
2215 assert(recorded_context);
2216 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002217 auto log_msg = [this](const HazardResult &hazard, const CommandBufferAccessContext &active_context, const char *func_name,
John Zulaufae842002021-04-15 18:20:55 -06002218 uint32_t index) {
2219 const auto cb_handle = active_context.cb_state_->commandBuffer();
2220 const auto recorded_handle = cb_state_->commandBuffer();
John Zulauf4fa68462021-04-26 21:04:22 -06002221 const auto *report_data = sync_state_->report_data;
John Zulaufae842002021-04-15 18:20:55 -06002222 return sync_state_->LogError(cb_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf4fa68462021-04-26 21:04:22 -06002223 "%s: Hazard %s for entry %" PRIu32 ", %s, Recorded access info %s. Access info %s.", func_name,
2224 string_SyncHazard(hazard.hazard), index, report_data->FormatHandle(recorded_handle).c_str(),
2225 FormatUsage(*hazard.recorded_access).c_str(), active_context.FormatUsage(hazard).c_str());
John Zulaufae842002021-04-15 18:20:55 -06002226 };
2227 for (const auto &sync_op : sync_ops_) {
John Zulauf4fa68462021-04-26 21:04:22 -06002228 // we update the range to any include layout transition first use writes,
2229 // as they are stored along with the source scope (as effective barrier) when recorded
2230 tag_range.end = sync_op.tag + 1;
2231
John Zulaufae842002021-04-15 18:20:55 -06002232 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2233 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002234 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002235 }
2236 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
John Zulauf4fa68462021-04-26 21:04:22 -06002237 // Record the barrier into the proxy context.
2238 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2239 tag_range.begin = tag_range.end;
John Zulaufae842002021-04-15 18:20:55 -06002240 }
2241
2242 // and anything after the last syncop
John Zulaufae842002021-04-15 18:20:55 -06002243 tag_range.end = ResourceUsageRecord::kMaxIndex;
2244 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2245 if (hazard.hazard) {
John Zulauf4fa68462021-04-26 21:04:22 -06002246 skip |= log_msg(hazard, *proxy_context, func_name, index);
John Zulaufae842002021-04-15 18:20:55 -06002247 }
2248
2249 return skip;
2250}
2251
John Zulauf4fa68462021-04-26 21:04:22 -06002252void CommandBufferAccessContext::RecordExecutedCommandBuffer(const CommandBufferAccessContext &recorded_cb_context, CMD_TYPE cmd) {
2253 auto *events_context = GetCurrentEventsContext();
2254 auto *access_context = GetCurrentAccessContext();
2255 const AccessContext *recorded_context = recorded_cb_context.GetCurrentAccessContext();
2256 assert(recorded_context);
2257
2258 // Just run through the barriers ignoring the usage from the recorded context, as Resolve will overwrite outdated state
2259 const ResourceUsageTag base_tag = GetTagLimit();
2260 for (const auto &sync_op : recorded_cb_context.sync_ops_) {
2261 // we update the range to any include layout transition first use writes,
2262 // as they are stored along with the source scope (as effective barrier) when recorded
2263 sync_op.sync_op->DoRecord(base_tag + sync_op.tag, access_context, events_context);
2264 }
2265
2266 ResourceUsageRange tag_range = ImportRecordedAccessLog(recorded_cb_context);
2267 assert(base_tag == tag_range.begin); // to ensure the to offset calculation agree
2268 ResolveRecordedContext(*recorded_context, tag_range.begin);
2269}
2270
2271void CommandBufferAccessContext::ResolveRecordedContext(const AccessContext &recorded_context, ResourceUsageTag offset) {
2272 auto tag_offset = [offset](ResourceAccessState *access) { access->OffsetTag(offset); };
2273
2274 auto *access_context = GetCurrentAccessContext();
2275 for (auto address_type : kAddressTypes) {
2276 recorded_context.ResolveAccessRange(address_type, kFullRange, tag_offset, &access_context->GetAccessStateMap(address_type),
2277 nullptr, false);
2278 }
2279}
2280
2281ResourceUsageRange CommandBufferAccessContext::ImportRecordedAccessLog(const CommandBufferAccessContext &recorded_context) {
2282 // The execution references ensure lifespan for the referenced child CB's...
2283 ResourceUsageRange tag_range(GetTagLimit(), 0);
2284 const auto &rec_cb = recorded_context.cb_state_;
2285 const CMD_BUFFER_STATE *const_rec_cb_plain = rec_cb.get();
2286 cb_execution_reference_.emplace(const_rec_cb_plain, CmdBufReference(rec_cb, recorded_context.reset_count_));
2287 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;
John Zulauf1507ee42020-05-18 11:33:09 -06002497 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002498 skip |=
2499 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002500 if (!skip) {
2501 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2502 // on a copy of the (empty) next context.
2503 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2504 AccessContext temp_context(next_context);
2505 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002506 skip |=
2507 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002508 }
John Zulauf7635de32020-05-29 17:14:15 -06002509 return skip;
2510}
John Zulauf64ffe552021-02-06 10:25:07 -07002511bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002512 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002513 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002514 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002515 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002516 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2517
2518 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002519 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002520 return skip;
2521}
2522
John Zulauf64ffe552021-02-06 10:25:07 -07002523AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002524 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002525}
2526
John Zulauf64ffe552021-02-06 10:25:07 -07002527bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2528 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002529 bool skip = false;
2530
John Zulauf7635de32020-05-29 17:14:15 -06002531 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2532 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2533 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2534 // to apply and only copy then, if this proves a hot spot.
2535 std::unique_ptr<AccessContext> proxy_for_current;
2536
John Zulauf355e49b2020-04-24 15:11:15 -06002537 // Validate the "finalLayout" transitions to external
2538 // Get them from where there we're hidding in the extra entry.
2539 const auto &final_transitions = rp_state_->subpass_transitions.back();
2540 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002541 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002542 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2543 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002544 auto *context = trackback.context;
2545
2546 if (transition.prev_pass == current_subpass_) {
2547 if (!proxy_for_current) {
2548 // 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 -07002549 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002550 }
2551 context = proxy_for_current.get();
2552 }
2553
John Zulaufa0a98292020-09-18 09:30:10 -06002554 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2555 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002556 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002557 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002558 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002559 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002560 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2561 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2562 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2563 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002564 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002565 }
2566 }
2567 return skip;
2568}
2569
John Zulauf14940722021-04-12 15:19:02 -06002570void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002571 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002572 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002573}
2574
John Zulauf14940722021-04-12 15:19:02 -06002575void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002576 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2577 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002578
2579 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2580 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002581 const AttachmentViewGen &view_gen = attachment_views_[i];
2582 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002583
2584 const auto &ci = attachment_ci[i];
2585 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002586 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002587 const bool is_color = !(has_depth || has_stencil);
2588
2589 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002590 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2591 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2592 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2593 SyncOrdering::kColorAttachment, tag);
2594 }
John Zulauf1507ee42020-05-18 11:33:09 -06002595 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002596 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002597 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2598 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2599 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2600 SyncOrdering::kDepthStencilAttachment, tag);
2601 }
John Zulauf1507ee42020-05-18 11:33:09 -06002602 }
2603 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002604 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2605 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2606 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2607 SyncOrdering::kDepthStencilAttachment, tag);
2608 }
John Zulauf1507ee42020-05-18 11:33:09 -06002609 }
2610 }
2611 }
2612 }
2613}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002614AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2615 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2616 AttachmentViewGenVector view_gens;
2617 VkExtent3D extent = CastTo3D(render_area.extent);
2618 VkOffset3D offset = CastTo3D(render_area.offset);
2619 view_gens.reserve(attachment_views.size());
2620 for (const auto *view : attachment_views) {
2621 view_gens.emplace_back(view, offset, extent);
2622 }
2623 return view_gens;
2624}
John Zulauf64ffe552021-02-06 10:25:07 -07002625RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2626 VkQueueFlags queue_flags,
2627 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2628 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002629 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002630 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002631 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002632 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002633 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002634 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002635 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002636}
John Zulauf14940722021-04-12 15:19:02 -06002637void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002638 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002639 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002640 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002641 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002642}
John Zulauf1507ee42020-05-18 11:33:09 -06002643
John Zulauf14940722021-04-12 15:19:02 -06002644void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag prev_subpass_tag, const ResourceUsageTag next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002645 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002646 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2647 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002648
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002649 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2650 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002651 current_subpass_++;
2652 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002653 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2654 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002655 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002656}
2657
John Zulauf14940722021-04-12 15:19:02 -06002658void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002659 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002660 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2661 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002662
John Zulauf355e49b2020-04-24 15:11:15 -06002663 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002664 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002665
2666 // Add the "finalLayout" transitions to external
2667 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002668 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2669 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2670 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002671 const auto &final_transitions = rp_state_->subpass_transitions.back();
2672 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002673 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002674 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002675 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002676 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002677 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002678 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002679 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002680 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002681 }
2682}
2683
Jeremy Gebben40a22942020-12-22 14:22:06 -07002684SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002685 SyncExecScope result;
2686 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002687 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2688 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002689 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2690 return result;
2691}
2692
Jeremy Gebben40a22942020-12-22 14:22:06 -07002693SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002694 SyncExecScope result;
2695 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002696 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2697 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002698 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2699 return result;
2700}
2701
2702SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002703 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002704 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002705 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002706 dst_access_scope = 0;
2707}
2708
2709template <typename Barrier>
2710SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002711 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002712 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002713 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002714 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2715}
2716
2717SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002718 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2719 if (barrier) {
2720 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002721 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002722 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002723
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002724 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002725 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002726 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2727
2728 } else {
2729 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002730 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002731 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2732
2733 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002734 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002735 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2736 }
2737}
2738
2739template <typename Barrier>
2740SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2741 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2742 src_exec_scope = src.exec_scope;
2743 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2744
2745 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002746 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002747 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002748}
2749
John Zulaufb02c1eb2020-10-06 16:33:36 -06002750// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2751void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2752 for (const auto &barrier : barriers) {
2753 ApplyBarrier(barrier, layout_transition);
2754 }
2755}
2756
John Zulauf89311b42020-09-29 16:28:47 -06002757// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2758// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2759// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002760void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002761 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002762 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002763 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002764 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002765 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002766 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002767 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002768}
John Zulauf9cb530d2019-09-30 14:14:10 -06002769HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2770 HazardResult hazard;
2771 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002772 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002773 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002774 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002775 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002776 }
2777 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002778 // Write operation:
2779 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2780 // If reads exists -- test only against them because either:
2781 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2782 // * 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
2783 // the current write happens after the reads, so just test the write against the reades
2784 // Otherwise test against last_write
2785 //
2786 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002787 if (last_reads.size()) {
2788 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002789 if (IsReadHazard(usage_stage, read_access)) {
2790 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2791 break;
2792 }
2793 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002794 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002795 // Write-After-Write check -- if we have a previous write to test against
2796 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002797 }
2798 }
2799 return hazard;
2800}
2801
John Zulauf4fa68462021-04-26 21:04:22 -06002802HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering ordering_rule) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07002803 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf4fa68462021-04-26 21:04:22 -06002804 return DetectHazard(usage_index, ordering);
2805}
2806
2807HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const OrderingBarrier &ordering) const {
John Zulauf69133422020-05-20 14:55:53 -06002808 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2809 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002810 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002811 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002812 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2813 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002814 if (IsRead(usage_bit)) {
2815 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2816 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2817 if (is_raw_hazard) {
2818 // NOTE: we know last_write is non-zero
2819 // See if the ordering rules save us from the simple RAW check above
2820 // First check to see if the current usage is covered by the ordering rules
2821 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2822 const bool usage_is_ordered =
2823 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2824 if (usage_is_ordered) {
2825 // Now see of the most recent write (or a subsequent read) are ordered
2826 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2827 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002828 }
2829 }
John Zulauf4285ee92020-09-23 10:20:52 -06002830 if (is_raw_hazard) {
2831 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2832 }
John Zulauf5c628d02021-05-04 15:46:36 -06002833 } else if (usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2834 // For Image layout transitions, the barrier represents the first synchronization/access scope of the layout transition
2835 return DetectBarrierHazard(usage_index, ordering.exec_scope, ordering.access_scope);
John Zulauf361fb532020-07-22 10:45:39 -06002836 } else {
2837 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002838 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002839 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002840 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002841 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002842 if (usage_write_is_ordered) {
2843 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2844 ordered_stages = GetOrderedStages(ordering);
2845 }
2846 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2847 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002848 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002849 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2850 if (IsReadHazard(usage_stage, read_access)) {
2851 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2852 break;
2853 }
John Zulaufd14743a2020-07-03 09:42:39 -06002854 }
2855 }
John Zulauf4285ee92020-09-23 10:20:52 -06002856 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002857 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002858 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002859 }
John Zulauf69133422020-05-20 14:55:53 -06002860 }
2861 }
2862 return hazard;
2863}
2864
John Zulaufae842002021-04-15 18:20:55 -06002865HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2866 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002867 using Size = FirstAccesses::size_type;
2868 const auto &recorded_accesses = recorded_use.first_accesses_;
2869 Size count = recorded_accesses.size();
2870 if (count) {
2871 const auto &last_access = recorded_accesses.back();
2872 bool do_write_last = IsWrite(last_access.usage_index);
2873 if (do_write_last) --count;
John Zulaufae842002021-04-15 18:20:55 -06002874
John Zulauf4fa68462021-04-26 21:04:22 -06002875 for (Size i = 0; i < count; ++count) {
2876 const auto &first = recorded_accesses[i];
2877 // Skip and quit logic
2878 if (first.tag < tag_range.begin) continue;
2879 if (first.tag >= tag_range.end) {
2880 do_write_last = false; // ignore last since we know it can't be in tag_range
2881 break;
2882 }
2883
2884 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2885 if (hazard.hazard) {
2886 hazard.AddRecordedAccess(first);
2887 break;
2888 }
2889 }
2890
2891 if (do_write_last && tag_range.includes(last_access.tag)) {
2892 // Writes are a bit special... both for the "most recent" access logic, and layout transition specific logic
2893 OrderingBarrier barrier = GetOrderingRules(last_access.ordering_rule);
2894 if (last_access.usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION) {
2895 // Or in the layout first access scope as a barrier... IFF the usage is an ILT
2896 // this was saved off in the "apply barriers" logic to simplify ILT access checks as they straddle
2897 // the barrier that applies them
2898 barrier |= recorded_use.first_write_layout_ordering_;
2899 }
2900 // Any read stages present in the recorded context (this) are most recent to the write, and thus mask those stages in
2901 // the active context
2902 if (recorded_use.first_read_stages_) {
2903 // we need to ignore the first use read stage in the active context (so we add them to the ordering rule),
2904 // reads in the active context are not "most recent" as all recorded context operations are *after* them
2905 // This supresses only RAW checks for stages present in the recorded context, but not those only present in the
2906 // active context.
2907 barrier.exec_scope |= recorded_use.first_read_stages_;
2908 // if there are any first use reads, we suppress WAW by injecting the active context write in the ordering rule
2909 barrier.access_scope |= FlagBit(last_access.usage_index);
2910 }
2911 hazard = DetectHazard(last_access.usage_index, barrier);
2912 if (hazard.hazard) {
2913 hazard.AddRecordedAccess(last_access);
2914 }
2915 }
John Zulaufae842002021-04-15 18:20:55 -06002916 }
2917 return hazard;
2918}
2919
John Zulauf2f952d22020-02-10 11:34:51 -07002920// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002921HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002922 HazardResult hazard;
2923 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002924 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2925 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2926 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002927 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002928 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002929 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002930 }
2931 } else {
John Zulauf14940722021-04-12 15:19:02 -06002932 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002933 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002934 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002935 // 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 -07002936 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002937 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002938 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002939 break;
2940 }
2941 }
John Zulauf2f952d22020-02-10 11:34:51 -07002942 }
2943 }
2944 return hazard;
2945}
2946
John Zulaufae842002021-04-15 18:20:55 -06002947HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2948 ResourceUsageTag start_tag) const {
2949 HazardResult hazard;
John Zulauf4fa68462021-04-26 21:04:22 -06002950 for (const auto &first : recorded_use.first_accesses_) {
John Zulaufae842002021-04-15 18:20:55 -06002951 // Skip and quit logic
2952 if (first.tag < tag_range.begin) continue;
2953 if (first.tag >= tag_range.end) break;
John Zulaufae842002021-04-15 18:20:55 -06002954
2955 hazard = DetectAsyncHazard(first.usage_index, start_tag);
John Zulauf4fa68462021-04-26 21:04:22 -06002956 if (hazard.hazard) {
2957 hazard.AddRecordedAccess(first);
2958 break;
2959 }
John Zulaufae842002021-04-15 18:20:55 -06002960 }
2961 return hazard;
2962}
2963
Jeremy Gebben40a22942020-12-22 14:22:06 -07002964HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002965 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002966 // Only supporting image layout transitions for now
2967 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2968 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002969 // only test for WAW if there no intervening read operations.
2970 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002971 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002972 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002973 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002974 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002975 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002976 break;
2977 }
2978 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002979 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2980 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2981 }
2982
2983 return hazard;
2984}
2985
Jeremy Gebben40a22942020-12-22 14:22:06 -07002986HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002987 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06002988 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002989 // Only supporting image layout transitions for now
2990 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2991 HazardResult hazard;
2992 // only test for WAW if there no intervening read operations.
2993 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2994
John Zulaufab7756b2020-12-29 16:10:16 -07002995 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002996 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2997 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002998 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002999 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003000 // The read is in the events first synchronization scope, so we use a barrier hazard check
3001 // If the read stage is not in the src sync scope
3002 // *AND* not execution chained with an existing sync barrier (that's the or)
3003 // then the barrier access is unsafe (R/W after R)
3004 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
3005 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3006 break;
3007 }
3008 } else {
3009 // The read not in the event first sync scope and so is a hazard vs. the layout transition
3010 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
3011 }
3012 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003013 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003014 // 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 -06003015 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003016 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
3017 // So do a normal barrier hazard check
3018 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
3019 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3020 }
3021 } else {
3022 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06003023 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
3024 }
John Zulaufd14743a2020-07-03 09:42:39 -06003025 }
John Zulauf361fb532020-07-22 10:45:39 -06003026
John Zulauf0cb5be22020-01-23 12:18:22 -07003027 return hazard;
3028}
3029
John Zulauf5f13a792020-03-10 07:31:21 -06003030// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
3031// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
3032// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
3033void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06003034 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003035 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
3036 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06003037 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06003038 } else if (other.write_tag == write_tag) {
3039 // 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 -06003040 // dependency chaining logic or any stage expansion)
3041 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003042 pending_write_barriers |= other.pending_write_barriers;
3043 pending_layout_transition |= other.pending_layout_transition;
3044 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf4fa68462021-04-26 21:04:22 -06003045 pending_layout_ordering_ |= other.pending_layout_ordering_;
John Zulauf5f13a792020-03-10 07:31:21 -06003046
John Zulaufd14743a2020-07-03 09:42:39 -06003047 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07003048 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06003049 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07003050 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003051 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06003052 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06003053 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06003054 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
3055 // but we should wait on profiling data for that.
3056 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06003057 auto &my_read = last_reads[my_read_index];
3058 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06003059 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06003060 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06003061 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06003062 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06003063 my_read.pending_dep_chain = other_read.pending_dep_chain;
3064 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
3065 // May require tracking more than one access per stage.
3066 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003067 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06003068 // Since I'm overwriting the fragement stage read, also update the input attachment info
3069 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06003070 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003071 }
John Zulauf14940722021-04-12 15:19:02 -06003072 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06003073 // The read tags match so merge the barriers
3074 my_read.barriers |= other_read.barriers;
3075 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06003076 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06003077
John Zulauf5f13a792020-03-10 07:31:21 -06003078 break;
3079 }
3080 }
3081 } else {
3082 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07003083 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06003084 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07003085 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003086 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06003087 }
John Zulauf5f13a792020-03-10 07:31:21 -06003088 }
3089 }
John Zulauf361fb532020-07-22 10:45:39 -06003090 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06003091 } // the else clause would be that other write is before this write... in which case we supercede the other state and
3092 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07003093
3094 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
3095 // of the copy and other into this using the update first logic.
3096 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
3097 // of the other first_accesses... )
3098 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
3099 FirstAccesses firsts(std::move(first_accesses_));
3100 first_accesses_.clear();
3101 first_read_stages_ = 0U;
3102 auto a = firsts.begin();
3103 auto a_end = firsts.end();
3104 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06003105 // TODO: Determine whether some tag offset will be needed for PHASE II
3106 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003107 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3108 ++a;
3109 }
3110 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
3111 }
3112 for (; a != a_end; ++a) {
3113 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
3114 }
3115 }
John Zulauf5f13a792020-03-10 07:31:21 -06003116}
3117
John Zulauf14940722021-04-12 15:19:02 -06003118void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003119 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
3120 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06003121 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06003122 // Mulitple outstanding reads may be of interest and do dependency chains independently
3123 // However, for purposes of barrier tracking, only one read per pipeline stage matters
3124 const auto usage_stage = PipelineStageBit(usage_index);
3125 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07003126 for (auto &read_access : last_reads) {
3127 if (read_access.stage == usage_stage) {
3128 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003129 break;
3130 }
3131 }
3132 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07003133 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003134 last_read_stages |= usage_stage;
3135 }
John Zulauf4285ee92020-09-23 10:20:52 -06003136
3137 // 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 -07003138 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06003139 // TODO Revisit re: multiple reads for a given stage
3140 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06003141 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003142 } else {
3143 // Assume write
3144 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06003145 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003146 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003147 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06003148}
John Zulauf5f13a792020-03-10 07:31:21 -06003149
John Zulauf89311b42020-09-29 16:28:47 -06003150// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
3151// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
3152// We can overwrite them as *this* write is now after them.
3153//
3154// 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 -06003155void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07003156 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06003157 last_read_stages = 0;
3158 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06003159 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06003160
3161 write_barriers = 0;
3162 write_dependency_chain = 0;
3163 write_tag = tag;
3164 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06003165}
3166
John Zulauf89311b42020-09-29 16:28:47 -06003167// Apply the memory barrier without updating the existing barriers. The execution barrier
3168// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
3169// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
3170// replace the current write barriers or add to them, so accumulate to pending as well.
3171void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
3172 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
3173 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06003174 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3175 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3176 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3177 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003178 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003179 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003180 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003181 if (layout_transition) {
3182 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3183 }
John Zulaufa0a98292020-09-18 09:30:10 -06003184 }
John Zulauf89311b42020-09-29 16:28:47 -06003185 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3186 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003187
John Zulauf89311b42020-09-29 16:28:47 -06003188 if (!pending_layout_transition) {
3189 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3190 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003191 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003192 // 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 -07003193 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3194 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003195 }
3196 }
John Zulaufa0a98292020-09-18 09:30:10 -06003197 }
John Zulaufa0a98292020-09-18 09:30:10 -06003198}
3199
John Zulauf4a6105a2020-11-17 15:11:05 -07003200// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3201// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003202void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003203 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3204 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3205 // in order to know if it's in the excecution scope
3206 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3207 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3208 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003209 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003210 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003211 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4fa68462021-04-26 21:04:22 -06003212 if (layout_transition) {
3213 pending_layout_ordering_ |= OrderingBarrier(barrier.src_exec_scope.exec_scope, barrier.src_access_scope);
3214 }
John Zulauf4a6105a2020-11-17 15:11:05 -07003215 }
3216 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3217 pending_layout_transition |= layout_transition;
3218
3219 if (!pending_layout_transition) {
3220 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3221 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003222 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003223 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3224 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3225 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3226 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3227 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3228 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3229 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003230 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 -07003231 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003232 }
3233 }
3234 }
3235}
John Zulauf14940722021-04-12 15:19:02 -06003236void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003237 if (pending_layout_transition) {
John Zulauf4fa68462021-04-26 21:04:22 -06003238 // 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 -06003239 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003240 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf4fa68462021-04-26 21:04:22 -06003241 TouchupFirstForLayoutTransition(tag, pending_layout_ordering_);
3242 pending_layout_ordering_ = OrderingBarrier();
John Zulauf89311b42020-09-29 16:28:47 -06003243 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003244 }
John Zulauf89311b42020-09-29 16:28:47 -06003245
3246 // Apply the accumulate execution barriers (and thus update chaining information)
John Zulauf4fa68462021-04-26 21:04:22 -06003247 // for layout transition, last_reads is reset by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003248 for (auto &read_access : last_reads) {
3249 read_access.barriers |= read_access.pending_dep_chain;
3250 read_execution_barriers |= read_access.barriers;
3251 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003252 }
3253
3254 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3255 write_dependency_chain |= pending_write_dep_chain;
3256 write_barriers |= pending_write_barriers;
3257 pending_write_dep_chain = 0;
3258 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003259}
3260
John Zulaufae842002021-04-15 18:20:55 -06003261bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3262 if (!first_accesses_.size()) return false;
3263 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3264 return tag_range.intersects(first_access_range);
3265}
3266
John Zulauf59e25072020-07-17 10:55:21 -06003267// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003268VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3269 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003270
John Zulaufab7756b2020-12-29 16:10:16 -07003271 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003272 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003273 barriers = read_access.barriers;
3274 break;
John Zulauf59e25072020-07-17 10:55:21 -06003275 }
3276 }
John Zulauf4285ee92020-09-23 10:20:52 -06003277
John Zulauf59e25072020-07-17 10:55:21 -06003278 return barriers;
3279}
3280
Jeremy Gebben40a22942020-12-22 14:22:06 -07003281inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003282 assert(IsRead(usage));
3283 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3284 // * the previous reads are not hazards, and thus last_write must be visible and available to
3285 // any reads that happen after.
3286 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3287 // 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 -07003288 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003289}
3290
Jeremy Gebben40a22942020-12-22 14:22:06 -07003291VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003292 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003293 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003294 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003295 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003296 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003297 // 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 -07003298 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003299 }
3300
3301 return ordered_stages;
3302}
3303
John Zulauf14940722021-04-12 15:19:02 -06003304void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003305 // Only record until we record a write.
3306 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003307 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003308 if (0 == (usage_stage & first_read_stages_)) {
3309 // If this is a read we haven't seen or a write, record.
John Zulauf4fa68462021-04-26 21:04:22 -06003310 // We always need to know what stages were found prior to write
John Zulauffaea0ee2021-01-14 14:01:32 -07003311 first_read_stages_ |= usage_stage;
John Zulauf4fa68462021-04-26 21:04:22 -06003312 if (0 == (read_execution_barriers & usage_stage)) {
3313 // If this stage isn't masked then we add it (since writes map to usage_stage 0, this also records writes)
3314 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3315 }
John Zulauffaea0ee2021-01-14 14:01:32 -07003316 }
3317 }
3318}
3319
John Zulauf4fa68462021-04-26 21:04:22 -06003320void ResourceAccessState::TouchupFirstForLayoutTransition(ResourceUsageTag tag, const OrderingBarrier &layout_ordering) {
3321 // Only call this after recording an image layout transition
3322 assert(first_accesses_.size());
3323 if (first_accesses_.back().tag == tag) {
3324 // If this layout transition is the the first write, add the additional ordering rules that guard the ILT
3325 assert(first_accesses_.back().usage_index = SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
3326 first_write_layout_ordering_ = layout_ordering;
3327 }
3328}
3329
John Zulaufd1f85d42020-04-15 12:23:15 -06003330void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003331 auto *access_context = GetAccessContextNoInsert(command_buffer);
3332 if (access_context) {
3333 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003334 }
3335}
3336
John Zulaufd1f85d42020-04-15 12:23:15 -06003337void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3338 auto access_found = cb_access_state.find(command_buffer);
3339 if (access_found != cb_access_state.end()) {
3340 access_found->second->Reset();
John Zulauf4fa68462021-04-26 21:04:22 -06003341 access_found->second->MarkDestroyed();
John Zulaufd1f85d42020-04-15 12:23:15 -06003342 cb_access_state.erase(access_found);
3343 }
3344}
3345
John Zulauf9cb530d2019-09-30 14:14:10 -06003346bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3347 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3348 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003349 const auto *cb_context = GetAccessContext(commandBuffer);
3350 assert(cb_context);
3351 if (!cb_context) return skip;
3352 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003353
John Zulauf3d84f1b2020-03-09 13:33:25 -06003354 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003355 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003356 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003357
3358 for (uint32_t region = 0; region < regionCount; region++) {
3359 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003360 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003361 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003362 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003363 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003364 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003365 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003366 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003367 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003368 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003369 }
John Zulauf16adfc92020-04-08 10:28:33 -06003370 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003371 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003372 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003373 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003374 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003375 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003376 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003377 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003378 }
3379 }
3380 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003381 }
3382 return skip;
3383}
3384
3385void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3386 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003387 auto *cb_context = GetAccessContext(commandBuffer);
3388 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003389 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003390 auto *context = cb_context->GetCurrentAccessContext();
3391
John Zulauf9cb530d2019-09-30 14:14:10 -06003392 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003393 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003394
3395 for (uint32_t region = 0; region < regionCount; region++) {
3396 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003397 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003398 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003399 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003400 }
John Zulauf16adfc92020-04-08 10:28:33 -06003401 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003402 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003403 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003404 }
3405 }
3406}
3407
John Zulauf4a6105a2020-11-17 15:11:05 -07003408void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3409 // Clear out events from the command buffer contexts
3410 for (auto &cb_context : cb_access_state) {
3411 cb_context.second->RecordDestroyEvent(event);
3412 }
3413}
3414
Jeff Leger178b1e52020-10-05 12:22:23 -04003415bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3416 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3417 bool skip = false;
3418 const auto *cb_context = GetAccessContext(commandBuffer);
3419 assert(cb_context);
3420 if (!cb_context) return skip;
3421 const auto *context = cb_context->GetCurrentAccessContext();
3422
3423 // If we have no previous accesses, we have no hazards
3424 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3425 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3426
3427 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3428 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3429 if (src_buffer) {
3430 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003431 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003432 if (hazard.hazard) {
3433 // TODO -- add tag information to log msg when useful.
3434 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3435 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3436 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003437 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003438 }
3439 }
3440 if (dst_buffer && !skip) {
3441 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003442 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003443 if (hazard.hazard) {
3444 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3445 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3446 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003447 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003448 }
3449 }
3450 if (skip) break;
3451 }
3452 return skip;
3453}
3454
3455void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3456 auto *cb_context = GetAccessContext(commandBuffer);
3457 assert(cb_context);
3458 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3459 auto *context = cb_context->GetCurrentAccessContext();
3460
3461 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3462 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3463
3464 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3465 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3466 if (src_buffer) {
3467 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003468 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003469 }
3470 if (dst_buffer) {
3471 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003472 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003473 }
3474 }
3475}
3476
John Zulauf5c5e88d2019-12-26 11:22:02 -07003477bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3478 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3479 const VkImageCopy *pRegions) const {
3480 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003481 const auto *cb_access_context = GetAccessContext(commandBuffer);
3482 assert(cb_access_context);
3483 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003484
John Zulauf3d84f1b2020-03-09 13:33:25 -06003485 const auto *context = cb_access_context->GetCurrentAccessContext();
3486 assert(context);
3487 if (!context) return skip;
3488
3489 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3490 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003491 for (uint32_t region = 0; region < regionCount; region++) {
3492 const auto &copy_region = pRegions[region];
3493 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003494 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003495 copy_region.srcOffset, copy_region.extent);
3496 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003497 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003498 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003499 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003500 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003501 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003502 }
3503
3504 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003505 VkExtent3D dst_copy_extent =
3506 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003507 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003508 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003509 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003510 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003511 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003512 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003513 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003514 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003515 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003516 }
3517 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003518
John Zulauf5c5e88d2019-12-26 11:22:02 -07003519 return skip;
3520}
3521
3522void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3523 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3524 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003525 auto *cb_access_context = GetAccessContext(commandBuffer);
3526 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003527 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003528 auto *context = cb_access_context->GetCurrentAccessContext();
3529 assert(context);
3530
John Zulauf5c5e88d2019-12-26 11:22:02 -07003531 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003532 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003533
3534 for (uint32_t region = 0; region < regionCount; region++) {
3535 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003536 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003537 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003538 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003539 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003540 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003541 VkExtent3D dst_copy_extent =
3542 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003543 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003544 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003545 }
3546 }
3547}
3548
Jeff Leger178b1e52020-10-05 12:22:23 -04003549bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3550 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3551 bool skip = false;
3552 const auto *cb_access_context = GetAccessContext(commandBuffer);
3553 assert(cb_access_context);
3554 if (!cb_access_context) return skip;
3555
3556 const auto *context = cb_access_context->GetCurrentAccessContext();
3557 assert(context);
3558 if (!context) return skip;
3559
3560 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3561 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3562 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3563 const auto &copy_region = pCopyImageInfo->pRegions[region];
3564 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003565 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003566 copy_region.srcOffset, copy_region.extent);
3567 if (hazard.hazard) {
3568 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3569 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3570 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003571 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003572 }
3573 }
3574
3575 if (dst_image) {
3576 VkExtent3D dst_copy_extent =
3577 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003578 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003579 copy_region.dstOffset, dst_copy_extent);
3580 if (hazard.hazard) {
3581 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3582 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3583 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003584 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003585 }
3586 if (skip) break;
3587 }
3588 }
3589
3590 return skip;
3591}
3592
3593void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3594 auto *cb_access_context = GetAccessContext(commandBuffer);
3595 assert(cb_access_context);
3596 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3597 auto *context = cb_access_context->GetCurrentAccessContext();
3598 assert(context);
3599
3600 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3601 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3602
3603 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3604 const auto &copy_region = pCopyImageInfo->pRegions[region];
3605 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003606 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003607 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003608 }
3609 if (dst_image) {
3610 VkExtent3D dst_copy_extent =
3611 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003612 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003613 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003614 }
3615 }
3616}
3617
John Zulauf9cb530d2019-09-30 14:14:10 -06003618bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3619 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3620 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3621 uint32_t bufferMemoryBarrierCount,
3622 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3623 uint32_t imageMemoryBarrierCount,
3624 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3625 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003626 const auto *cb_access_context = GetAccessContext(commandBuffer);
3627 assert(cb_access_context);
3628 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003629
John Zulauf36ef9282021-02-02 11:47:24 -07003630 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3631 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3632 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3633 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003634 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003635 return skip;
3636}
3637
3638void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3639 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3640 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3641 uint32_t bufferMemoryBarrierCount,
3642 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3643 uint32_t imageMemoryBarrierCount,
3644 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003645 auto *cb_access_context = GetAccessContext(commandBuffer);
3646 assert(cb_access_context);
3647 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003648
John Zulauf8eda1562021-04-13 17:06:41 -06003649 CommandBufferAccessContext::SyncOpPointer sync_op(
3650 new SyncOpPipelineBarrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask, dstStageMask,
3651 dependencyFlags, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
3652 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers));
3653 const auto tag = sync_op->Record(cb_access_context);
3654 cb_access_context->AddSyncOp(tag, std::move(sync_op));
John Zulauf9cb530d2019-09-30 14:14:10 -06003655}
3656
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003657bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3658 const VkDependencyInfoKHR *pDependencyInfo) const {
3659 bool skip = false;
3660 const auto *cb_access_context = GetAccessContext(commandBuffer);
3661 assert(cb_access_context);
3662 if (!cb_access_context) return skip;
3663
3664 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3665 skip = pipeline_barrier.Validate(*cb_access_context);
3666 return skip;
3667}
3668
3669void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3670 auto *cb_access_context = GetAccessContext(commandBuffer);
3671 assert(cb_access_context);
3672 if (!cb_access_context) return;
3673
John Zulauf8eda1562021-04-13 17:06:41 -06003674 CommandBufferAccessContext::SyncOpPointer sync_op(
3675 new SyncOpPipelineBarrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo));
3676 const auto tag = sync_op->Record(cb_access_context);
3677 cb_access_context->AddSyncOp(tag, std::move(sync_op));
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003678}
3679
John Zulauf9cb530d2019-09-30 14:14:10 -06003680void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3681 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3682 // The state tracker sets up the device state
3683 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3684
John Zulauf5f13a792020-03-10 07:31:21 -06003685 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3686 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003687 // TODO: Find a good way to do this hooklessly.
3688 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3689 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3690 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3691
John Zulaufd1f85d42020-04-15 12:23:15 -06003692 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3693 sync_device_state->ResetCommandBufferCallback(command_buffer);
3694 });
3695 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3696 sync_device_state->FreeCommandBufferCallback(command_buffer);
3697 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003698}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003699
John Zulauf355e49b2020-04-24 15:11:15 -06003700bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003701 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003702 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003703 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003704 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003705 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003706 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003707 }
John Zulauf355e49b2020-04-24 15:11:15 -06003708 return skip;
3709}
3710
3711bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3712 VkSubpassContents contents) const {
3713 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003714 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003715 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003716 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003717 return skip;
3718}
3719
3720bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003721 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003722 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003723 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003724 return skip;
3725}
3726
3727bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3728 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003729 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003730 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003731 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003732 return skip;
3733}
3734
John Zulauf3d84f1b2020-03-09 13:33:25 -06003735void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3736 VkResult result) {
3737 // The state tracker sets up the command buffer state
3738 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3739
3740 // Create/initialize the structure that trackers accesses at the command buffer scope.
3741 auto cb_access_context = GetAccessContext(commandBuffer);
3742 assert(cb_access_context);
3743 cb_access_context->Reset();
3744}
3745
3746void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003747 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003748 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003749 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003750 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003751 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003752 }
3753}
3754
3755void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3756 VkSubpassContents contents) {
3757 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003758 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003759 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003760 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003761}
3762
3763void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3764 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3765 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003766 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003767}
3768
3769void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3770 const VkRenderPassBeginInfo *pRenderPassBegin,
3771 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3772 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003773 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003774}
3775
Mike Schuchardt2df08912020-12-15 16:28:09 -08003776bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003777 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003778 bool skip = false;
3779
3780 auto cb_context = GetAccessContext(commandBuffer);
3781 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003782 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003783 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003784 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003785}
3786
3787bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3788 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003789 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003790 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003791 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003792 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3793 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003794 return skip;
3795}
3796
Mike Schuchardt2df08912020-12-15 16:28:09 -08003797bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3798 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003799 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003800 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003801 return skip;
3802}
3803
3804bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3805 const VkSubpassEndInfo *pSubpassEndInfo) const {
3806 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003807 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003808 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003809}
3810
3811void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003812 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003813 auto cb_context = GetAccessContext(commandBuffer);
3814 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003815 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003816
sfricke-samsung85584a72021-09-30 21:43:38 -07003817 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003818 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003819}
3820
3821void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3822 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003823 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003824 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003825 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003826}
3827
3828void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3829 const VkSubpassEndInfo *pSubpassEndInfo) {
3830 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003831 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003832}
3833
3834void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3835 const VkSubpassEndInfo *pSubpassEndInfo) {
3836 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003837 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003838}
3839
sfricke-samsung85584a72021-09-30 21:43:38 -07003840bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3841 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003842 bool skip = false;
3843
3844 auto cb_context = GetAccessContext(commandBuffer);
3845 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003846 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003847
sfricke-samsung85584a72021-09-30 21:43:38 -07003848 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003849 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003850 return skip;
3851}
3852
3853bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3854 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003855 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003856 return skip;
3857}
3858
Mike Schuchardt2df08912020-12-15 16:28:09 -08003859bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003860 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003861 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003862 return skip;
3863}
3864
3865bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003866 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003867 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003868 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003869 return skip;
3870}
3871
sfricke-samsung85584a72021-09-30 21:43:38 -07003872void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003873 // Resolve the all subpass contexts to the command buffer contexts
3874 auto cb_context = GetAccessContext(commandBuffer);
3875 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003876 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003877
sfricke-samsung85584a72021-09-30 21:43:38 -07003878 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003879 sync_op.Record(cb_context);
3880 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003881}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003882
John Zulauf33fc1d52020-07-17 11:01:10 -06003883// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3884// updates to a resource which do not conflict at the byte level.
3885// TODO: Revisit this rule to see if it needs to be tighter or looser
3886// TODO: Add programatic control over suppression heuristics
3887bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3888 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3889}
3890
John Zulauf3d84f1b2020-03-09 13:33:25 -06003891void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003892 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003893 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003894}
3895
3896void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003897 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003898 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003899}
3900
3901void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003902 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003903 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003904}
locke-lunarga19c71d2020-03-02 18:17:04 -07003905
Jeff Leger178b1e52020-10-05 12:22:23 -04003906template <typename BufferImageCopyRegionType>
3907bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3908 VkImageLayout dstImageLayout, uint32_t regionCount,
3909 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003910 bool skip = false;
3911 const auto *cb_access_context = GetAccessContext(commandBuffer);
3912 assert(cb_access_context);
3913 if (!cb_access_context) return skip;
3914
Jeff Leger178b1e52020-10-05 12:22:23 -04003915 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3916 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3917
locke-lunarga19c71d2020-03-02 18:17:04 -07003918 const auto *context = cb_access_context->GetCurrentAccessContext();
3919 assert(context);
3920 if (!context) return skip;
3921
3922 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003923 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3924
3925 for (uint32_t region = 0; region < regionCount; region++) {
3926 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003927 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003928 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003929 if (src_buffer) {
3930 ResourceAccessRange src_range =
3931 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003932 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003933 if (hazard.hazard) {
3934 // PHASE1 TODO -- add tag information to log msg when useful.
3935 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3936 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3937 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003938 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003939 }
3940 }
3941
Jeremy Gebben40a22942020-12-22 14:22:06 -07003942 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003943 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003944 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003945 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003946 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003947 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003948 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003949 }
3950 if (skip) break;
3951 }
3952 if (skip) break;
3953 }
3954 return skip;
3955}
3956
Jeff Leger178b1e52020-10-05 12:22:23 -04003957bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3958 VkImageLayout dstImageLayout, uint32_t regionCount,
3959 const VkBufferImageCopy *pRegions) const {
3960 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3961 COPY_COMMAND_VERSION_1);
3962}
3963
3964bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3965 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3966 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3967 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3968 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3969}
3970
3971template <typename BufferImageCopyRegionType>
3972void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3973 VkImageLayout dstImageLayout, uint32_t regionCount,
3974 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003975 auto *cb_access_context = GetAccessContext(commandBuffer);
3976 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003977
3978 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3979 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3980
3981 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003982 auto *context = cb_access_context->GetCurrentAccessContext();
3983 assert(context);
3984
3985 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003986 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003987
3988 for (uint32_t region = 0; region < regionCount; region++) {
3989 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003990 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003991 if (src_buffer) {
3992 ResourceAccessRange src_range =
3993 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003994 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003995 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003996 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003997 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003998 }
3999 }
4000}
4001
Jeff Leger178b1e52020-10-05 12:22:23 -04004002void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
4003 VkImageLayout dstImageLayout, uint32_t regionCount,
4004 const VkBufferImageCopy *pRegions) {
4005 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
4006 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4007}
4008
4009void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
4010 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
4011 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
4012 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
4013 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
4014 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
4015}
4016
4017template <typename BufferImageCopyRegionType>
4018bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4019 VkBuffer dstBuffer, uint32_t regionCount,
4020 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004021 bool skip = false;
4022 const auto *cb_access_context = GetAccessContext(commandBuffer);
4023 assert(cb_access_context);
4024 if (!cb_access_context) return skip;
4025
Jeff Leger178b1e52020-10-05 12:22:23 -04004026 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4027 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
4028
locke-lunarga19c71d2020-03-02 18:17:04 -07004029 const auto *context = cb_access_context->GetCurrentAccessContext();
4030 assert(context);
4031 if (!context) return skip;
4032
4033 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4034 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004035 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07004036 for (uint32_t region = 0; region < regionCount; region++) {
4037 const auto &copy_region = pRegions[region];
4038 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004039 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07004040 copy_region.imageOffset, copy_region.imageExtent);
4041 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004042 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004043 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06004044 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004045 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004046 }
John Zulauf477700e2021-01-06 11:41:49 -07004047 if (dst_mem) {
4048 ResourceAccessRange dst_range =
4049 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004050 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07004051 if (hazard.hazard) {
4052 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4053 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
4054 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004055 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07004056 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004057 }
4058 }
4059 if (skip) break;
4060 }
4061 return skip;
4062}
4063
Jeff Leger178b1e52020-10-05 12:22:23 -04004064bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
4065 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
4066 const VkBufferImageCopy *pRegions) const {
4067 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
4068 COPY_COMMAND_VERSION_1);
4069}
4070
4071bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4072 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
4073 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4074 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4075 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4076}
4077
4078template <typename BufferImageCopyRegionType>
4079void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4080 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
4081 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004082 auto *cb_access_context = GetAccessContext(commandBuffer);
4083 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04004084
4085 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
4086 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
4087
4088 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07004089 auto *context = cb_access_context->GetCurrentAccessContext();
4090 assert(context);
4091
4092 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004093 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06004094 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06004095 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07004096
4097 for (uint32_t region = 0; region < regionCount; region++) {
4098 const auto &copy_region = pRegions[region];
4099 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004100 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004101 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004102 if (dst_buffer) {
4103 ResourceAccessRange dst_range =
4104 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07004105 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07004106 }
locke-lunarga19c71d2020-03-02 18:17:04 -07004107 }
4108 }
4109}
4110
Jeff Leger178b1e52020-10-05 12:22:23 -04004111void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4112 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
4113 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
4114 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
4115}
4116
4117void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
4118 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
4119 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
4120 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
4121 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
4122 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
4123}
4124
4125template <typename RegionType>
4126bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4127 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4128 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07004129 bool skip = false;
4130 const auto *cb_access_context = GetAccessContext(commandBuffer);
4131 assert(cb_access_context);
4132 if (!cb_access_context) return skip;
4133
4134 const auto *context = cb_access_context->GetCurrentAccessContext();
4135 assert(context);
4136 if (!context) return skip;
4137
4138 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4139 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4140
4141 for (uint32_t region = 0; region < regionCount; region++) {
4142 const auto &blit_region = pRegions[region];
4143 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004144 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4145 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4146 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4147 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4148 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4149 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004150 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004151 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004152 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004153 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004154 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004155 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004156 }
4157 }
4158
4159 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004160 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4161 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4162 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4163 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4164 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4165 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004166 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07004167 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06004168 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04004169 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06004170 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004171 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07004172 }
4173 if (skip) break;
4174 }
4175 }
4176
4177 return skip;
4178}
4179
Jeff Leger178b1e52020-10-05 12:22:23 -04004180bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4181 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4182 const VkImageBlit *pRegions, VkFilter filter) const {
4183 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
4184 "vkCmdBlitImage");
4185}
4186
4187bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
4188 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
4189 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4190 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4191 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
4192}
4193
4194template <typename RegionType>
4195void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4196 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4197 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004198 auto *cb_access_context = GetAccessContext(commandBuffer);
4199 assert(cb_access_context);
4200 auto *context = cb_access_context->GetCurrentAccessContext();
4201 assert(context);
4202
4203 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004204 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004205
4206 for (uint32_t region = 0; region < regionCount; region++) {
4207 const auto &blit_region = pRegions[region];
4208 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004209 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4210 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4211 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4212 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4213 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4214 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004215 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004216 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004217 }
4218 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004219 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4220 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4221 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4222 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4223 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4224 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004225 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004226 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004227 }
4228 }
4229}
locke-lunarg36ba2592020-04-03 09:42:04 -06004230
Jeff Leger178b1e52020-10-05 12:22:23 -04004231void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4232 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4233 const VkImageBlit *pRegions, VkFilter filter) {
4234 auto *cb_access_context = GetAccessContext(commandBuffer);
4235 assert(cb_access_context);
4236 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4237 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4238 pRegions, filter);
4239 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4240}
4241
4242void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4243 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4244 auto *cb_access_context = GetAccessContext(commandBuffer);
4245 assert(cb_access_context);
4246 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4247 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4248 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4249 pBlitImageInfo->filter, tag);
4250}
4251
John Zulauffaea0ee2021-01-14 14:01:32 -07004252bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4253 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4254 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4255 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004256 bool skip = false;
4257 if (drawCount == 0) return skip;
4258
4259 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4260 VkDeviceSize size = struct_size;
4261 if (drawCount == 1 || stride == size) {
4262 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004263 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004264 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4265 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004266 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004267 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004268 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004269 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004270 }
4271 } else {
4272 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004273 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004274 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4275 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004276 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004277 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4278 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004279 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004280 break;
4281 }
4282 }
4283 }
4284 return skip;
4285}
4286
John Zulauf14940722021-04-12 15:19:02 -06004287void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004288 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4289 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004290 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4291 VkDeviceSize size = struct_size;
4292 if (drawCount == 1 || stride == size) {
4293 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004294 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004295 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004296 } else {
4297 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004298 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004299 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4300 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004301 }
4302 }
4303}
4304
John Zulauffaea0ee2021-01-14 14:01:32 -07004305bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4306 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4307 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004308 bool skip = false;
4309
4310 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004311 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004312 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4313 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004314 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004315 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004316 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004317 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004318 }
4319 return skip;
4320}
4321
John Zulauf14940722021-04-12 15:19:02 -06004322void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004323 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004324 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004325 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004326}
4327
locke-lunarg36ba2592020-04-03 09:42:04 -06004328bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004329 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004330 const auto *cb_access_context = GetAccessContext(commandBuffer);
4331 assert(cb_access_context);
4332 if (!cb_access_context) return skip;
4333
locke-lunarg61870c22020-06-09 14:51:50 -06004334 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004335 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004336}
4337
4338void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004339 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004340 auto *cb_access_context = GetAccessContext(commandBuffer);
4341 assert(cb_access_context);
4342 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004343
locke-lunarg61870c22020-06-09 14:51:50 -06004344 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004345}
locke-lunarge1a67022020-04-29 00:15:36 -06004346
4347bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004348 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004349 const auto *cb_access_context = GetAccessContext(commandBuffer);
4350 assert(cb_access_context);
4351 if (!cb_access_context) return skip;
4352
4353 const auto *context = cb_access_context->GetCurrentAccessContext();
4354 assert(context);
4355 if (!context) return skip;
4356
locke-lunarg61870c22020-06-09 14:51:50 -06004357 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004358 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4359 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004360 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004361}
4362
4363void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004364 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004365 auto *cb_access_context = GetAccessContext(commandBuffer);
4366 assert(cb_access_context);
4367 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4368 auto *context = cb_access_context->GetCurrentAccessContext();
4369 assert(context);
4370
locke-lunarg61870c22020-06-09 14:51:50 -06004371 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4372 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004373}
4374
4375bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4376 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004377 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004378 const auto *cb_access_context = GetAccessContext(commandBuffer);
4379 assert(cb_access_context);
4380 if (!cb_access_context) return skip;
4381
locke-lunarg61870c22020-06-09 14:51:50 -06004382 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4383 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4384 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004385 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004386}
4387
4388void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4389 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004390 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004391 auto *cb_access_context = GetAccessContext(commandBuffer);
4392 assert(cb_access_context);
4393 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004394
locke-lunarg61870c22020-06-09 14:51:50 -06004395 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4396 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4397 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004398}
4399
4400bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4401 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004402 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004403 const auto *cb_access_context = GetAccessContext(commandBuffer);
4404 assert(cb_access_context);
4405 if (!cb_access_context) return skip;
4406
locke-lunarg61870c22020-06-09 14:51:50 -06004407 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4408 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4409 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004410 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004411}
4412
4413void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4414 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004415 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004416 auto *cb_access_context = GetAccessContext(commandBuffer);
4417 assert(cb_access_context);
4418 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004419
locke-lunarg61870c22020-06-09 14:51:50 -06004420 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4421 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4422 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004423}
4424
4425bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4426 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004427 bool skip = false;
4428 if (drawCount == 0) return skip;
4429
locke-lunargff255f92020-05-13 18:53:52 -06004430 const auto *cb_access_context = GetAccessContext(commandBuffer);
4431 assert(cb_access_context);
4432 if (!cb_access_context) return skip;
4433
4434 const auto *context = cb_access_context->GetCurrentAccessContext();
4435 assert(context);
4436 if (!context) return skip;
4437
locke-lunarg61870c22020-06-09 14:51:50 -06004438 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4439 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004440 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4441 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004442
4443 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4444 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4445 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004446 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004447 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004448}
4449
4450void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4451 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004452 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004453 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004454 auto *cb_access_context = GetAccessContext(commandBuffer);
4455 assert(cb_access_context);
4456 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4457 auto *context = cb_access_context->GetCurrentAccessContext();
4458 assert(context);
4459
locke-lunarg61870c22020-06-09 14:51:50 -06004460 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4461 cb_access_context->RecordDrawSubpassAttachment(tag);
4462 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004463
4464 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4465 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4466 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004467 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004468}
4469
4470bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4471 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004472 bool skip = false;
4473 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004474 const auto *cb_access_context = GetAccessContext(commandBuffer);
4475 assert(cb_access_context);
4476 if (!cb_access_context) return skip;
4477
4478 const auto *context = cb_access_context->GetCurrentAccessContext();
4479 assert(context);
4480 if (!context) return skip;
4481
locke-lunarg61870c22020-06-09 14:51:50 -06004482 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4483 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004484 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4485 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004486
4487 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4488 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4489 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004490 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004491 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004492}
4493
4494void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4495 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004496 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004497 auto *cb_access_context = GetAccessContext(commandBuffer);
4498 assert(cb_access_context);
4499 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4500 auto *context = cb_access_context->GetCurrentAccessContext();
4501 assert(context);
4502
locke-lunarg61870c22020-06-09 14:51:50 -06004503 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4504 cb_access_context->RecordDrawSubpassAttachment(tag);
4505 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004506
4507 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4508 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4509 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004510 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004511}
4512
4513bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4514 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4515 uint32_t stride, const char *function) const {
4516 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004517 const auto *cb_access_context = GetAccessContext(commandBuffer);
4518 assert(cb_access_context);
4519 if (!cb_access_context) return skip;
4520
4521 const auto *context = cb_access_context->GetCurrentAccessContext();
4522 assert(context);
4523 if (!context) return skip;
4524
locke-lunarg61870c22020-06-09 14:51:50 -06004525 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4526 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004527 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4528 maxDrawCount, stride, function);
4529 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004530
4531 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4532 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4533 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004534 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004535 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004536}
4537
4538bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4539 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4540 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004541 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4542 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004543}
4544
sfricke-samsung85584a72021-09-30 21:43:38 -07004545void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4546 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4547 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004548 auto *cb_access_context = GetAccessContext(commandBuffer);
4549 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004550 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004551 auto *context = cb_access_context->GetCurrentAccessContext();
4552 assert(context);
4553
locke-lunarg61870c22020-06-09 14:51:50 -06004554 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4555 cb_access_context->RecordDrawSubpassAttachment(tag);
4556 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4557 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
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
sfricke-samsung85584a72021-09-30 21:43:38 -07004565void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4566 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4567 uint32_t stride) {
4568 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4569 stride);
4570 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4571 CMD_DRAWINDIRECTCOUNT);
4572}
locke-lunarge1a67022020-04-29 00:15:36 -06004573bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4574 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4575 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004576 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4577 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004578}
4579
4580void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4581 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4582 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004583 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4584 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004585 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4586 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004587}
4588
4589bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4590 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4591 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004592 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4593 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004594}
4595
4596void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4597 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4598 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004599 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4600 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004601 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4602 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004603}
4604
4605bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4606 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4607 uint32_t stride, const char *function) const {
4608 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004609 const auto *cb_access_context = GetAccessContext(commandBuffer);
4610 assert(cb_access_context);
4611 if (!cb_access_context) return skip;
4612
4613 const auto *context = cb_access_context->GetCurrentAccessContext();
4614 assert(context);
4615 if (!context) return skip;
4616
locke-lunarg61870c22020-06-09 14:51:50 -06004617 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4618 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004619 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4620 offset, maxDrawCount, stride, function);
4621 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004622
4623 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4624 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4625 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004626 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004627 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004628}
4629
4630bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4631 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4632 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004633 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4634 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004635}
4636
sfricke-samsung85584a72021-09-30 21:43:38 -07004637void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4638 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4639 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004640 auto *cb_access_context = GetAccessContext(commandBuffer);
4641 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004642 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004643 auto *context = cb_access_context->GetCurrentAccessContext();
4644 assert(context);
4645
locke-lunarg61870c22020-06-09 14:51:50 -06004646 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4647 cb_access_context->RecordDrawSubpassAttachment(tag);
4648 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4649 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004650
4651 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4652 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004653 // We will update the index and vertex buffer in SubmitQueue in the future.
4654 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004655}
4656
sfricke-samsung85584a72021-09-30 21:43:38 -07004657void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4658 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4659 uint32_t maxDrawCount, uint32_t stride) {
4660 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4661 maxDrawCount, stride);
4662 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4663 CMD_DRAWINDEXEDINDIRECTCOUNT);
4664}
4665
locke-lunarge1a67022020-04-29 00:15:36 -06004666bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4667 VkDeviceSize offset, VkBuffer countBuffer,
4668 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4669 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004670 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4671 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004672}
4673
4674void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4675 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4676 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004677 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4678 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004679 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4680 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004681}
4682
4683bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4684 VkDeviceSize offset, VkBuffer countBuffer,
4685 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4686 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004687 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4688 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004689}
4690
4691void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(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::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4695 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004696 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4697 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004698}
4699
4700bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4701 const VkClearColorValue *pColor, uint32_t rangeCount,
4702 const VkImageSubresourceRange *pRanges) const {
4703 bool skip = false;
4704 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
4712 const auto *image_state = Get<IMAGE_STATE>(image);
4713
4714 for (uint32_t index = 0; index < rangeCount; index++) {
4715 const auto &range = pRanges[index];
4716 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004717 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004718 if (hazard.hazard) {
4719 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004720 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004721 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004722 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004723 }
4724 }
4725 }
4726 return skip;
4727}
4728
4729void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4730 const VkClearColorValue *pColor, uint32_t rangeCount,
4731 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004732 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004733 auto *cb_access_context = GetAccessContext(commandBuffer);
4734 assert(cb_access_context);
4735 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4736 auto *context = cb_access_context->GetCurrentAccessContext();
4737 assert(context);
4738
4739 const auto *image_state = Get<IMAGE_STATE>(image);
4740
4741 for (uint32_t index = 0; index < rangeCount; index++) {
4742 const auto &range = pRanges[index];
4743 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004744 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004745 }
4746 }
4747}
4748
4749bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4750 VkImageLayout imageLayout,
4751 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4752 const VkImageSubresourceRange *pRanges) const {
4753 bool skip = false;
4754 const auto *cb_access_context = GetAccessContext(commandBuffer);
4755 assert(cb_access_context);
4756 if (!cb_access_context) return skip;
4757
4758 const auto *context = cb_access_context->GetCurrentAccessContext();
4759 assert(context);
4760 if (!context) return skip;
4761
4762 const auto *image_state = Get<IMAGE_STATE>(image);
4763
4764 for (uint32_t index = 0; index < rangeCount; index++) {
4765 const auto &range = pRanges[index];
4766 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004767 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004768 if (hazard.hazard) {
4769 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004770 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004771 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004772 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004773 }
4774 }
4775 }
4776 return skip;
4777}
4778
4779void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4780 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4781 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004782 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004783 auto *cb_access_context = GetAccessContext(commandBuffer);
4784 assert(cb_access_context);
4785 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4786 auto *context = cb_access_context->GetCurrentAccessContext();
4787 assert(context);
4788
4789 const auto *image_state = Get<IMAGE_STATE>(image);
4790
4791 for (uint32_t index = 0; index < rangeCount; index++) {
4792 const auto &range = pRanges[index];
4793 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004794 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004795 }
4796 }
4797}
4798
4799bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4800 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4801 VkDeviceSize dstOffset, VkDeviceSize stride,
4802 VkQueryResultFlags flags) const {
4803 bool skip = false;
4804 const auto *cb_access_context = GetAccessContext(commandBuffer);
4805 assert(cb_access_context);
4806 if (!cb_access_context) return skip;
4807
4808 const auto *context = cb_access_context->GetCurrentAccessContext();
4809 assert(context);
4810 if (!context) return skip;
4811
4812 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4813
4814 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004815 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004816 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004817 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004818 skip |=
4819 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4820 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004821 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004822 }
4823 }
locke-lunargff255f92020-05-13 18:53:52 -06004824
4825 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004826 return skip;
4827}
4828
4829void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4830 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4831 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004832 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4833 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004834 auto *cb_access_context = GetAccessContext(commandBuffer);
4835 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004836 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004837 auto *context = cb_access_context->GetCurrentAccessContext();
4838 assert(context);
4839
4840 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4841
4842 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004843 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004844 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004845 }
locke-lunargff255f92020-05-13 18:53:52 -06004846
4847 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004848}
4849
4850bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4851 VkDeviceSize size, uint32_t data) const {
4852 bool skip = false;
4853 const auto *cb_access_context = GetAccessContext(commandBuffer);
4854 assert(cb_access_context);
4855 if (!cb_access_context) return skip;
4856
4857 const auto *context = cb_access_context->GetCurrentAccessContext();
4858 assert(context);
4859 if (!context) return skip;
4860
4861 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4862
4863 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004864 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004865 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004866 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004867 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004868 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004869 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004870 }
4871 }
4872 return skip;
4873}
4874
4875void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4876 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004877 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
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_FILLBUFFER);
4881 auto *context = cb_access_context->GetCurrentAccessContext();
4882 assert(context);
4883
4884 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4885
4886 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004887 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004888 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004889 }
4890}
4891
4892bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4893 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4894 const VkImageResolve *pRegions) const {
4895 bool skip = false;
4896 const auto *cb_access_context = GetAccessContext(commandBuffer);
4897 assert(cb_access_context);
4898 if (!cb_access_context) return skip;
4899
4900 const auto *context = cb_access_context->GetCurrentAccessContext();
4901 assert(context);
4902 if (!context) return skip;
4903
4904 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4905 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4906
4907 for (uint32_t region = 0; region < regionCount; region++) {
4908 const auto &resolve_region = pRegions[region];
4909 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004910 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004911 resolve_region.srcOffset, resolve_region.extent);
4912 if (hazard.hazard) {
4913 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004914 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004915 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004916 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004917 }
4918 }
4919
4920 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004921 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004922 resolve_region.dstOffset, resolve_region.extent);
4923 if (hazard.hazard) {
4924 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004925 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004926 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004927 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004928 }
4929 if (skip) break;
4930 }
4931 }
4932
4933 return skip;
4934}
4935
4936void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4937 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4938 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004939 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4940 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004941 auto *cb_access_context = GetAccessContext(commandBuffer);
4942 assert(cb_access_context);
4943 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4944 auto *context = cb_access_context->GetCurrentAccessContext();
4945 assert(context);
4946
4947 auto *src_image = Get<IMAGE_STATE>(srcImage);
4948 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4949
4950 for (uint32_t region = 0; region < regionCount; region++) {
4951 const auto &resolve_region = pRegions[region];
4952 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004953 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004954 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004955 }
4956 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004957 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004958 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004959 }
4960 }
4961}
4962
Jeff Leger178b1e52020-10-05 12:22:23 -04004963bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4964 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4965 bool skip = false;
4966 const auto *cb_access_context = GetAccessContext(commandBuffer);
4967 assert(cb_access_context);
4968 if (!cb_access_context) return skip;
4969
4970 const auto *context = cb_access_context->GetCurrentAccessContext();
4971 assert(context);
4972 if (!context) return skip;
4973
4974 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4975 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4976
4977 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4978 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4979 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004980 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004981 resolve_region.srcOffset, resolve_region.extent);
4982 if (hazard.hazard) {
4983 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4984 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4985 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004986 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004987 }
4988 }
4989
4990 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004991 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004992 resolve_region.dstOffset, resolve_region.extent);
4993 if (hazard.hazard) {
4994 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4995 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4996 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004997 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004998 }
4999 if (skip) break;
5000 }
5001 }
5002
5003 return skip;
5004}
5005
5006void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
5007 const VkResolveImageInfo2KHR *pResolveImageInfo) {
5008 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
5009 auto *cb_access_context = GetAccessContext(commandBuffer);
5010 assert(cb_access_context);
5011 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
5012 auto *context = cb_access_context->GetCurrentAccessContext();
5013 assert(context);
5014
5015 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
5016 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
5017
5018 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
5019 const auto &resolve_region = pResolveImageInfo->pRegions[region];
5020 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005021 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005022 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005023 }
5024 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005025 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07005026 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04005027 }
5028 }
5029}
5030
locke-lunarge1a67022020-04-29 00:15:36 -06005031bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5032 VkDeviceSize dataSize, const void *pData) const {
5033 bool skip = false;
5034 const auto *cb_access_context = GetAccessContext(commandBuffer);
5035 assert(cb_access_context);
5036 if (!cb_access_context) return skip;
5037
5038 const auto *context = cb_access_context->GetCurrentAccessContext();
5039 assert(context);
5040 if (!context) return skip;
5041
5042 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5043
5044 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005045 // VK_WHOLE_SIZE not allowed
5046 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005047 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06005048 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06005049 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06005050 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005051 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06005052 }
5053 }
5054 return skip;
5055}
5056
5057void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
5058 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005059 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06005060 auto *cb_access_context = GetAccessContext(commandBuffer);
5061 assert(cb_access_context);
5062 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
5063 auto *context = cb_access_context->GetCurrentAccessContext();
5064 assert(context);
5065
5066 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5067
5068 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005069 // VK_WHOLE_SIZE not allowed
5070 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005071 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06005072 }
5073}
locke-lunargff255f92020-05-13 18:53:52 -06005074
5075bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5076 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5077 bool skip = false;
5078 const auto *cb_access_context = GetAccessContext(commandBuffer);
5079 assert(cb_access_context);
5080 if (!cb_access_context) return skip;
5081
5082 const auto *context = cb_access_context->GetCurrentAccessContext();
5083 assert(context);
5084 if (!context) return skip;
5085
5086 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5087
5088 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005089 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005090 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06005091 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06005092 skip |=
5093 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5094 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07005095 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06005096 }
5097 }
5098 return skip;
5099}
5100
5101void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
5102 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06005103 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06005104 auto *cb_access_context = GetAccessContext(commandBuffer);
5105 assert(cb_access_context);
5106 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5107 auto *context = cb_access_context->GetCurrentAccessContext();
5108 assert(context);
5109
5110 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5111
5112 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06005113 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07005114 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06005115 }
5116}
John Zulauf49beb112020-11-04 16:06:31 -07005117
5118bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
5119 bool skip = false;
5120 const auto *cb_context = GetAccessContext(commandBuffer);
5121 assert(cb_context);
5122 if (!cb_context) return skip;
5123
John Zulauf36ef9282021-02-02 11:47:24 -07005124 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005125 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005126}
5127
5128void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5129 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
5130 auto *cb_context = GetAccessContext(commandBuffer);
5131 assert(cb_context);
5132 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07005133 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
5134 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005135}
5136
John Zulauf4edde622021-02-15 08:54:50 -07005137bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5138 const VkDependencyInfoKHR *pDependencyInfo) const {
5139 bool skip = false;
5140 const auto *cb_context = GetAccessContext(commandBuffer);
5141 assert(cb_context);
5142 if (!cb_context || !pDependencyInfo) return skip;
5143
5144 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5145 return set_event_op.Validate(*cb_context);
5146}
5147
5148void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5149 const VkDependencyInfoKHR *pDependencyInfo) {
5150 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
5151 auto *cb_context = GetAccessContext(commandBuffer);
5152 assert(cb_context);
5153 if (!cb_context || !pDependencyInfo) return;
5154
5155 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
5156 set_event_op.Record(cb_context);
5157}
5158
John Zulauf49beb112020-11-04 16:06:31 -07005159bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
5160 VkPipelineStageFlags stageMask) const {
5161 bool skip = false;
5162 const auto *cb_context = GetAccessContext(commandBuffer);
5163 assert(cb_context);
5164 if (!cb_context) return skip;
5165
John Zulauf36ef9282021-02-02 11:47:24 -07005166 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07005167 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005168}
5169
5170void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
5171 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
5172 auto *cb_context = GetAccessContext(commandBuffer);
5173 assert(cb_context);
5174 if (!cb_context) return;
5175
John Zulauf36ef9282021-02-02 11:47:24 -07005176 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
5177 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005178}
5179
John Zulauf4edde622021-02-15 08:54:50 -07005180bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5181 VkPipelineStageFlags2KHR stageMask) const {
5182 bool skip = false;
5183 const auto *cb_context = GetAccessContext(commandBuffer);
5184 assert(cb_context);
5185 if (!cb_context) return skip;
5186
5187 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5188 return reset_event_op.Validate(*cb_context);
5189}
5190
5191void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
5192 VkPipelineStageFlags2KHR stageMask) {
5193 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
5194 auto *cb_context = GetAccessContext(commandBuffer);
5195 assert(cb_context);
5196 if (!cb_context) return;
5197
5198 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5199 reset_event_op.Record(cb_context);
5200}
5201
John Zulauf49beb112020-11-04 16:06:31 -07005202bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5203 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5204 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5205 uint32_t bufferMemoryBarrierCount,
5206 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5207 uint32_t imageMemoryBarrierCount,
5208 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5209 bool skip = false;
5210 const auto *cb_context = GetAccessContext(commandBuffer);
5211 assert(cb_context);
5212 if (!cb_context) return skip;
5213
John Zulauf36ef9282021-02-02 11:47:24 -07005214 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5215 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5216 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005217 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005218}
5219
5220void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5221 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5222 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5223 uint32_t bufferMemoryBarrierCount,
5224 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5225 uint32_t imageMemoryBarrierCount,
5226 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5227 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5228 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5229 imageMemoryBarrierCount, pImageMemoryBarriers);
5230
5231 auto *cb_context = GetAccessContext(commandBuffer);
5232 assert(cb_context);
5233 if (!cb_context) return;
5234
John Zulauf36ef9282021-02-02 11:47:24 -07005235 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5236 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5237 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf8eda1562021-04-13 17:06:41 -06005238 wait_events_op.Record(cb_context);
5239 return;
John Zulauf4a6105a2020-11-17 15:11:05 -07005240}
5241
John Zulauf4edde622021-02-15 08:54:50 -07005242bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5243 const VkDependencyInfoKHR *pDependencyInfos) const {
5244 bool skip = false;
5245 const auto *cb_context = GetAccessContext(commandBuffer);
5246 assert(cb_context);
5247 if (!cb_context) return skip;
5248
5249 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5250 skip |= wait_events_op.Validate(*cb_context);
5251 return skip;
5252}
5253
5254void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5255 const VkDependencyInfoKHR *pDependencyInfos) {
5256 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5257
5258 auto *cb_context = GetAccessContext(commandBuffer);
5259 assert(cb_context);
5260 if (!cb_context) return;
5261
5262 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5263 wait_events_op.Record(cb_context);
5264}
5265
John Zulauf4a6105a2020-11-17 15:11:05 -07005266void SyncEventState::ResetFirstScope() {
5267 for (const auto address_type : kAddressTypes) {
5268 first_scope[static_cast<size_t>(address_type)].clear();
5269 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005270 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07005271}
5272
5273// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005274SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005275 IgnoreReason reason = NotIgnored;
5276
John Zulauf4edde622021-02-15 08:54:50 -07005277 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
5278 reason = SetVsWait2;
5279 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5280 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005281 } else if (unsynchronized_set) {
5282 reason = SetRace;
5283 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005284 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005285 if (missing_bits) reason = MissingStageBits;
5286 }
5287
5288 return reason;
5289}
5290
Jeremy Gebben40a22942020-12-22 14:22:06 -07005291bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005292 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5293 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5294 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005295}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005296
John Zulauf36ef9282021-02-02 11:47:24 -07005297SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5298 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5299 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005300 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5301 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5302 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005303 : SyncOpBase(cmd), barriers_(1) {
5304 auto &barrier_set = barriers_[0];
5305 barrier_set.dependency_flags = dependencyFlags;
5306 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5307 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005308 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005309 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5310 pMemoryBarriers);
5311 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5312 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5313 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5314 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005315}
5316
John Zulauf4edde622021-02-15 08:54:50 -07005317SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5318 const VkDependencyInfoKHR *dep_infos)
5319 : SyncOpBase(cmd), barriers_(event_count) {
5320 for (uint32_t i = 0; i < event_count; i++) {
5321 const auto &dep_info = dep_infos[i];
5322 auto &barrier_set = barriers_[i];
5323 barrier_set.dependency_flags = dep_info.dependencyFlags;
5324 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5325 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5326 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5327 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5328 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5329 dep_info.pMemoryBarriers);
5330 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5331 dep_info.pBufferMemoryBarriers);
5332 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5333 dep_info.pImageMemoryBarriers);
5334 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005335}
5336
John Zulauf36ef9282021-02-02 11:47:24 -07005337SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005338 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5339 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5340 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5341 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5342 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005343 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005344 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5345
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005346SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5347 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005348 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005349
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005350bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5351 bool skip = false;
5352 const auto *context = cb_context.GetCurrentAccessContext();
5353 assert(context);
5354 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005355 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5356
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005357 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005358 const auto &barrier_set = barriers_[0];
5359 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5360 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5361 const auto *image_state = image_barrier.image.get();
5362 if (!image_state) continue;
5363 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5364 if (hazard.hazard) {
5365 // PHASE1 TODO -- add tag information to log msg when useful.
5366 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005367 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005368 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5369 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5370 string_SyncHazard(hazard.hazard), image_barrier.index,
5371 sync_state.report_data->FormatHandle(image_handle).c_str(),
5372 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005373 }
5374 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005375 return skip;
5376}
5377
John Zulaufd5115702021-01-18 12:34:33 -07005378struct SyncOpPipelineBarrierFunctorFactory {
5379 using BarrierOpFunctor = PipelineBarrierOp;
5380 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5381 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5382 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5383 using BufferRange = ResourceAccessRange;
5384 using ImageRange = subresource_adapter::ImageRangeGenerator;
5385 using GlobalRange = ResourceAccessRange;
5386
5387 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5388 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5389 }
John Zulauf14940722021-04-12 15:19:02 -06005390 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005391 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5392 }
5393 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5394 return GlobalBarrierOpFunctor(barrier, false);
5395 }
5396
5397 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5398 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5399 const auto base_address = ResourceBaseAddress(buffer);
5400 return (range + base_address);
5401 }
John Zulauf110413c2021-03-20 05:38:38 -06005402 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005403 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005404
5405 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005406 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005407 return range_gen;
5408 }
5409 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5410};
5411
5412template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005413void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005414 AccessContext *context) {
5415 for (const auto &barrier : barriers) {
5416 const auto *state = barrier.GetState();
5417 if (state) {
5418 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5419 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5420 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5421 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5422 }
5423 }
5424}
5425
5426template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005427void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005428 AccessContext *access_context) {
5429 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5430 for (const auto &barrier : barriers) {
5431 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5432 }
5433 for (const auto address_type : kAddressTypes) {
5434 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5435 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5436 }
5437}
5438
John Zulauf8eda1562021-04-13 17:06:41 -06005439ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005440 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005441 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005442 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf4fa68462021-04-26 21:04:22 -06005443 DoRecord(tag, access_context, events_context);
5444 return tag;
5445}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005446
John Zulauf4fa68462021-04-26 21:04:22 -06005447void SyncOpPipelineBarrier::DoRecord(const ResourceUsageTag tag, AccessContext *access_context,
5448 SyncEventsContext *events_context) const {
John Zulauf8eda1562021-04-13 17:06:41 -06005449 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005450 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5451 assert(barriers_.size() == 1);
5452 const auto &barrier_set = barriers_[0];
5453 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5454 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5455 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005456 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005457 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005458 } else {
5459 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005460 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005461 }
5462 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005463}
5464
John Zulauf8eda1562021-04-13 17:06:41 -06005465bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5466 CommandBufferAccessContext *active_context) const {
John Zulauf4fa68462021-04-26 21:04:22 -06005467 // No Validation for replay, as the layout transition accesses are checked directly, and the src*Mask ordering is captured
5468 // with first access information.
John Zulauf8eda1562021-04-13 17:06:41 -06005469 return false;
5470}
5471
John Zulauf8eda1562021-04-13 17:06:41 -06005472
John Zulauf4edde622021-02-15 08:54:50 -07005473void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5474 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5475 const VkMemoryBarrier *barriers) {
5476 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005477 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005478 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005479 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005480 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005481 }
5482 if (0 == memory_barrier_count) {
5483 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005484 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005485 }
John Zulauf4edde622021-02-15 08:54:50 -07005486 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005487}
5488
John Zulauf4edde622021-02-15 08:54:50 -07005489void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5490 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5491 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5492 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005493 for (uint32_t index = 0; index < barrier_count; index++) {
5494 const auto &barrier = barriers[index];
5495 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5496 if (buffer) {
5497 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5498 const auto range = MakeRange(barrier.offset, barrier_size);
5499 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005500 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005501 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005502 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005503 }
5504 }
5505}
5506
John Zulauf4edde622021-02-15 08:54:50 -07005507void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5508 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5509 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005510 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005511 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005512 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5513 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5514 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005515 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005516 }
John Zulauf4edde622021-02-15 08:54:50 -07005517 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005518}
5519
John Zulauf4edde622021-02-15 08:54:50 -07005520void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5521 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5522 const VkBufferMemoryBarrier2KHR *barriers) {
5523 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005524 for (uint32_t index = 0; index < barrier_count; index++) {
5525 const auto &barrier = barriers[index];
5526 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5527 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5528 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5529 if (buffer) {
5530 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5531 const auto range = MakeRange(barrier.offset, barrier_size);
5532 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005533 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005534 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005535 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005536 }
5537 }
5538}
5539
John Zulauf4edde622021-02-15 08:54:50 -07005540void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5541 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5542 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5543 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005544 for (uint32_t index = 0; index < barrier_count; index++) {
5545 const auto &barrier = barriers[index];
5546 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5547 if (image) {
5548 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5549 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005550 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005551 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005552 image_memory_barriers.emplace_back();
5553 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005554 }
5555 }
5556}
John Zulaufd5115702021-01-18 12:34:33 -07005557
John Zulauf4edde622021-02-15 08:54:50 -07005558void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5559 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5560 const VkImageMemoryBarrier2KHR *barriers) {
5561 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005562 for (uint32_t index = 0; index < barrier_count; index++) {
5563 const auto &barrier = barriers[index];
5564 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5565 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5566 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5567 if (image) {
5568 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5569 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005570 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005571 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005572 image_memory_barriers.emplace_back();
5573 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005574 }
5575 }
5576}
5577
John Zulauf36ef9282021-02-02 11:47:24 -07005578SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005579 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5580 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5581 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5582 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005583 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005584 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5585 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005586 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005587}
5588
John Zulauf4edde622021-02-15 08:54:50 -07005589SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5590 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5591 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5592 MakeEventsList(sync_state, eventCount, pEvents);
5593 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5594}
5595
John Zulaufd5115702021-01-18 12:34:33 -07005596bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005597 const char *const ignored = "Wait operation is ignored for this event.";
5598 bool skip = false;
5599 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005600 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005601
John Zulauf4edde622021-02-15 08:54:50 -07005602 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5603 const auto &barrier_set = barriers_[barrier_set_index];
5604 if (barrier_set.single_exec_scope) {
5605 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5606 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5607 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5608 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5609 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5610 } else {
5611 const auto &barriers = barrier_set.memory_barriers;
5612 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5613 const auto &barrier = barriers[barrier_index];
5614 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5615 const std::string vuid =
5616 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5617 skip =
5618 sync_state.LogInfo(command_buffer_handle, vuid,
5619 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5620 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5621 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5622 }
5623 }
5624 }
5625 }
John Zulaufd5115702021-01-18 12:34:33 -07005626 }
5627
Jeremy Gebben40a22942020-12-22 14:22:06 -07005628 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005629 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005630 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005631 const auto *events_context = cb_context.GetCurrentEventsContext();
5632 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005633 size_t barrier_set_index = 0;
5634 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5635 for (size_t event_index = 0; event_index < events_.size(); event_index++)
5636 for (const auto &event : events_) {
5637 const auto *sync_event = events_context->Get(event.get());
5638 const auto &barrier_set = barriers_[barrier_set_index];
5639 if (!sync_event) {
5640 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5641 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5642 // new validation error... wait without previously submitted set event...
5643 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
5644 barrier_set_index += barrier_set_incr;
5645 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005646 }
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005647 const auto event_handle = sync_event->event->event();
John Zulauf4edde622021-02-15 08:54:50 -07005648 // TODO add "destroyed" checks
5649
5650 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5651 const auto &src_exec_scope = barrier_set.src_exec_scope;
5652 event_stage_masks |= sync_event->scope.mask_param;
5653 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5654 if (ignore_reason) {
5655 switch (ignore_reason) {
5656 case SyncEventState::ResetWaitRace:
5657 case SyncEventState::Reset2WaitRace: {
5658 // Four permuations of Reset and Wait calls...
5659 const char *vuid =
5660 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5661 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5662 vuid =
Jeremy Gebben476f5e22021-03-01 15:27:20 -07005663 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831" : "VUID-vkCmdResetEvent2KHR-event-03832";
John Zulauf4edde622021-02-15 08:54:50 -07005664 }
5665 const char *const message =
5666 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5667 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5668 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5669 CommandTypeString(sync_event->last_command), ignored);
5670 break;
5671 }
5672 case SyncEventState::SetRace: {
5673 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5674 // this event
5675 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5676 const char *const message =
5677 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5678 const char *const reason = "First synchronization scope is undefined.";
5679 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5680 sync_state.report_data->FormatHandle(event_handle).c_str(),
5681 CommandTypeString(sync_event->last_command), reason, ignored);
5682 break;
5683 }
5684 case SyncEventState::MissingStageBits: {
5685 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5686 // Issue error message that event waited for is not in wait events scope
5687 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5688 const char *const message =
5689 "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5690 ". Bits missing from srcStageMask %s. %s";
5691 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5692 sync_state.report_data->FormatHandle(event_handle).c_str(),
5693 sync_event->scope.mask_param, src_exec_scope.mask_param,
5694 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5695 break;
5696 }
5697 case SyncEventState::SetVsWait2: {
5698 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5699 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5700 sync_state.report_data->FormatHandle(event_handle).c_str(),
5701 CommandTypeString(sync_event->last_command));
5702 break;
5703 }
5704 default:
5705 assert(ignore_reason == SyncEventState::NotIgnored);
5706 }
5707 } else if (barrier_set.image_memory_barriers.size()) {
5708 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5709 const auto *context = cb_context.GetCurrentAccessContext();
5710 assert(context);
5711 for (const auto &image_memory_barrier : image_memory_barriers) {
5712 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5713 const auto *image_state = image_memory_barrier.image.get();
5714 if (!image_state) continue;
John Zulauf110413c2021-03-20 05:38:38 -06005715 const auto &subresource_range = image_memory_barrier.range;
John Zulauf4edde622021-02-15 08:54:50 -07005716 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5717 const auto hazard =
5718 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5719 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5720 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005721 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
John Zulauf4edde622021-02-15 08:54:50 -07005722 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5723 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005724 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
John Zulauf4edde622021-02-15 08:54:50 -07005725 cb_context.FormatUsage(hazard).c_str());
5726 break;
5727 }
John Zulaufd5115702021-01-18 12:34:33 -07005728 }
5729 }
John Zulauf4edde622021-02-15 08:54:50 -07005730 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5731 // 03839
5732 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005733 }
John Zulaufd5115702021-01-18 12:34:33 -07005734
5735 // 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 -07005736 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 -07005737 if (extra_stage_bits) {
5738 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005739 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5740 const char *const vuid =
5741 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005742 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005743 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005744 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005745 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005746 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005747 " vkCmdSetEvent may be in previously submitted command buffer.");
5748 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005749 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005750 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005751 }
5752 }
5753 return skip;
5754}
5755
5756struct SyncOpWaitEventsFunctorFactory {
5757 using BarrierOpFunctor = WaitEventBarrierOp;
5758 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5759 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5760 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5761 using BufferRange = EventSimpleRangeGenerator;
5762 using ImageRange = EventImageRangeGenerator;
5763 using GlobalRange = EventSimpleRangeGenerator;
5764
5765 // Need to restrict to only valid exec and access scope for this event
5766 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5767 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005768 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005769 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5770 return barrier;
5771 }
5772 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5773 auto barrier = RestrictToEvent(barrier_arg);
5774 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5775 }
John Zulauf14940722021-04-12 15:19:02 -06005776 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005777 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5778 }
5779 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5780 auto barrier = RestrictToEvent(barrier_arg);
5781 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5782 }
5783
5784 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5785 const AccessAddressType address_type = GetAccessAddressType(buffer);
5786 const auto base_address = ResourceBaseAddress(buffer);
5787 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5788 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5789 return filtered_range_gen;
5790 }
John Zulauf110413c2021-03-20 05:38:38 -06005791 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005792 if (!SimpleBinding(image)) return ImageRange();
5793 const auto address_type = GetAccessAddressType(image);
5794 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005795 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005796 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5797
5798 return filtered_range_gen;
5799 }
5800 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5801 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5802 }
5803 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5804 SyncEventState *sync_event;
5805};
5806
John Zulauf8eda1562021-04-13 17:06:41 -06005807ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07005808 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005809 auto *access_context = cb_context->GetCurrentAccessContext();
5810 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005811 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07005812 auto *events_context = cb_context->GetCurrentEventsContext();
5813 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005814 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07005815
5816 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5817 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5818 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5819 access_context->ResolvePreviousAccesses();
5820
John Zulaufd5115702021-01-18 12:34:33 -07005821 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5822 // sync_event will be in the recorded context, but we need to update the sync_events in the current context....
John Zulauf4edde622021-02-15 08:54:50 -07005823 size_t barrier_set_index = 0;
5824 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5825 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005826 for (auto &event_shared : events_) {
5827 if (!event_shared.get()) continue;
5828 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005829
John Zulauf4edde622021-02-15 08:54:50 -07005830 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005831
John Zulauf4edde622021-02-15 08:54:50 -07005832 const auto &barrier_set = barriers_[barrier_set_index];
5833 const auto &dst = barrier_set.dst_exec_scope;
5834 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005835 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5836 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5837 // of the barriers is maintained.
5838 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005839 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5840 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5841 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005842
5843 // Apply the global barrier to the event itself (for race condition tracking)
5844 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5845 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5846 sync_event->barriers |= dst.exec_scope;
5847 } else {
5848 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5849 sync_event->barriers = 0U;
5850 }
John Zulauf4edde622021-02-15 08:54:50 -07005851 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005852 }
5853
5854 // Apply the pending barriers
5855 ResolvePendingBarrierFunctor apply_pending_action(tag);
5856 access_context->ApplyToContext(apply_pending_action);
John Zulauf8eda1562021-04-13 17:06:41 -06005857
5858 return tag;
John Zulaufd5115702021-01-18 12:34:33 -07005859}
5860
John Zulauf8eda1562021-04-13 17:06:41 -06005861bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5862 CommandBufferAccessContext *active_context) const {
5863 return false;
5864}
5865
John Zulauf4fa68462021-04-26 21:04:22 -06005866void SyncOpWaitEvents::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06005867
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005868bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5869 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5870 bool skip = false;
5871 const auto *cb_access_context = GetAccessContext(commandBuffer);
5872 assert(cb_access_context);
5873 if (!cb_access_context) return skip;
5874
5875 const auto *context = cb_access_context->GetCurrentAccessContext();
5876 assert(context);
5877 if (!context) return skip;
5878
5879 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5880
5881 if (dst_buffer) {
5882 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5883 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5884 if (hazard.hazard) {
5885 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5886 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5887 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06005888 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005889 }
5890 }
5891 return skip;
5892}
5893
John Zulauf669dfd52021-01-27 17:15:28 -07005894void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005895 events_.reserve(event_count);
5896 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005897 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005898 }
5899}
John Zulauf6ce24372021-01-30 05:56:25 -07005900
John Zulauf36ef9282021-02-02 11:47:24 -07005901SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005902 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005903 : SyncOpBase(cmd),
5904 event_(sync_state.GetShared<EVENT_STATE>(event)),
5905 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005906
5907bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005908 auto *events_context = cb_context.GetCurrentEventsContext();
5909 assert(events_context);
5910 bool skip = false;
5911 if (!events_context) return skip;
5912
5913 const auto &sync_state = cb_context.GetSyncState();
5914 const auto *sync_event = events_context->Get(event_);
5915 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5916
5917 const char *const set_wait =
5918 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5919 "hazards.";
5920 const char *message = set_wait; // Only one message this call.
5921 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5922 const char *vuid = nullptr;
5923 switch (sync_event->last_command) {
5924 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005925 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005926 // Needs a barrier between set and reset
5927 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5928 break;
John Zulauf4edde622021-02-15 08:54:50 -07005929 case CMD_WAITEVENTS:
5930 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005931 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5932 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5933 break;
5934 }
5935 default:
5936 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005937 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5938 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005939 break;
5940 }
5941 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005942 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
5943 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005944 CommandTypeString(sync_event->last_command));
5945 }
5946 }
5947 return skip;
5948}
5949
John Zulauf8eda1562021-04-13 17:06:41 -06005950ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
5951 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005952 auto *events_context = cb_context->GetCurrentEventsContext();
5953 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005954 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005955
5956 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06005957 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07005958
5959 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005960 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005961 sync_event->unsynchronized_set = CMD_NONE;
5962 sync_event->ResetFirstScope();
5963 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06005964
5965 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005966}
5967
John Zulauf8eda1562021-04-13 17:06:41 -06005968bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5969 CommandBufferAccessContext *active_context) const {
5970 return false;
5971}
5972
John Zulauf4fa68462021-04-26 21:04:22 -06005973void SyncOpResetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06005974
John Zulauf36ef9282021-02-02 11:47:24 -07005975SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005976 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005977 : SyncOpBase(cmd),
5978 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005979 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5980 dep_info_() {}
5981
5982SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5983 const VkDependencyInfoKHR &dep_info)
5984 : SyncOpBase(cmd),
5985 event_(sync_state.GetShared<EVENT_STATE>(event)),
5986 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5987 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005988
5989bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5990 // I'll put this here just in case we need to pass this in for future extension support
John Zulauf6ce24372021-01-30 05:56:25 -07005991 bool skip = false;
5992
5993 const auto &sync_state = cb_context.GetSyncState();
5994 auto *events_context = cb_context.GetCurrentEventsContext();
5995 assert(events_context);
5996 if (!events_context) return skip;
5997
5998 const auto *sync_event = events_context->Get(event_);
5999 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
6000
6001 const char *const reset_set =
6002 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
6003 "hazards.";
6004 const char *const wait =
6005 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
6006
6007 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07006008 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07006009 const char *message = nullptr;
6010 switch (sync_event->last_command) {
6011 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006012 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006013 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07006014 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07006015 message = reset_set;
6016 break;
6017 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07006018 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006019 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07006020 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07006021 message = reset_set;
6022 break;
6023 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07006024 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07006025 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07006026 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07006027 message = wait;
6028 break;
6029 default:
6030 // The only other valid last command that wasn't one.
6031 assert(sync_event->last_command == CMD_NONE);
6032 break;
6033 }
John Zulauf4edde622021-02-15 08:54:50 -07006034 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07006035 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07006036 std::string vuid("SYNC-");
6037 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06006038 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
6039 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07006040 CommandTypeString(sync_event->last_command));
6041 }
6042 }
6043
6044 return skip;
6045}
6046
John Zulauf8eda1562021-04-13 17:06:41 -06006047ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07006048 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07006049 auto *events_context = cb_context->GetCurrentEventsContext();
6050 auto *access_context = cb_context->GetCurrentAccessContext();
6051 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06006052 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006053
6054 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06006055 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07006056
6057 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
6058 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
6059 // any issues caused by naive scope setting here.
6060
6061 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
6062 // Given:
6063 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
6064 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
6065
6066 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
6067 sync_event->unsynchronized_set = sync_event->last_command;
6068 sync_event->ResetFirstScope();
6069 } else if (sync_event->scope.exec_scope == 0) {
6070 // We only set the scope if there isn't one
6071 sync_event->scope = src_exec_scope_;
6072
6073 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
6074 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
6075 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
6076 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
6077 }
6078 };
6079 access_context->ForAll(set_scope);
6080 sync_event->unsynchronized_set = CMD_NONE;
6081 sync_event->first_scope_tag = tag;
6082 }
John Zulauf4edde622021-02-15 08:54:50 -07006083 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
6084 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07006085 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06006086
6087 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07006088}
John Zulauf64ffe552021-02-06 10:25:07 -07006089
John Zulauf8eda1562021-04-13 17:06:41 -06006090bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6091 CommandBufferAccessContext *active_context) const {
6092 return false;
6093}
6094
John Zulauf4fa68462021-04-26 21:04:22 -06006095void SyncOpSetEvent::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006096
John Zulauf64ffe552021-02-06 10:25:07 -07006097SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
6098 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07006099 const VkSubpassBeginInfo *pSubpassBeginInfo)
6100 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006101 if (pRenderPassBegin) {
6102 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
6103 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
6104 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
6105 if (fb_state) {
6106 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
6107 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
6108 // Note that this a safe to presist as long as shared_attachments is not cleared
6109 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08006110 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07006111 attachments_.emplace_back(attachment.get());
6112 }
6113 }
6114 if (pSubpassBeginInfo) {
6115 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
6116 }
6117 }
6118}
6119
6120bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6121 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
6122 bool skip = false;
6123
6124 assert(rp_state_.get());
6125 if (nullptr == rp_state_.get()) return skip;
6126 auto &rp_state = *rp_state_.get();
6127
6128 const uint32_t subpass = 0;
6129
6130 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
6131 // hasn't happened yet)
6132 const std::vector<AccessContext> empty_context_vector;
6133 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
6134 cb_context.GetCurrentAccessContext());
6135
6136 // Validate attachment operations
6137 if (attachments_.size() == 0) return skip;
6138 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07006139
6140 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
6141 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
6142 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
6143 // operations (though it's currently a messy approach)
6144 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
6145 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006146
6147 // Validate load operations if there were no layout transition hazards
6148 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07006149 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
6150 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07006151 }
6152
6153 return skip;
6154}
6155
John Zulauf8eda1562021-04-13 17:06:41 -06006156ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
6157 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07006158 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
6159 assert(rp_state_.get());
John Zulauf8eda1562021-04-13 17:06:41 -06006160 if (nullptr == rp_state_.get()) return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006161 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
John Zulauf8eda1562021-04-13 17:06:41 -06006162
6163 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006164}
6165
John Zulauf8eda1562021-04-13 17:06:41 -06006166bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6167 CommandBufferAccessContext *active_context) const {
6168 return false;
6169}
6170
John Zulauf4fa68462021-04-26 21:04:22 -06006171void SyncOpBeginRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {
6172}
John Zulauf8eda1562021-04-13 17:06:41 -06006173
John Zulauf64ffe552021-02-06 10:25:07 -07006174SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07006175 const VkSubpassEndInfo *pSubpassEndInfo)
6176 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006177 if (pSubpassBeginInfo) {
6178 subpass_begin_info_.initialize(pSubpassBeginInfo);
6179 }
6180 if (pSubpassEndInfo) {
6181 subpass_end_info_.initialize(pSubpassEndInfo);
6182 }
6183}
6184
6185bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
6186 bool skip = false;
6187 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6188 if (!renderpass_context) return skip;
6189
6190 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
6191 return skip;
6192}
6193
John Zulauf8eda1562021-04-13 17:06:41 -06006194ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006195 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006196 // TODO PHASE2 Need to fix renderpass tagging/segregation of barrier and access operations for QueueSubmit time validation
6197 auto prev_tag = cb_context->NextCommandTag(cmd_);
6198 auto next_tag = cb_context->NextSubcommandTag(cmd_);
6199
6200 cb_context->RecordNextSubpass(prev_tag, next_tag);
6201 // TODO PHASE2 This needs to be the tag of the barrier operations
6202 return prev_tag;
6203}
6204
6205bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6206 CommandBufferAccessContext *active_context) const {
6207 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006208}
6209
sfricke-samsung85584a72021-09-30 21:43:38 -07006210SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6211 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006212 if (pSubpassEndInfo) {
6213 subpass_end_info_.initialize(pSubpassEndInfo);
6214 }
6215}
6216
John Zulauf4fa68462021-04-26 21:04:22 -06006217void SyncOpNextSubpass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006218
John Zulauf64ffe552021-02-06 10:25:07 -07006219bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6220 bool skip = false;
6221 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6222
6223 if (!renderpass_context) return skip;
6224 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6225 return skip;
6226}
6227
John Zulauf8eda1562021-04-13 17:06:41 -06006228ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006229 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006230 const auto tag = cb_context->NextCommandTag(cmd_);
6231 cb_context->RecordEndRenderPass(tag);
6232 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006233}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006234
John Zulauf8eda1562021-04-13 17:06:41 -06006235bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6236 CommandBufferAccessContext *active_context) const {
6237 return false;
6238}
6239
John Zulauf4fa68462021-04-26 21:04:22 -06006240void SyncOpEndRenderPass::DoRecord(ResourceUsageTag tag, AccessContext *access_context, SyncEventsContext *events_context) const {}
John Zulauf8eda1562021-04-13 17:06:41 -06006241
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006242void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6243 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6244 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6245 auto *cb_access_context = GetAccessContext(commandBuffer);
6246 assert(cb_access_context);
6247 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6248 auto *context = cb_access_context->GetCurrentAccessContext();
6249 assert(context);
6250
6251 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
6252
6253 if (dst_buffer) {
6254 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6255 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6256 }
6257}
John Zulaufd05c5842021-03-26 11:32:16 -06006258
John Zulaufae842002021-04-15 18:20:55 -06006259bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6260 const VkCommandBuffer *pCommandBuffers) const {
6261 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6262 const char *func_name = "vkCmdExecuteCommands";
6263 const auto *cb_context = GetAccessContext(commandBuffer);
6264 assert(cb_context);
John Zulauf4fa68462021-04-26 21:04:22 -06006265
6266 // Heavyweight, but we need a proxy copy of the active command buffer access context
6267 CommandBufferAccessContext proxy_cb_context(*cb_context, CommandBufferAccessContext::AsProxyContext());
John Zulaufae842002021-04-15 18:20:55 -06006268
6269 // Make working copies of the access and events contexts
John Zulauf4fa68462021-04-26 21:04:22 -06006270 proxy_cb_context.NextCommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006271
6272 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
John Zulauf4fa68462021-04-26 21:04:22 -06006273 proxy_cb_context.NextSubcommandTag(CMD_EXECUTECOMMANDS);
John Zulaufae842002021-04-15 18:20:55 -06006274 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6275 if (!recorded_cb_context) continue;
John Zulauf4fa68462021-04-26 21:04:22 -06006276
6277 const auto *recorded_context = recorded_cb_context->GetCurrentAccessContext();
6278 assert(recorded_context);
6279 skip |= recorded_cb_context->ValidateFirstUse(&proxy_cb_context, func_name, cb_index);
6280
6281 // The barriers have already been applied in ValidatFirstUse
6282 ResourceUsageRange tag_range = proxy_cb_context.ImportRecordedAccessLog(*recorded_cb_context);
6283 proxy_cb_context.ResolveRecordedContext(*recorded_context, tag_range.begin);
John Zulaufae842002021-04-15 18:20:55 -06006284 }
6285
John Zulaufae842002021-04-15 18:20:55 -06006286 return skip;
6287}
6288
6289void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6290 const VkCommandBuffer *pCommandBuffers) {
6291 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
John Zulauf4fa68462021-04-26 21:04:22 -06006292 auto *cb_context = GetAccessContext(commandBuffer);
6293 assert(cb_context);
6294 cb_context->NextCommandTag(CMD_EXECUTECOMMANDS);
6295 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
6296 cb_context->NextSubcommandTag(CMD_EXECUTECOMMANDS);
6297 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6298 if (!recorded_cb_context) continue;
6299 cb_context->RecordExecutedCommandBuffer(*recorded_cb_context, CMD_EXECUTECOMMANDS);
6300 }
John Zulaufae842002021-04-15 18:20:55 -06006301}
6302
John Zulaufd0ec59f2021-03-13 14:25:08 -07006303AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6304 : view_(view), view_mask_(), gen_store_() {
6305 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6306 const IMAGE_STATE &image_state = *view_->image_state.get();
6307 const auto base_address = ResourceBaseAddress(image_state);
6308 const auto *encoder = image_state.fragment_encoder.get();
6309 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006310 // Get offset and extent for the view, accounting for possible depth slicing
6311 const VkOffset3D zero_offset = view->GetOffset();
6312 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006313 // Intentional copy
6314 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6315 view_mask_ = subres_range.aspectMask;
6316 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6317 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6318
6319 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6320 if (depth && (depth != view_mask_)) {
6321 subres_range.aspectMask = depth;
6322 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6323 }
6324 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6325 if (stencil && (stencil != view_mask_)) {
6326 subres_range.aspectMask = stencil;
6327 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6328 }
6329}
6330
6331const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6332 const ImageRangeGen *got = nullptr;
6333 switch (gen_type) {
6334 case kViewSubresource:
6335 got = &gen_store_[kViewSubresource];
6336 break;
6337 case kRenderArea:
6338 got = &gen_store_[kRenderArea];
6339 break;
6340 case kDepthOnlyRenderArea:
6341 got =
6342 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6343 break;
6344 case kStencilOnlyRenderArea:
6345 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6346 : &gen_store_[Gen::kStencilOnlyRenderArea];
6347 break;
6348 default:
6349 assert(got);
6350 }
6351 return got;
6352}
6353
6354AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6355 assert(IsValid());
6356 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6357 if (depth_op) {
6358 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6359 if (stencil_op) {
6360 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6361 return kRenderArea;
6362 }
6363 return kDepthOnlyRenderArea;
6364 }
6365 if (stencil_op) {
6366 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6367 return kStencilOnlyRenderArea;
6368 }
6369
6370 assert(depth_op || stencil_op);
6371 return kRenderArea;
6372}
6373
6374AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006375
6376void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6377 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6378 for (auto &event_pair : map_) {
6379 assert(event_pair.second); // Shouldn't be storing empty
6380 auto &sync_event = *event_pair.second;
6381 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6382 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6383 sync_event.barriers |= dst.exec_scope;
6384 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6385 }
6386 }
6387}