blob: ed5e2a769ac781c5c08c8d2c989879dd1485362e [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 Zulauf43cc7462020-12-03 12:33:12 -070040const static std::array<AccessAddressType, static_cast<size_t>(AccessAddressType::kTypeCount)> kAddressTypes = {
41 AccessAddressType::kLinear, AccessAddressType::kIdealized};
42
John Zulaufd5115702021-01-18 12:34:33 -070043static constexpr AccessAddressType GetAccessAddressType(const BUFFER_STATE &) { return AccessAddressType::kLinear; };
John Zulauf264cce02021-02-05 14:40:47 -070044static AccessAddressType GetAccessAddressType(const IMAGE_STATE &image) {
45 return SimpleBinding(image) ? AccessContext::ImageAddressType(image) : AccessAddressType::kIdealized;
46}
John Zulaufd5115702021-01-18 12:34:33 -070047
John Zulauf9cb530d2019-09-30 14:14:10 -060048static const char *string_SyncHazardVUID(SyncHazard hazard) {
49 switch (hazard) {
50 case SyncHazard::NONE:
John Zulauf2f952d22020-02-10 11:34:51 -070051 return "SYNC-HAZARD-NONE";
John Zulauf9cb530d2019-09-30 14:14:10 -060052 break;
53 case SyncHazard::READ_AFTER_WRITE:
54 return "SYNC-HAZARD-READ_AFTER_WRITE";
55 break;
56 case SyncHazard::WRITE_AFTER_READ:
57 return "SYNC-HAZARD-WRITE_AFTER_READ";
58 break;
59 case SyncHazard::WRITE_AFTER_WRITE:
60 return "SYNC-HAZARD-WRITE_AFTER_WRITE";
61 break;
John Zulauf2f952d22020-02-10 11:34:51 -070062 case SyncHazard::READ_RACING_WRITE:
63 return "SYNC-HAZARD-READ-RACING-WRITE";
64 break;
65 case SyncHazard::WRITE_RACING_WRITE:
66 return "SYNC-HAZARD-WRITE-RACING-WRITE";
67 break;
68 case SyncHazard::WRITE_RACING_READ:
69 return "SYNC-HAZARD-WRITE-RACING-READ";
70 break;
John Zulauf9cb530d2019-09-30 14:14:10 -060071 default:
72 assert(0);
73 }
74 return "SYNC-HAZARD-INVALID";
75}
76
John Zulauf59e25072020-07-17 10:55:21 -060077static bool IsHazardVsRead(SyncHazard hazard) {
78 switch (hazard) {
79 case SyncHazard::NONE:
80 return false;
81 break;
82 case SyncHazard::READ_AFTER_WRITE:
83 return false;
84 break;
85 case SyncHazard::WRITE_AFTER_READ:
86 return true;
87 break;
88 case SyncHazard::WRITE_AFTER_WRITE:
89 return false;
90 break;
91 case SyncHazard::READ_RACING_WRITE:
92 return false;
93 break;
94 case SyncHazard::WRITE_RACING_WRITE:
95 return false;
96 break;
97 case SyncHazard::WRITE_RACING_READ:
98 return true;
99 break;
100 default:
101 assert(0);
102 }
103 return false;
104}
105
John Zulauf9cb530d2019-09-30 14:14:10 -0600106static const char *string_SyncHazard(SyncHazard hazard) {
107 switch (hazard) {
108 case SyncHazard::NONE:
109 return "NONR";
110 break;
111 case SyncHazard::READ_AFTER_WRITE:
112 return "READ_AFTER_WRITE";
113 break;
114 case SyncHazard::WRITE_AFTER_READ:
115 return "WRITE_AFTER_READ";
116 break;
117 case SyncHazard::WRITE_AFTER_WRITE:
118 return "WRITE_AFTER_WRITE";
119 break;
John Zulauf2f952d22020-02-10 11:34:51 -0700120 case SyncHazard::READ_RACING_WRITE:
121 return "READ_RACING_WRITE";
122 break;
123 case SyncHazard::WRITE_RACING_WRITE:
124 return "WRITE_RACING_WRITE";
125 break;
126 case SyncHazard::WRITE_RACING_READ:
127 return "WRITE_RACING_READ";
128 break;
John Zulauf9cb530d2019-09-30 14:14:10 -0600129 default:
130 assert(0);
131 }
132 return "INVALID HAZARD";
133}
134
John Zulauf37ceaed2020-07-03 16:18:15 -0600135static const SyncStageAccessInfoType *SyncStageAccessInfoFromMask(SyncStageAccessFlags flags) {
136 // Return the info for the first bit found
137 const SyncStageAccessInfoType *info = nullptr;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700138 for (size_t i = 0; i < flags.size(); i++) {
139 if (flags.test(i)) {
140 info = &syncStageAccessInfoByStageAccessIndex[i];
141 break;
John Zulauf37ceaed2020-07-03 16:18:15 -0600142 }
143 }
144 return info;
145}
146
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700147static std::string string_SyncStageAccessFlags(const SyncStageAccessFlags &flags, const char *sep = "|") {
John Zulauf59e25072020-07-17 10:55:21 -0600148 std::string out_str;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700149 if (flags.none()) {
John Zulauf389c34b2020-07-28 11:19:35 -0600150 out_str = "0";
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700151 } else {
152 for (size_t i = 0; i < syncStageAccessInfoByStageAccessIndex.size(); i++) {
153 const auto &info = syncStageAccessInfoByStageAccessIndex[i];
154 if ((flags & info.stage_access_bit).any()) {
155 if (!out_str.empty()) {
156 out_str.append(sep);
157 }
158 out_str.append(info.name);
John Zulauf59e25072020-07-17 10:55:21 -0600159 }
John Zulauf59e25072020-07-17 10:55:21 -0600160 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700161 if (out_str.length() == 0) {
162 out_str.append("Unhandled SyncStageAccess");
163 }
John Zulauf59e25072020-07-17 10:55:21 -0600164 }
165 return out_str;
166}
167
John Zulauf14940722021-04-12 15:19:02 -0600168static std::string string_UsageTag(const ResourceUsageRecord &tag) {
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700169 std::stringstream out;
170
John Zulauffaea0ee2021-01-14 14:01:32 -0700171 out << "command: " << CommandTypeString(tag.command);
172 out << ", seq_no: " << tag.seq_num;
173 if (tag.sub_command != 0) {
174 out << ", subcmd: " << tag.sub_command;
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700175 }
176 return out.str();
177}
178
John Zulauffaea0ee2021-01-14 14:01:32 -0700179std::string CommandBufferAccessContext::FormatUsage(const HazardResult &hazard) const {
John Zulauf37ceaed2020-07-03 16:18:15 -0600180 const auto &tag = hazard.tag;
John Zulauf59e25072020-07-17 10:55:21 -0600181 assert(hazard.usage_index < static_cast<SyncStageAccessIndex>(syncStageAccessInfoByStageAccessIndex.size()));
182 const auto &usage_info = syncStageAccessInfoByStageAccessIndex[hazard.usage_index];
John Zulauf1dae9192020-06-16 15:46:44 -0600183 std::stringstream out;
John Zulauf37ceaed2020-07-03 16:18:15 -0600184 const auto *info = SyncStageAccessInfoFromMask(hazard.prior_access);
185 const char *stage_access_name = info ? info->name : "INVALID_STAGE_ACCESS";
John Zulauf59e25072020-07-17 10:55:21 -0600186 out << "(usage: " << usage_info.name << ", prior_usage: " << stage_access_name;
187 if (IsHazardVsRead(hazard.hazard)) {
188 const auto barriers = hazard.access_state->GetReadBarriers(hazard.prior_access);
Jeremy Gebben40a22942020-12-22 14:22:06 -0700189 out << ", read_barriers: " << string_VkPipelineStageFlags2KHR(barriers);
John Zulauf59e25072020-07-17 10:55:21 -0600190 } else {
191 SyncStageAccessFlags write_barrier = hazard.access_state->GetWriteBarriers();
192 out << ", write_barriers: " << string_SyncStageAccessFlags(write_barrier);
193 }
194
John Zulauffaea0ee2021-01-14 14:01:32 -0700195 // PHASE2 TODO -- add comand buffer and reset from secondary if applicable
John Zulauf14940722021-04-12 15:19:02 -0600196 assert(tag < access_log_.size());
197 out << ", " << string_UsageTag(access_log_[tag]) << ", reset_no: " << reset_count_ << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600198 return out.str();
199}
200
John Zulaufd14743a2020-07-03 09:42:39 -0600201// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
202// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
203// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700204static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700205static const SyncStageAccessFlags kColorAttachmentAccessScope =
206 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
207 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
208 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
209 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700210static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
211 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 -0700212static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
213 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
214 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
215 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700216static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700217static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600218
John Zulauf8e3c3e92021-01-06 11:19:36 -0700219ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700220 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700221 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
222 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
223 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
224
John Zulauf7635de32020-05-29 17:14:15 -0600225// 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 -0600226static const ResourceUsageTag kCurrentCommandTag(ResourceUsageRecord::kMaxIndex);
John Zulaufb027cdb2020-05-21 14:25:22 -0600227
Jeremy Gebben62c3bf42021-07-21 15:38:24 -0600228static VkDeviceSize ResourceBaseAddress(const BINDABLE &bindable) { return bindable.GetFakeBaseAddress(); }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600229
locke-lunarg3c038002020-04-30 23:08:08 -0600230inline VkDeviceSize GetRealWholeSize(VkDeviceSize offset, VkDeviceSize size, VkDeviceSize whole_size) {
231 if (size == VK_WHOLE_SIZE) {
232 return (whole_size - offset);
233 }
234 return size;
235}
236
John Zulauf3e86bf02020-09-12 10:47:57 -0600237static inline VkDeviceSize GetBufferWholeSize(const BUFFER_STATE &buf_state, VkDeviceSize offset, VkDeviceSize size) {
238 return GetRealWholeSize(offset, size, buf_state.createInfo.size);
239}
240
John Zulauf16adfc92020-04-08 10:28:33 -0600241template <typename T>
John Zulauf355e49b2020-04-24 15:11:15 -0600242static ResourceAccessRange MakeRange(const T &has_offset_and_size) {
John Zulauf16adfc92020-04-08 10:28:33 -0600243 return ResourceAccessRange(has_offset_and_size.offset, (has_offset_and_size.offset + has_offset_and_size.size));
244}
245
John Zulauf355e49b2020-04-24 15:11:15 -0600246static ResourceAccessRange MakeRange(VkDeviceSize start, VkDeviceSize size) { return ResourceAccessRange(start, (start + size)); }
John Zulauf16adfc92020-04-08 10:28:33 -0600247
John Zulauf3e86bf02020-09-12 10:47:57 -0600248static inline ResourceAccessRange MakeRange(const BUFFER_STATE &buffer, VkDeviceSize offset, VkDeviceSize size) {
249 return MakeRange(offset, GetBufferWholeSize(buffer, offset, size));
250}
251
252static inline ResourceAccessRange MakeRange(const BUFFER_VIEW_STATE &buf_view_state) {
253 return MakeRange(*buf_view_state.buffer_state.get(), buf_view_state.create_info.offset, buf_view_state.create_info.range);
254}
255
John Zulauf4a6105a2020-11-17 15:11:05 -0700256// Range generators for to allow event scope filtration to be limited to the top of the resource access traversal pipeline
257//
John Zulauf10f1f522020-12-18 12:00:35 -0700258// Note: there is no "begin/end" or reset facility. These are each written as "one time through" generators.
259//
John Zulauf4a6105a2020-11-17 15:11:05 -0700260// Usage:
261// Constructor() -- initializes the generator to point to the begin of the space declared.
262// * -- the current range of the generator empty signfies end
263// ++ -- advance to the next non-empty range (or end)
264
265// A wrapper for a single range with the same semantics as the actual generators below
266template <typename KeyType>
267class SingleRangeGenerator {
268 public:
269 SingleRangeGenerator(const KeyType &range) : current_(range) {}
John Zulaufd5115702021-01-18 12:34:33 -0700270 const KeyType &operator*() const { return current_; }
271 const KeyType *operator->() const { return &current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700272 SingleRangeGenerator &operator++() {
273 current_ = KeyType(); // just one real range
274 return *this;
275 }
276
277 bool operator==(const SingleRangeGenerator &other) const { return current_ == other.current_; }
278
279 private:
280 SingleRangeGenerator() = default;
281 const KeyType range_;
282 KeyType current_;
283};
284
John Zulaufae842002021-04-15 18:20:55 -0600285// Generate the ranges that are the intersection of range and the entries in the RangeMap
286template <typename RangeMap, typename KeyType = typename RangeMap::key_type>
287class MapRangesRangeGenerator {
John Zulauf4a6105a2020-11-17 15:11:05 -0700288 public:
John Zulaufd5115702021-01-18 12:34:33 -0700289 // Default constructed is safe to dereference for "empty" test, but for no other operation.
John Zulaufae842002021-04-15 18:20:55 -0600290 MapRangesRangeGenerator() : range_(), map_(nullptr), map_pos_(), current_() {
John Zulaufd5115702021-01-18 12:34:33 -0700291 // Default construction for KeyType *must* be empty range
292 assert(current_.empty());
293 }
John Zulaufae842002021-04-15 18:20:55 -0600294 MapRangesRangeGenerator(const RangeMap &filter, const KeyType &range) : range_(range), map_(&filter), map_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700295 SeekBegin();
296 }
John Zulaufae842002021-04-15 18:20:55 -0600297 MapRangesRangeGenerator(const MapRangesRangeGenerator &from) = default;
John Zulaufd5115702021-01-18 12:34:33 -0700298
John Zulauf4a6105a2020-11-17 15:11:05 -0700299 const KeyType &operator*() const { return current_; }
300 const KeyType *operator->() const { return &current_; }
John Zulaufae842002021-04-15 18:20:55 -0600301 MapRangesRangeGenerator &operator++() {
302 ++map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700303 UpdateCurrent();
304 return *this;
305 }
306
John Zulaufae842002021-04-15 18:20:55 -0600307 bool operator==(const MapRangesRangeGenerator &other) const { return current_ == other.current_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700308
John Zulaufae842002021-04-15 18:20:55 -0600309 protected:
John Zulauf4a6105a2020-11-17 15:11:05 -0700310 void UpdateCurrent() {
John Zulaufae842002021-04-15 18:20:55 -0600311 if (map_pos_ != map_->cend()) {
312 current_ = range_ & map_pos_->first;
John Zulauf4a6105a2020-11-17 15:11:05 -0700313 } else {
314 current_ = KeyType();
315 }
316 }
317 void SeekBegin() {
John Zulaufae842002021-04-15 18:20:55 -0600318 map_pos_ = map_->lower_bound(range_);
John Zulauf4a6105a2020-11-17 15:11:05 -0700319 UpdateCurrent();
320 }
John Zulaufae842002021-04-15 18:20:55 -0600321
322 // Adding this functionality here, to avoid gratuitous Base:: qualifiers in the derived class
323 // Note: Not exposed in this classes public interface to encourage using a consistent ++/empty generator semantic
324 template <typename Pred>
325 MapRangesRangeGenerator &PredicatedIncrement(Pred &pred) {
326 do {
327 ++map_pos_;
328 } while (map_pos_ != map_->cend() && map_pos_->first.intersects(range_) && !pred(map_pos_));
329 UpdateCurrent();
330 return *this;
331 }
332
John Zulauf4a6105a2020-11-17 15:11:05 -0700333 const KeyType range_;
John Zulaufae842002021-04-15 18:20:55 -0600334 const RangeMap *map_;
335 typename RangeMap::const_iterator map_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700336 KeyType current_;
337};
John Zulaufd5115702021-01-18 12:34:33 -0700338using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulaufae842002021-04-15 18:20:55 -0600339using EventSimpleRangeGenerator = MapRangesRangeGenerator<SyncEventState::ScopeMap>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700340
John Zulaufae842002021-04-15 18:20:55 -0600341// Generate the ranges for entries meeting the predicate that are the intersection of range and the entries in the RangeMap
342template <typename RangeMap, typename Predicate, typename KeyType = typename RangeMap::key_type>
343class PredicatedMapRangesRangeGenerator : public MapRangesRangeGenerator<RangeMap, KeyType> {
344 public:
345 using Base = MapRangesRangeGenerator<RangeMap, KeyType>;
346 // Default constructed is safe to dereference for "empty" test, but for no other operation.
347 PredicatedMapRangesRangeGenerator() : Base(), pred_() {}
348 PredicatedMapRangesRangeGenerator(const RangeMap &filter, const KeyType &range, Predicate pred)
349 : Base(filter, range), pred_(pred) {}
350 PredicatedMapRangesRangeGenerator(const PredicatedMapRangesRangeGenerator &from) = default;
351
352 PredicatedMapRangesRangeGenerator &operator++() {
353 Base::PredicatedIncrement(pred_);
354 return *this;
355 }
356
357 protected:
358 Predicate pred_;
359};
John Zulauf4a6105a2020-11-17 15:11:05 -0700360
361// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulaufae842002021-04-15 18:20:55 -0600362// Templated to allow for different Range generators or map sources...
363template <typename RangeMap, typename RangeGen, typename KeyType = typename RangeMap::key_type>
John Zulauf4a6105a2020-11-17 15:11:05 -0700364class FilteredGeneratorGenerator {
365 public:
John Zulaufd5115702021-01-18 12:34:33 -0700366 // Default constructed is safe to dereference for "empty" test, but for no other operation.
367 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
368 // Default construction for KeyType *must* be empty range
369 assert(current_.empty());
370 }
John Zulaufae842002021-04-15 18:20:55 -0600371 FilteredGeneratorGenerator(const RangeMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700372 SeekBegin();
373 }
John Zulaufd5115702021-01-18 12:34:33 -0700374 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700375 const KeyType &operator*() const { return current_; }
376 const KeyType *operator->() const { return &current_; }
377 FilteredGeneratorGenerator &operator++() {
378 KeyType gen_range = GenRange();
379 KeyType filter_range = FilterRange();
380 current_ = KeyType();
381 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
382 if (gen_range.end > filter_range.end) {
383 // if the generated range is beyond the filter_range, advance the filter range
384 filter_range = AdvanceFilter();
385 } else {
386 gen_range = AdvanceGen();
387 }
388 current_ = gen_range & filter_range;
389 }
390 return *this;
391 }
392
393 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
394
395 private:
396 KeyType AdvanceFilter() {
397 ++filter_pos_;
398 auto filter_range = FilterRange();
399 if (filter_range.valid()) {
400 FastForwardGen(filter_range);
401 }
402 return filter_range;
403 }
404 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700405 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700406 auto gen_range = GenRange();
407 if (gen_range.valid()) {
408 FastForwardFilter(gen_range);
409 }
410 return gen_range;
411 }
412
413 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700414 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700415
416 KeyType FastForwardFilter(const KeyType &range) {
417 auto filter_range = FilterRange();
418 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700419 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700420 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
421 if (retry_count < kRetryLimit) {
422 ++filter_pos_;
423 filter_range = FilterRange();
424 retry_count++;
425 } else {
426 // Okay we've tried walking, do a seek.
427 filter_pos_ = filter_->lower_bound(range);
428 break;
429 }
430 }
431 return FilterRange();
432 }
433
434 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
435 // faster.
436 KeyType FastForwardGen(const KeyType &range) {
437 auto gen_range = GenRange();
438 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700439 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700440 gen_range = GenRange();
441 }
442 return gen_range;
443 }
444
445 void SeekBegin() {
446 auto gen_range = GenRange();
447 if (gen_range.empty()) {
448 current_ = KeyType();
449 filter_pos_ = filter_->cend();
450 } else {
451 filter_pos_ = filter_->lower_bound(gen_range);
452 current_ = gen_range & FilterRange();
453 }
454 }
455
John Zulaufae842002021-04-15 18:20:55 -0600456 const RangeMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700457 RangeGen gen_;
John Zulaufae842002021-04-15 18:20:55 -0600458 typename RangeMap::const_iterator filter_pos_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700459 KeyType current_;
460};
461
462using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
463
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700464static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700465
John Zulauf3e86bf02020-09-12 10:47:57 -0600466ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
467 VkDeviceSize stride) {
468 VkDeviceSize range_start = offset + first_index * stride;
469 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600470 if (count == UINT32_MAX) {
471 range_size = buf_whole_size - range_start;
472 } else {
473 range_size = count * stride;
474 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600475 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600476}
477
locke-lunarg654e3692020-06-04 17:19:15 -0600478SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
479 VkShaderStageFlagBits stage_flag) {
480 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
481 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
482 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
483 }
484 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
485 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
486 assert(0);
487 }
488 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
489 return stage_access->second.uniform_read;
490 }
491
492 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
493 // Because if write hazard happens, read hazard might or might not happen.
494 // But if write hazard doesn't happen, read hazard is impossible to happen.
495 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700496 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600497 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700498 // TODO: sampled_read
499 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600500}
501
locke-lunarg37047832020-06-12 13:44:45 -0600502bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
503 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
504 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
505 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
506 ? true
507 : false;
508}
509
510bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
511 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
512 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
513 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
514 ? true
515 : false;
516}
517
John Zulauf355e49b2020-04-24 15:11:15 -0600518// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600519template <typename Action>
520static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
521 Action &action) {
522 // At this point the "apply over range" logic only supports a single memory binding
523 if (!SimpleBinding(image_state)) return;
524 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600525 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700526 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
527 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600528 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700529 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600530 }
531}
532
John Zulauf7635de32020-05-29 17:14:15 -0600533// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
534// Used by both validation and record operations
535//
536// The signature for Action() reflect the needs of both uses.
537template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700538void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
539 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600540 const auto &rp_ci = rp_state.createInfo;
541 const auto *attachment_ci = rp_ci.pAttachments;
542 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
543
544 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
545 const auto *color_attachments = subpass_ci.pColorAttachments;
546 const auto *color_resolve = subpass_ci.pResolveAttachments;
547 if (color_resolve && color_attachments) {
548 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
549 const auto &color_attach = color_attachments[i].attachment;
550 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
551 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
552 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700553 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
554 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600555 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700556 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
557 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600558 }
559 }
560 }
561
562 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700563 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600564 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
565 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
566 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
567 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
568 const auto src_ci = attachment_ci[src_at];
569 // The formats are required to match so we can pick either
570 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
571 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
572 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600573
574 // Figure out which aspects are actually touched during resolve operations
575 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700576 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600577 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600578 aspect_string = "depth/stencil";
579 } else if (resolve_depth) {
580 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700581 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600582 aspect_string = "depth";
583 } else if (resolve_stencil) {
584 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700585 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600586 aspect_string = "stencil";
587 }
588
John Zulaufd0ec59f2021-03-13 14:25:08 -0700589 if (aspect_string) {
590 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
591 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
592 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
593 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600594 }
595 }
596}
597
598// Action for validating resolve operations
599class ValidateResolveAction {
600 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700601 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700602 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600603 : render_pass_(render_pass),
604 subpass_(subpass),
605 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700606 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600607 func_name_(func_name),
608 skip_(false) {}
609 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 -0700610 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
611 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600612 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700613 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600614 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700615 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700616 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700617 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
618 " to resolve attachment %" PRIu32 ". Access info %s.",
619 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700620 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600621 }
622 }
623 // Providing a mechanism for the constructing caller to get the result of the validation
624 bool GetSkip() const { return skip_; }
625
626 private:
627 VkRenderPass render_pass_;
628 const uint32_t subpass_;
629 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700630 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600631 const char *func_name_;
632 bool skip_;
633};
634
635// Update action for resolve operations
636class UpdateStateResolveAction {
637 public:
John Zulauf14940722021-04-12 15:19:02 -0600638 UpdateStateResolveAction(AccessContext &context, ResourceUsageTag tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700639 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
640 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600641 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700642 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600643 }
644
645 private:
646 AccessContext &context_;
John Zulauf14940722021-04-12 15:19:02 -0600647 const ResourceUsageTag tag_;
John Zulauf7635de32020-05-29 17:14:15 -0600648};
649
John Zulauf59e25072020-07-17 10:55:21 -0600650void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
John Zulauf14940722021-04-12 15:19:02 -0600651 const SyncStageAccessFlags &prior_, const ResourceUsageTag tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600652 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
653 usage_index = usage_index_;
654 hazard = hazard_;
655 prior_access = prior_;
656 tag = tag_;
657}
658
John Zulauf540266b2020-04-06 18:54:53 -0600659AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
660 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600661 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600662 Reset();
663 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700664 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
665 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600666 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600667 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600668 const auto prev_pass = prev_dep.first->pass;
669 const auto &prev_barriers = prev_dep.second;
670 assert(prev_dep.second.size());
671 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
672 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700673 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600674
675 async_.reserve(subpass_dep.async.size());
676 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700677 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600678 }
John Zulauf22aefed2021-03-11 18:14:35 -0700679 if (has_barrier_from_external) {
680 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
681 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
682 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600683 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600684 if (subpass_dep.barrier_to_external.size()) {
685 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600686 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700687}
688
John Zulauf5f13a792020-03-10 07:31:21 -0600689template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700690HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600691 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600692 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600693 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600694
695 HazardResult hazard;
696 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
697 hazard = detector.Detect(prev);
698 }
699 return hazard;
700}
701
John Zulauf4a6105a2020-11-17 15:11:05 -0700702template <typename Action>
703void AccessContext::ForAll(Action &&action) {
704 for (const auto address_type : kAddressTypes) {
705 auto &accesses = GetAccessStateMap(address_type);
706 for (const auto &access : accesses) {
707 action(address_type, access);
708 }
709 }
710}
711
John Zulauf3d84f1b2020-03-09 13:33:25 -0600712// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
713// the DAG of the contexts (for example subpasses)
714template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700715HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600716 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600717 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600718
John Zulauf1a224292020-06-30 14:52:13 -0600719 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600720 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
721 // so we'll check these first
722 for (const auto &async_context : async_) {
723 hazard = async_context->DetectAsyncHazard(type, detector, range);
724 if (hazard.hazard) return hazard;
725 }
John Zulauf5f13a792020-03-10 07:31:21 -0600726 }
727
John Zulauf1a224292020-06-30 14:52:13 -0600728 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600729
John Zulauf69133422020-05-20 14:55:53 -0600730 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600731 const auto the_end = accesses.cend(); // End is not invalidated
732 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600733 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600734
John Zulauf3cafbf72021-03-26 16:55:19 -0600735 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600736 // Cover any leading gap, or gap between entries
737 if (detect_prev) {
738 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
739 // Cover any leading gap, or gap between entries
740 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600741 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600742 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600743 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600744 if (hazard.hazard) return hazard;
745 }
John Zulauf69133422020-05-20 14:55:53 -0600746 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
747 gap.begin = pos->first.end;
748 }
749
750 hazard = detector.Detect(pos);
751 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600752 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600753 }
754
755 if (detect_prev) {
756 // Detect in the trailing empty as needed
757 gap.end = range.end;
758 if (gap.non_empty()) {
759 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600760 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600761 }
762
763 return hazard;
764}
765
766// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
767template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700768HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
769 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600770 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600771 auto pos = accesses.lower_bound(range);
772 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600773
John Zulauf3d84f1b2020-03-09 13:33:25 -0600774 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600775 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700776 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600777 if (hazard.hazard) break;
778 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600779 }
John Zulauf16adfc92020-04-08 10:28:33 -0600780
John Zulauf3d84f1b2020-03-09 13:33:25 -0600781 return hazard;
782}
783
John Zulaufb02c1eb2020-10-06 16:33:36 -0600784struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700785 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600786 void operator()(ResourceAccessState *access) const {
787 assert(access);
788 access->ApplyBarriers(barriers, true);
789 }
790 const std::vector<SyncBarrier> &barriers;
791};
792
John Zulauf22aefed2021-03-11 18:14:35 -0700793struct ApplyTrackbackStackAction {
794 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
795 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
796 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600797 void operator()(ResourceAccessState *access) const {
798 assert(access);
799 assert(!access->HasPendingState());
800 access->ApplyBarriers(barriers, false);
801 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700802 if (previous_barrier) {
803 assert(bool(*previous_barrier));
804 (*previous_barrier)(access);
805 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600806 }
807 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700808 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600809};
810
811// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
812// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
813// *different* map from dest.
814// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
815// range [first, last)
816template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600817static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
818 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600819 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600820 auto at = entry;
821 for (auto pos = first; pos != last; ++pos) {
822 // Every member of the input iterator range must fit within the remaining portion of entry
823 assert(at->first.includes(pos->first));
824 assert(at != dest->end());
825 // Trim up at to the same size as the entry to resolve
826 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600827 auto access = pos->second; // intentional copy
828 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600829 at->second.Resolve(access);
830 ++at; // Go to the remaining unused section of entry
831 }
832}
833
John Zulaufa0a98292020-09-18 09:30:10 -0600834static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
835 SyncBarrier merged = {};
836 for (const auto &barrier : barriers) {
837 merged.Merge(barrier);
838 }
839 return merged;
840}
841
John Zulaufb02c1eb2020-10-06 16:33:36 -0600842template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700843void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600844 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
845 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600846 if (!range.non_empty()) return;
847
John Zulauf355e49b2020-04-24 15:11:15 -0600848 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
849 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600850 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600851 if (current->pos_B->valid) {
852 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600853 auto access = src_pos->second; // intentional copy
854 barrier_action(&access);
855
John Zulauf16adfc92020-04-08 10:28:33 -0600856 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600857 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
858 trimmed->second.Resolve(access);
859 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600860 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600861 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600862 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600863 }
John Zulauf16adfc92020-04-08 10:28:33 -0600864 } else {
865 // we have to descend to fill this gap
866 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700867 ResourceAccessRange recurrence_range = current_range;
868 // The current context is empty for the current range, so recur to fill the gap.
869 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
870 // is not valid, to minimize that recurrence
871 if (current->pos_B.at_end()) {
872 // Do the remainder here....
873 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600874 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700875 // Recur only over the range until B becomes valid (within the limits of range).
876 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600877 }
John Zulauf22aefed2021-03-11 18:14:35 -0700878 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
879
John Zulauf355e49b2020-04-24 15:11:15 -0600880 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
881 // iterator of the outer while.
882
883 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
884 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
885 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700886 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 -0600887 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600888 current.seek(seek_to);
889 } else if (!current->pos_A->valid && infill_state) {
890 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
891 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
892 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600893 }
John Zulauf5f13a792020-03-10 07:31:21 -0600894 }
John Zulauf16adfc92020-04-08 10:28:33 -0600895 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600896 }
John Zulauf1a224292020-06-30 14:52:13 -0600897
898 // Infill if range goes passed both the current and resolve map prior contents
899 if (recur_to_infill && (current->range.end < range.end)) {
900 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700901 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600902 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600903}
904
John Zulauf22aefed2021-03-11 18:14:35 -0700905template <typename BarrierAction>
906void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
907 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
908 const BarrierAction &previous_barrier) const {
909 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
910 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
911}
912
John Zulauf43cc7462020-12-03 12:33:12 -0700913void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700914 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
915 const ResourceAccessStateFunction *previous_barrier) const {
916 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600917 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700918 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
919 ResourceAccessState state_copy;
920 if (previous_barrier) {
921 assert(bool(*previous_barrier));
922 state_copy = *infill_state;
923 (*previous_barrier)(&state_copy);
924 infill_state = &state_copy;
925 }
926 sparse_container::update_range_value(*descent_map, range, *infill_state,
927 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -0600928 }
929 } else {
930 // Look for something to fill the gap further along.
931 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -0700932 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600933 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600934 }
John Zulauf5f13a792020-03-10 07:31:21 -0600935 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600936}
937
John Zulauf4a6105a2020-11-17 15:11:05 -0700938// Non-lazy import of all accesses, WaitEvents needs this.
939void AccessContext::ResolvePreviousAccesses() {
940 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -0700941 if (!prev_.size()) return; // If no previous contexts, nothing to do
942
John Zulauf4a6105a2020-11-17 15:11:05 -0700943 for (const auto address_type : kAddressTypes) {
944 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
945 }
946}
947
John Zulauf43cc7462020-12-03 12:33:12 -0700948AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
949 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600950}
951
John Zulauf1507ee42020-05-18 11:33:09 -0600952static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -0600953 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
954 ? SYNC_ACCESS_INDEX_NONE
955 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
956 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -0600957 return stage_access;
958}
959static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -0600960 const auto stage_access =
961 (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
962 ? SYNC_ACCESS_INDEX_NONE
963 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
964 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -0600965 return stage_access;
966}
967
John Zulauf7635de32020-05-29 17:14:15 -0600968// Caller must manage returned pointer
969static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700970 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -0600971 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -0700972 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
973 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600974 return proxy;
975}
976
John Zulaufb02c1eb2020-10-06 16:33:36 -0600977template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700978void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
979 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
980 const ResourceAccessState *infill_state) const {
981 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
982 if (!attachment_gen) return;
983
984 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
985 const AccessAddressType address_type = view_gen.GetAddressType();
986 for (; range_gen->non_empty(); ++range_gen) {
987 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600988 }
John Zulauf62f10592020-04-03 12:20:02 -0600989}
990
John Zulauf7635de32020-05-29 17:14:15 -0600991// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -0700992bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600993 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700994 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600995 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600996 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
997 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
998 // those affects have not been recorded yet.
999 //
1000 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
1001 // to apply and only copy then, if this proves a hot spot.
1002 std::unique_ptr<AccessContext> proxy_for_prev;
1003 TrackBack proxy_track_back;
1004
John Zulauf355e49b2020-04-24 15:11:15 -06001005 const auto &transitions = rp_state.subpass_transitions[subpass];
1006 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -06001007 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
1008
1009 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -07001010 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -06001011 if (prev_needs_proxy) {
1012 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001013 proxy_for_prev.reset(
1014 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -06001015 proxy_track_back = *track_back;
1016 proxy_track_back.context = proxy_for_prev.get();
1017 }
1018 track_back = &proxy_track_back;
1019 }
1020 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -06001021 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001022 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001023 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1024 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
1025 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
1026 string_VkImageLayout(transition.old_layout),
1027 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07001028 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06001029 }
1030 }
1031 return skip;
1032}
1033
John Zulauf64ffe552021-02-06 10:25:07 -07001034bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001035 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001036 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001037 bool skip = false;
1038 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001039
John Zulauf1507ee42020-05-18 11:33:09 -06001040 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1041 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001042 const auto &view_gen = attachment_views[i];
1043 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001044 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001045
1046 // Need check in the following way
1047 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1048 // vs. transition
1049 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1050 // for each aspect loaded.
1051
1052 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001053 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001054 const bool is_color = !(has_depth || has_stencil);
1055
1056 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001057 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001058
John Zulaufaff20662020-06-01 14:07:58 -06001059 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001060 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001061
John Zulaufb02c1eb2020-10-06 16:33:36 -06001062 bool checked_stencil = false;
John Zulauf57261402021-08-13 11:32:06 -06001063 if (is_color && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001064 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001065 aspect = "color";
1066 } else {
John Zulauf57261402021-08-13 11:32:06 -06001067 if (has_depth && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001068 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1069 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001070 aspect = "depth";
1071 }
John Zulauf57261402021-08-13 11:32:06 -06001072 if (!hazard.hazard && has_stencil && (stencil_load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001073 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1074 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001075 aspect = "stencil";
1076 checked_stencil = true;
1077 }
1078 }
1079
1080 if (hazard.hazard) {
1081 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001082 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001083 if (hazard.tag == kCurrentCommandTag) {
1084 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001085 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001086 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1087 " aspect %s during load with loadOp %s.",
1088 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1089 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001090 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001091 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001092 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001093 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001094 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001095 }
1096 }
1097 }
1098 }
1099 return skip;
1100}
1101
John Zulaufaff20662020-06-01 14:07:58 -06001102// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1103// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1104// store is part of the same Next/End operation.
1105// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001106bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001107 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001108 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001109 bool skip = false;
1110 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001111
1112 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1113 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001114 const AttachmentViewGen &view_gen = attachment_views[i];
1115 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001116 const auto &ci = attachment_ci[i];
1117
1118 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1119 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1120 // sake, we treat DONT_CARE as writing.
1121 const bool has_depth = FormatHasDepth(ci.format);
1122 const bool has_stencil = FormatHasStencil(ci.format);
1123 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001124 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001125 if (!has_stencil && !store_op_stores) continue;
1126
1127 HazardResult hazard;
1128 const char *aspect = nullptr;
1129 bool checked_stencil = false;
1130 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001131 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1132 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001133 aspect = "color";
1134 } else {
John Zulauf57261402021-08-13 11:32:06 -06001135 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001136 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001137 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1138 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001139 aspect = "depth";
1140 }
1141 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001142 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1143 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001144 aspect = "stencil";
1145 checked_stencil = true;
1146 }
1147 }
1148
1149 if (hazard.hazard) {
1150 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1151 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001152 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001153 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1154 " %s aspect during store with %s %s. Access info %s",
1155 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001156 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001157 }
1158 }
1159 }
1160 return skip;
1161}
1162
John Zulauf64ffe552021-02-06 10:25:07 -07001163bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001164 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1165 const char *func_name, uint32_t subpass) const {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001166 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001167 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001168 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001169}
1170
John Zulauf3d84f1b2020-03-09 13:33:25 -06001171class HazardDetector {
1172 SyncStageAccessIndex usage_index_;
1173
1174 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001175 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf14940722021-04-12 15:19:02 -06001176 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001177 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001178 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001179 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001180};
1181
John Zulauf69133422020-05-20 14:55:53 -06001182class HazardDetectorWithOrdering {
1183 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001184 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001185
1186 public:
1187 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001188 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001189 }
John Zulauf14940722021-04-12 15:19:02 -06001190 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001191 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001192 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001193 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001194};
1195
John Zulauf16adfc92020-04-08 10:28:33 -06001196HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001197 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001198 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001199 const auto base_address = ResourceBaseAddress(buffer);
1200 HazardDetector detector(usage_index);
1201 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001202}
1203
John Zulauf69133422020-05-20 14:55:53 -06001204template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001205HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1206 DetectOptions options) const {
1207 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1208 if (!attachment_gen) return HazardResult();
1209
1210 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1211 const auto address_type = view_gen.GetAddressType();
1212 for (; range_gen->non_empty(); ++range_gen) {
1213 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1214 if (hazard.hazard) return hazard;
1215 }
1216
1217 return HazardResult();
1218}
1219
1220template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001221HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1222 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1223 const VkExtent3D &extent, DetectOptions options) const {
1224 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001225 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001226 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1227 base_address);
1228 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001229 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001230 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001231 if (hazard.hazard) return hazard;
1232 }
1233 return HazardResult();
1234}
John Zulauf110413c2021-03-20 05:38:38 -06001235template <typename Detector>
1236HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1237 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1238 if (!SimpleBinding(image)) return HazardResult();
1239 const auto base_address = ResourceBaseAddress(image);
1240 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1241 const auto address_type = ImageAddressType(image);
1242 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001243 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1244 if (hazard.hazard) return hazard;
1245 }
1246 return HazardResult();
1247}
John Zulauf69133422020-05-20 14:55:53 -06001248
John Zulauf540266b2020-04-06 18:54:53 -06001249HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1250 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1251 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001252 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1253 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001254 HazardDetector detector(current_usage);
1255 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001256}
1257
1258HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001259 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001260 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001261 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001262}
1263
John Zulaufd0ec59f2021-03-13 14:25:08 -07001264HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1265 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1266 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1267 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1268}
1269
John Zulauf69133422020-05-20 14:55:53 -06001270HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001271 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001272 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001273 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001274 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001275}
1276
John Zulauf3d84f1b2020-03-09 13:33:25 -06001277class BarrierHazardDetector {
1278 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001279 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001280 SyncStageAccessFlags src_access_scope)
1281 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1282
John Zulauf5f13a792020-03-10 07:31:21 -06001283 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1284 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001285 }
John Zulauf14940722021-04-12 15:19:02 -06001286 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001287 // 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 -07001288 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001289 }
1290
1291 private:
1292 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001293 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001294 SyncStageAccessFlags src_access_scope_;
1295};
1296
John Zulauf4a6105a2020-11-17 15:11:05 -07001297class EventBarrierHazardDetector {
1298 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001299 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001300 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
John Zulauf14940722021-04-12 15:19:02 -06001301 ResourceUsageTag scope_tag)
John Zulauf4a6105a2020-11-17 15:11:05 -07001302 : usage_index_(usage_index),
1303 src_exec_scope_(src_exec_scope),
1304 src_access_scope_(src_access_scope),
1305 event_scope_(event_scope),
1306 scope_pos_(event_scope.cbegin()),
1307 scope_end_(event_scope.cend()),
1308 scope_tag_(scope_tag) {}
1309
1310 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1311 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1312 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1313 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1314 if (scope_pos_ == scope_end_) return HazardResult();
1315 if (!scope_pos_->first.intersects(pos->first)) {
1316 event_scope_.lower_bound(pos->first);
1317 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1318 }
1319
1320 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1321 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1322 }
John Zulauf14940722021-04-12 15:19:02 -06001323 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07001324 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1325 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1326 }
1327
1328 private:
1329 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001330 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001331 SyncStageAccessFlags src_access_scope_;
1332 const SyncEventState::ScopeMap &event_scope_;
1333 SyncEventState::ScopeMap::const_iterator scope_pos_;
1334 SyncEventState::ScopeMap::const_iterator scope_end_;
John Zulauf14940722021-04-12 15:19:02 -06001335 const ResourceUsageTag scope_tag_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001336};
1337
Jeremy Gebben40a22942020-12-22 14:22:06 -07001338HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001339 const SyncStageAccessFlags &src_access_scope,
1340 const VkImageSubresourceRange &subresource_range,
1341 const SyncEventState &sync_event, DetectOptions options) const {
1342 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1343 // first access scope map to use, and there's no easy way to plumb it in below.
1344 const auto address_type = ImageAddressType(image);
1345 const auto &event_scope = sync_event.FirstScope(address_type);
1346
1347 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1348 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001349 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001350}
1351
John Zulaufd0ec59f2021-03-13 14:25:08 -07001352HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1353 DetectOptions options) const {
1354 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1355 barrier.src_access_scope);
1356 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1357}
1358
Jeremy Gebben40a22942020-12-22 14:22:06 -07001359HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001360 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001361 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001362 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001363 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001364 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001365}
1366
Jeremy Gebben40a22942020-12-22 14:22:06 -07001367HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001368 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001369 const VkImageMemoryBarrier &barrier) const {
1370 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1371 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1372 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1373}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001374HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001375 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001376 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001377}
John Zulauf355e49b2020-04-24 15:11:15 -06001378
John Zulauf9cb530d2019-09-30 14:14:10 -06001379template <typename Flags, typename Map>
1380SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1381 SyncStageAccessFlags scope = 0;
1382 for (const auto &bit_scope : map) {
1383 if (flag_mask < bit_scope.first) break;
1384
1385 if (flag_mask & bit_scope.first) {
1386 scope |= bit_scope.second;
1387 }
1388 }
1389 return scope;
1390}
1391
Jeremy Gebben40a22942020-12-22 14:22:06 -07001392SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001393 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1394}
1395
Jeremy Gebben40a22942020-12-22 14:22:06 -07001396SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1397 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001398}
1399
Jeremy Gebben40a22942020-12-22 14:22:06 -07001400// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1401SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001402 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1403 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1404 // 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 -06001405 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1406}
1407
1408template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001409void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001410 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1411 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001412 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001413 auto pos = accesses->lower_bound(range);
1414 if (pos == accesses->end() || !pos->first.intersects(range)) {
1415 // The range is empty, fill it with a default value.
1416 pos = action.Infill(accesses, pos, range);
1417 } else if (range.begin < pos->first.begin) {
1418 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001419 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001420 } else if (pos->first.begin < range.begin) {
1421 // Trim the beginning if needed
1422 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1423 ++pos;
1424 }
1425
1426 const auto the_end = accesses->end();
1427 while ((pos != the_end) && pos->first.intersects(range)) {
1428 if (pos->first.end > range.end) {
1429 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1430 }
1431
1432 pos = action(accesses, pos);
1433 if (pos == the_end) break;
1434
1435 auto next = pos;
1436 ++next;
1437 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1438 // Need to infill if next is disjoint
1439 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001440 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001441 next = action.Infill(accesses, next, new_range);
1442 }
1443 pos = next;
1444 }
1445}
John Zulaufd5115702021-01-18 12:34:33 -07001446
1447// Give a comparable interface for range generators and ranges
1448template <typename Action>
1449inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1450 assert(range);
1451 UpdateMemoryAccessState(accesses, *range, action);
1452}
1453
John Zulauf4a6105a2020-11-17 15:11:05 -07001454template <typename Action, typename RangeGen>
1455void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1456 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001457 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 -07001458 for (; range_gen->non_empty(); ++range_gen) {
1459 UpdateMemoryAccessState(accesses, *range_gen, action);
1460 }
1461}
John Zulauf9cb530d2019-09-30 14:14:10 -06001462
John Zulaufd0ec59f2021-03-13 14:25:08 -07001463template <typename Action, typename RangeGen>
1464void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1465 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1466 for (; range_gen->non_empty(); ++range_gen) {
1467 UpdateMemoryAccessState(accesses, *range_gen, action);
1468 }
1469}
John Zulauf9cb530d2019-09-30 14:14:10 -06001470struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001471 using Iterator = ResourceAccessRangeMap::iterator;
1472 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001473 // this is only called on gaps, and never returns a gap.
1474 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001475 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001476 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001477 }
John Zulauf5f13a792020-03-10 07:31:21 -06001478
John Zulauf5c5e88d2019-12-26 11:22:02 -07001479 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001480 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001481 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001482 return pos;
1483 }
1484
John Zulauf43cc7462020-12-03 12:33:12 -07001485 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf14940722021-04-12 15:19:02 -06001486 SyncOrdering ordering_rule_, ResourceUsageTag tag_)
John Zulauf8e3c3e92021-01-06 11:19:36 -07001487 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001488 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001489 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001490 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001491 const SyncOrdering ordering_rule;
John Zulauf14940722021-04-12 15:19:02 -06001492 const ResourceUsageTag tag;
John Zulauf9cb530d2019-09-30 14:14:10 -06001493};
1494
John Zulauf4a6105a2020-11-17 15:11:05 -07001495// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001496struct PipelineBarrierOp {
1497 SyncBarrier barrier;
1498 bool layout_transition;
1499 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1500 : barrier(barrier_), layout_transition(layout_transition_) {}
1501 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001502 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001503 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1504};
John Zulauf4a6105a2020-11-17 15:11:05 -07001505// The barrier operation for wait events
1506struct WaitEventBarrierOp {
John Zulauf14940722021-04-12 15:19:02 -06001507 ResourceUsageTag scope_tag;
John Zulauf4a6105a2020-11-17 15:11:05 -07001508 SyncBarrier barrier;
1509 bool layout_transition;
John Zulauf14940722021-04-12 15:19:02 -06001510 WaitEventBarrierOp(const ResourceUsageTag scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1511 : scope_tag(scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
John Zulauf4a6105a2020-11-17 15:11:05 -07001512 WaitEventBarrierOp() = default;
John Zulauf14940722021-04-12 15:19:02 -06001513 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(scope_tag, barrier, layout_transition); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001514};
John Zulauf1e331ec2020-12-04 18:29:38 -07001515
John Zulauf4a6105a2020-11-17 15:11:05 -07001516// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1517// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1518// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001519template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001520class ApplyBarrierOpsFunctor {
1521 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001522 using Iterator = ResourceAccessRangeMap::iterator;
1523 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001524
John Zulauf5c5e88d2019-12-26 11:22:02 -07001525 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001526 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001527 for (const auto &op : barrier_ops_) {
1528 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001529 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001530
John Zulauf89311b42020-09-29 16:28:47 -06001531 if (resolve_) {
1532 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1533 // another walk
1534 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001535 }
1536 return pos;
1537 }
1538
John Zulauf89311b42020-09-29 16:28:47 -06001539 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf14940722021-04-12 15:19:02 -06001540 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, ResourceUsageTag tag) : resolve_(resolve), barrier_ops_(), tag_(tag) {
John Zulaufd5115702021-01-18 12:34:33 -07001541 barrier_ops_.reserve(size_hint);
1542 }
1543 void EmplaceBack(const BarrierOp &op) { barrier_ops_.emplace_back(op); }
John Zulauf89311b42020-09-29 16:28:47 -06001544
1545 private:
1546 bool resolve_;
John Zulaufd5115702021-01-18 12:34:33 -07001547 std::vector<BarrierOp> barrier_ops_;
John Zulauf14940722021-04-12 15:19:02 -06001548 const ResourceUsageTag tag_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001549};
1550
John Zulauf4a6105a2020-11-17 15:11:05 -07001551// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1552// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1553template <typename BarrierOp>
1554class ApplyBarrierFunctor {
1555 public:
1556 using Iterator = ResourceAccessRangeMap::iterator;
1557 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1558
1559 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1560 auto &access_state = pos->second;
1561 barrier_op_(&access_state);
1562 return pos;
1563 }
1564
1565 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1566
1567 private:
John Zulaufd5115702021-01-18 12:34:33 -07001568 BarrierOp barrier_op_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001569};
1570
John Zulauf1e331ec2020-12-04 18:29:38 -07001571// This functor resolves the pendinging state.
1572class ResolvePendingBarrierFunctor {
1573 public:
1574 using Iterator = ResourceAccessRangeMap::iterator;
1575 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1576
1577 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1578 auto &access_state = pos->second;
1579 access_state.ApplyPendingBarriers(tag_);
1580 return pos;
1581 }
1582
John Zulauf14940722021-04-12 15:19:02 -06001583 ResolvePendingBarrierFunctor(ResourceUsageTag tag) : tag_(tag) {}
John Zulauf1e331ec2020-12-04 18:29:38 -07001584
1585 private:
John Zulauf14940722021-04-12 15:19:02 -06001586 const ResourceUsageTag tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001587};
1588
John Zulauf8e3c3e92021-01-06 11:19:36 -07001589void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001590 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001591 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001592 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001593}
1594
John Zulauf8e3c3e92021-01-06 11:19:36 -07001595void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001596 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001597 if (!SimpleBinding(buffer)) return;
1598 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001599 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001600}
John Zulauf355e49b2020-04-24 15:11:15 -06001601
John Zulauf8e3c3e92021-01-06 11:19:36 -07001602void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001603 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1604 if (!SimpleBinding(image)) return;
1605 const auto base_address = ResourceBaseAddress(image);
1606 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1607 const auto address_type = ImageAddressType(image);
1608 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1609 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1610}
1611void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001612 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001613 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001614 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001615 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001616 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1617 base_address);
1618 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001619 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001620 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001621}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001622
1623void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
John Zulauf14940722021-04-12 15:19:02 -06001624 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001625 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1626 if (!gen) return;
1627 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1628 const auto address_type = view_gen.GetAddressType();
1629 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1630 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001631}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001632
John Zulauf8e3c3e92021-01-06 11:19:36 -07001633void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001634 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001635 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001636 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1637 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001638 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001639}
1640
John Zulaufd0ec59f2021-03-13 14:25:08 -07001641template <typename Action, typename RangeGen>
1642void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1643 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1644 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001645}
1646
1647template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001648void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1649 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1650 if (!gen) return;
1651 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001652}
1653
John Zulaufd0ec59f2021-03-13 14:25:08 -07001654void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1655 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001656 const ResourceUsageTag tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001657 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001658 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001659}
1660
John Zulaufd0ec59f2021-03-13 14:25:08 -07001661void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06001662 uint32_t subpass, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001663 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001664
1665 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1666 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001667 const auto &view_gen = attachment_views[i];
1668 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001669
1670 const auto &ci = attachment_ci[i];
1671 const bool has_depth = FormatHasDepth(ci.format);
1672 const bool has_stencil = FormatHasStencil(ci.format);
1673 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001674 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001675
1676 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001677 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1678 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001679 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001680 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001681 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1682 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001683 }
John Zulauf57261402021-08-13 11:32:06 -06001684 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001685 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001686 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1687 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001688 }
1689 }
1690 }
1691 }
1692}
1693
John Zulauf540266b2020-04-06 18:54:53 -06001694template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001695void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001696 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001697 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001698 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001699 }
1700}
1701
1702void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001703 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1704 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001705 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001706 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001707 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001708 }
1709 }
1710}
1711
John Zulauf355e49b2020-04-24 15:11:15 -06001712// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001713HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1714 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001715
John Zulauf355e49b2020-04-24 15:11:15 -06001716 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001717 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001718
1719 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001720 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1721 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001722 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001723 if (!hazard.hazard) {
1724 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001725 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001726 }
John Zulaufa0a98292020-09-18 09:30:10 -06001727
John Zulauf355e49b2020-04-24 15:11:15 -06001728 return hazard;
1729}
1730
John Zulaufb02c1eb2020-10-06 16:33:36 -06001731void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001732 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001733 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001734 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001735 for (const auto &transition : transitions) {
1736 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001737 const auto &view_gen = attachment_views[transition.attachment];
1738 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001739
1740 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1741 assert(trackback);
1742
1743 // Import the attachments into the current context
1744 const auto *prev_context = trackback->context;
1745 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001746 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001747 auto &target_map = GetAccessStateMap(address_type);
1748 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001749 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1750 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001751 }
1752
John Zulauf86356ca2020-10-19 11:46:41 -06001753 // If there were no transitions skip this global map walk
1754 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001755 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001756 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001757 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001758}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001759
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001760void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulauf669dfd52021-01-27 17:15:28 -07001761 auto *events_context = GetCurrentEventsContext();
1762 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06001763 events_context->ApplyBarrier(src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001764}
1765
locke-lunarg61870c22020-06-09 14:51:50 -06001766bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1767 const char *func_name) const {
1768 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001769 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001770 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001771 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001772 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001773 return skip;
1774 }
1775
1776 using DescriptorClass = cvdescriptorset::DescriptorClass;
1777 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1778 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1779 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1780 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1781
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001782 for (const auto &stage_state : pipe->stage_state) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001783 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1784 pipe->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001785 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001786 }
locke-lunarg61870c22020-06-09 14:51:50 -06001787 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001788 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set;
locke-lunarg61870c22020-06-09 14:51:50 -06001789 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001790 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001791 const auto descriptor_type = binding_it.GetType();
1792 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1793 auto array_idx = 0;
1794
1795 if (binding_it.IsVariableDescriptorCount()) {
1796 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1797 }
1798 SyncStageAccessIndex sync_index =
1799 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1800
1801 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1802 uint32_t index = i - index_range.start;
1803 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1804 switch (descriptor->GetClass()) {
1805 case DescriptorClass::ImageSampler:
1806 case DescriptorClass::Image: {
1807 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001808 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001809 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001810 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1811 img_view_state = image_sampler_descriptor->GetImageViewState();
1812 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001813 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001814 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1815 img_view_state = image_descriptor->GetImageViewState();
1816 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001817 }
1818 if (!img_view_state) continue;
John Zulauf361fb532020-07-22 10:45:39 -06001819 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001820 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1821 // Descriptors, so we do not have to worry about depth slicing here.
1822 // See: VUID 00343
1823 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001824 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001825 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001826
1827 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1828 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1829 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001830 // Input attachments are subject to raster ordering rules
1831 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001832 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001833 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001834 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001835 }
John Zulauf110413c2021-03-20 05:38:38 -06001836
John Zulauf33fc1d52020-07-17 11:01:10 -06001837 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001838 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001839 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001840 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1841 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001842 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001843 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1844 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1845 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001846 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1847 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001848 set_binding.first.binding, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001849 }
1850 break;
1851 }
1852 case DescriptorClass::TexelBuffer: {
1853 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1854 if (!buf_view_state) continue;
1855 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001856 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001857 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001858 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001859 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001860 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001861 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1862 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001863 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1864 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1865 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001866 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001867 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001868 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001869 }
1870 break;
1871 }
1872 case DescriptorClass::GeneralBuffer: {
1873 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1874 auto buf_state = buffer_descriptor->GetBufferState();
1875 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001876 const ResourceAccessRange range =
1877 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001878 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001879 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001880 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001881 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001882 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1883 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001884 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1885 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1886 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001887 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001888 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001889 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001890 }
1891 break;
1892 }
1893 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1894 default:
1895 break;
1896 }
1897 }
1898 }
1899 }
1900 return skip;
1901}
1902
1903void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
John Zulauf14940722021-04-12 15:19:02 -06001904 const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001905 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001906 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001907 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001908 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001909 return;
1910 }
1911
1912 using DescriptorClass = cvdescriptorset::DescriptorClass;
1913 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1914 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1915 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1916 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1917
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001918 for (const auto &stage_state : pipe->stage_state) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001919 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1920 pipe->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001921 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001922 }
locke-lunarg61870c22020-06-09 14:51:50 -06001923 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001924 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set;
locke-lunarg61870c22020-06-09 14:51:50 -06001925 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001926 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001927 const auto descriptor_type = binding_it.GetType();
1928 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1929 auto array_idx = 0;
1930
1931 if (binding_it.IsVariableDescriptorCount()) {
1932 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1933 }
1934 SyncStageAccessIndex sync_index =
1935 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1936
1937 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1938 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1939 switch (descriptor->GetClass()) {
1940 case DescriptorClass::ImageSampler:
1941 case DescriptorClass::Image: {
1942 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1943 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1944 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1945 } else {
1946 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1947 }
1948 if (!img_view_state) continue;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001949 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1950 // Descriptors, so we do not have to worry about depth slicing here.
1951 // See: VUID 00343
1952 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06001953 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001954 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06001955 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1956 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1957 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
1958 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001959 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001960 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
1961 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001962 }
locke-lunarg61870c22020-06-09 14:51:50 -06001963 break;
1964 }
1965 case DescriptorClass::TexelBuffer: {
1966 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1967 if (!buf_view_state) continue;
1968 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001969 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001970 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001971 break;
1972 }
1973 case DescriptorClass::GeneralBuffer: {
1974 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1975 auto buf_state = buffer_descriptor->GetBufferState();
1976 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001977 const ResourceAccessRange range =
1978 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07001979 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001980 break;
1981 }
1982 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1983 default:
1984 break;
1985 }
1986 }
1987 }
1988 }
1989}
1990
1991bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1992 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001993 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001994 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001995 return skip;
1996 }
1997
1998 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1999 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002000 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002001
2002 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002003 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002004 if (binding_description.binding < binding_buffers_size) {
2005 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002006 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002007
locke-lunarg1ae57d62020-11-18 10:49:19 -07002008 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002009 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2010 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002011 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002012 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002013 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002014 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
2015 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2016 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002017 }
2018 }
2019 }
2020 return skip;
2021}
2022
John Zulauf14940722021-04-12 15:19:02 -06002023void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002024 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002025 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002026 return;
2027 }
2028 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2029 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002030 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002031
2032 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002033 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002034 if (binding_description.binding < binding_buffers_size) {
2035 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002036 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002037
locke-lunarg1ae57d62020-11-18 10:49:19 -07002038 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002039 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2040 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002041 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2042 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002043 }
2044 }
2045}
2046
2047bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2048 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002049 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002050 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002051 }
locke-lunarg61870c22020-06-09 14:51:50 -06002052
locke-lunarg1ae57d62020-11-18 10:49:19 -07002053 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002054 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002055 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2056 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002057 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002058 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002059 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002060 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2061 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2062 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002063 }
2064
2065 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2066 // We will detect more accurate range in the future.
2067 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2068 return skip;
2069}
2070
John Zulauf14940722021-04-12 15:19:02 -06002071void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002072 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 -06002073
locke-lunarg1ae57d62020-11-18 10:49:19 -07002074 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002075 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002076 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2077 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002078 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002079
2080 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2081 // We will detect more accurate range in the future.
2082 RecordDrawVertex(UINT32_MAX, 0, tag);
2083}
2084
2085bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002086 bool skip = false;
2087 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002088 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002089 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002090}
2091
John Zulauf14940722021-04-12 15:19:02 -06002092void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002093 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002094 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002095 }
locke-lunarg61870c22020-06-09 14:51:50 -06002096}
2097
John Zulauf64ffe552021-02-06 10:25:07 -07002098void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2099 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06002100 const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002101 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002102 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002103 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002104 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002105 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002106}
2107
John Zulauf8eda1562021-04-13 17:06:41 -06002108void CommandBufferAccessContext::RecordNextSubpass(ResourceUsageTag prev_tag, ResourceUsageTag next_tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002109 assert(current_renderpass_context_);
John Zulauf64ffe552021-02-06 10:25:07 -07002110 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002111 current_context_ = &current_renderpass_context_->CurrentContext();
2112}
2113
John Zulauf8eda1562021-04-13 17:06:41 -06002114void CommandBufferAccessContext::RecordEndRenderPass(const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002115 assert(current_renderpass_context_);
2116 if (!current_renderpass_context_) return;
2117
John Zulauf8eda1562021-04-13 17:06:41 -06002118 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002119 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002120 current_renderpass_context_ = nullptr;
2121}
2122
John Zulauf4a6105a2020-11-17 15:11:05 -07002123void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2124 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002125 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2126 if (event_state) {
2127 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002128 }
2129}
2130
John Zulaufae842002021-04-15 18:20:55 -06002131// The is the recorded cb context
2132bool CommandBufferAccessContext::ValidateFirstUse(const CommandBufferAccessContext &active_context, AccessContext *access_context,
2133 SyncEventsContext *events_context, const char *func_name, uint32_t index) const {
2134 bool skip = false;
2135 ResourceUsageRange tag_range = {0, 0};
2136 const AccessContext *recorded_context = GetCurrentAccessContext();
2137 assert(recorded_context);
2138 HazardResult hazard;
2139 auto log_msg = [this](const HazardResult &hazard, const CommandBufferAccessContext active_context, const char *func_name,
2140 uint32_t index) {
2141 const auto cb_handle = active_context.cb_state_->commandBuffer();
2142 const auto recorded_handle = cb_state_->commandBuffer();
2143 return sync_state_->LogError(cb_handle, string_SyncHazardVUID(hazard.hazard),
2144 "%s: Hazard %s for entry %" PRIu32 ", %s. Access info %s.", func_name,
2145 string_SyncHazard(hazard.hazard), index,
2146 sync_state_->report_data->FormatHandle(recorded_handle).c_str(), FormatUsage(hazard));
2147 };
2148 for (const auto &sync_op : sync_ops_) {
2149 tag_range.end = sync_op.tag;
2150 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2151 if (hazard.hazard) {
2152 skip |= log_msg(hazard, active_context, func_name, index);
2153 }
2154 // NOTE: Add call to replay validate here when we add support for syncop with non-trivial replay
2155 }
2156
2157 // and anything after the last syncop
2158 tag_range.begin = tag_range.end;
2159 tag_range.end = ResourceUsageRecord::kMaxIndex;
2160 hazard = recorded_context->DetectFirstUseHazard(tag_range, *access_context);
2161 if (hazard.hazard) {
2162 skip |= log_msg(hazard, active_context, func_name, index);
2163 }
2164
2165 return skip;
2166}
2167
2168class HazardDetectFirstUse {
2169 public:
2170 HazardDetectFirstUse(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range)
2171 : recorded_use_(recorded_use), tag_range_(tag_range) {}
2172 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
2173 return pos->second.DetectHazard(recorded_use_, tag_range_);
2174 }
2175 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
2176 return pos->second.DetectAsyncHazard(recorded_use_, tag_range_, start_tag);
2177 }
2178
2179 private:
2180 const ResourceAccessState &recorded_use_;
2181 const ResourceUsageRange &tag_range_;
2182};
2183
2184// This is called with the *recorded* command buffers access context, with the *active* access context pass in, againsts which
2185// hazards will be detected
2186HazardResult AccessContext::DetectFirstUseHazard(const ResourceUsageRange &tag_range, const AccessContext &access_context) const {
2187 HazardResult hazard;
2188 for (const auto address_type : kAddressTypes) {
2189 const auto &recorded_access_map = GetAccessStateMap(address_type);
2190 for (const auto &recorded_access : recorded_access_map) {
2191 // Cull any entries not in the current tag range
2192 if (!recorded_access.second.FirstAccessInTagRange(tag_range)) continue;
2193 HazardDetectFirstUse detector(recorded_access.second, tag_range);
2194 hazard = access_context.DetectHazard(address_type, detector, recorded_access.first, DetectOptions::kDetectAll);
2195 if (hazard.hazard) break;
2196 }
2197 }
2198
2199 return hazard;
2200}
2201
John Zulauf64ffe552021-02-06 10:25:07 -07002202bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002203 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002204 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002205 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002206 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002207 if (!pipe) {
2208 return skip;
2209 }
2210
2211 const auto &create_info = pipe->create_info.graphics;
2212 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002213 return skip;
2214 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002215 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002216 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002217
John Zulauf1a224292020-06-30 14:52:13 -06002218 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002219 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002220 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2221 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002222 if (location >= subpass.colorAttachmentCount ||
2223 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002224 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002225 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002226 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2227 if (!view_gen.IsValid()) continue;
2228 HazardResult hazard =
2229 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2230 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002231 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002232 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002233 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002234 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002235 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002236 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002237 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002238 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002239 }
2240 }
2241 }
locke-lunarg37047832020-06-12 13:44:45 -06002242
2243 // PHASE1 TODO: Add layout based read/vs. write selection.
2244 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002245 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002246 GetSubpassDepthStencilAttachmentIndex(pipe->create_info.graphics.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002247
2248 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2249 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2250 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002251 bool depth_write = false, stencil_write = false;
2252
2253 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002254 if (!FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2255 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002256 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2257 depth_write = true;
2258 }
2259 // PHASE1 TODO: It needs to check if stencil is writable.
2260 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2261 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2262 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002263 if (!FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002264 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2265 stencil_write = true;
2266 }
2267
2268 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2269 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002270 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2271 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2272 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002273 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002274 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002275 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002276 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002277 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002278 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2279 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002280 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002281 }
2282 }
2283 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002284 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2285 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2286 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002287 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002288 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002289 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002290 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002291 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002292 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2293 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002294 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002295 }
locke-lunarg61870c22020-06-09 14:51:50 -06002296 }
2297 }
2298 return skip;
2299}
2300
John Zulauf14940722021-04-12 15:19:02 -06002301void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002302 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002303 if (!pipe) {
2304 return;
2305 }
2306
2307 const auto &create_info = pipe->create_info.graphics;
2308 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002309 return;
2310 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002311 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002312 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002313
John Zulauf1a224292020-06-30 14:52:13 -06002314 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002315 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002316 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2317 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002318 if (location >= subpass.colorAttachmentCount ||
2319 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002320 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002321 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002322 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2323 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2324 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2325 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002326 }
2327 }
locke-lunarg37047832020-06-12 13:44:45 -06002328
2329 // PHASE1 TODO: Add layout based read/vs. write selection.
2330 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002331 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002332 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002333 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2334 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2335 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002336 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002337 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2338 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002339
2340 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002341 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2342 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002343 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2344 depth_write = true;
2345 }
2346 // PHASE1 TODO: It needs to check if stencil is writable.
2347 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2348 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2349 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002350 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002351 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2352 stencil_write = true;
2353 }
2354
John Zulaufd0ec59f2021-03-13 14:25:08 -07002355 if (depth_write || stencil_write) {
2356 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2357 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2358 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2359 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002360 }
locke-lunarg61870c22020-06-09 14:51:50 -06002361 }
2362}
2363
John Zulauf64ffe552021-02-06 10:25:07 -07002364bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002365 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002366 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002367 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002368 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002369 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002370 func_name);
2371
John Zulauf355e49b2020-04-24 15:11:15 -06002372 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002373 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002374 skip |=
2375 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002376 if (!skip) {
2377 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2378 // on a copy of the (empty) next context.
2379 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2380 AccessContext temp_context(next_context);
2381 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002382 skip |=
2383 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002384 }
John Zulauf7635de32020-05-29 17:14:15 -06002385 return skip;
2386}
John Zulauf64ffe552021-02-06 10:25:07 -07002387bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002388 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002389 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002390 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002391 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002392 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2393
2394 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002395 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002396 return skip;
2397}
2398
John Zulauf64ffe552021-02-06 10:25:07 -07002399AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002400 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002401}
2402
John Zulauf64ffe552021-02-06 10:25:07 -07002403bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2404 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002405 bool skip = false;
2406
John Zulauf7635de32020-05-29 17:14:15 -06002407 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2408 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2409 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2410 // to apply and only copy then, if this proves a hot spot.
2411 std::unique_ptr<AccessContext> proxy_for_current;
2412
John Zulauf355e49b2020-04-24 15:11:15 -06002413 // Validate the "finalLayout" transitions to external
2414 // Get them from where there we're hidding in the extra entry.
2415 const auto &final_transitions = rp_state_->subpass_transitions.back();
2416 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002417 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002418 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2419 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002420 auto *context = trackback.context;
2421
2422 if (transition.prev_pass == current_subpass_) {
2423 if (!proxy_for_current) {
2424 // 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 -07002425 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002426 }
2427 context = proxy_for_current.get();
2428 }
2429
John Zulaufa0a98292020-09-18 09:30:10 -06002430 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2431 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002432 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002433 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002434 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002435 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002436 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2437 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2438 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2439 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002440 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002441 }
2442 }
2443 return skip;
2444}
2445
John Zulauf14940722021-04-12 15:19:02 -06002446void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002447 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002448 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002449}
2450
John Zulauf14940722021-04-12 15:19:02 -06002451void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002452 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2453 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002454
2455 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2456 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002457 const AttachmentViewGen &view_gen = attachment_views_[i];
2458 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002459
2460 const auto &ci = attachment_ci[i];
2461 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002462 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002463 const bool is_color = !(has_depth || has_stencil);
2464
2465 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002466 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2467 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2468 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2469 SyncOrdering::kColorAttachment, tag);
2470 }
John Zulauf1507ee42020-05-18 11:33:09 -06002471 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002472 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002473 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2474 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2475 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2476 SyncOrdering::kDepthStencilAttachment, tag);
2477 }
John Zulauf1507ee42020-05-18 11:33:09 -06002478 }
2479 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002480 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2481 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2482 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2483 SyncOrdering::kDepthStencilAttachment, tag);
2484 }
John Zulauf1507ee42020-05-18 11:33:09 -06002485 }
2486 }
2487 }
2488 }
2489}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002490AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2491 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2492 AttachmentViewGenVector view_gens;
2493 VkExtent3D extent = CastTo3D(render_area.extent);
2494 VkOffset3D offset = CastTo3D(render_area.offset);
2495 view_gens.reserve(attachment_views.size());
2496 for (const auto *view : attachment_views) {
2497 view_gens.emplace_back(view, offset, extent);
2498 }
2499 return view_gens;
2500}
John Zulauf64ffe552021-02-06 10:25:07 -07002501RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2502 VkQueueFlags queue_flags,
2503 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2504 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002505 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002506 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002507 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002508 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002509 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002510 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002511 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002512}
John Zulauf14940722021-04-12 15:19:02 -06002513void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002514 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002515 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002516 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002517 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002518}
John Zulauf1507ee42020-05-18 11:33:09 -06002519
John Zulauf14940722021-04-12 15:19:02 -06002520void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag prev_subpass_tag, const ResourceUsageTag next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002521 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002522 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2523 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002524
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002525 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2526 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002527 current_subpass_++;
2528 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002529 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2530 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002531 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002532}
2533
John Zulauf14940722021-04-12 15:19:02 -06002534void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002535 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002536 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2537 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002538
John Zulauf355e49b2020-04-24 15:11:15 -06002539 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002540 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002541
2542 // Add the "finalLayout" transitions to external
2543 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002544 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2545 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2546 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002547 const auto &final_transitions = rp_state_->subpass_transitions.back();
2548 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002549 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002550 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002551 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002552 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002553 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002554 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002555 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002556 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002557 }
2558}
2559
Jeremy Gebben40a22942020-12-22 14:22:06 -07002560SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002561 SyncExecScope result;
2562 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002563 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2564 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002565 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2566 return result;
2567}
2568
Jeremy Gebben40a22942020-12-22 14:22:06 -07002569SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002570 SyncExecScope result;
2571 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002572 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2573 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002574 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2575 return result;
2576}
2577
2578SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002579 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002580 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002581 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002582 dst_access_scope = 0;
2583}
2584
2585template <typename Barrier>
2586SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002587 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002588 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002589 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002590 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2591}
2592
2593SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002594 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2595 if (barrier) {
2596 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002597 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002598 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002599
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002600 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002601 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002602 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2603
2604 } else {
2605 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002606 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002607 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2608
2609 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002610 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002611 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2612 }
2613}
2614
2615template <typename Barrier>
2616SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2617 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2618 src_exec_scope = src.exec_scope;
2619 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2620
2621 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002622 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002623 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002624}
2625
John Zulaufb02c1eb2020-10-06 16:33:36 -06002626// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2627void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2628 for (const auto &barrier : barriers) {
2629 ApplyBarrier(barrier, layout_transition);
2630 }
2631}
2632
John Zulauf89311b42020-09-29 16:28:47 -06002633// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2634// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2635// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002636void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002637 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002638 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002639 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002640 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002641 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002642 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002643 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002644}
John Zulauf9cb530d2019-09-30 14:14:10 -06002645HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2646 HazardResult hazard;
2647 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002648 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002649 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002650 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002651 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002652 }
2653 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002654 // Write operation:
2655 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2656 // If reads exists -- test only against them because either:
2657 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2658 // * 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
2659 // the current write happens after the reads, so just test the write against the reades
2660 // Otherwise test against last_write
2661 //
2662 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002663 if (last_reads.size()) {
2664 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002665 if (IsReadHazard(usage_stage, read_access)) {
2666 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2667 break;
2668 }
2669 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002670 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002671 // Write-After-Write check -- if we have a previous write to test against
2672 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002673 }
2674 }
2675 return hazard;
2676}
2677
John Zulauf8e3c3e92021-01-06 11:19:36 -07002678HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2679 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002680 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2681 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002682 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002683 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002684 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2685 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002686 if (IsRead(usage_bit)) {
2687 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2688 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2689 if (is_raw_hazard) {
2690 // NOTE: we know last_write is non-zero
2691 // See if the ordering rules save us from the simple RAW check above
2692 // First check to see if the current usage is covered by the ordering rules
2693 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2694 const bool usage_is_ordered =
2695 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2696 if (usage_is_ordered) {
2697 // Now see of the most recent write (or a subsequent read) are ordered
2698 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2699 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002700 }
2701 }
John Zulauf4285ee92020-09-23 10:20:52 -06002702 if (is_raw_hazard) {
2703 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2704 }
John Zulauf361fb532020-07-22 10:45:39 -06002705 } else {
2706 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002707 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002708 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002709 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002710 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002711 if (usage_write_is_ordered) {
2712 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2713 ordered_stages = GetOrderedStages(ordering);
2714 }
2715 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2716 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002717 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002718 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2719 if (IsReadHazard(usage_stage, read_access)) {
2720 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2721 break;
2722 }
John Zulaufd14743a2020-07-03 09:42:39 -06002723 }
2724 }
John Zulauf4285ee92020-09-23 10:20:52 -06002725 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002726 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002727 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002728 }
John Zulauf69133422020-05-20 14:55:53 -06002729 }
2730 }
2731 return hazard;
2732}
2733
John Zulaufae842002021-04-15 18:20:55 -06002734HazardResult ResourceAccessState::DetectHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range) const {
2735 HazardResult hazard;
2736 for (const auto &first : first_accesses_) {
2737 // Skip and quit logic
2738 if (first.tag < tag_range.begin) continue;
2739 if (first.tag >= tag_range.end) break;
2740 ;
2741
2742 hazard = DetectHazard(first.usage_index, first.ordering_rule);
2743 if (hazard.hazard) break;
2744 }
2745 return hazard;
2746}
2747
John Zulauf2f952d22020-02-10 11:34:51 -07002748// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002749HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002750 HazardResult hazard;
2751 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002752 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2753 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2754 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002755 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002756 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002757 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002758 }
2759 } else {
John Zulauf14940722021-04-12 15:19:02 -06002760 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002761 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002762 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002763 // 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 -07002764 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002765 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002766 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002767 break;
2768 }
2769 }
John Zulauf2f952d22020-02-10 11:34:51 -07002770 }
2771 }
2772 return hazard;
2773}
2774
John Zulaufae842002021-04-15 18:20:55 -06002775HazardResult ResourceAccessState::DetectAsyncHazard(const ResourceAccessState &recorded_use, const ResourceUsageRange &tag_range,
2776 ResourceUsageTag start_tag) const {
2777 HazardResult hazard;
2778 for (const auto &first : first_accesses_) {
2779 // Skip and quit logic
2780 if (first.tag < tag_range.begin) continue;
2781 if (first.tag >= tag_range.end) break;
2782 ;
2783
2784 hazard = DetectAsyncHazard(first.usage_index, start_tag);
2785 if (hazard.hazard) break;
2786 }
2787 return hazard;
2788}
2789
Jeremy Gebben40a22942020-12-22 14:22:06 -07002790HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002791 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002792 // Only supporting image layout transitions for now
2793 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2794 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002795 // only test for WAW if there no intervening read operations.
2796 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002797 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002798 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002799 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002800 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002801 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002802 break;
2803 }
2804 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002805 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2806 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2807 }
2808
2809 return hazard;
2810}
2811
Jeremy Gebben40a22942020-12-22 14:22:06 -07002812HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002813 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06002814 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002815 // Only supporting image layout transitions for now
2816 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2817 HazardResult hazard;
2818 // only test for WAW if there no intervening read operations.
2819 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2820
John Zulaufab7756b2020-12-29 16:10:16 -07002821 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002822 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2823 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002824 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002825 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002826 // The read is in the events first synchronization scope, so we use a barrier hazard check
2827 // If the read stage is not in the src sync scope
2828 // *AND* not execution chained with an existing sync barrier (that's the or)
2829 // then the barrier access is unsafe (R/W after R)
2830 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2831 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2832 break;
2833 }
2834 } else {
2835 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2836 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2837 }
2838 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002839 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002840 // 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 -06002841 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002842 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2843 // So do a normal barrier hazard check
2844 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2845 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2846 }
2847 } else {
2848 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002849 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2850 }
John Zulaufd14743a2020-07-03 09:42:39 -06002851 }
John Zulauf361fb532020-07-22 10:45:39 -06002852
John Zulauf0cb5be22020-01-23 12:18:22 -07002853 return hazard;
2854}
2855
John Zulauf5f13a792020-03-10 07:31:21 -06002856// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2857// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2858// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2859void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06002860 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06002861 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2862 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002863 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06002864 } else if (other.write_tag == write_tag) {
2865 // 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 -06002866 // dependency chaining logic or any stage expansion)
2867 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002868 pending_write_barriers |= other.pending_write_barriers;
2869 pending_layout_transition |= other.pending_layout_transition;
2870 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002871
John Zulaufd14743a2020-07-03 09:42:39 -06002872 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002873 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002874 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002875 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002876 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002877 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002878 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002879 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2880 // but we should wait on profiling data for that.
2881 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002882 auto &my_read = last_reads[my_read_index];
2883 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06002884 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06002885 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002886 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002887 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002888 my_read.pending_dep_chain = other_read.pending_dep_chain;
2889 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2890 // May require tracking more than one access per stage.
2891 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002892 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06002893 // Since I'm overwriting the fragement stage read, also update the input attachment info
2894 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002895 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002896 }
John Zulauf14940722021-04-12 15:19:02 -06002897 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002898 // The read tags match so merge the barriers
2899 my_read.barriers |= other_read.barriers;
2900 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002901 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002902
John Zulauf5f13a792020-03-10 07:31:21 -06002903 break;
2904 }
2905 }
2906 } else {
2907 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002908 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002909 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002910 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002911 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002912 }
John Zulauf5f13a792020-03-10 07:31:21 -06002913 }
2914 }
John Zulauf361fb532020-07-22 10:45:39 -06002915 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002916 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2917 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002918
2919 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2920 // of the copy and other into this using the update first logic.
2921 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2922 // of the other first_accesses... )
2923 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2924 FirstAccesses firsts(std::move(first_accesses_));
2925 first_accesses_.clear();
2926 first_read_stages_ = 0U;
2927 auto a = firsts.begin();
2928 auto a_end = firsts.end();
2929 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06002930 // TODO: Determine whether some tag offset will be needed for PHASE II
2931 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07002932 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2933 ++a;
2934 }
2935 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2936 }
2937 for (; a != a_end; ++a) {
2938 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2939 }
2940 }
John Zulauf5f13a792020-03-10 07:31:21 -06002941}
2942
John Zulauf14940722021-04-12 15:19:02 -06002943void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002944 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2945 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002946 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002947 // Mulitple outstanding reads may be of interest and do dependency chains independently
2948 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2949 const auto usage_stage = PipelineStageBit(usage_index);
2950 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002951 for (auto &read_access : last_reads) {
2952 if (read_access.stage == usage_stage) {
2953 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002954 break;
2955 }
2956 }
2957 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002958 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002959 last_read_stages |= usage_stage;
2960 }
John Zulauf4285ee92020-09-23 10:20:52 -06002961
2962 // 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 -07002963 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002964 // TODO Revisit re: multiple reads for a given stage
2965 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002966 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002967 } else {
2968 // Assume write
2969 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002970 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002971 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002972 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002973}
John Zulauf5f13a792020-03-10 07:31:21 -06002974
John Zulauf89311b42020-09-29 16:28:47 -06002975// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2976// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2977// We can overwrite them as *this* write is now after them.
2978//
2979// 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 -06002980void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002981 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002982 last_read_stages = 0;
2983 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002984 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002985
2986 write_barriers = 0;
2987 write_dependency_chain = 0;
2988 write_tag = tag;
2989 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002990}
2991
John Zulauf89311b42020-09-29 16:28:47 -06002992// Apply the memory barrier without updating the existing barriers. The execution barrier
2993// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2994// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2995// replace the current write barriers or add to them, so accumulate to pending as well.
2996void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2997 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2998 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002999 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
3000 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
3001 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
3002 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07003003 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06003004 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003005 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003006 }
John Zulauf89311b42020-09-29 16:28:47 -06003007 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3008 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06003009
John Zulauf89311b42020-09-29 16:28:47 -06003010 if (!pending_layout_transition) {
3011 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3012 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003013 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06003014 // 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 -07003015 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
3016 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06003017 }
3018 }
John Zulaufa0a98292020-09-18 09:30:10 -06003019 }
John Zulaufa0a98292020-09-18 09:30:10 -06003020}
3021
John Zulauf4a6105a2020-11-17 15:11:05 -07003022// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
3023// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06003024void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003025 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
3026 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
3027 // in order to know if it's in the excecution scope
3028 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
3029 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
3030 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06003031 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003032 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07003033 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003034 }
3035 // Track layout transistion as pending as we can't modify last_write until all barriers processed
3036 pending_layout_transition |= layout_transition;
3037
3038 if (!pending_layout_transition) {
3039 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
3040 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07003041 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07003042 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
3043 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
3044 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
3045 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
3046 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
3047 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
3048 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06003049 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 -07003050 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07003051 }
3052 }
3053 }
3054}
John Zulauf14940722021-04-12 15:19:02 -06003055void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06003056 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06003057 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
3058 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07003059 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06003060 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06003061 }
John Zulauf89311b42020-09-29 16:28:47 -06003062
3063 // Apply the accumulate execution barriers (and thus update chaining information)
3064 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07003065 for (auto &read_access : last_reads) {
3066 read_access.barriers |= read_access.pending_dep_chain;
3067 read_execution_barriers |= read_access.barriers;
3068 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06003069 }
3070
3071 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
3072 write_dependency_chain |= pending_write_dep_chain;
3073 write_barriers |= pending_write_barriers;
3074 pending_write_dep_chain = 0;
3075 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06003076}
3077
John Zulaufae842002021-04-15 18:20:55 -06003078bool ResourceAccessState::FirstAccessInTagRange(const ResourceUsageRange &tag_range) const {
3079 if (!first_accesses_.size()) return false;
3080 const ResourceUsageRange first_access_range = {first_accesses_.front().tag, first_accesses_.back().tag + 1};
3081 return tag_range.intersects(first_access_range);
3082}
3083
John Zulauf59e25072020-07-17 10:55:21 -06003084// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07003085VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
3086 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06003087
John Zulaufab7756b2020-12-29 16:10:16 -07003088 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003089 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06003090 barriers = read_access.barriers;
3091 break;
John Zulauf59e25072020-07-17 10:55:21 -06003092 }
3093 }
John Zulauf4285ee92020-09-23 10:20:52 -06003094
John Zulauf59e25072020-07-17 10:55:21 -06003095 return barriers;
3096}
3097
Jeremy Gebben40a22942020-12-22 14:22:06 -07003098inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003099 assert(IsRead(usage));
3100 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
3101 // * the previous reads are not hazards, and thus last_write must be visible and available to
3102 // any reads that happen after.
3103 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
3104 // 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 -07003105 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06003106}
3107
Jeremy Gebben40a22942020-12-22 14:22:06 -07003108VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06003109 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07003110 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06003111 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07003112 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06003113 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06003114 // 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 -07003115 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06003116 }
3117
3118 return ordered_stages;
3119}
3120
John Zulauf14940722021-04-12 15:19:02 -06003121void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07003122 // Only record until we record a write.
3123 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003124 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003125 if (0 == (usage_stage & first_read_stages_)) {
3126 // If this is a read we haven't seen or a write, record.
3127 first_read_stages_ |= usage_stage;
3128 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3129 }
3130 }
3131}
3132
John Zulaufd1f85d42020-04-15 12:23:15 -06003133void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003134 auto *access_context = GetAccessContextNoInsert(command_buffer);
3135 if (access_context) {
3136 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003137 }
3138}
3139
John Zulaufd1f85d42020-04-15 12:23:15 -06003140void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3141 auto access_found = cb_access_state.find(command_buffer);
3142 if (access_found != cb_access_state.end()) {
3143 access_found->second->Reset();
3144 cb_access_state.erase(access_found);
3145 }
3146}
3147
John Zulauf9cb530d2019-09-30 14:14:10 -06003148bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3149 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3150 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003151 const auto *cb_context = GetAccessContext(commandBuffer);
3152 assert(cb_context);
3153 if (!cb_context) return skip;
3154 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003155
John Zulauf3d84f1b2020-03-09 13:33:25 -06003156 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003157 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003158 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003159
3160 for (uint32_t region = 0; region < regionCount; region++) {
3161 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003162 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003163 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003164 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003165 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003166 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003167 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003168 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003169 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003170 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003171 }
John Zulauf16adfc92020-04-08 10:28:33 -06003172 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003173 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003174 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003175 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003176 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003177 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003178 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003179 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003180 }
3181 }
3182 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003183 }
3184 return skip;
3185}
3186
3187void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3188 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003189 auto *cb_context = GetAccessContext(commandBuffer);
3190 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003191 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003192 auto *context = cb_context->GetCurrentAccessContext();
3193
John Zulauf9cb530d2019-09-30 14:14:10 -06003194 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003195 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003196
3197 for (uint32_t region = 0; region < regionCount; region++) {
3198 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003199 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003200 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003201 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003202 }
John Zulauf16adfc92020-04-08 10:28:33 -06003203 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003204 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003205 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003206 }
3207 }
3208}
3209
John Zulauf4a6105a2020-11-17 15:11:05 -07003210void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3211 // Clear out events from the command buffer contexts
3212 for (auto &cb_context : cb_access_state) {
3213 cb_context.second->RecordDestroyEvent(event);
3214 }
3215}
3216
Jeff Leger178b1e52020-10-05 12:22:23 -04003217bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3218 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3219 bool skip = false;
3220 const auto *cb_context = GetAccessContext(commandBuffer);
3221 assert(cb_context);
3222 if (!cb_context) return skip;
3223 const auto *context = cb_context->GetCurrentAccessContext();
3224
3225 // If we have no previous accesses, we have no hazards
3226 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3227 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3228
3229 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3230 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3231 if (src_buffer) {
3232 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003233 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003234 if (hazard.hazard) {
3235 // TODO -- add tag information to log msg when useful.
3236 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3237 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3238 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003239 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003240 }
3241 }
3242 if (dst_buffer && !skip) {
3243 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003244 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003245 if (hazard.hazard) {
3246 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3247 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3248 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003249 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003250 }
3251 }
3252 if (skip) break;
3253 }
3254 return skip;
3255}
3256
3257void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3258 auto *cb_context = GetAccessContext(commandBuffer);
3259 assert(cb_context);
3260 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3261 auto *context = cb_context->GetCurrentAccessContext();
3262
3263 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3264 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3265
3266 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3267 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3268 if (src_buffer) {
3269 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003270 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003271 }
3272 if (dst_buffer) {
3273 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003274 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003275 }
3276 }
3277}
3278
John Zulauf5c5e88d2019-12-26 11:22:02 -07003279bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3280 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3281 const VkImageCopy *pRegions) const {
3282 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003283 const auto *cb_access_context = GetAccessContext(commandBuffer);
3284 assert(cb_access_context);
3285 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003286
John Zulauf3d84f1b2020-03-09 13:33:25 -06003287 const auto *context = cb_access_context->GetCurrentAccessContext();
3288 assert(context);
3289 if (!context) return skip;
3290
3291 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3292 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003293 for (uint32_t region = 0; region < regionCount; region++) {
3294 const auto &copy_region = pRegions[region];
3295 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003296 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003297 copy_region.srcOffset, copy_region.extent);
3298 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003299 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003300 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003301 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003302 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003303 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003304 }
3305
3306 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003307 VkExtent3D dst_copy_extent =
3308 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003309 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003310 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003311 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003312 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003313 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003314 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003315 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003316 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003317 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003318 }
3319 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003320
John Zulauf5c5e88d2019-12-26 11:22:02 -07003321 return skip;
3322}
3323
3324void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3325 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3326 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003327 auto *cb_access_context = GetAccessContext(commandBuffer);
3328 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003329 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003330 auto *context = cb_access_context->GetCurrentAccessContext();
3331 assert(context);
3332
John Zulauf5c5e88d2019-12-26 11:22:02 -07003333 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003334 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003335
3336 for (uint32_t region = 0; region < regionCount; region++) {
3337 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003338 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003339 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003340 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003341 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003342 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003343 VkExtent3D dst_copy_extent =
3344 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003345 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003346 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003347 }
3348 }
3349}
3350
Jeff Leger178b1e52020-10-05 12:22:23 -04003351bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3352 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3353 bool skip = false;
3354 const auto *cb_access_context = GetAccessContext(commandBuffer);
3355 assert(cb_access_context);
3356 if (!cb_access_context) return skip;
3357
3358 const auto *context = cb_access_context->GetCurrentAccessContext();
3359 assert(context);
3360 if (!context) return skip;
3361
3362 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3363 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3364 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3365 const auto &copy_region = pCopyImageInfo->pRegions[region];
3366 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003367 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003368 copy_region.srcOffset, copy_region.extent);
3369 if (hazard.hazard) {
3370 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3371 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3372 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003373 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003374 }
3375 }
3376
3377 if (dst_image) {
3378 VkExtent3D dst_copy_extent =
3379 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003380 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003381 copy_region.dstOffset, dst_copy_extent);
3382 if (hazard.hazard) {
3383 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3384 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3385 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003386 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003387 }
3388 if (skip) break;
3389 }
3390 }
3391
3392 return skip;
3393}
3394
3395void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3396 auto *cb_access_context = GetAccessContext(commandBuffer);
3397 assert(cb_access_context);
3398 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3399 auto *context = cb_access_context->GetCurrentAccessContext();
3400 assert(context);
3401
3402 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3403 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3404
3405 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3406 const auto &copy_region = pCopyImageInfo->pRegions[region];
3407 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003408 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003409 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003410 }
3411 if (dst_image) {
3412 VkExtent3D dst_copy_extent =
3413 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003414 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003415 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003416 }
3417 }
3418}
3419
John Zulauf9cb530d2019-09-30 14:14:10 -06003420bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3421 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3422 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3423 uint32_t bufferMemoryBarrierCount,
3424 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3425 uint32_t imageMemoryBarrierCount,
3426 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3427 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003428 const auto *cb_access_context = GetAccessContext(commandBuffer);
3429 assert(cb_access_context);
3430 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003431
John Zulauf36ef9282021-02-02 11:47:24 -07003432 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3433 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3434 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3435 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003436 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003437 return skip;
3438}
3439
3440void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3441 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3442 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3443 uint32_t bufferMemoryBarrierCount,
3444 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3445 uint32_t imageMemoryBarrierCount,
3446 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003447 auto *cb_access_context = GetAccessContext(commandBuffer);
3448 assert(cb_access_context);
3449 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003450
John Zulauf8eda1562021-04-13 17:06:41 -06003451 CommandBufferAccessContext::SyncOpPointer sync_op(
3452 new SyncOpPipelineBarrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask, dstStageMask,
3453 dependencyFlags, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
3454 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers));
3455 const auto tag = sync_op->Record(cb_access_context);
3456 cb_access_context->AddSyncOp(tag, std::move(sync_op));
John Zulauf9cb530d2019-09-30 14:14:10 -06003457}
3458
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003459bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3460 const VkDependencyInfoKHR *pDependencyInfo) const {
3461 bool skip = false;
3462 const auto *cb_access_context = GetAccessContext(commandBuffer);
3463 assert(cb_access_context);
3464 if (!cb_access_context) return skip;
3465
3466 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3467 skip = pipeline_barrier.Validate(*cb_access_context);
3468 return skip;
3469}
3470
3471void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3472 auto *cb_access_context = GetAccessContext(commandBuffer);
3473 assert(cb_access_context);
3474 if (!cb_access_context) return;
3475
John Zulauf8eda1562021-04-13 17:06:41 -06003476 CommandBufferAccessContext::SyncOpPointer sync_op(
3477 new SyncOpPipelineBarrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo));
3478 const auto tag = sync_op->Record(cb_access_context);
3479 cb_access_context->AddSyncOp(tag, std::move(sync_op));
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003480}
3481
John Zulauf9cb530d2019-09-30 14:14:10 -06003482void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3483 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3484 // The state tracker sets up the device state
3485 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3486
John Zulauf5f13a792020-03-10 07:31:21 -06003487 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3488 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003489 // TODO: Find a good way to do this hooklessly.
3490 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3491 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3492 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3493
John Zulaufd1f85d42020-04-15 12:23:15 -06003494 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3495 sync_device_state->ResetCommandBufferCallback(command_buffer);
3496 });
3497 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3498 sync_device_state->FreeCommandBufferCallback(command_buffer);
3499 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003500}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003501
John Zulauf355e49b2020-04-24 15:11:15 -06003502bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003503 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003504 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003505 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003506 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003507 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003508 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003509 }
John Zulauf355e49b2020-04-24 15:11:15 -06003510 return skip;
3511}
3512
3513bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3514 VkSubpassContents contents) const {
3515 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003516 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003517 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003518 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003519 return skip;
3520}
3521
3522bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003523 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003524 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003525 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003526 return skip;
3527}
3528
3529bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3530 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003531 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003532 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003533 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003534 return skip;
3535}
3536
John Zulauf3d84f1b2020-03-09 13:33:25 -06003537void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3538 VkResult result) {
3539 // The state tracker sets up the command buffer state
3540 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3541
3542 // Create/initialize the structure that trackers accesses at the command buffer scope.
3543 auto cb_access_context = GetAccessContext(commandBuffer);
3544 assert(cb_access_context);
3545 cb_access_context->Reset();
3546}
3547
3548void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003549 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003550 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003551 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003552 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003553 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003554 }
3555}
3556
3557void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3558 VkSubpassContents contents) {
3559 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003560 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003561 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003562 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003563}
3564
3565void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3566 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3567 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003568 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003569}
3570
3571void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3572 const VkRenderPassBeginInfo *pRenderPassBegin,
3573 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3574 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003575 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003576}
3577
Mike Schuchardt2df08912020-12-15 16:28:09 -08003578bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003579 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003580 bool skip = false;
3581
3582 auto cb_context = GetAccessContext(commandBuffer);
3583 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003584 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003585 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003586 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003587}
3588
3589bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3590 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003591 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003592 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003593 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003594 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3595 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003596 return skip;
3597}
3598
Mike Schuchardt2df08912020-12-15 16:28:09 -08003599bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3600 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003601 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003602 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003603 return skip;
3604}
3605
3606bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3607 const VkSubpassEndInfo *pSubpassEndInfo) const {
3608 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003609 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003610 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003611}
3612
3613void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003614 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003615 auto cb_context = GetAccessContext(commandBuffer);
3616 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003617 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003618
sfricke-samsung85584a72021-09-30 21:43:38 -07003619 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003620 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003621}
3622
3623void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3624 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003625 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003626 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003627 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003628}
3629
3630void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3631 const VkSubpassEndInfo *pSubpassEndInfo) {
3632 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003633 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003634}
3635
3636void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3637 const VkSubpassEndInfo *pSubpassEndInfo) {
3638 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003639 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003640}
3641
sfricke-samsung85584a72021-09-30 21:43:38 -07003642bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3643 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003644 bool skip = false;
3645
3646 auto cb_context = GetAccessContext(commandBuffer);
3647 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003648 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003649
sfricke-samsung85584a72021-09-30 21:43:38 -07003650 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003651 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003652 return skip;
3653}
3654
3655bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3656 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003657 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003658 return skip;
3659}
3660
Mike Schuchardt2df08912020-12-15 16:28:09 -08003661bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003662 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003663 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003664 return skip;
3665}
3666
3667bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003668 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003669 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003670 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003671 return skip;
3672}
3673
sfricke-samsung85584a72021-09-30 21:43:38 -07003674void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003675 // Resolve the all subpass contexts to the command buffer contexts
3676 auto cb_context = GetAccessContext(commandBuffer);
3677 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003678 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003679
sfricke-samsung85584a72021-09-30 21:43:38 -07003680 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003681 sync_op.Record(cb_context);
3682 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003683}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003684
John Zulauf33fc1d52020-07-17 11:01:10 -06003685// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3686// updates to a resource which do not conflict at the byte level.
3687// TODO: Revisit this rule to see if it needs to be tighter or looser
3688// TODO: Add programatic control over suppression heuristics
3689bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3690 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3691}
3692
John Zulauf3d84f1b2020-03-09 13:33:25 -06003693void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003694 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003695 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003696}
3697
3698void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003699 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003700 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003701}
3702
3703void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003704 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003705 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003706}
locke-lunarga19c71d2020-03-02 18:17:04 -07003707
Jeff Leger178b1e52020-10-05 12:22:23 -04003708template <typename BufferImageCopyRegionType>
3709bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3710 VkImageLayout dstImageLayout, uint32_t regionCount,
3711 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003712 bool skip = false;
3713 const auto *cb_access_context = GetAccessContext(commandBuffer);
3714 assert(cb_access_context);
3715 if (!cb_access_context) return skip;
3716
Jeff Leger178b1e52020-10-05 12:22:23 -04003717 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3718 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3719
locke-lunarga19c71d2020-03-02 18:17:04 -07003720 const auto *context = cb_access_context->GetCurrentAccessContext();
3721 assert(context);
3722 if (!context) return skip;
3723
3724 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003725 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3726
3727 for (uint32_t region = 0; region < regionCount; region++) {
3728 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003729 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003730 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003731 if (src_buffer) {
3732 ResourceAccessRange src_range =
3733 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003734 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003735 if (hazard.hazard) {
3736 // PHASE1 TODO -- add tag information to log msg when useful.
3737 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3738 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3739 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003740 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003741 }
3742 }
3743
Jeremy Gebben40a22942020-12-22 14:22:06 -07003744 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003745 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003746 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003747 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003748 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003749 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003750 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003751 }
3752 if (skip) break;
3753 }
3754 if (skip) break;
3755 }
3756 return skip;
3757}
3758
Jeff Leger178b1e52020-10-05 12:22:23 -04003759bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3760 VkImageLayout dstImageLayout, uint32_t regionCount,
3761 const VkBufferImageCopy *pRegions) const {
3762 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3763 COPY_COMMAND_VERSION_1);
3764}
3765
3766bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3767 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3768 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3769 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3770 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3771}
3772
3773template <typename BufferImageCopyRegionType>
3774void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3775 VkImageLayout dstImageLayout, uint32_t regionCount,
3776 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003777 auto *cb_access_context = GetAccessContext(commandBuffer);
3778 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003779
3780 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3781 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3782
3783 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003784 auto *context = cb_access_context->GetCurrentAccessContext();
3785 assert(context);
3786
3787 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003788 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003789
3790 for (uint32_t region = 0; region < regionCount; region++) {
3791 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003792 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003793 if (src_buffer) {
3794 ResourceAccessRange src_range =
3795 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003796 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003797 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003798 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003799 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003800 }
3801 }
3802}
3803
Jeff Leger178b1e52020-10-05 12:22:23 -04003804void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3805 VkImageLayout dstImageLayout, uint32_t regionCount,
3806 const VkBufferImageCopy *pRegions) {
3807 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3808 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3809}
3810
3811void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3812 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3813 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3814 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3815 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3816 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3817}
3818
3819template <typename BufferImageCopyRegionType>
3820bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3821 VkBuffer dstBuffer, uint32_t regionCount,
3822 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003823 bool skip = false;
3824 const auto *cb_access_context = GetAccessContext(commandBuffer);
3825 assert(cb_access_context);
3826 if (!cb_access_context) return skip;
3827
Jeff Leger178b1e52020-10-05 12:22:23 -04003828 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3829 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3830
locke-lunarga19c71d2020-03-02 18:17:04 -07003831 const auto *context = cb_access_context->GetCurrentAccessContext();
3832 assert(context);
3833 if (!context) return skip;
3834
3835 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3836 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06003837 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07003838 for (uint32_t region = 0; region < regionCount; region++) {
3839 const auto &copy_region = pRegions[region];
3840 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003841 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003842 copy_region.imageOffset, copy_region.imageExtent);
3843 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003844 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003845 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003846 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003847 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003848 }
John Zulauf477700e2021-01-06 11:41:49 -07003849 if (dst_mem) {
3850 ResourceAccessRange dst_range =
3851 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003852 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07003853 if (hazard.hazard) {
3854 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3855 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3856 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003857 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003858 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003859 }
3860 }
3861 if (skip) break;
3862 }
3863 return skip;
3864}
3865
Jeff Leger178b1e52020-10-05 12:22:23 -04003866bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3867 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3868 const VkBufferImageCopy *pRegions) const {
3869 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3870 COPY_COMMAND_VERSION_1);
3871}
3872
3873bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3874 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3875 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3876 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3877 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3878}
3879
3880template <typename BufferImageCopyRegionType>
3881void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3882 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3883 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003884 auto *cb_access_context = GetAccessContext(commandBuffer);
3885 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003886
3887 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3888 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3889
3890 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003891 auto *context = cb_access_context->GetCurrentAccessContext();
3892 assert(context);
3893
3894 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003895 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06003896 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06003897 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003898
3899 for (uint32_t region = 0; region < regionCount; region++) {
3900 const auto &copy_region = pRegions[region];
3901 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003902 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003903 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003904 if (dst_buffer) {
3905 ResourceAccessRange dst_range =
3906 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003907 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003908 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003909 }
3910 }
3911}
3912
Jeff Leger178b1e52020-10-05 12:22:23 -04003913void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3914 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3915 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3916 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3917}
3918
3919void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3920 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3921 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3922 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3923 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3924 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3925}
3926
3927template <typename RegionType>
3928bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3929 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3930 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003931 bool skip = false;
3932 const auto *cb_access_context = GetAccessContext(commandBuffer);
3933 assert(cb_access_context);
3934 if (!cb_access_context) return skip;
3935
3936 const auto *context = cb_access_context->GetCurrentAccessContext();
3937 assert(context);
3938 if (!context) return skip;
3939
3940 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3941 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3942
3943 for (uint32_t region = 0; region < regionCount; region++) {
3944 const auto &blit_region = pRegions[region];
3945 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003946 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3947 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3948 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3949 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3950 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3951 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003952 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003953 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003954 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003955 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003956 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003957 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003958 }
3959 }
3960
3961 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003962 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3963 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3964 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3965 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3966 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3967 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003968 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003969 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003970 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003971 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003972 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003973 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003974 }
3975 if (skip) break;
3976 }
3977 }
3978
3979 return skip;
3980}
3981
Jeff Leger178b1e52020-10-05 12:22:23 -04003982bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3983 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3984 const VkImageBlit *pRegions, VkFilter filter) const {
3985 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3986 "vkCmdBlitImage");
3987}
3988
3989bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3990 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3991 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3992 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3993 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3994}
3995
3996template <typename RegionType>
3997void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3998 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3999 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07004000 auto *cb_access_context = GetAccessContext(commandBuffer);
4001 assert(cb_access_context);
4002 auto *context = cb_access_context->GetCurrentAccessContext();
4003 assert(context);
4004
4005 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004006 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07004007
4008 for (uint32_t region = 0; region < regionCount; region++) {
4009 const auto &blit_region = pRegions[region];
4010 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004011 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
4012 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
4013 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
4014 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
4015 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
4016 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004017 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004018 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004019 }
4020 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06004021 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
4022 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
4023 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
4024 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
4025 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
4026 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07004027 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004028 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07004029 }
4030 }
4031}
locke-lunarg36ba2592020-04-03 09:42:04 -06004032
Jeff Leger178b1e52020-10-05 12:22:23 -04004033void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4034 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4035 const VkImageBlit *pRegions, VkFilter filter) {
4036 auto *cb_access_context = GetAccessContext(commandBuffer);
4037 assert(cb_access_context);
4038 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
4039 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4040 pRegions, filter);
4041 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
4042}
4043
4044void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
4045 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
4046 auto *cb_access_context = GetAccessContext(commandBuffer);
4047 assert(cb_access_context);
4048 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
4049 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
4050 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
4051 pBlitImageInfo->filter, tag);
4052}
4053
John Zulauffaea0ee2021-01-14 14:01:32 -07004054bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4055 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
4056 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
4057 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004058 bool skip = false;
4059 if (drawCount == 0) return skip;
4060
4061 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4062 VkDeviceSize size = struct_size;
4063 if (drawCount == 1 || stride == size) {
4064 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004065 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06004066 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4067 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004068 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004069 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004070 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004071 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004072 }
4073 } else {
4074 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004075 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06004076 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4077 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004078 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004079 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
4080 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004081 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004082 break;
4083 }
4084 }
4085 }
4086 return skip;
4087}
4088
John Zulauf14940722021-04-12 15:19:02 -06004089void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06004090 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
4091 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06004092 const auto *buf_state = Get<BUFFER_STATE>(buffer);
4093 VkDeviceSize size = struct_size;
4094 if (drawCount == 1 || stride == size) {
4095 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06004096 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004097 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004098 } else {
4099 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004100 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004101 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
4102 tag);
locke-lunargff255f92020-05-13 18:53:52 -06004103 }
4104 }
4105}
4106
John Zulauffaea0ee2021-01-14 14:01:32 -07004107bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
4108 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4109 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06004110 bool skip = false;
4111
4112 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004113 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06004114 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
4115 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06004116 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004117 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06004118 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004119 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004120 }
4121 return skip;
4122}
4123
John Zulauf14940722021-04-12 15:19:02 -06004124void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004125 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004126 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004127 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004128}
4129
locke-lunarg36ba2592020-04-03 09:42:04 -06004130bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004131 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004132 const auto *cb_access_context = GetAccessContext(commandBuffer);
4133 assert(cb_access_context);
4134 if (!cb_access_context) return skip;
4135
locke-lunarg61870c22020-06-09 14:51:50 -06004136 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004137 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004138}
4139
4140void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004141 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004142 auto *cb_access_context = GetAccessContext(commandBuffer);
4143 assert(cb_access_context);
4144 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004145
locke-lunarg61870c22020-06-09 14:51:50 -06004146 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004147}
locke-lunarge1a67022020-04-29 00:15:36 -06004148
4149bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004150 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004151 const auto *cb_access_context = GetAccessContext(commandBuffer);
4152 assert(cb_access_context);
4153 if (!cb_access_context) return skip;
4154
4155 const auto *context = cb_access_context->GetCurrentAccessContext();
4156 assert(context);
4157 if (!context) return skip;
4158
locke-lunarg61870c22020-06-09 14:51:50 -06004159 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004160 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4161 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004162 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004163}
4164
4165void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004166 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004167 auto *cb_access_context = GetAccessContext(commandBuffer);
4168 assert(cb_access_context);
4169 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4170 auto *context = cb_access_context->GetCurrentAccessContext();
4171 assert(context);
4172
locke-lunarg61870c22020-06-09 14:51:50 -06004173 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4174 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004175}
4176
4177bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4178 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004179 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004180 const auto *cb_access_context = GetAccessContext(commandBuffer);
4181 assert(cb_access_context);
4182 if (!cb_access_context) return skip;
4183
locke-lunarg61870c22020-06-09 14:51:50 -06004184 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4185 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4186 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004187 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004188}
4189
4190void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4191 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004192 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004193 auto *cb_access_context = GetAccessContext(commandBuffer);
4194 assert(cb_access_context);
4195 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004196
locke-lunarg61870c22020-06-09 14:51:50 -06004197 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4198 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4199 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004200}
4201
4202bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4203 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004204 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004205 const auto *cb_access_context = GetAccessContext(commandBuffer);
4206 assert(cb_access_context);
4207 if (!cb_access_context) return skip;
4208
locke-lunarg61870c22020-06-09 14:51:50 -06004209 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4210 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4211 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004212 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004213}
4214
4215void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4216 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004217 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004218 auto *cb_access_context = GetAccessContext(commandBuffer);
4219 assert(cb_access_context);
4220 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004221
locke-lunarg61870c22020-06-09 14:51:50 -06004222 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4223 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4224 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004225}
4226
4227bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4228 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004229 bool skip = false;
4230 if (drawCount == 0) return skip;
4231
locke-lunargff255f92020-05-13 18:53:52 -06004232 const auto *cb_access_context = GetAccessContext(commandBuffer);
4233 assert(cb_access_context);
4234 if (!cb_access_context) return skip;
4235
4236 const auto *context = cb_access_context->GetCurrentAccessContext();
4237 assert(context);
4238 if (!context) return skip;
4239
locke-lunarg61870c22020-06-09 14:51:50 -06004240 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4241 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004242 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4243 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004244
4245 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4246 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4247 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004248 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004249 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004250}
4251
4252void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4253 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004254 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004255 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004256 auto *cb_access_context = GetAccessContext(commandBuffer);
4257 assert(cb_access_context);
4258 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4259 auto *context = cb_access_context->GetCurrentAccessContext();
4260 assert(context);
4261
locke-lunarg61870c22020-06-09 14:51:50 -06004262 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4263 cb_access_context->RecordDrawSubpassAttachment(tag);
4264 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004265
4266 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4267 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4268 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004269 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004270}
4271
4272bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4273 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004274 bool skip = false;
4275 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004276 const auto *cb_access_context = GetAccessContext(commandBuffer);
4277 assert(cb_access_context);
4278 if (!cb_access_context) return skip;
4279
4280 const auto *context = cb_access_context->GetCurrentAccessContext();
4281 assert(context);
4282 if (!context) return skip;
4283
locke-lunarg61870c22020-06-09 14:51:50 -06004284 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4285 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004286 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4287 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004288
4289 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4290 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4291 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004292 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004293 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004294}
4295
4296void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4297 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004298 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004299 auto *cb_access_context = GetAccessContext(commandBuffer);
4300 assert(cb_access_context);
4301 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4302 auto *context = cb_access_context->GetCurrentAccessContext();
4303 assert(context);
4304
locke-lunarg61870c22020-06-09 14:51:50 -06004305 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4306 cb_access_context->RecordDrawSubpassAttachment(tag);
4307 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004308
4309 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4310 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4311 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004312 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004313}
4314
4315bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4316 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4317 uint32_t stride, const char *function) const {
4318 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004319 const auto *cb_access_context = GetAccessContext(commandBuffer);
4320 assert(cb_access_context);
4321 if (!cb_access_context) return skip;
4322
4323 const auto *context = cb_access_context->GetCurrentAccessContext();
4324 assert(context);
4325 if (!context) return skip;
4326
locke-lunarg61870c22020-06-09 14:51:50 -06004327 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4328 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004329 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4330 maxDrawCount, stride, function);
4331 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004332
4333 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4334 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4335 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004336 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004337 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004338}
4339
4340bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4341 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4342 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004343 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4344 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004345}
4346
sfricke-samsung85584a72021-09-30 21:43:38 -07004347void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4348 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4349 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004350 auto *cb_access_context = GetAccessContext(commandBuffer);
4351 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004352 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004353 auto *context = cb_access_context->GetCurrentAccessContext();
4354 assert(context);
4355
locke-lunarg61870c22020-06-09 14:51:50 -06004356 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4357 cb_access_context->RecordDrawSubpassAttachment(tag);
4358 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4359 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004360
4361 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4362 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4363 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004364 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004365}
4366
sfricke-samsung85584a72021-09-30 21:43:38 -07004367void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4368 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4369 uint32_t stride) {
4370 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4371 stride);
4372 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4373 CMD_DRAWINDIRECTCOUNT);
4374}
locke-lunarge1a67022020-04-29 00:15:36 -06004375bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4376 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4377 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004378 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4379 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004380}
4381
4382void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4383 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4384 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004385 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4386 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004387 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4388 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004389}
4390
4391bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4392 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4393 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004394 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4395 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004396}
4397
4398void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4399 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4400 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004401 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4402 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004403 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4404 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004405}
4406
4407bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4408 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4409 uint32_t stride, const char *function) const {
4410 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004411 const auto *cb_access_context = GetAccessContext(commandBuffer);
4412 assert(cb_access_context);
4413 if (!cb_access_context) return skip;
4414
4415 const auto *context = cb_access_context->GetCurrentAccessContext();
4416 assert(context);
4417 if (!context) return skip;
4418
locke-lunarg61870c22020-06-09 14:51:50 -06004419 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4420 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004421 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4422 offset, maxDrawCount, stride, function);
4423 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004424
4425 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4426 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4427 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004428 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004429 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004430}
4431
4432bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4433 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4434 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004435 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4436 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004437}
4438
sfricke-samsung85584a72021-09-30 21:43:38 -07004439void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4440 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4441 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004442 auto *cb_access_context = GetAccessContext(commandBuffer);
4443 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004444 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004445 auto *context = cb_access_context->GetCurrentAccessContext();
4446 assert(context);
4447
locke-lunarg61870c22020-06-09 14:51:50 -06004448 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4449 cb_access_context->RecordDrawSubpassAttachment(tag);
4450 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4451 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004452
4453 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4454 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004455 // We will update the index and vertex buffer in SubmitQueue in the future.
4456 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004457}
4458
sfricke-samsung85584a72021-09-30 21:43:38 -07004459void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4460 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4461 uint32_t maxDrawCount, uint32_t stride) {
4462 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4463 maxDrawCount, stride);
4464 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4465 CMD_DRAWINDEXEDINDIRECTCOUNT);
4466}
4467
locke-lunarge1a67022020-04-29 00:15:36 -06004468bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4469 VkDeviceSize offset, VkBuffer countBuffer,
4470 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4471 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004472 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4473 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004474}
4475
4476void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4477 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4478 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004479 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4480 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004481 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4482 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004483}
4484
4485bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4486 VkDeviceSize offset, VkBuffer countBuffer,
4487 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4488 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004489 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4490 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004491}
4492
4493void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4494 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4495 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004496 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4497 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004498 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4499 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004500}
4501
4502bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4503 const VkClearColorValue *pColor, uint32_t rangeCount,
4504 const VkImageSubresourceRange *pRanges) const {
4505 bool skip = false;
4506 const auto *cb_access_context = GetAccessContext(commandBuffer);
4507 assert(cb_access_context);
4508 if (!cb_access_context) return skip;
4509
4510 const auto *context = cb_access_context->GetCurrentAccessContext();
4511 assert(context);
4512 if (!context) return skip;
4513
4514 const auto *image_state = Get<IMAGE_STATE>(image);
4515
4516 for (uint32_t index = 0; index < rangeCount; index++) {
4517 const auto &range = pRanges[index];
4518 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004519 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004520 if (hazard.hazard) {
4521 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004522 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004523 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004524 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004525 }
4526 }
4527 }
4528 return skip;
4529}
4530
4531void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4532 const VkClearColorValue *pColor, uint32_t rangeCount,
4533 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004534 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004535 auto *cb_access_context = GetAccessContext(commandBuffer);
4536 assert(cb_access_context);
4537 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4538 auto *context = cb_access_context->GetCurrentAccessContext();
4539 assert(context);
4540
4541 const auto *image_state = Get<IMAGE_STATE>(image);
4542
4543 for (uint32_t index = 0; index < rangeCount; index++) {
4544 const auto &range = pRanges[index];
4545 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004546 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004547 }
4548 }
4549}
4550
4551bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4552 VkImageLayout imageLayout,
4553 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4554 const VkImageSubresourceRange *pRanges) const {
4555 bool skip = false;
4556 const auto *cb_access_context = GetAccessContext(commandBuffer);
4557 assert(cb_access_context);
4558 if (!cb_access_context) return skip;
4559
4560 const auto *context = cb_access_context->GetCurrentAccessContext();
4561 assert(context);
4562 if (!context) return skip;
4563
4564 const auto *image_state = Get<IMAGE_STATE>(image);
4565
4566 for (uint32_t index = 0; index < rangeCount; index++) {
4567 const auto &range = pRanges[index];
4568 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004569 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004570 if (hazard.hazard) {
4571 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004572 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004573 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004574 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004575 }
4576 }
4577 }
4578 return skip;
4579}
4580
4581void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4582 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4583 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004584 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004585 auto *cb_access_context = GetAccessContext(commandBuffer);
4586 assert(cb_access_context);
4587 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4588 auto *context = cb_access_context->GetCurrentAccessContext();
4589 assert(context);
4590
4591 const auto *image_state = Get<IMAGE_STATE>(image);
4592
4593 for (uint32_t index = 0; index < rangeCount; index++) {
4594 const auto &range = pRanges[index];
4595 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004596 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004597 }
4598 }
4599}
4600
4601bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4602 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4603 VkDeviceSize dstOffset, VkDeviceSize stride,
4604 VkQueryResultFlags flags) const {
4605 bool skip = false;
4606 const auto *cb_access_context = GetAccessContext(commandBuffer);
4607 assert(cb_access_context);
4608 if (!cb_access_context) return skip;
4609
4610 const auto *context = cb_access_context->GetCurrentAccessContext();
4611 assert(context);
4612 if (!context) return skip;
4613
4614 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4615
4616 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004617 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004618 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004619 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004620 skip |=
4621 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4622 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004623 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004624 }
4625 }
locke-lunargff255f92020-05-13 18:53:52 -06004626
4627 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004628 return skip;
4629}
4630
4631void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4632 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4633 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004634 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4635 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004636 auto *cb_access_context = GetAccessContext(commandBuffer);
4637 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004638 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004639 auto *context = cb_access_context->GetCurrentAccessContext();
4640 assert(context);
4641
4642 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4643
4644 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004645 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004646 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004647 }
locke-lunargff255f92020-05-13 18:53:52 -06004648
4649 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004650}
4651
4652bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4653 VkDeviceSize size, uint32_t data) const {
4654 bool skip = false;
4655 const auto *cb_access_context = GetAccessContext(commandBuffer);
4656 assert(cb_access_context);
4657 if (!cb_access_context) return skip;
4658
4659 const auto *context = cb_access_context->GetCurrentAccessContext();
4660 assert(context);
4661 if (!context) return skip;
4662
4663 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4664
4665 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004666 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004667 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004668 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004669 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004670 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004671 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004672 }
4673 }
4674 return skip;
4675}
4676
4677void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4678 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004679 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004680 auto *cb_access_context = GetAccessContext(commandBuffer);
4681 assert(cb_access_context);
4682 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4683 auto *context = cb_access_context->GetCurrentAccessContext();
4684 assert(context);
4685
4686 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4687
4688 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004689 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004690 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004691 }
4692}
4693
4694bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4695 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4696 const VkImageResolve *pRegions) const {
4697 bool skip = false;
4698 const auto *cb_access_context = GetAccessContext(commandBuffer);
4699 assert(cb_access_context);
4700 if (!cb_access_context) return skip;
4701
4702 const auto *context = cb_access_context->GetCurrentAccessContext();
4703 assert(context);
4704 if (!context) return skip;
4705
4706 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4707 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4708
4709 for (uint32_t region = 0; region < regionCount; region++) {
4710 const auto &resolve_region = pRegions[region];
4711 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004712 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004713 resolve_region.srcOffset, resolve_region.extent);
4714 if (hazard.hazard) {
4715 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004716 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004717 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004718 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004719 }
4720 }
4721
4722 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004723 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004724 resolve_region.dstOffset, resolve_region.extent);
4725 if (hazard.hazard) {
4726 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004727 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004728 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004729 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004730 }
4731 if (skip) break;
4732 }
4733 }
4734
4735 return skip;
4736}
4737
4738void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4739 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4740 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004741 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4742 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004743 auto *cb_access_context = GetAccessContext(commandBuffer);
4744 assert(cb_access_context);
4745 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4746 auto *context = cb_access_context->GetCurrentAccessContext();
4747 assert(context);
4748
4749 auto *src_image = Get<IMAGE_STATE>(srcImage);
4750 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4751
4752 for (uint32_t region = 0; region < regionCount; region++) {
4753 const auto &resolve_region = pRegions[region];
4754 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004755 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004756 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004757 }
4758 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004759 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004760 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004761 }
4762 }
4763}
4764
Jeff Leger178b1e52020-10-05 12:22:23 -04004765bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4766 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4767 bool skip = false;
4768 const auto *cb_access_context = GetAccessContext(commandBuffer);
4769 assert(cb_access_context);
4770 if (!cb_access_context) return skip;
4771
4772 const auto *context = cb_access_context->GetCurrentAccessContext();
4773 assert(context);
4774 if (!context) return skip;
4775
4776 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4777 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4778
4779 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4780 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4781 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004782 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004783 resolve_region.srcOffset, resolve_region.extent);
4784 if (hazard.hazard) {
4785 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4786 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4787 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004788 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004789 }
4790 }
4791
4792 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004793 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004794 resolve_region.dstOffset, resolve_region.extent);
4795 if (hazard.hazard) {
4796 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4797 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4798 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004799 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004800 }
4801 if (skip) break;
4802 }
4803 }
4804
4805 return skip;
4806}
4807
4808void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4809 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4810 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4811 auto *cb_access_context = GetAccessContext(commandBuffer);
4812 assert(cb_access_context);
4813 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4814 auto *context = cb_access_context->GetCurrentAccessContext();
4815 assert(context);
4816
4817 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4818 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4819
4820 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4821 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4822 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004823 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004824 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004825 }
4826 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004827 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004828 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004829 }
4830 }
4831}
4832
locke-lunarge1a67022020-04-29 00:15:36 -06004833bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4834 VkDeviceSize dataSize, const void *pData) const {
4835 bool skip = false;
4836 const auto *cb_access_context = GetAccessContext(commandBuffer);
4837 assert(cb_access_context);
4838 if (!cb_access_context) return skip;
4839
4840 const auto *context = cb_access_context->GetCurrentAccessContext();
4841 assert(context);
4842 if (!context) return skip;
4843
4844 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4845
4846 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004847 // VK_WHOLE_SIZE not allowed
4848 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004849 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004850 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004851 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004852 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004853 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004854 }
4855 }
4856 return skip;
4857}
4858
4859void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4860 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004861 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004862 auto *cb_access_context = GetAccessContext(commandBuffer);
4863 assert(cb_access_context);
4864 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4865 auto *context = cb_access_context->GetCurrentAccessContext();
4866 assert(context);
4867
4868 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4869
4870 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004871 // VK_WHOLE_SIZE not allowed
4872 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004873 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004874 }
4875}
locke-lunargff255f92020-05-13 18:53:52 -06004876
4877bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4878 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4879 bool skip = false;
4880 const auto *cb_access_context = GetAccessContext(commandBuffer);
4881 assert(cb_access_context);
4882 if (!cb_access_context) return skip;
4883
4884 const auto *context = cb_access_context->GetCurrentAccessContext();
4885 assert(context);
4886 if (!context) return skip;
4887
4888 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4889
4890 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004891 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004892 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06004893 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004894 skip |=
4895 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4896 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004897 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004898 }
4899 }
4900 return skip;
4901}
4902
4903void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4904 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004905 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004906 auto *cb_access_context = GetAccessContext(commandBuffer);
4907 assert(cb_access_context);
4908 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4909 auto *context = cb_access_context->GetCurrentAccessContext();
4910 assert(context);
4911
4912 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4913
4914 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004915 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004916 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004917 }
4918}
John Zulauf49beb112020-11-04 16:06:31 -07004919
4920bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4921 bool skip = false;
4922 const auto *cb_context = GetAccessContext(commandBuffer);
4923 assert(cb_context);
4924 if (!cb_context) return skip;
4925
John Zulauf36ef9282021-02-02 11:47:24 -07004926 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004927 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004928}
4929
4930void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4931 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4932 auto *cb_context = GetAccessContext(commandBuffer);
4933 assert(cb_context);
4934 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07004935 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4936 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004937}
4938
John Zulauf4edde622021-02-15 08:54:50 -07004939bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4940 const VkDependencyInfoKHR *pDependencyInfo) const {
4941 bool skip = false;
4942 const auto *cb_context = GetAccessContext(commandBuffer);
4943 assert(cb_context);
4944 if (!cb_context || !pDependencyInfo) return skip;
4945
4946 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4947 return set_event_op.Validate(*cb_context);
4948}
4949
4950void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4951 const VkDependencyInfoKHR *pDependencyInfo) {
4952 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
4953 auto *cb_context = GetAccessContext(commandBuffer);
4954 assert(cb_context);
4955 if (!cb_context || !pDependencyInfo) return;
4956
4957 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4958 set_event_op.Record(cb_context);
4959}
4960
John Zulauf49beb112020-11-04 16:06:31 -07004961bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4962 VkPipelineStageFlags stageMask) const {
4963 bool skip = false;
4964 const auto *cb_context = GetAccessContext(commandBuffer);
4965 assert(cb_context);
4966 if (!cb_context) return skip;
4967
John Zulauf36ef9282021-02-02 11:47:24 -07004968 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004969 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004970}
4971
4972void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4973 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4974 auto *cb_context = GetAccessContext(commandBuffer);
4975 assert(cb_context);
4976 if (!cb_context) return;
4977
John Zulauf36ef9282021-02-02 11:47:24 -07004978 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4979 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004980}
4981
John Zulauf4edde622021-02-15 08:54:50 -07004982bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4983 VkPipelineStageFlags2KHR stageMask) const {
4984 bool skip = false;
4985 const auto *cb_context = GetAccessContext(commandBuffer);
4986 assert(cb_context);
4987 if (!cb_context) return skip;
4988
4989 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4990 return reset_event_op.Validate(*cb_context);
4991}
4992
4993void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4994 VkPipelineStageFlags2KHR stageMask) {
4995 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
4996 auto *cb_context = GetAccessContext(commandBuffer);
4997 assert(cb_context);
4998 if (!cb_context) return;
4999
5000 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
5001 reset_event_op.Record(cb_context);
5002}
5003
John Zulauf49beb112020-11-04 16:06:31 -07005004bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5005 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5006 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5007 uint32_t bufferMemoryBarrierCount,
5008 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5009 uint32_t imageMemoryBarrierCount,
5010 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
5011 bool skip = false;
5012 const auto *cb_context = GetAccessContext(commandBuffer);
5013 assert(cb_context);
5014 if (!cb_context) return skip;
5015
John Zulauf36ef9282021-02-02 11:47:24 -07005016 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5017 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5018 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07005019 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07005020}
5021
5022void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5023 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5024 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5025 uint32_t bufferMemoryBarrierCount,
5026 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5027 uint32_t imageMemoryBarrierCount,
5028 const VkImageMemoryBarrier *pImageMemoryBarriers) {
5029 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
5030 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
5031 imageMemoryBarrierCount, pImageMemoryBarriers);
5032
5033 auto *cb_context = GetAccessContext(commandBuffer);
5034 assert(cb_context);
5035 if (!cb_context) return;
5036
John Zulauf36ef9282021-02-02 11:47:24 -07005037 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
5038 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
5039 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf8eda1562021-04-13 17:06:41 -06005040 wait_events_op.Record(cb_context);
5041 return;
John Zulauf4a6105a2020-11-17 15:11:05 -07005042}
5043
John Zulauf4edde622021-02-15 08:54:50 -07005044bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5045 const VkDependencyInfoKHR *pDependencyInfos) const {
5046 bool skip = false;
5047 const auto *cb_context = GetAccessContext(commandBuffer);
5048 assert(cb_context);
5049 if (!cb_context) return skip;
5050
5051 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5052 skip |= wait_events_op.Validate(*cb_context);
5053 return skip;
5054}
5055
5056void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
5057 const VkDependencyInfoKHR *pDependencyInfos) {
5058 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
5059
5060 auto *cb_context = GetAccessContext(commandBuffer);
5061 assert(cb_context);
5062 if (!cb_context) return;
5063
5064 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
5065 wait_events_op.Record(cb_context);
5066}
5067
John Zulauf4a6105a2020-11-17 15:11:05 -07005068void SyncEventState::ResetFirstScope() {
5069 for (const auto address_type : kAddressTypes) {
5070 first_scope[static_cast<size_t>(address_type)].clear();
5071 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07005072 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07005073}
5074
5075// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07005076SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005077 IgnoreReason reason = NotIgnored;
5078
John Zulauf4edde622021-02-15 08:54:50 -07005079 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
5080 reason = SetVsWait2;
5081 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
5082 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07005083 } else if (unsynchronized_set) {
5084 reason = SetRace;
5085 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07005086 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07005087 if (missing_bits) reason = MissingStageBits;
5088 }
5089
5090 return reason;
5091}
5092
Jeremy Gebben40a22942020-12-22 14:22:06 -07005093bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07005094 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
5095 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
5096 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07005097}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005098
John Zulauf36ef9282021-02-02 11:47:24 -07005099SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5100 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5101 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005102 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5103 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5104 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07005105 : SyncOpBase(cmd), barriers_(1) {
5106 auto &barrier_set = barriers_[0];
5107 barrier_set.dependency_flags = dependencyFlags;
5108 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
5109 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005110 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07005111 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
5112 pMemoryBarriers);
5113 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5114 bufferMemoryBarrierCount, pBufferMemoryBarriers);
5115 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
5116 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005117}
5118
John Zulauf4edde622021-02-15 08:54:50 -07005119SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
5120 const VkDependencyInfoKHR *dep_infos)
5121 : SyncOpBase(cmd), barriers_(event_count) {
5122 for (uint32_t i = 0; i < event_count; i++) {
5123 const auto &dep_info = dep_infos[i];
5124 auto &barrier_set = barriers_[i];
5125 barrier_set.dependency_flags = dep_info.dependencyFlags;
5126 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5127 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5128 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5129 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5130 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5131 dep_info.pMemoryBarriers);
5132 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5133 dep_info.pBufferMemoryBarriers);
5134 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5135 dep_info.pImageMemoryBarriers);
5136 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005137}
5138
John Zulauf36ef9282021-02-02 11:47:24 -07005139SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005140 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5141 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5142 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5143 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5144 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005145 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005146 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5147
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005148SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5149 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005150 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005151
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005152bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5153 bool skip = false;
5154 const auto *context = cb_context.GetCurrentAccessContext();
5155 assert(context);
5156 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005157 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5158
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005159 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005160 const auto &barrier_set = barriers_[0];
5161 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5162 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5163 const auto *image_state = image_barrier.image.get();
5164 if (!image_state) continue;
5165 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5166 if (hazard.hazard) {
5167 // PHASE1 TODO -- add tag information to log msg when useful.
5168 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005169 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005170 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5171 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5172 string_SyncHazard(hazard.hazard), image_barrier.index,
5173 sync_state.report_data->FormatHandle(image_handle).c_str(),
5174 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005175 }
5176 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005177 return skip;
5178}
5179
John Zulaufd5115702021-01-18 12:34:33 -07005180struct SyncOpPipelineBarrierFunctorFactory {
5181 using BarrierOpFunctor = PipelineBarrierOp;
5182 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5183 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5184 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5185 using BufferRange = ResourceAccessRange;
5186 using ImageRange = subresource_adapter::ImageRangeGenerator;
5187 using GlobalRange = ResourceAccessRange;
5188
5189 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5190 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5191 }
John Zulauf14940722021-04-12 15:19:02 -06005192 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005193 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5194 }
5195 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5196 return GlobalBarrierOpFunctor(barrier, false);
5197 }
5198
5199 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5200 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5201 const auto base_address = ResourceBaseAddress(buffer);
5202 return (range + base_address);
5203 }
John Zulauf110413c2021-03-20 05:38:38 -06005204 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005205 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005206
5207 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005208 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005209 return range_gen;
5210 }
5211 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5212};
5213
5214template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005215void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005216 AccessContext *context) {
5217 for (const auto &barrier : barriers) {
5218 const auto *state = barrier.GetState();
5219 if (state) {
5220 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5221 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5222 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5223 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5224 }
5225 }
5226}
5227
5228template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005229void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005230 AccessContext *access_context) {
5231 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5232 for (const auto &barrier : barriers) {
5233 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5234 }
5235 for (const auto address_type : kAddressTypes) {
5236 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5237 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5238 }
5239}
5240
John Zulauf8eda1562021-04-13 17:06:41 -06005241ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005242 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005243 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005244 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005245
John Zulauf8eda1562021-04-13 17:06:41 -06005246 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005247 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5248 assert(barriers_.size() == 1);
5249 const auto &barrier_set = barriers_[0];
5250 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5251 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5252 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005253 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005254 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005255 } else {
5256 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005257 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005258 }
5259 }
John Zulauf8eda1562021-04-13 17:06:41 -06005260
5261 return tag;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005262}
5263
John Zulauf8eda1562021-04-13 17:06:41 -06005264bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5265 CommandBufferAccessContext *active_context) const {
5266 return false;
5267}
5268
5269void SyncOpPipelineBarrier::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5270 CommandBufferAccessContext *active_context) const {}
5271
John Zulauf4edde622021-02-15 08:54:50 -07005272void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5273 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5274 const VkMemoryBarrier *barriers) {
5275 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005276 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005277 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005278 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005279 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005280 }
5281 if (0 == memory_barrier_count) {
5282 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005283 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005284 }
John Zulauf4edde622021-02-15 08:54:50 -07005285 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005286}
5287
John Zulauf4edde622021-02-15 08:54:50 -07005288void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5289 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5290 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5291 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005292 for (uint32_t index = 0; index < barrier_count; index++) {
5293 const auto &barrier = barriers[index];
5294 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5295 if (buffer) {
5296 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5297 const auto range = MakeRange(barrier.offset, barrier_size);
5298 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005299 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005300 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005301 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005302 }
5303 }
5304}
5305
John Zulauf4edde622021-02-15 08:54:50 -07005306void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5307 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5308 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005309 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005310 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005311 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5312 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5313 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005314 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005315 }
John Zulauf4edde622021-02-15 08:54:50 -07005316 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005317}
5318
John Zulauf4edde622021-02-15 08:54:50 -07005319void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5320 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5321 const VkBufferMemoryBarrier2KHR *barriers) {
5322 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005323 for (uint32_t index = 0; index < barrier_count; index++) {
5324 const auto &barrier = barriers[index];
5325 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5326 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5327 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5328 if (buffer) {
5329 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5330 const auto range = MakeRange(barrier.offset, barrier_size);
5331 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005332 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005333 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005334 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005335 }
5336 }
5337}
5338
John Zulauf4edde622021-02-15 08:54:50 -07005339void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5340 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5341 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5342 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005343 for (uint32_t index = 0; index < barrier_count; index++) {
5344 const auto &barrier = barriers[index];
5345 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5346 if (image) {
5347 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5348 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005349 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005350 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005351 image_memory_barriers.emplace_back();
5352 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005353 }
5354 }
5355}
John Zulaufd5115702021-01-18 12:34:33 -07005356
John Zulauf4edde622021-02-15 08:54:50 -07005357void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5358 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5359 const VkImageMemoryBarrier2KHR *barriers) {
5360 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005361 for (uint32_t index = 0; index < barrier_count; index++) {
5362 const auto &barrier = barriers[index];
5363 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5364 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5365 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5366 if (image) {
5367 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5368 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005369 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005370 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005371 image_memory_barriers.emplace_back();
5372 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005373 }
5374 }
5375}
5376
John Zulauf36ef9282021-02-02 11:47:24 -07005377SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005378 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5379 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5380 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5381 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005382 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005383 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5384 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005385 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005386}
5387
John Zulauf4edde622021-02-15 08:54:50 -07005388SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5389 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5390 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5391 MakeEventsList(sync_state, eventCount, pEvents);
5392 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5393}
5394
John Zulaufd5115702021-01-18 12:34:33 -07005395bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005396 const char *const ignored = "Wait operation is ignored for this event.";
5397 bool skip = false;
5398 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005399 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005400
John Zulauf4edde622021-02-15 08:54:50 -07005401 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5402 const auto &barrier_set = barriers_[barrier_set_index];
5403 if (barrier_set.single_exec_scope) {
5404 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5405 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5406 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5407 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5408 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5409 } else {
5410 const auto &barriers = barrier_set.memory_barriers;
5411 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5412 const auto &barrier = barriers[barrier_index];
5413 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5414 const std::string vuid =
5415 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5416 skip =
5417 sync_state.LogInfo(command_buffer_handle, vuid,
5418 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5419 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5420 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5421 }
5422 }
5423 }
5424 }
John Zulaufd5115702021-01-18 12:34:33 -07005425 }
5426
Jeremy Gebben40a22942020-12-22 14:22:06 -07005427 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005428 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005429 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005430 const auto *events_context = cb_context.GetCurrentEventsContext();
5431 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005432 size_t barrier_set_index = 0;
5433 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5434 for (size_t event_index = 0; event_index < events_.size(); event_index++)
5435 for (const auto &event : events_) {
5436 const auto *sync_event = events_context->Get(event.get());
5437 const auto &barrier_set = barriers_[barrier_set_index];
5438 if (!sync_event) {
5439 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5440 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5441 // new validation error... wait without previously submitted set event...
5442 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
5443 barrier_set_index += barrier_set_incr;
5444 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005445 }
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005446 const auto event_handle = sync_event->event->event();
John Zulauf4edde622021-02-15 08:54:50 -07005447 // TODO add "destroyed" checks
5448
5449 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5450 const auto &src_exec_scope = barrier_set.src_exec_scope;
5451 event_stage_masks |= sync_event->scope.mask_param;
5452 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5453 if (ignore_reason) {
5454 switch (ignore_reason) {
5455 case SyncEventState::ResetWaitRace:
5456 case SyncEventState::Reset2WaitRace: {
5457 // Four permuations of Reset and Wait calls...
5458 const char *vuid =
5459 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5460 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5461 vuid =
Jeremy Gebben476f5e22021-03-01 15:27:20 -07005462 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831" : "VUID-vkCmdResetEvent2KHR-event-03832";
John Zulauf4edde622021-02-15 08:54:50 -07005463 }
5464 const char *const message =
5465 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5466 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5467 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5468 CommandTypeString(sync_event->last_command), ignored);
5469 break;
5470 }
5471 case SyncEventState::SetRace: {
5472 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5473 // this event
5474 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5475 const char *const message =
5476 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5477 const char *const reason = "First synchronization scope is undefined.";
5478 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5479 sync_state.report_data->FormatHandle(event_handle).c_str(),
5480 CommandTypeString(sync_event->last_command), reason, ignored);
5481 break;
5482 }
5483 case SyncEventState::MissingStageBits: {
5484 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5485 // Issue error message that event waited for is not in wait events scope
5486 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5487 const char *const message =
5488 "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5489 ". Bits missing from srcStageMask %s. %s";
5490 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5491 sync_state.report_data->FormatHandle(event_handle).c_str(),
5492 sync_event->scope.mask_param, src_exec_scope.mask_param,
5493 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5494 break;
5495 }
5496 case SyncEventState::SetVsWait2: {
5497 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5498 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5499 sync_state.report_data->FormatHandle(event_handle).c_str(),
5500 CommandTypeString(sync_event->last_command));
5501 break;
5502 }
5503 default:
5504 assert(ignore_reason == SyncEventState::NotIgnored);
5505 }
5506 } else if (barrier_set.image_memory_barriers.size()) {
5507 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5508 const auto *context = cb_context.GetCurrentAccessContext();
5509 assert(context);
5510 for (const auto &image_memory_barrier : image_memory_barriers) {
5511 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5512 const auto *image_state = image_memory_barrier.image.get();
5513 if (!image_state) continue;
John Zulauf110413c2021-03-20 05:38:38 -06005514 const auto &subresource_range = image_memory_barrier.range;
John Zulauf4edde622021-02-15 08:54:50 -07005515 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5516 const auto hazard =
5517 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5518 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5519 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005520 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
John Zulauf4edde622021-02-15 08:54:50 -07005521 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5522 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005523 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
John Zulauf4edde622021-02-15 08:54:50 -07005524 cb_context.FormatUsage(hazard).c_str());
5525 break;
5526 }
John Zulaufd5115702021-01-18 12:34:33 -07005527 }
5528 }
John Zulauf4edde622021-02-15 08:54:50 -07005529 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5530 // 03839
5531 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005532 }
John Zulaufd5115702021-01-18 12:34:33 -07005533
5534 // 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 -07005535 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 -07005536 if (extra_stage_bits) {
5537 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005538 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5539 const char *const vuid =
5540 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005541 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005542 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005543 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005544 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005545 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005546 " vkCmdSetEvent may be in previously submitted command buffer.");
5547 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005548 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005549 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005550 }
5551 }
5552 return skip;
5553}
5554
5555struct SyncOpWaitEventsFunctorFactory {
5556 using BarrierOpFunctor = WaitEventBarrierOp;
5557 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5558 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5559 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5560 using BufferRange = EventSimpleRangeGenerator;
5561 using ImageRange = EventImageRangeGenerator;
5562 using GlobalRange = EventSimpleRangeGenerator;
5563
5564 // Need to restrict to only valid exec and access scope for this event
5565 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5566 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005567 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005568 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5569 return barrier;
5570 }
5571 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5572 auto barrier = RestrictToEvent(barrier_arg);
5573 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5574 }
John Zulauf14940722021-04-12 15:19:02 -06005575 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005576 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5577 }
5578 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5579 auto barrier = RestrictToEvent(barrier_arg);
5580 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5581 }
5582
5583 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5584 const AccessAddressType address_type = GetAccessAddressType(buffer);
5585 const auto base_address = ResourceBaseAddress(buffer);
5586 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5587 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5588 return filtered_range_gen;
5589 }
John Zulauf110413c2021-03-20 05:38:38 -06005590 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005591 if (!SimpleBinding(image)) return ImageRange();
5592 const auto address_type = GetAccessAddressType(image);
5593 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005594 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005595 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5596
5597 return filtered_range_gen;
5598 }
5599 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5600 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5601 }
5602 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5603 SyncEventState *sync_event;
5604};
5605
John Zulauf8eda1562021-04-13 17:06:41 -06005606ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07005607 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005608 auto *access_context = cb_context->GetCurrentAccessContext();
5609 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005610 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07005611 auto *events_context = cb_context->GetCurrentEventsContext();
5612 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005613 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07005614
5615 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5616 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5617 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5618 access_context->ResolvePreviousAccesses();
5619
John Zulaufd5115702021-01-18 12:34:33 -07005620 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5621 // 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 -07005622 size_t barrier_set_index = 0;
5623 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5624 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005625 for (auto &event_shared : events_) {
5626 if (!event_shared.get()) continue;
5627 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005628
John Zulauf4edde622021-02-15 08:54:50 -07005629 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005630
John Zulauf4edde622021-02-15 08:54:50 -07005631 const auto &barrier_set = barriers_[barrier_set_index];
5632 const auto &dst = barrier_set.dst_exec_scope;
5633 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005634 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5635 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5636 // of the barriers is maintained.
5637 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005638 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5639 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5640 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005641
5642 // Apply the global barrier to the event itself (for race condition tracking)
5643 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5644 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5645 sync_event->barriers |= dst.exec_scope;
5646 } else {
5647 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5648 sync_event->barriers = 0U;
5649 }
John Zulauf4edde622021-02-15 08:54:50 -07005650 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005651 }
5652
5653 // Apply the pending barriers
5654 ResolvePendingBarrierFunctor apply_pending_action(tag);
5655 access_context->ApplyToContext(apply_pending_action);
John Zulauf8eda1562021-04-13 17:06:41 -06005656
5657 return tag;
John Zulaufd5115702021-01-18 12:34:33 -07005658}
5659
John Zulauf8eda1562021-04-13 17:06:41 -06005660bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5661 CommandBufferAccessContext *active_context) const {
5662 return false;
5663}
5664
5665void SyncOpWaitEvents::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5666 CommandBufferAccessContext *active_context) const {}
5667
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005668bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5669 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5670 bool skip = false;
5671 const auto *cb_access_context = GetAccessContext(commandBuffer);
5672 assert(cb_access_context);
5673 if (!cb_access_context) return skip;
5674
5675 const auto *context = cb_access_context->GetCurrentAccessContext();
5676 assert(context);
5677 if (!context) return skip;
5678
5679 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5680
5681 if (dst_buffer) {
5682 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5683 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5684 if (hazard.hazard) {
5685 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5686 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5687 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06005688 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005689 }
5690 }
5691 return skip;
5692}
5693
John Zulauf669dfd52021-01-27 17:15:28 -07005694void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005695 events_.reserve(event_count);
5696 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005697 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005698 }
5699}
John Zulauf6ce24372021-01-30 05:56:25 -07005700
John Zulauf36ef9282021-02-02 11:47:24 -07005701SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005702 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005703 : SyncOpBase(cmd),
5704 event_(sync_state.GetShared<EVENT_STATE>(event)),
5705 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005706
5707bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005708 auto *events_context = cb_context.GetCurrentEventsContext();
5709 assert(events_context);
5710 bool skip = false;
5711 if (!events_context) return skip;
5712
5713 const auto &sync_state = cb_context.GetSyncState();
5714 const auto *sync_event = events_context->Get(event_);
5715 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5716
5717 const char *const set_wait =
5718 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5719 "hazards.";
5720 const char *message = set_wait; // Only one message this call.
5721 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5722 const char *vuid = nullptr;
5723 switch (sync_event->last_command) {
5724 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005725 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005726 // Needs a barrier between set and reset
5727 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5728 break;
John Zulauf4edde622021-02-15 08:54:50 -07005729 case CMD_WAITEVENTS:
5730 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005731 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5732 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5733 break;
5734 }
5735 default:
5736 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005737 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5738 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005739 break;
5740 }
5741 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005742 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
5743 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005744 CommandTypeString(sync_event->last_command));
5745 }
5746 }
5747 return skip;
5748}
5749
John Zulauf8eda1562021-04-13 17:06:41 -06005750ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
5751 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005752 auto *events_context = cb_context->GetCurrentEventsContext();
5753 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005754 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005755
5756 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06005757 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07005758
5759 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005760 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005761 sync_event->unsynchronized_set = CMD_NONE;
5762 sync_event->ResetFirstScope();
5763 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06005764
5765 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005766}
5767
John Zulauf8eda1562021-04-13 17:06:41 -06005768bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5769 CommandBufferAccessContext *active_context) const {
5770 return false;
5771}
5772
5773void SyncOpResetEvent::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5774 CommandBufferAccessContext *active_context) const {}
5775
John Zulauf36ef9282021-02-02 11:47:24 -07005776SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005777 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005778 : SyncOpBase(cmd),
5779 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005780 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5781 dep_info_() {}
5782
5783SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5784 const VkDependencyInfoKHR &dep_info)
5785 : SyncOpBase(cmd),
5786 event_(sync_state.GetShared<EVENT_STATE>(event)),
5787 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5788 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005789
5790bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5791 // 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 -07005792 bool skip = false;
5793
5794 const auto &sync_state = cb_context.GetSyncState();
5795 auto *events_context = cb_context.GetCurrentEventsContext();
5796 assert(events_context);
5797 if (!events_context) return skip;
5798
5799 const auto *sync_event = events_context->Get(event_);
5800 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5801
5802 const char *const reset_set =
5803 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5804 "hazards.";
5805 const char *const wait =
5806 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5807
5808 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07005809 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07005810 const char *message = nullptr;
5811 switch (sync_event->last_command) {
5812 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005813 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005814 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07005815 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07005816 message = reset_set;
5817 break;
5818 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005819 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005820 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07005821 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07005822 message = reset_set;
5823 break;
5824 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07005825 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005826 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07005827 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07005828 message = wait;
5829 break;
5830 default:
5831 // The only other valid last command that wasn't one.
5832 assert(sync_event->last_command == CMD_NONE);
5833 break;
5834 }
John Zulauf4edde622021-02-15 08:54:50 -07005835 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07005836 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07005837 std::string vuid("SYNC-");
5838 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005839 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
5840 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005841 CommandTypeString(sync_event->last_command));
5842 }
5843 }
5844
5845 return skip;
5846}
5847
John Zulauf8eda1562021-04-13 17:06:41 -06005848ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07005849 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005850 auto *events_context = cb_context->GetCurrentEventsContext();
5851 auto *access_context = cb_context->GetCurrentAccessContext();
5852 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005853 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005854
5855 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06005856 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07005857
5858 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5859 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5860 // any issues caused by naive scope setting here.
5861
5862 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5863 // Given:
5864 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5865 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5866
5867 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5868 sync_event->unsynchronized_set = sync_event->last_command;
5869 sync_event->ResetFirstScope();
5870 } else if (sync_event->scope.exec_scope == 0) {
5871 // We only set the scope if there isn't one
5872 sync_event->scope = src_exec_scope_;
5873
5874 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5875 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5876 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5877 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5878 }
5879 };
5880 access_context->ForAll(set_scope);
5881 sync_event->unsynchronized_set = CMD_NONE;
5882 sync_event->first_scope_tag = tag;
5883 }
John Zulauf4edde622021-02-15 08:54:50 -07005884 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
5885 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005886 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06005887
5888 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005889}
John Zulauf64ffe552021-02-06 10:25:07 -07005890
John Zulauf8eda1562021-04-13 17:06:41 -06005891bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5892 CommandBufferAccessContext *active_context) const {
5893 return false;
5894}
5895
5896void SyncOpSetEvent::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5897 CommandBufferAccessContext *active_context) const {}
5898
John Zulauf64ffe552021-02-06 10:25:07 -07005899SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
5900 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07005901 const VkSubpassBeginInfo *pSubpassBeginInfo)
5902 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07005903 if (pRenderPassBegin) {
5904 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
5905 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
5906 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
5907 if (fb_state) {
5908 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
5909 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
5910 // Note that this a safe to presist as long as shared_attachments is not cleared
5911 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08005912 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07005913 attachments_.emplace_back(attachment.get());
5914 }
5915 }
5916 if (pSubpassBeginInfo) {
5917 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
5918 }
5919 }
5920}
5921
5922bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5923 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
5924 bool skip = false;
5925
5926 assert(rp_state_.get());
5927 if (nullptr == rp_state_.get()) return skip;
5928 auto &rp_state = *rp_state_.get();
5929
5930 const uint32_t subpass = 0;
5931
5932 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
5933 // hasn't happened yet)
5934 const std::vector<AccessContext> empty_context_vector;
5935 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
5936 cb_context.GetCurrentAccessContext());
5937
5938 // Validate attachment operations
5939 if (attachments_.size() == 0) return skip;
5940 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07005941
5942 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
5943 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
5944 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
5945 // operations (though it's currently a messy approach)
5946 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
5947 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005948
5949 // Validate load operations if there were no layout transition hazards
5950 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07005951 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
5952 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005953 }
5954
5955 return skip;
5956}
5957
John Zulauf8eda1562021-04-13 17:06:41 -06005958ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5959 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07005960 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5961 assert(rp_state_.get());
John Zulauf8eda1562021-04-13 17:06:41 -06005962 if (nullptr == rp_state_.get()) return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07005963 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
John Zulauf8eda1562021-04-13 17:06:41 -06005964
5965 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07005966}
5967
John Zulauf8eda1562021-04-13 17:06:41 -06005968bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5969 CommandBufferAccessContext *active_context) const {
5970 return false;
5971}
5972
5973void SyncOpBeginRenderPass::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5974 CommandBufferAccessContext *active_context) const {}
5975
John Zulauf64ffe552021-02-06 10:25:07 -07005976SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07005977 const VkSubpassEndInfo *pSubpassEndInfo)
5978 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07005979 if (pSubpassBeginInfo) {
5980 subpass_begin_info_.initialize(pSubpassBeginInfo);
5981 }
5982 if (pSubpassEndInfo) {
5983 subpass_end_info_.initialize(pSubpassEndInfo);
5984 }
5985}
5986
5987bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
5988 bool skip = false;
5989 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5990 if (!renderpass_context) return skip;
5991
5992 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
5993 return skip;
5994}
5995
John Zulauf8eda1562021-04-13 17:06:41 -06005996ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07005997 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06005998 // TODO PHASE2 Need to fix renderpass tagging/segregation of barrier and access operations for QueueSubmit time validation
5999 auto prev_tag = cb_context->NextCommandTag(cmd_);
6000 auto next_tag = cb_context->NextSubcommandTag(cmd_);
6001
6002 cb_context->RecordNextSubpass(prev_tag, next_tag);
6003 // TODO PHASE2 This needs to be the tag of the barrier operations
6004 return prev_tag;
6005}
6006
6007bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6008 CommandBufferAccessContext *active_context) const {
6009 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07006010}
6011
sfricke-samsung85584a72021-09-30 21:43:38 -07006012SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
6013 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07006014 if (pSubpassEndInfo) {
6015 subpass_end_info_.initialize(pSubpassEndInfo);
6016 }
6017}
6018
John Zulauf8eda1562021-04-13 17:06:41 -06006019void SyncOpNextSubpass::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6020 CommandBufferAccessContext *active_context) const {}
6021
John Zulauf64ffe552021-02-06 10:25:07 -07006022bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
6023 bool skip = false;
6024 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
6025
6026 if (!renderpass_context) return skip;
6027 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
6028 return skip;
6029}
6030
John Zulauf8eda1562021-04-13 17:06:41 -06006031ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07006032 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06006033 const auto tag = cb_context->NextCommandTag(cmd_);
6034 cb_context->RecordEndRenderPass(tag);
6035 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07006036}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006037
John Zulauf8eda1562021-04-13 17:06:41 -06006038bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6039 CommandBufferAccessContext *active_context) const {
6040 return false;
6041}
6042
6043void SyncOpEndRenderPass::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
6044 CommandBufferAccessContext *active_context) const {}
6045
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07006046void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
6047 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
6048 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
6049 auto *cb_access_context = GetAccessContext(commandBuffer);
6050 assert(cb_access_context);
6051 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
6052 auto *context = cb_access_context->GetCurrentAccessContext();
6053 assert(context);
6054
6055 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
6056
6057 if (dst_buffer) {
6058 const ResourceAccessRange range = MakeRange(dstOffset, 4);
6059 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
6060 }
6061}
John Zulaufd05c5842021-03-26 11:32:16 -06006062
John Zulaufae842002021-04-15 18:20:55 -06006063bool SyncValidator::PreCallValidateCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6064 const VkCommandBuffer *pCommandBuffers) const {
6065 bool skip = StateTracker::PreCallValidateCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6066 const char *func_name = "vkCmdExecuteCommands";
6067 const auto *cb_context = GetAccessContext(commandBuffer);
6068 assert(cb_context);
6069 const auto *context = cb_context->GetCurrentAccessContext();
6070 assert(context);
6071
6072 // Make working copies of the access and events contexts
6073 // NOTE: Performance optimization for queue submit... only copy what the recorded contexts reference
6074 AccessContext proxy_context(*context);
6075 SyncEventsContext proxy_event_context(*cb_context->GetCurrentEventsContext());
6076
6077 for (uint32_t cb_index = 0; cb_index < commandBufferCount; ++cb_index) {
6078 const auto *recorded_cb_context = GetAccessContext(pCommandBuffers[cb_index]);
6079 if (!recorded_cb_context) continue;
6080 skip |= recorded_cb_context->ValidateFirstUse(*cb_context, &proxy_context, &proxy_event_context, func_name, cb_index);
6081 }
6082
6083 // NOTE: this is where we call the cb_contexts validate replay functionality
6084 return skip;
6085}
6086
6087void SyncValidator::PreCallRecordCmdExecuteCommands(VkCommandBuffer commandBuffer, uint32_t commandBufferCount,
6088 const VkCommandBuffer *pCommandBuffers) {
6089 StateTracker::PreCallRecordCmdExecuteCommands(commandBuffer, commandBufferCount, pCommandBuffers);
6090 // NOTE: this is where we call the cb_contexts replay record functionality
6091}
6092
John Zulaufd0ec59f2021-03-13 14:25:08 -07006093AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
6094 : view_(view), view_mask_(), gen_store_() {
6095 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
6096 const IMAGE_STATE &image_state = *view_->image_state.get();
6097 const auto base_address = ResourceBaseAddress(image_state);
6098 const auto *encoder = image_state.fragment_encoder.get();
6099 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06006100 // Get offset and extent for the view, accounting for possible depth slicing
6101 const VkOffset3D zero_offset = view->GetOffset();
6102 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07006103 // Intentional copy
6104 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
6105 view_mask_ = subres_range.aspectMask;
6106 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
6107 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6108
6109 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
6110 if (depth && (depth != view_mask_)) {
6111 subres_range.aspectMask = depth;
6112 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6113 }
6114 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
6115 if (stencil && (stencil != view_mask_)) {
6116 subres_range.aspectMask = stencil;
6117 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
6118 }
6119}
6120
6121const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
6122 const ImageRangeGen *got = nullptr;
6123 switch (gen_type) {
6124 case kViewSubresource:
6125 got = &gen_store_[kViewSubresource];
6126 break;
6127 case kRenderArea:
6128 got = &gen_store_[kRenderArea];
6129 break;
6130 case kDepthOnlyRenderArea:
6131 got =
6132 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
6133 break;
6134 case kStencilOnlyRenderArea:
6135 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
6136 : &gen_store_[Gen::kStencilOnlyRenderArea];
6137 break;
6138 default:
6139 assert(got);
6140 }
6141 return got;
6142}
6143
6144AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
6145 assert(IsValid());
6146 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
6147 if (depth_op) {
6148 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
6149 if (stencil_op) {
6150 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6151 return kRenderArea;
6152 }
6153 return kDepthOnlyRenderArea;
6154 }
6155 if (stencil_op) {
6156 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
6157 return kStencilOnlyRenderArea;
6158 }
6159
6160 assert(depth_op || stencil_op);
6161 return kRenderArea;
6162}
6163
6164AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06006165
6166void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6167 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6168 for (auto &event_pair : map_) {
6169 assert(event_pair.second); // Shouldn't be storing empty
6170 auto &sync_event = *event_pair.second;
6171 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6172 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6173 sync_event.barriers |= dst.exec_scope;
6174 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6175 }
6176 }
6177}