blob: f88efa7e2763f1d144261a0631203615102344c8 [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
285// Generate the ranges that are the intersection of range and the entries in the FilterMap
286template <typename FilterMap, typename KeyType = typename FilterMap::key_type>
287class FilteredRangeGenerator {
288 public:
John Zulaufd5115702021-01-18 12:34:33 -0700289 // Default constructed is safe to dereference for "empty" test, but for no other operation.
290 FilteredRangeGenerator() : range_(), filter_(nullptr), filter_pos_(), current_() {
291 // Default construction for KeyType *must* be empty range
292 assert(current_.empty());
293 }
John Zulauf4a6105a2020-11-17 15:11:05 -0700294 FilteredRangeGenerator(const FilterMap &filter, const KeyType &range)
295 : range_(range), filter_(&filter), filter_pos_(), current_() {
296 SeekBegin();
297 }
John Zulaufd5115702021-01-18 12:34:33 -0700298 FilteredRangeGenerator(const FilteredRangeGenerator &from) = default;
299
John Zulauf4a6105a2020-11-17 15:11:05 -0700300 const KeyType &operator*() const { return current_; }
301 const KeyType *operator->() const { return &current_; }
302 FilteredRangeGenerator &operator++() {
303 ++filter_pos_;
304 UpdateCurrent();
305 return *this;
306 }
307
308 bool operator==(const FilteredRangeGenerator &other) const { return current_ == other.current_; }
309
310 private:
John Zulauf4a6105a2020-11-17 15:11:05 -0700311 void UpdateCurrent() {
312 if (filter_pos_ != filter_->cend()) {
313 current_ = range_ & filter_pos_->first;
314 } else {
315 current_ = KeyType();
316 }
317 }
318 void SeekBegin() {
319 filter_pos_ = filter_->lower_bound(range_);
320 UpdateCurrent();
321 }
322 const KeyType range_;
323 const FilterMap *filter_;
324 typename FilterMap::const_iterator filter_pos_;
325 KeyType current_;
326};
John Zulaufd5115702021-01-18 12:34:33 -0700327using SingleAccessRangeGenerator = SingleRangeGenerator<ResourceAccessRange>;
John Zulauf4a6105a2020-11-17 15:11:05 -0700328using EventSimpleRangeGenerator = FilteredRangeGenerator<SyncEventState::ScopeMap>;
329
330// Templated to allow for different Range generators or map sources...
331
332// Generate the ranges that are the intersection of the RangeGen ranges and the entries in the FilterMap
John Zulauf4a6105a2020-11-17 15:11:05 -0700333template <typename FilterMap, typename RangeGen, typename KeyType = typename FilterMap::key_type>
334class FilteredGeneratorGenerator {
335 public:
John Zulaufd5115702021-01-18 12:34:33 -0700336 // Default constructed is safe to dereference for "empty" test, but for no other operation.
337 FilteredGeneratorGenerator() : filter_(nullptr), gen_(), filter_pos_(), current_() {
338 // Default construction for KeyType *must* be empty range
339 assert(current_.empty());
340 }
341 FilteredGeneratorGenerator(const FilterMap &filter, RangeGen &gen) : filter_(&filter), gen_(gen), filter_pos_(), current_() {
John Zulauf4a6105a2020-11-17 15:11:05 -0700342 SeekBegin();
343 }
John Zulaufd5115702021-01-18 12:34:33 -0700344 FilteredGeneratorGenerator(const FilteredGeneratorGenerator &from) = default;
John Zulauf4a6105a2020-11-17 15:11:05 -0700345 const KeyType &operator*() const { return current_; }
346 const KeyType *operator->() const { return &current_; }
347 FilteredGeneratorGenerator &operator++() {
348 KeyType gen_range = GenRange();
349 KeyType filter_range = FilterRange();
350 current_ = KeyType();
351 while (gen_range.non_empty() && filter_range.non_empty() && current_.empty()) {
352 if (gen_range.end > filter_range.end) {
353 // if the generated range is beyond the filter_range, advance the filter range
354 filter_range = AdvanceFilter();
355 } else {
356 gen_range = AdvanceGen();
357 }
358 current_ = gen_range & filter_range;
359 }
360 return *this;
361 }
362
363 bool operator==(const FilteredGeneratorGenerator &other) const { return current_ == other.current_; }
364
365 private:
366 KeyType AdvanceFilter() {
367 ++filter_pos_;
368 auto filter_range = FilterRange();
369 if (filter_range.valid()) {
370 FastForwardGen(filter_range);
371 }
372 return filter_range;
373 }
374 KeyType AdvanceGen() {
John Zulaufd5115702021-01-18 12:34:33 -0700375 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700376 auto gen_range = GenRange();
377 if (gen_range.valid()) {
378 FastForwardFilter(gen_range);
379 }
380 return gen_range;
381 }
382
383 KeyType FilterRange() const { return (filter_pos_ != filter_->cend()) ? filter_pos_->first : KeyType(); }
John Zulaufd5115702021-01-18 12:34:33 -0700384 KeyType GenRange() const { return *gen_; }
John Zulauf4a6105a2020-11-17 15:11:05 -0700385
386 KeyType FastForwardFilter(const KeyType &range) {
387 auto filter_range = FilterRange();
388 int retry_count = 0;
John Zulauf10f1f522020-12-18 12:00:35 -0700389 const static int kRetryLimit = 2; // TODO -- determine whether this limit is optimal
John Zulauf4a6105a2020-11-17 15:11:05 -0700390 while (!filter_range.empty() && (filter_range.end <= range.begin)) {
391 if (retry_count < kRetryLimit) {
392 ++filter_pos_;
393 filter_range = FilterRange();
394 retry_count++;
395 } else {
396 // Okay we've tried walking, do a seek.
397 filter_pos_ = filter_->lower_bound(range);
398 break;
399 }
400 }
401 return FilterRange();
402 }
403
404 // TODO: Consider adding "seek" (or an absolute bound "get" to range generators to make this walk
405 // faster.
406 KeyType FastForwardGen(const KeyType &range) {
407 auto gen_range = GenRange();
408 while (!gen_range.empty() && (gen_range.end <= range.begin)) {
John Zulaufd5115702021-01-18 12:34:33 -0700409 ++gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700410 gen_range = GenRange();
411 }
412 return gen_range;
413 }
414
415 void SeekBegin() {
416 auto gen_range = GenRange();
417 if (gen_range.empty()) {
418 current_ = KeyType();
419 filter_pos_ = filter_->cend();
420 } else {
421 filter_pos_ = filter_->lower_bound(gen_range);
422 current_ = gen_range & FilterRange();
423 }
424 }
425
John Zulauf4a6105a2020-11-17 15:11:05 -0700426 const FilterMap *filter_;
John Zulaufd5115702021-01-18 12:34:33 -0700427 RangeGen gen_;
John Zulauf4a6105a2020-11-17 15:11:05 -0700428 typename FilterMap::const_iterator filter_pos_;
429 KeyType current_;
430};
431
432using EventImageRangeGenerator = FilteredGeneratorGenerator<SyncEventState::ScopeMap, subresource_adapter::ImageRangeGenerator>;
433
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700434static const ResourceAccessRange kFullRange(std::numeric_limits<VkDeviceSize>::min(), std::numeric_limits<VkDeviceSize>::max());
John Zulauf5c5e88d2019-12-26 11:22:02 -0700435
John Zulauf3e86bf02020-09-12 10:47:57 -0600436ResourceAccessRange GetBufferRange(VkDeviceSize offset, VkDeviceSize buf_whole_size, uint32_t first_index, uint32_t count,
437 VkDeviceSize stride) {
438 VkDeviceSize range_start = offset + first_index * stride;
439 VkDeviceSize range_size = 0;
locke-lunargff255f92020-05-13 18:53:52 -0600440 if (count == UINT32_MAX) {
441 range_size = buf_whole_size - range_start;
442 } else {
443 range_size = count * stride;
444 }
John Zulauf3e86bf02020-09-12 10:47:57 -0600445 return MakeRange(range_start, range_size);
locke-lunargff255f92020-05-13 18:53:52 -0600446}
447
locke-lunarg654e3692020-06-04 17:19:15 -0600448SyncStageAccessIndex GetSyncStageAccessIndexsByDescriptorSet(VkDescriptorType descriptor_type, const interface_var &descriptor_data,
449 VkShaderStageFlagBits stage_flag) {
450 if (descriptor_type == VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT) {
451 assert(stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT);
452 return SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ;
453 }
454 auto stage_access = syncStageAccessMaskByShaderStage.find(stage_flag);
455 if (stage_access == syncStageAccessMaskByShaderStage.end()) {
456 assert(0);
457 }
458 if (descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER || descriptor_type == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) {
459 return stage_access->second.uniform_read;
460 }
461
462 // If the desriptorSet is writable, we don't need to care SHADER_READ. SHADER_WRITE is enough.
463 // Because if write hazard happens, read hazard might or might not happen.
464 // But if write hazard doesn't happen, read hazard is impossible to happen.
465 if (descriptor_data.is_writable) {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700466 return stage_access->second.storage_write;
locke-lunarg654e3692020-06-04 17:19:15 -0600467 }
Jeremy Gebben40a22942020-12-22 14:22:06 -0700468 // TODO: sampled_read
469 return stage_access->second.storage_read;
locke-lunarg654e3692020-06-04 17:19:15 -0600470}
471
locke-lunarg37047832020-06-12 13:44:45 -0600472bool IsImageLayoutDepthWritable(VkImageLayout image_layout) {
473 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
474 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_STENCIL_READ_ONLY_OPTIMAL ||
475 image_layout == VK_IMAGE_LAYOUT_DEPTH_ATTACHMENT_OPTIMAL)
476 ? true
477 : false;
478}
479
480bool IsImageLayoutStencilWritable(VkImageLayout image_layout) {
481 return (image_layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL ||
482 image_layout == VK_IMAGE_LAYOUT_DEPTH_READ_ONLY_STENCIL_ATTACHMENT_OPTIMAL ||
483 image_layout == VK_IMAGE_LAYOUT_STENCIL_ATTACHMENT_OPTIMAL)
484 ? true
485 : false;
486}
487
John Zulauf355e49b2020-04-24 15:11:15 -0600488// Class AccessContext stores the state of accesses specific to a Command, Subpass, or Queue
John Zulaufb02c1eb2020-10-06 16:33:36 -0600489template <typename Action>
490static void ApplyOverImageRange(const IMAGE_STATE &image_state, const VkImageSubresourceRange &subresource_range_arg,
491 Action &action) {
492 // At this point the "apply over range" logic only supports a single memory binding
493 if (!SimpleBinding(image_state)) return;
494 auto subresource_range = NormalizeSubresourceRange(image_state.createInfo, subresource_range_arg);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600495 const auto base_address = ResourceBaseAddress(image_state);
John Zulauf150e5332020-12-03 08:52:52 -0700496 subresource_adapter::ImageRangeGenerator range_gen(*image_state.fragment_encoder.get(), subresource_range, {0, 0, 0},
497 image_state.createInfo.extent, base_address);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600498 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -0700499 action(*range_gen);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600500 }
501}
502
John Zulauf7635de32020-05-29 17:14:15 -0600503// Tranverse the attachment resolves for this a specific subpass, and do action() to them.
504// Used by both validation and record operations
505//
506// The signature for Action() reflect the needs of both uses.
507template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700508void ResolveOperation(Action &action, const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
509 uint32_t subpass) {
John Zulauf7635de32020-05-29 17:14:15 -0600510 const auto &rp_ci = rp_state.createInfo;
511 const auto *attachment_ci = rp_ci.pAttachments;
512 const auto &subpass_ci = rp_ci.pSubpasses[subpass];
513
514 // Color resolves -- require an inuse color attachment and a matching inuse resolve attachment
515 const auto *color_attachments = subpass_ci.pColorAttachments;
516 const auto *color_resolve = subpass_ci.pResolveAttachments;
517 if (color_resolve && color_attachments) {
518 for (uint32_t i = 0; i < subpass_ci.colorAttachmentCount; i++) {
519 const auto &color_attach = color_attachments[i].attachment;
520 const auto &resolve_attach = subpass_ci.pResolveAttachments[i].attachment;
521 if ((color_attach != VK_ATTACHMENT_UNUSED) && (resolve_attach != VK_ATTACHMENT_UNUSED)) {
522 action("color", "resolve read", color_attach, resolve_attach, attachment_views[color_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700523 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ,
524 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600525 action("color", "resolve write", color_attach, resolve_attach, attachment_views[resolve_attach],
John Zulaufd0ec59f2021-03-13 14:25:08 -0700526 AttachmentViewGen::Gen::kRenderArea, SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE,
527 SyncOrdering::kColorAttachment);
John Zulauf7635de32020-05-29 17:14:15 -0600528 }
529 }
530 }
531
532 // Depth stencil resolve only if the extension is present
Mark Lobodzinski1f887d32020-12-30 15:31:33 -0700533 const auto ds_resolve = LvlFindInChain<VkSubpassDescriptionDepthStencilResolve>(subpass_ci.pNext);
John Zulauf7635de32020-05-29 17:14:15 -0600534 if (ds_resolve && ds_resolve->pDepthStencilResolveAttachment &&
535 (ds_resolve->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED) && subpass_ci.pDepthStencilAttachment &&
536 (subpass_ci.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)) {
537 const auto src_at = subpass_ci.pDepthStencilAttachment->attachment;
538 const auto src_ci = attachment_ci[src_at];
539 // The formats are required to match so we can pick either
540 const bool resolve_depth = (ds_resolve->depthResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasDepth(src_ci.format);
541 const bool resolve_stencil = (ds_resolve->stencilResolveMode != VK_RESOLVE_MODE_NONE) && FormatHasStencil(src_ci.format);
542 const auto dst_at = ds_resolve->pDepthStencilResolveAttachment->attachment;
John Zulauf7635de32020-05-29 17:14:15 -0600543
544 // Figure out which aspects are actually touched during resolve operations
545 const char *aspect_string = nullptr;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700546 AttachmentViewGen::Gen gen_type = AttachmentViewGen::Gen::kRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600547 if (resolve_depth && resolve_stencil) {
John Zulauf7635de32020-05-29 17:14:15 -0600548 aspect_string = "depth/stencil";
549 } else if (resolve_depth) {
550 // Validate depth only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700551 gen_type = AttachmentViewGen::Gen::kDepthOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600552 aspect_string = "depth";
553 } else if (resolve_stencil) {
554 // Validate all stencil only
John Zulaufd0ec59f2021-03-13 14:25:08 -0700555 gen_type = AttachmentViewGen::Gen::kStencilOnlyRenderArea;
John Zulauf7635de32020-05-29 17:14:15 -0600556 aspect_string = "stencil";
557 }
558
John Zulaufd0ec59f2021-03-13 14:25:08 -0700559 if (aspect_string) {
560 action(aspect_string, "resolve read", src_at, dst_at, attachment_views[src_at], gen_type,
561 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ, SyncOrdering::kRaster);
562 action(aspect_string, "resolve write", src_at, dst_at, attachment_views[dst_at], gen_type,
563 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulauf7635de32020-05-29 17:14:15 -0600564 }
565 }
566}
567
568// Action for validating resolve operations
569class ValidateResolveAction {
570 public:
John Zulauffaea0ee2021-01-14 14:01:32 -0700571 ValidateResolveAction(VkRenderPass render_pass, uint32_t subpass, const AccessContext &context,
John Zulauf64ffe552021-02-06 10:25:07 -0700572 const CommandExecutionContext &ex_context, const char *func_name)
John Zulauf7635de32020-05-29 17:14:15 -0600573 : render_pass_(render_pass),
574 subpass_(subpass),
575 context_(context),
John Zulauf64ffe552021-02-06 10:25:07 -0700576 ex_context_(ex_context),
John Zulauf7635de32020-05-29 17:14:15 -0600577 func_name_(func_name),
578 skip_(false) {}
579 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 -0700580 const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage,
581 SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600582 HazardResult hazard;
John Zulaufd0ec59f2021-03-13 14:25:08 -0700583 hazard = context_.DetectHazard(view_gen, gen_type, current_usage, ordering_rule);
John Zulauf7635de32020-05-29 17:14:15 -0600584 if (hazard.hazard) {
John Zulauffaea0ee2021-01-14 14:01:32 -0700585 skip_ |=
John Zulauf64ffe552021-02-06 10:25:07 -0700586 ex_context_.GetSyncState().LogError(render_pass_, string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700587 "%s: Hazard %s in subpass %" PRIu32 "during %s %s, from attachment %" PRIu32
588 " to resolve attachment %" PRIu32 ". Access info %s.",
589 func_name_, string_SyncHazard(hazard.hazard), subpass_, aspect_name,
John Zulauf64ffe552021-02-06 10:25:07 -0700590 attachment_name, src_at, dst_at, ex_context_.FormatUsage(hazard).c_str());
John Zulauf7635de32020-05-29 17:14:15 -0600591 }
592 }
593 // Providing a mechanism for the constructing caller to get the result of the validation
594 bool GetSkip() const { return skip_; }
595
596 private:
597 VkRenderPass render_pass_;
598 const uint32_t subpass_;
599 const AccessContext &context_;
John Zulauf64ffe552021-02-06 10:25:07 -0700600 const CommandExecutionContext &ex_context_;
John Zulauf7635de32020-05-29 17:14:15 -0600601 const char *func_name_;
602 bool skip_;
603};
604
605// Update action for resolve operations
606class UpdateStateResolveAction {
607 public:
John Zulauf14940722021-04-12 15:19:02 -0600608 UpdateStateResolveAction(AccessContext &context, ResourceUsageTag tag) : context_(context), tag_(tag) {}
John Zulaufd0ec59f2021-03-13 14:25:08 -0700609 void operator()(const char *, const char *, uint32_t, uint32_t, const AttachmentViewGen &view_gen,
610 AttachmentViewGen::Gen gen_type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) {
John Zulauf7635de32020-05-29 17:14:15 -0600611 // Ignores validation only arguments...
John Zulaufd0ec59f2021-03-13 14:25:08 -0700612 context_.UpdateAccessState(view_gen, gen_type, current_usage, ordering_rule, tag_);
John Zulauf7635de32020-05-29 17:14:15 -0600613 }
614
615 private:
616 AccessContext &context_;
John Zulauf14940722021-04-12 15:19:02 -0600617 const ResourceUsageTag tag_;
John Zulauf7635de32020-05-29 17:14:15 -0600618};
619
John Zulauf59e25072020-07-17 10:55:21 -0600620void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
John Zulauf14940722021-04-12 15:19:02 -0600621 const SyncStageAccessFlags &prior_, const ResourceUsageTag tag_) {
John Zulauf59e25072020-07-17 10:55:21 -0600622 access_state = std::unique_ptr<const ResourceAccessState>(new ResourceAccessState(*access_state_));
623 usage_index = usage_index_;
624 hazard = hazard_;
625 prior_access = prior_;
626 tag = tag_;
627}
628
John Zulauf540266b2020-04-06 18:54:53 -0600629AccessContext::AccessContext(uint32_t subpass, VkQueueFlags queue_flags,
630 const std::vector<SubpassDependencyGraphNode> &dependencies,
John Zulauf1a224292020-06-30 14:52:13 -0600631 const std::vector<AccessContext> &contexts, const AccessContext *external_context) {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600632 Reset();
633 const auto &subpass_dep = dependencies[subpass];
John Zulauf22aefed2021-03-11 18:14:35 -0700634 bool has_barrier_from_external = subpass_dep.barrier_from_external.size() > 0U;
635 prev_.reserve(subpass_dep.prev.size() + (has_barrier_from_external ? 1U : 0U));
John Zulauf355e49b2020-04-24 15:11:15 -0600636 prev_by_subpass_.resize(subpass, nullptr); // Can't be more prevs than the subpass we're on
John Zulauf3d84f1b2020-03-09 13:33:25 -0600637 for (const auto &prev_dep : subpass_dep.prev) {
John Zulaufbaea94f2020-09-15 17:55:16 -0600638 const auto prev_pass = prev_dep.first->pass;
639 const auto &prev_barriers = prev_dep.second;
640 assert(prev_dep.second.size());
641 prev_.emplace_back(&contexts[prev_pass], queue_flags, prev_barriers);
642 prev_by_subpass_[prev_pass] = &prev_.back();
John Zulauf5c5e88d2019-12-26 11:22:02 -0700643 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600644
645 async_.reserve(subpass_dep.async.size());
646 for (const auto async_subpass : subpass_dep.async) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700647 async_.emplace_back(&contexts[async_subpass]);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600648 }
John Zulauf22aefed2021-03-11 18:14:35 -0700649 if (has_barrier_from_external) {
650 // Store the barrier from external with the reat, but save pointer for "by subpass" lookups.
651 prev_.emplace_back(external_context, queue_flags, subpass_dep.barrier_from_external);
652 src_external_ = &prev_.back();
John Zulaufe5da6e52020-03-18 15:32:18 -0600653 }
John Zulaufbaea94f2020-09-15 17:55:16 -0600654 if (subpass_dep.barrier_to_external.size()) {
655 dst_external_ = TrackBack(this, queue_flags, subpass_dep.barrier_to_external);
John Zulauf3d84f1b2020-03-09 13:33:25 -0600656 }
John Zulauf5c5e88d2019-12-26 11:22:02 -0700657}
658
John Zulauf5f13a792020-03-10 07:31:21 -0600659template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700660HazardResult AccessContext::DetectPreviousHazard(AccessAddressType type, const Detector &detector,
John Zulauf540266b2020-04-06 18:54:53 -0600661 const ResourceAccessRange &range) const {
John Zulauf5f13a792020-03-10 07:31:21 -0600662 ResourceAccessRangeMap descent_map;
John Zulauf69133422020-05-20 14:55:53 -0600663 ResolvePreviousAccess(type, range, &descent_map, nullptr);
John Zulauf5f13a792020-03-10 07:31:21 -0600664
665 HazardResult hazard;
666 for (auto prev = descent_map.begin(); prev != descent_map.end() && !hazard.hazard; ++prev) {
667 hazard = detector.Detect(prev);
668 }
669 return hazard;
670}
671
John Zulauf4a6105a2020-11-17 15:11:05 -0700672template <typename Action>
673void AccessContext::ForAll(Action &&action) {
674 for (const auto address_type : kAddressTypes) {
675 auto &accesses = GetAccessStateMap(address_type);
676 for (const auto &access : accesses) {
677 action(address_type, access);
678 }
679 }
680}
681
John Zulauf3d84f1b2020-03-09 13:33:25 -0600682// A recursive range walker for hazard detection, first for the current context and the (DetectHazardRecur) to walk
683// the DAG of the contexts (for example subpasses)
684template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700685HazardResult AccessContext::DetectHazard(AccessAddressType type, const Detector &detector, const ResourceAccessRange &range,
John Zulauf355e49b2020-04-24 15:11:15 -0600686 DetectOptions options) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -0600687 HazardResult hazard;
John Zulauf5f13a792020-03-10 07:31:21 -0600688
John Zulauf1a224292020-06-30 14:52:13 -0600689 if (static_cast<uint32_t>(options) & DetectOptions::kDetectAsync) {
John Zulauf355e49b2020-04-24 15:11:15 -0600690 // Async checks don't require recursive lookups, as the async lists are exhaustive for the top-level context
691 // so we'll check these first
692 for (const auto &async_context : async_) {
693 hazard = async_context->DetectAsyncHazard(type, detector, range);
694 if (hazard.hazard) return hazard;
695 }
John Zulauf5f13a792020-03-10 07:31:21 -0600696 }
697
John Zulauf1a224292020-06-30 14:52:13 -0600698 const bool detect_prev = (static_cast<uint32_t>(options) & DetectOptions::kDetectPrevious) != 0;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600699
John Zulauf69133422020-05-20 14:55:53 -0600700 const auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600701 const auto the_end = accesses.cend(); // End is not invalidated
702 auto pos = accesses.lower_bound(range);
John Zulauf69133422020-05-20 14:55:53 -0600703 ResourceAccessRange gap = {range.begin, range.begin};
John Zulauf5f13a792020-03-10 07:31:21 -0600704
John Zulauf3cafbf72021-03-26 16:55:19 -0600705 while (pos != the_end && pos->first.begin < range.end) {
John Zulauf69133422020-05-20 14:55:53 -0600706 // Cover any leading gap, or gap between entries
707 if (detect_prev) {
708 // TODO: After profiling we may want to change the descent logic such that we don't recur per gap...
709 // Cover any leading gap, or gap between entries
710 gap.end = pos->first.begin; // We know this begin is < range.end
John Zulauf355e49b2020-04-24 15:11:15 -0600711 if (gap.non_empty()) {
John Zulauf69133422020-05-20 14:55:53 -0600712 // Recur on all gaps
John Zulauf16adfc92020-04-08 10:28:33 -0600713 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf5f13a792020-03-10 07:31:21 -0600714 if (hazard.hazard) return hazard;
715 }
John Zulauf69133422020-05-20 14:55:53 -0600716 // Set up for the next gap. If pos..end is >= range.end, loop will exit, and trailing gap will be empty
717 gap.begin = pos->first.end;
718 }
719
720 hazard = detector.Detect(pos);
721 if (hazard.hazard) return hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600722 ++pos;
John Zulauf69133422020-05-20 14:55:53 -0600723 }
724
725 if (detect_prev) {
726 // Detect in the trailing empty as needed
727 gap.end = range.end;
728 if (gap.non_empty()) {
729 hazard = DetectPreviousHazard(type, detector, gap);
John Zulauf16adfc92020-04-08 10:28:33 -0600730 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600731 }
732
733 return hazard;
734}
735
736// A non recursive range walker for the asynchronous contexts (those we have no barriers with)
737template <typename Detector>
John Zulauf43cc7462020-12-03 12:33:12 -0700738HazardResult AccessContext::DetectAsyncHazard(AccessAddressType type, const Detector &detector,
739 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -0600740 auto &accesses = GetAccessStateMap(type);
John Zulauf3cafbf72021-03-26 16:55:19 -0600741 auto pos = accesses.lower_bound(range);
742 const auto the_end = accesses.end();
John Zulauf16adfc92020-04-08 10:28:33 -0600743
John Zulauf3d84f1b2020-03-09 13:33:25 -0600744 HazardResult hazard;
John Zulauf3cafbf72021-03-26 16:55:19 -0600745 while (pos != the_end && pos->first.begin < range.end) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -0700746 hazard = detector.DetectAsync(pos, start_tag_);
John Zulauf3cafbf72021-03-26 16:55:19 -0600747 if (hazard.hazard) break;
748 ++pos;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600749 }
John Zulauf16adfc92020-04-08 10:28:33 -0600750
John Zulauf3d84f1b2020-03-09 13:33:25 -0600751 return hazard;
752}
753
John Zulaufb02c1eb2020-10-06 16:33:36 -0600754struct ApplySubpassTransitionBarriersAction {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -0700755 explicit ApplySubpassTransitionBarriersAction(const std::vector<SyncBarrier> &barriers_) : barriers(barriers_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600756 void operator()(ResourceAccessState *access) const {
757 assert(access);
758 access->ApplyBarriers(barriers, true);
759 }
760 const std::vector<SyncBarrier> &barriers;
761};
762
John Zulauf22aefed2021-03-11 18:14:35 -0700763struct ApplyTrackbackStackAction {
764 explicit ApplyTrackbackStackAction(const std::vector<SyncBarrier> &barriers_,
765 const ResourceAccessStateFunction *previous_barrier_ = nullptr)
766 : barriers(barriers_), previous_barrier(previous_barrier_) {}
John Zulaufb02c1eb2020-10-06 16:33:36 -0600767 void operator()(ResourceAccessState *access) const {
768 assert(access);
769 assert(!access->HasPendingState());
770 access->ApplyBarriers(barriers, false);
771 access->ApplyPendingBarriers(kCurrentCommandTag);
John Zulauf22aefed2021-03-11 18:14:35 -0700772 if (previous_barrier) {
773 assert(bool(*previous_barrier));
774 (*previous_barrier)(access);
775 }
John Zulaufb02c1eb2020-10-06 16:33:36 -0600776 }
777 const std::vector<SyncBarrier> &barriers;
John Zulauf22aefed2021-03-11 18:14:35 -0700778 const ResourceAccessStateFunction *previous_barrier;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600779};
780
781// Splits a single map entry into piece matching the entries in [first, last) the total range over [first, last) must be
782// contained with entry. Entry must be an iterator pointing to dest, first and last must be iterators pointing to a
783// *different* map from dest.
784// Returns the position past the last resolved range -- the entry covering the remainder of entry->first not included in the
785// range [first, last)
786template <typename BarrierAction>
John Zulauf355e49b2020-04-24 15:11:15 -0600787static void ResolveMapToEntry(ResourceAccessRangeMap *dest, ResourceAccessRangeMap::iterator entry,
788 ResourceAccessRangeMap::const_iterator first, ResourceAccessRangeMap::const_iterator last,
John Zulaufb02c1eb2020-10-06 16:33:36 -0600789 BarrierAction &barrier_action) {
John Zulauf355e49b2020-04-24 15:11:15 -0600790 auto at = entry;
791 for (auto pos = first; pos != last; ++pos) {
792 // Every member of the input iterator range must fit within the remaining portion of entry
793 assert(at->first.includes(pos->first));
794 assert(at != dest->end());
795 // Trim up at to the same size as the entry to resolve
796 at = sparse_container::split(at, *dest, pos->first);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600797 auto access = pos->second; // intentional copy
798 barrier_action(&access);
John Zulauf355e49b2020-04-24 15:11:15 -0600799 at->second.Resolve(access);
800 ++at; // Go to the remaining unused section of entry
801 }
802}
803
John Zulaufa0a98292020-09-18 09:30:10 -0600804static SyncBarrier MergeBarriers(const std::vector<SyncBarrier> &barriers) {
805 SyncBarrier merged = {};
806 for (const auto &barrier : barriers) {
807 merged.Merge(barrier);
808 }
809 return merged;
810}
811
John Zulaufb02c1eb2020-10-06 16:33:36 -0600812template <typename BarrierAction>
John Zulauf43cc7462020-12-03 12:33:12 -0700813void AccessContext::ResolveAccessRange(AccessAddressType type, const ResourceAccessRange &range, BarrierAction &barrier_action,
John Zulauf355e49b2020-04-24 15:11:15 -0600814 ResourceAccessRangeMap *resolve_map, const ResourceAccessState *infill_state,
815 bool recur_to_infill) const {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600816 if (!range.non_empty()) return;
817
John Zulauf355e49b2020-04-24 15:11:15 -0600818 ResourceRangeMergeIterator current(*resolve_map, GetAccessStateMap(type), range.begin);
819 while (current->range.non_empty() && range.includes(current->range.begin)) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600820 const auto current_range = current->range & range;
John Zulauf16adfc92020-04-08 10:28:33 -0600821 if (current->pos_B->valid) {
822 const auto &src_pos = current->pos_B->lower_bound;
John Zulaufb02c1eb2020-10-06 16:33:36 -0600823 auto access = src_pos->second; // intentional copy
824 barrier_action(&access);
825
John Zulauf16adfc92020-04-08 10:28:33 -0600826 if (current->pos_A->valid) {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600827 const auto trimmed = sparse_container::split(current->pos_A->lower_bound, *resolve_map, current_range);
828 trimmed->second.Resolve(access);
829 current.invalidate_A(trimmed);
John Zulauf5f13a792020-03-10 07:31:21 -0600830 } else {
John Zulauf3bcab5e2020-06-19 14:42:32 -0600831 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current_range, access));
John Zulauf355e49b2020-04-24 15:11:15 -0600832 current.invalidate_A(inserted); // Update the parallel iterator to point at the insert segment
John Zulauf5f13a792020-03-10 07:31:21 -0600833 }
John Zulauf16adfc92020-04-08 10:28:33 -0600834 } else {
835 // we have to descend to fill this gap
836 if (recur_to_infill) {
John Zulauf22aefed2021-03-11 18:14:35 -0700837 ResourceAccessRange recurrence_range = current_range;
838 // The current context is empty for the current range, so recur to fill the gap.
839 // Since we will be recurring back up the DAG, expand the gap descent to cover the full range for which B
840 // is not valid, to minimize that recurrence
841 if (current->pos_B.at_end()) {
842 // Do the remainder here....
843 recurrence_range.end = range.end;
John Zulauf355e49b2020-04-24 15:11:15 -0600844 } else {
John Zulauf22aefed2021-03-11 18:14:35 -0700845 // Recur only over the range until B becomes valid (within the limits of range).
846 recurrence_range.end = std::min(range.end, current->pos_B->lower_bound->first.begin);
John Zulauf355e49b2020-04-24 15:11:15 -0600847 }
John Zulauf22aefed2021-03-11 18:14:35 -0700848 ResolvePreviousAccessStack(type, recurrence_range, resolve_map, infill_state, barrier_action);
849
John Zulauf355e49b2020-04-24 15:11:15 -0600850 // Given that there could be gaps we need to seek carefully to not repeatedly search the same gaps in the next
851 // iterator of the outer while.
852
853 // Set the parallel iterator to the end of this range s.t. ++ will move us to the next range whether or
854 // not the end of the range is a gap. For the seek to work, first we need to warn the parallel iterator
855 // we stepped on the dest map
John Zulauf22aefed2021-03-11 18:14:35 -0700856 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 -0600857 current.invalidate_A(); // Changes current->range
John Zulauf355e49b2020-04-24 15:11:15 -0600858 current.seek(seek_to);
859 } else if (!current->pos_A->valid && infill_state) {
860 // If we didn't find anything in the current range, and we aren't reccuring... we infill if required
861 auto inserted = resolve_map->insert(current->pos_A->lower_bound, std::make_pair(current->range, *infill_state));
862 current.invalidate_A(inserted); // Update the parallel iterator to point at the correct segment after insert
John Zulauf16adfc92020-04-08 10:28:33 -0600863 }
John Zulauf5f13a792020-03-10 07:31:21 -0600864 }
John Zulauf16adfc92020-04-08 10:28:33 -0600865 ++current;
John Zulauf3d84f1b2020-03-09 13:33:25 -0600866 }
John Zulauf1a224292020-06-30 14:52:13 -0600867
868 // Infill if range goes passed both the current and resolve map prior contents
869 if (recur_to_infill && (current->range.end < range.end)) {
870 ResourceAccessRange trailing_fill_range = {current->range.end, range.end};
John Zulauf22aefed2021-03-11 18:14:35 -0700871 ResolvePreviousAccessStack<BarrierAction>(type, trailing_fill_range, resolve_map, infill_state, barrier_action);
John Zulauf1a224292020-06-30 14:52:13 -0600872 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600873}
874
John Zulauf22aefed2021-03-11 18:14:35 -0700875template <typename BarrierAction>
876void AccessContext::ResolvePreviousAccessStack(AccessAddressType type, const ResourceAccessRange &range,
877 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
878 const BarrierAction &previous_barrier) const {
879 ResourceAccessStateFunction stacked_barrier(std::ref(previous_barrier));
880 ResolvePreviousAccess(type, range, descent_map, infill_state, &stacked_barrier);
881}
882
John Zulauf43cc7462020-12-03 12:33:12 -0700883void AccessContext::ResolvePreviousAccess(AccessAddressType type, const ResourceAccessRange &range,
John Zulauf22aefed2021-03-11 18:14:35 -0700884 ResourceAccessRangeMap *descent_map, const ResourceAccessState *infill_state,
885 const ResourceAccessStateFunction *previous_barrier) const {
886 if (prev_.size() == 0) {
John Zulauf5f13a792020-03-10 07:31:21 -0600887 if (range.non_empty() && infill_state) {
John Zulauf22aefed2021-03-11 18:14:35 -0700888 // Fill the empty poritions of descent_map with the default_state with the barrier function applied (iff present)
889 ResourceAccessState state_copy;
890 if (previous_barrier) {
891 assert(bool(*previous_barrier));
892 state_copy = *infill_state;
893 (*previous_barrier)(&state_copy);
894 infill_state = &state_copy;
895 }
896 sparse_container::update_range_value(*descent_map, range, *infill_state,
897 sparse_container::value_precedence::prefer_dest);
John Zulauf5f13a792020-03-10 07:31:21 -0600898 }
899 } else {
900 // Look for something to fill the gap further along.
901 for (const auto &prev_dep : prev_) {
John Zulauf22aefed2021-03-11 18:14:35 -0700902 const ApplyTrackbackStackAction barrier_action(prev_dep.barriers, previous_barrier);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600903 prev_dep.context->ResolveAccessRange(type, range, barrier_action, descent_map, infill_state);
John Zulauf5f13a792020-03-10 07:31:21 -0600904 }
John Zulauf5f13a792020-03-10 07:31:21 -0600905 }
John Zulauf3d84f1b2020-03-09 13:33:25 -0600906}
907
John Zulauf4a6105a2020-11-17 15:11:05 -0700908// Non-lazy import of all accesses, WaitEvents needs this.
909void AccessContext::ResolvePreviousAccesses() {
910 ResourceAccessState default_state;
John Zulauf22aefed2021-03-11 18:14:35 -0700911 if (!prev_.size()) return; // If no previous contexts, nothing to do
912
John Zulauf4a6105a2020-11-17 15:11:05 -0700913 for (const auto address_type : kAddressTypes) {
914 ResolvePreviousAccess(address_type, kFullRange, &GetAccessStateMap(address_type), &default_state);
915 }
916}
917
John Zulauf43cc7462020-12-03 12:33:12 -0700918AccessAddressType AccessContext::ImageAddressType(const IMAGE_STATE &image) {
919 return (image.fragment_encoder->IsLinearImage()) ? AccessAddressType::kLinear : AccessAddressType::kIdealized;
John Zulauf16adfc92020-04-08 10:28:33 -0600920}
921
John Zulauf1507ee42020-05-18 11:33:09 -0600922static SyncStageAccessIndex ColorLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -0600923 const auto stage_access = (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
924 ? SYNC_ACCESS_INDEX_NONE
925 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ
926 : SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -0600927 return stage_access;
928}
929static SyncStageAccessIndex DepthStencilLoadUsage(VkAttachmentLoadOp load_op) {
John Zulauf57261402021-08-13 11:32:06 -0600930 const auto stage_access =
931 (load_op == VK_ATTACHMENT_LOAD_OP_NONE_EXT)
932 ? SYNC_ACCESS_INDEX_NONE
933 : ((load_op == VK_ATTACHMENT_LOAD_OP_LOAD) ? SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ
934 : SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE);
John Zulauf1507ee42020-05-18 11:33:09 -0600935 return stage_access;
936}
937
John Zulauf7635de32020-05-29 17:14:15 -0600938// Caller must manage returned pointer
939static AccessContext *CreateStoreResolveProxyContext(const AccessContext &context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700940 uint32_t subpass, const AttachmentViewGenVector &attachment_views) {
John Zulauf7635de32020-05-29 17:14:15 -0600941 auto *proxy = new AccessContext(context);
John Zulaufd0ec59f2021-03-13 14:25:08 -0700942 proxy->UpdateAttachmentResolveAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
943 proxy->UpdateAttachmentStoreAccess(rp_state, attachment_views, subpass, kCurrentCommandTag);
John Zulauf7635de32020-05-29 17:14:15 -0600944 return proxy;
945}
946
John Zulaufb02c1eb2020-10-06 16:33:36 -0600947template <typename BarrierAction>
John Zulaufd0ec59f2021-03-13 14:25:08 -0700948void AccessContext::ResolveAccessRange(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
949 BarrierAction &barrier_action, ResourceAccessRangeMap *descent_map,
950 const ResourceAccessState *infill_state) const {
951 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
952 if (!attachment_gen) return;
953
954 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
955 const AccessAddressType address_type = view_gen.GetAddressType();
956 for (; range_gen->non_empty(); ++range_gen) {
957 ResolveAccessRange(address_type, *range_gen, barrier_action, descent_map, infill_state);
John Zulaufb02c1eb2020-10-06 16:33:36 -0600958 }
John Zulauf62f10592020-04-03 12:20:02 -0600959}
960
John Zulauf7635de32020-05-29 17:14:15 -0600961// Layout transitions are handled as if the were occuring in the beginning of the next subpass
John Zulauf64ffe552021-02-06 10:25:07 -0700962bool AccessContext::ValidateLayoutTransitions(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -0600963 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -0700964 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -0600965 bool skip = false;
John Zulauf7635de32020-05-29 17:14:15 -0600966 // As validation methods are const and precede the record/update phase, for any tranistions from the immediately
967 // previous subpass, we have to validate them against a copy of the AccessContext, with resolve operations applied, as
968 // those affects have not been recorded yet.
969 //
970 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
971 // to apply and only copy then, if this proves a hot spot.
972 std::unique_ptr<AccessContext> proxy_for_prev;
973 TrackBack proxy_track_back;
974
John Zulauf355e49b2020-04-24 15:11:15 -0600975 const auto &transitions = rp_state.subpass_transitions[subpass];
976 for (const auto &transition : transitions) {
John Zulauf7635de32020-05-29 17:14:15 -0600977 const bool prev_needs_proxy = transition.prev_pass != VK_SUBPASS_EXTERNAL && (transition.prev_pass + 1 == subpass);
978
979 const auto *track_back = GetTrackBackFromSubpass(transition.prev_pass);
John Zulauf22aefed2021-03-11 18:14:35 -0700980 assert(track_back);
John Zulauf7635de32020-05-29 17:14:15 -0600981 if (prev_needs_proxy) {
982 if (!proxy_for_prev) {
John Zulaufd0ec59f2021-03-13 14:25:08 -0700983 proxy_for_prev.reset(
984 CreateStoreResolveProxyContext(*track_back->context, rp_state, transition.prev_pass, attachment_views));
John Zulauf7635de32020-05-29 17:14:15 -0600985 proxy_track_back = *track_back;
986 proxy_track_back.context = proxy_for_prev.get();
987 }
988 track_back = &proxy_track_back;
989 }
990 auto hazard = DetectSubpassTransitionHazard(*track_back, attachment_views[transition.attachment]);
John Zulauf355e49b2020-04-24 15:11:15 -0600991 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -0600992 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -0700993 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
994 " image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
995 func_name, string_SyncHazard(hazard.hazard), subpass, transition.attachment,
996 string_VkImageLayout(transition.old_layout),
997 string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -0700998 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -0600999 }
1000 }
1001 return skip;
1002}
1003
John Zulauf64ffe552021-02-06 10:25:07 -07001004bool AccessContext::ValidateLoadOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulauf7635de32020-05-29 17:14:15 -06001005 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001006 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulauf1507ee42020-05-18 11:33:09 -06001007 bool skip = false;
1008 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufa0a98292020-09-18 09:30:10 -06001009
John Zulauf1507ee42020-05-18 11:33:09 -06001010 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1011 if (subpass == rp_state.attachment_first_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001012 const auto &view_gen = attachment_views[i];
1013 if (!view_gen.IsValid()) continue;
John Zulauf1507ee42020-05-18 11:33:09 -06001014 const auto &ci = attachment_ci[i];
John Zulauf1507ee42020-05-18 11:33:09 -06001015
1016 // Need check in the following way
1017 // 1) if the usage bit isn't in the dest_access_scope, and there is layout traniition for initial use, report hazard
1018 // vs. transition
1019 // 2) if there isn't a layout transition, we need to look at the external context with a "detect hazard" operation
1020 // for each aspect loaded.
1021
1022 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06001023 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06001024 const bool is_color = !(has_depth || has_stencil);
1025
1026 const SyncStageAccessIndex load_index = has_depth ? DepthStencilLoadUsage(ci.loadOp) : ColorLoadUsage(ci.loadOp);
John Zulauf1507ee42020-05-18 11:33:09 -06001027 const SyncStageAccessIndex stencil_load_index = has_stencil ? DepthStencilLoadUsage(ci.stencilLoadOp) : load_index;
John Zulauf1507ee42020-05-18 11:33:09 -06001028
John Zulaufaff20662020-06-01 14:07:58 -06001029 HazardResult hazard;
John Zulauf1507ee42020-05-18 11:33:09 -06001030 const char *aspect = nullptr;
John Zulauf1507ee42020-05-18 11:33:09 -06001031
John Zulaufb02c1eb2020-10-06 16:33:36 -06001032 bool checked_stencil = false;
John Zulauf57261402021-08-13 11:32:06 -06001033 if (is_color && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001034 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea, load_index, SyncOrdering::kColorAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001035 aspect = "color";
1036 } else {
John Zulauf57261402021-08-13 11:32:06 -06001037 if (has_depth && (load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001038 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_index,
1039 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001040 aspect = "depth";
1041 }
John Zulauf57261402021-08-13 11:32:06 -06001042 if (!hazard.hazard && has_stencil && (stencil_load_index != SYNC_ACCESS_INDEX_NONE)) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001043 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, stencil_load_index,
1044 SyncOrdering::kDepthStencilAttachment);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001045 aspect = "stencil";
1046 checked_stencil = true;
1047 }
1048 }
1049
1050 if (hazard.hazard) {
1051 auto load_op_string = string_VkAttachmentLoadOp(checked_stencil ? ci.stencilLoadOp : ci.loadOp);
John Zulauf64ffe552021-02-06 10:25:07 -07001052 const auto &sync_state = ex_context.GetSyncState();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001053 if (hazard.tag == kCurrentCommandTag) {
1054 // Hazard vs. ILT
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001055 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulaufb02c1eb2020-10-06 16:33:36 -06001056 "%s: Hazard %s vs. layout transition in subpass %" PRIu32 " for attachment %" PRIu32
1057 " aspect %s during load with loadOp %s.",
1058 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string);
1059 } else {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001060 skip |= sync_state.LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauf1507ee42020-05-18 11:33:09 -06001061 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
John Zulauf59e25072020-07-17 10:55:21 -06001062 " aspect %s during load with loadOp %s. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06001063 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect, load_op_string,
John Zulauf64ffe552021-02-06 10:25:07 -07001064 ex_context.FormatUsage(hazard).c_str());
John Zulauf1507ee42020-05-18 11:33:09 -06001065 }
1066 }
1067 }
1068 }
1069 return skip;
1070}
1071
John Zulaufaff20662020-06-01 14:07:58 -06001072// Store operation validation can ignore resolve (before it) and layout tranistions after it. The first is ignored
1073// because of the ordering guarantees w.r.t. sample access and that the resolve validation hasn't altered the state, because
1074// store is part of the same Next/End operation.
1075// The latter is handled in layout transistion validation directly
John Zulauf64ffe552021-02-06 10:25:07 -07001076bool AccessContext::ValidateStoreOperation(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufaff20662020-06-01 14:07:58 -06001077 const VkRect2D &render_area, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001078 const AttachmentViewGenVector &attachment_views, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06001079 bool skip = false;
1080 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001081
1082 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1083 if (subpass == rp_state.attachment_last_subpass[i]) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001084 const AttachmentViewGen &view_gen = attachment_views[i];
1085 if (!view_gen.IsValid()) continue;
John Zulaufaff20662020-06-01 14:07:58 -06001086 const auto &ci = attachment_ci[i];
1087
1088 // The spec states that "don't care" is an operation with VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT,
1089 // so we assume that an implementation is *free* to write in that case, meaning that for correctness
1090 // sake, we treat DONT_CARE as writing.
1091 const bool has_depth = FormatHasDepth(ci.format);
1092 const bool has_stencil = FormatHasStencil(ci.format);
1093 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001094 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001095 if (!has_stencil && !store_op_stores) continue;
1096
1097 HazardResult hazard;
1098 const char *aspect = nullptr;
1099 bool checked_stencil = false;
1100 if (is_color) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001101 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
1102 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001103 aspect = "color";
1104 } else {
John Zulauf57261402021-08-13 11:32:06 -06001105 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001106 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001107 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1108 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001109 aspect = "depth";
1110 }
1111 if (!hazard.hazard && has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001112 hazard = DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1113 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster);
John Zulaufaff20662020-06-01 14:07:58 -06001114 aspect = "stencil";
1115 checked_stencil = true;
1116 }
1117 }
1118
1119 if (hazard.hazard) {
1120 const char *const op_type_string = checked_stencil ? "stencilStoreOp" : "storeOp";
1121 const char *const store_op_string = string_VkAttachmentStoreOp(checked_stencil ? ci.stencilStoreOp : ci.storeOp);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001122 skip |= ex_context.GetSyncState().LogError(rp_state.renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07001123 "%s: Hazard %s in subpass %" PRIu32 " for attachment %" PRIu32
1124 " %s aspect during store with %s %s. Access info %s",
1125 func_name, string_SyncHazard(hazard.hazard), subpass, i, aspect,
John Zulauf64ffe552021-02-06 10:25:07 -07001126 op_type_string, store_op_string, ex_context.FormatUsage(hazard).c_str());
John Zulaufaff20662020-06-01 14:07:58 -06001127 }
1128 }
1129 }
1130 return skip;
1131}
1132
John Zulauf64ffe552021-02-06 10:25:07 -07001133bool AccessContext::ValidateResolveOperations(const CommandExecutionContext &ex_context, const RENDER_PASS_STATE &rp_state,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001134 const VkRect2D &render_area, const AttachmentViewGenVector &attachment_views,
1135 const char *func_name, uint32_t subpass) const {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001136 ValidateResolveAction validate_action(rp_state.renderPass(), subpass, *this, ex_context, func_name);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001137 ResolveOperation(validate_action, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001138 return validate_action.GetSkip();
John Zulaufb027cdb2020-05-21 14:25:22 -06001139}
1140
John Zulauf3d84f1b2020-03-09 13:33:25 -06001141class HazardDetector {
1142 SyncStageAccessIndex usage_index_;
1143
1144 public:
John Zulauf5f13a792020-03-10 07:31:21 -06001145 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const { return pos->second.DetectHazard(usage_index_); }
John Zulauf14940722021-04-12 15:19:02 -06001146 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001147 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001148 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001149 explicit HazardDetector(SyncStageAccessIndex usage) : usage_index_(usage) {}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001150};
1151
John Zulauf69133422020-05-20 14:55:53 -06001152class HazardDetectorWithOrdering {
1153 const SyncStageAccessIndex usage_index_;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001154 const SyncOrdering ordering_rule_;
John Zulauf69133422020-05-20 14:55:53 -06001155
1156 public:
1157 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001158 return pos->second.DetectHazard(usage_index_, ordering_rule_);
John Zulauf69133422020-05-20 14:55:53 -06001159 }
John Zulauf14940722021-04-12 15:19:02 -06001160 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001161 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf69133422020-05-20 14:55:53 -06001162 }
John Zulauf8e3c3e92021-01-06 11:19:36 -07001163 HazardDetectorWithOrdering(SyncStageAccessIndex usage, SyncOrdering ordering) : usage_index_(usage), ordering_rule_(ordering) {}
John Zulauf69133422020-05-20 14:55:53 -06001164};
1165
John Zulauf16adfc92020-04-08 10:28:33 -06001166HazardResult AccessContext::DetectHazard(const BUFFER_STATE &buffer, SyncStageAccessIndex usage_index,
John Zulauf355e49b2020-04-24 15:11:15 -06001167 const ResourceAccessRange &range) const {
John Zulauf16adfc92020-04-08 10:28:33 -06001168 if (!SimpleBinding(buffer)) return HazardResult();
John Zulauf150e5332020-12-03 08:52:52 -07001169 const auto base_address = ResourceBaseAddress(buffer);
1170 HazardDetector detector(usage_index);
1171 return DetectHazard(AccessAddressType::kLinear, detector, (range + base_address), DetectOptions::kDetectAll);
John Zulaufe5da6e52020-03-18 15:32:18 -06001172}
1173
John Zulauf69133422020-05-20 14:55:53 -06001174template <typename Detector>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001175HazardResult AccessContext::DetectHazard(Detector &detector, const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1176 DetectOptions options) const {
1177 const auto *attachment_gen = view_gen.GetRangeGen(gen_type);
1178 if (!attachment_gen) return HazardResult();
1179
1180 subresource_adapter::ImageRangeGenerator range_gen(*attachment_gen);
1181 const auto address_type = view_gen.GetAddressType();
1182 for (; range_gen->non_empty(); ++range_gen) {
1183 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1184 if (hazard.hazard) return hazard;
1185 }
1186
1187 return HazardResult();
1188}
1189
1190template <typename Detector>
John Zulauf69133422020-05-20 14:55:53 -06001191HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1192 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
1193 const VkExtent3D &extent, DetectOptions options) const {
1194 if (!SimpleBinding(image)) return HazardResult();
John Zulauf69133422020-05-20 14:55:53 -06001195 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001196 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1197 base_address);
1198 const auto address_type = ImageAddressType(image);
John Zulauf69133422020-05-20 14:55:53 -06001199 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf150e5332020-12-03 08:52:52 -07001200 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
John Zulauf69133422020-05-20 14:55:53 -06001201 if (hazard.hazard) return hazard;
1202 }
1203 return HazardResult();
1204}
John Zulauf110413c2021-03-20 05:38:38 -06001205template <typename Detector>
1206HazardResult AccessContext::DetectHazard(Detector &detector, const IMAGE_STATE &image,
1207 const VkImageSubresourceRange &subresource_range, DetectOptions options) const {
1208 if (!SimpleBinding(image)) return HazardResult();
1209 const auto base_address = ResourceBaseAddress(image);
1210 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1211 const auto address_type = ImageAddressType(image);
1212 for (; range_gen->non_empty(); ++range_gen) {
John Zulauf110413c2021-03-20 05:38:38 -06001213 HazardResult hazard = DetectHazard(address_type, detector, *range_gen, options);
1214 if (hazard.hazard) return hazard;
1215 }
1216 return HazardResult();
1217}
John Zulauf69133422020-05-20 14:55:53 -06001218
John Zulauf540266b2020-04-06 18:54:53 -06001219HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
1220 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1221 const VkExtent3D &extent) const {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001222 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1223 subresource.layerCount};
John Zulauf110413c2021-03-20 05:38:38 -06001224 HazardDetector detector(current_usage);
1225 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf1507ee42020-05-18 11:33:09 -06001226}
1227
1228HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf110413c2021-03-20 05:38:38 -06001229 const VkImageSubresourceRange &subresource_range) const {
John Zulauf69133422020-05-20 14:55:53 -06001230 HazardDetector detector(current_usage);
John Zulauf110413c2021-03-20 05:38:38 -06001231 return DetectHazard(detector, image, subresource_range, DetectOptions::kDetectAll);
John Zulauf69133422020-05-20 14:55:53 -06001232}
1233
John Zulaufd0ec59f2021-03-13 14:25:08 -07001234HazardResult AccessContext::DetectHazard(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1235 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule) const {
1236 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
1237 return DetectHazard(detector, view_gen, gen_type, DetectOptions::kDetectAll);
1238}
1239
John Zulauf69133422020-05-20 14:55:53 -06001240HazardResult AccessContext::DetectHazard(const IMAGE_STATE &image, SyncStageAccessIndex current_usage,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001241 const VkImageSubresourceRange &subresource_range, SyncOrdering ordering_rule,
John Zulauf69133422020-05-20 14:55:53 -06001242 const VkOffset3D &offset, const VkExtent3D &extent) const {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001243 HazardDetectorWithOrdering detector(current_usage, ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06001244 return DetectHazard(detector, image, subresource_range, offset, extent, DetectOptions::kDetectAll);
John Zulauf9cb530d2019-09-30 14:14:10 -06001245}
1246
John Zulauf3d84f1b2020-03-09 13:33:25 -06001247class BarrierHazardDetector {
1248 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001249 BarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf3d84f1b2020-03-09 13:33:25 -06001250 SyncStageAccessFlags src_access_scope)
1251 : usage_index_(usage_index), src_exec_scope_(src_exec_scope), src_access_scope_(src_access_scope) {}
1252
John Zulauf5f13a792020-03-10 07:31:21 -06001253 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1254 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_);
John Zulauf0cb5be22020-01-23 12:18:22 -07001255 }
John Zulauf14940722021-04-12 15:19:02 -06001256 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf3d84f1b2020-03-09 13:33:25 -06001257 // 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 -07001258 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001259 }
1260
1261 private:
1262 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001263 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf3d84f1b2020-03-09 13:33:25 -06001264 SyncStageAccessFlags src_access_scope_;
1265};
1266
John Zulauf4a6105a2020-11-17 15:11:05 -07001267class EventBarrierHazardDetector {
1268 public:
Jeremy Gebben40a22942020-12-22 14:22:06 -07001269 EventBarrierHazardDetector(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001270 SyncStageAccessFlags src_access_scope, const SyncEventState::ScopeMap &event_scope,
John Zulauf14940722021-04-12 15:19:02 -06001271 ResourceUsageTag scope_tag)
John Zulauf4a6105a2020-11-17 15:11:05 -07001272 : usage_index_(usage_index),
1273 src_exec_scope_(src_exec_scope),
1274 src_access_scope_(src_access_scope),
1275 event_scope_(event_scope),
1276 scope_pos_(event_scope.cbegin()),
1277 scope_end_(event_scope.cend()),
1278 scope_tag_(scope_tag) {}
1279
1280 HazardResult Detect(const ResourceAccessRangeMap::const_iterator &pos) const {
1281 // TODO NOTE: This is almost the slowest way to do this... need to intelligently walk this...
1282 // Need to find a more efficient sync, since we know pos->first is strictly increasing call to call
1283 // NOTE: "cached_lower_bound_impl" with upgrades could do this.
1284 if (scope_pos_ == scope_end_) return HazardResult();
1285 if (!scope_pos_->first.intersects(pos->first)) {
1286 event_scope_.lower_bound(pos->first);
1287 if ((scope_pos_ == scope_end_) || !scope_pos_->first.intersects(pos->first)) return HazardResult();
1288 }
1289
1290 // Some portion of this pos is in the event_scope, so check for a barrier hazard
1291 return pos->second.DetectBarrierHazard(usage_index_, src_exec_scope_, src_access_scope_, scope_tag_);
1292 }
John Zulauf14940722021-04-12 15:19:02 -06001293 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, ResourceUsageTag start_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07001294 // Async barrier hazard detection can use the same path as the usage index is not IsRead, but is IsWrite
1295 return pos->second.DetectAsyncHazard(usage_index_, start_tag);
1296 }
1297
1298 private:
1299 SyncStageAccessIndex usage_index_;
Jeremy Gebben40a22942020-12-22 14:22:06 -07001300 VkPipelineStageFlags2KHR src_exec_scope_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001301 SyncStageAccessFlags src_access_scope_;
1302 const SyncEventState::ScopeMap &event_scope_;
1303 SyncEventState::ScopeMap::const_iterator scope_pos_;
1304 SyncEventState::ScopeMap::const_iterator scope_end_;
John Zulauf14940722021-04-12 15:19:02 -06001305 const ResourceUsageTag scope_tag_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001306};
1307
Jeremy Gebben40a22942020-12-22 14:22:06 -07001308HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07001309 const SyncStageAccessFlags &src_access_scope,
1310 const VkImageSubresourceRange &subresource_range,
1311 const SyncEventState &sync_event, DetectOptions options) const {
1312 // It's not particularly DRY to get the address type in this function as well as lower down, but we have to select the
1313 // first access scope map to use, and there's no easy way to plumb it in below.
1314 const auto address_type = ImageAddressType(image);
1315 const auto &event_scope = sync_event.FirstScope(address_type);
1316
1317 EventBarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope,
1318 event_scope, sync_event.first_scope_tag);
John Zulauf110413c2021-03-20 05:38:38 -06001319 return DetectHazard(detector, image, subresource_range, options);
John Zulauf4a6105a2020-11-17 15:11:05 -07001320}
1321
John Zulaufd0ec59f2021-03-13 14:25:08 -07001322HazardResult AccessContext::DetectImageBarrierHazard(const AttachmentViewGen &view_gen, const SyncBarrier &barrier,
1323 DetectOptions options) const {
1324 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, barrier.src_exec_scope.exec_scope,
1325 barrier.src_access_scope);
1326 return DetectHazard(detector, view_gen, AttachmentViewGen::Gen::kViewSubresource, options);
1327}
1328
Jeremy Gebben40a22942020-12-22 14:22:06 -07001329HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001330 const SyncStageAccessFlags &src_access_scope,
John Zulauf355e49b2020-04-24 15:11:15 -06001331 const VkImageSubresourceRange &subresource_range,
John Zulauf43cc7462020-12-03 12:33:12 -07001332 const DetectOptions options) const {
John Zulauf69133422020-05-20 14:55:53 -06001333 BarrierHazardDetector detector(SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION, src_exec_scope, src_access_scope);
John Zulauf110413c2021-03-20 05:38:38 -06001334 return DetectHazard(detector, image, subresource_range, options);
John Zulauf0cb5be22020-01-23 12:18:22 -07001335}
1336
Jeremy Gebben40a22942020-12-22 14:22:06 -07001337HazardResult AccessContext::DetectImageBarrierHazard(const IMAGE_STATE &image, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07001338 const SyncStageAccessFlags &src_stage_accesses,
John Zulauf355e49b2020-04-24 15:11:15 -06001339 const VkImageMemoryBarrier &barrier) const {
1340 auto subresource_range = NormalizeSubresourceRange(image.createInfo, barrier.subresourceRange);
1341 const auto src_access_scope = SyncStageAccess::AccessScope(src_stage_accesses, barrier.srcAccessMask);
1342 return DetectImageBarrierHazard(image, src_exec_scope, src_access_scope, subresource_range, kDetectAll);
1343}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001344HazardResult AccessContext::DetectImageBarrierHazard(const SyncImageMemoryBarrier &image_barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07001345 return DetectImageBarrierHazard(*image_barrier.image.get(), image_barrier.barrier.src_exec_scope.exec_scope,
John Zulauf110413c2021-03-20 05:38:38 -06001346 image_barrier.barrier.src_access_scope, image_barrier.range, kDetectAll);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07001347}
John Zulauf355e49b2020-04-24 15:11:15 -06001348
John Zulauf9cb530d2019-09-30 14:14:10 -06001349template <typename Flags, typename Map>
1350SyncStageAccessFlags AccessScopeImpl(Flags flag_mask, const Map &map) {
1351 SyncStageAccessFlags scope = 0;
1352 for (const auto &bit_scope : map) {
1353 if (flag_mask < bit_scope.first) break;
1354
1355 if (flag_mask & bit_scope.first) {
1356 scope |= bit_scope.second;
1357 }
1358 }
1359 return scope;
1360}
1361
Jeremy Gebben40a22942020-12-22 14:22:06 -07001362SyncStageAccessFlags SyncStageAccess::AccessScopeByStage(VkPipelineStageFlags2KHR stages) {
John Zulauf9cb530d2019-09-30 14:14:10 -06001363 return AccessScopeImpl(stages, syncStageAccessMaskByStageBit);
1364}
1365
Jeremy Gebben40a22942020-12-22 14:22:06 -07001366SyncStageAccessFlags SyncStageAccess::AccessScopeByAccess(VkAccessFlags2KHR accesses) {
1367 return AccessScopeImpl(sync_utils::ExpandAccessFlags(accesses), syncStageAccessMaskByAccessBit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001368}
1369
Jeremy Gebben40a22942020-12-22 14:22:06 -07001370// Getting from stage mask and access mask to stage/access masks is something we need to be good at...
1371SyncStageAccessFlags SyncStageAccess::AccessScope(VkPipelineStageFlags2KHR stages, VkAccessFlags2KHR accesses) {
John Zulauf5f13a792020-03-10 07:31:21 -06001372 // The access scope is the intersection of all stage/access types possible for the enabled stages and the enables
1373 // accesses (after doing a couple factoring of common terms the union of stage/access intersections is the intersections
1374 // 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 -06001375 return AccessScopeByStage(stages) & AccessScopeByAccess(accesses);
1376}
1377
1378template <typename Action>
John Zulauf5c5e88d2019-12-26 11:22:02 -07001379void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const ResourceAccessRange &range, const Action &action) {
John Zulauf7635de32020-05-29 17:14:15 -06001380 // TODO: Optimization for operations that do a pure overwrite (i.e. WRITE usages which rewrite the state, vs READ usages
1381 // that do incrementalupdates
John Zulauf4a6105a2020-11-17 15:11:05 -07001382 assert(accesses);
John Zulauf9cb530d2019-09-30 14:14:10 -06001383 auto pos = accesses->lower_bound(range);
1384 if (pos == accesses->end() || !pos->first.intersects(range)) {
1385 // The range is empty, fill it with a default value.
1386 pos = action.Infill(accesses, pos, range);
1387 } else if (range.begin < pos->first.begin) {
1388 // Leading empty space, infill
John Zulauf5c5e88d2019-12-26 11:22:02 -07001389 pos = action.Infill(accesses, pos, ResourceAccessRange(range.begin, pos->first.begin));
John Zulauf9cb530d2019-09-30 14:14:10 -06001390 } else if (pos->first.begin < range.begin) {
1391 // Trim the beginning if needed
1392 pos = accesses->split(pos, range.begin, sparse_container::split_op_keep_both());
1393 ++pos;
1394 }
1395
1396 const auto the_end = accesses->end();
1397 while ((pos != the_end) && pos->first.intersects(range)) {
1398 if (pos->first.end > range.end) {
1399 pos = accesses->split(pos, range.end, sparse_container::split_op_keep_both());
1400 }
1401
1402 pos = action(accesses, pos);
1403 if (pos == the_end) break;
1404
1405 auto next = pos;
1406 ++next;
1407 if ((pos->first.end < range.end) && (next != the_end) && !next->first.is_subsequent_to(pos->first)) {
1408 // Need to infill if next is disjoint
1409 VkDeviceSize limit = (next == the_end) ? range.end : std::min(range.end, next->first.begin);
John Zulauf5c5e88d2019-12-26 11:22:02 -07001410 ResourceAccessRange new_range(pos->first.end, limit);
John Zulauf9cb530d2019-09-30 14:14:10 -06001411 next = action.Infill(accesses, next, new_range);
1412 }
1413 pos = next;
1414 }
1415}
John Zulaufd5115702021-01-18 12:34:33 -07001416
1417// Give a comparable interface for range generators and ranges
1418template <typename Action>
1419inline void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, ResourceAccessRange *range) {
1420 assert(range);
1421 UpdateMemoryAccessState(accesses, *range, action);
1422}
1423
John Zulauf4a6105a2020-11-17 15:11:05 -07001424template <typename Action, typename RangeGen>
1425void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, RangeGen *range_gen_arg) {
1426 assert(range_gen_arg);
John Zulaufd5115702021-01-18 12:34:33 -07001427 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 -07001428 for (; range_gen->non_empty(); ++range_gen) {
1429 UpdateMemoryAccessState(accesses, *range_gen, action);
1430 }
1431}
John Zulauf9cb530d2019-09-30 14:14:10 -06001432
John Zulaufd0ec59f2021-03-13 14:25:08 -07001433template <typename Action, typename RangeGen>
1434void UpdateMemoryAccessState(ResourceAccessRangeMap *accesses, const Action &action, const RangeGen &range_gen_prebuilt) {
1435 RangeGen range_gen(range_gen_prebuilt); // RangeGenerators can be expensive to create from scratch... initialize from built
1436 for (; range_gen->non_empty(); ++range_gen) {
1437 UpdateMemoryAccessState(accesses, *range_gen, action);
1438 }
1439}
John Zulauf9cb530d2019-09-30 14:14:10 -06001440struct UpdateMemoryAccessStateFunctor {
John Zulauf5c5e88d2019-12-26 11:22:02 -07001441 using Iterator = ResourceAccessRangeMap::iterator;
1442 Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const {
John Zulauf5f13a792020-03-10 07:31:21 -06001443 // this is only called on gaps, and never returns a gap.
1444 ResourceAccessState default_state;
John Zulauf16adfc92020-04-08 10:28:33 -06001445 context.ResolvePreviousAccess(type, range, accesses, &default_state);
John Zulauf5f13a792020-03-10 07:31:21 -06001446 return accesses->lower_bound(range);
John Zulauf9cb530d2019-09-30 14:14:10 -06001447 }
John Zulauf5f13a792020-03-10 07:31:21 -06001448
John Zulauf5c5e88d2019-12-26 11:22:02 -07001449 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001450 auto &access_state = pos->second;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001451 access_state.Update(usage, ordering_rule, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06001452 return pos;
1453 }
1454
John Zulauf43cc7462020-12-03 12:33:12 -07001455 UpdateMemoryAccessStateFunctor(AccessAddressType type_, const AccessContext &context_, SyncStageAccessIndex usage_,
John Zulauf14940722021-04-12 15:19:02 -06001456 SyncOrdering ordering_rule_, ResourceUsageTag tag_)
John Zulauf8e3c3e92021-01-06 11:19:36 -07001457 : type(type_), context(context_), usage(usage_), ordering_rule(ordering_rule_), tag(tag_) {}
John Zulauf43cc7462020-12-03 12:33:12 -07001458 const AccessAddressType type;
John Zulauf540266b2020-04-06 18:54:53 -06001459 const AccessContext &context;
John Zulauf16adfc92020-04-08 10:28:33 -06001460 const SyncStageAccessIndex usage;
John Zulauf8e3c3e92021-01-06 11:19:36 -07001461 const SyncOrdering ordering_rule;
John Zulauf14940722021-04-12 15:19:02 -06001462 const ResourceUsageTag tag;
John Zulauf9cb530d2019-09-30 14:14:10 -06001463};
1464
John Zulauf4a6105a2020-11-17 15:11:05 -07001465// The barrier operation for pipeline and subpass dependencies`
John Zulauf1e331ec2020-12-04 18:29:38 -07001466struct PipelineBarrierOp {
1467 SyncBarrier barrier;
1468 bool layout_transition;
1469 PipelineBarrierOp(const SyncBarrier &barrier_, bool layout_transition_)
1470 : barrier(barrier_), layout_transition(layout_transition_) {}
1471 PipelineBarrierOp() = default;
John Zulaufd5115702021-01-18 12:34:33 -07001472 PipelineBarrierOp(const PipelineBarrierOp &) = default;
John Zulauf1e331ec2020-12-04 18:29:38 -07001473 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(barrier, layout_transition); }
1474};
John Zulauf4a6105a2020-11-17 15:11:05 -07001475// The barrier operation for wait events
1476struct WaitEventBarrierOp {
John Zulauf14940722021-04-12 15:19:02 -06001477 ResourceUsageTag scope_tag;
John Zulauf4a6105a2020-11-17 15:11:05 -07001478 SyncBarrier barrier;
1479 bool layout_transition;
John Zulauf14940722021-04-12 15:19:02 -06001480 WaitEventBarrierOp(const ResourceUsageTag scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1481 : scope_tag(scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
John Zulauf4a6105a2020-11-17 15:11:05 -07001482 WaitEventBarrierOp() = default;
John Zulauf14940722021-04-12 15:19:02 -06001483 void operator()(ResourceAccessState *access_state) const { access_state->ApplyBarrier(scope_tag, barrier, layout_transition); }
John Zulauf4a6105a2020-11-17 15:11:05 -07001484};
John Zulauf1e331ec2020-12-04 18:29:38 -07001485
John Zulauf4a6105a2020-11-17 15:11:05 -07001486// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1487// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1488// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001489template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001490class ApplyBarrierOpsFunctor {
1491 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001492 using Iterator = ResourceAccessRangeMap::iterator;
1493 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001494
John Zulauf5c5e88d2019-12-26 11:22:02 -07001495 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001496 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001497 for (const auto &op : barrier_ops_) {
1498 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001499 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001500
John Zulauf89311b42020-09-29 16:28:47 -06001501 if (resolve_) {
1502 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1503 // another walk
1504 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001505 }
1506 return pos;
1507 }
1508
John Zulauf89311b42020-09-29 16:28:47 -06001509 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulauf14940722021-04-12 15:19:02 -06001510 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, ResourceUsageTag tag) : resolve_(resolve), barrier_ops_(), tag_(tag) {
John Zulaufd5115702021-01-18 12:34:33 -07001511 barrier_ops_.reserve(size_hint);
1512 }
1513 void EmplaceBack(const BarrierOp &op) { barrier_ops_.emplace_back(op); }
John Zulauf89311b42020-09-29 16:28:47 -06001514
1515 private:
1516 bool resolve_;
John Zulaufd5115702021-01-18 12:34:33 -07001517 std::vector<BarrierOp> barrier_ops_;
John Zulauf14940722021-04-12 15:19:02 -06001518 const ResourceUsageTag tag_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001519};
1520
John Zulauf4a6105a2020-11-17 15:11:05 -07001521// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1522// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1523template <typename BarrierOp>
1524class ApplyBarrierFunctor {
1525 public:
1526 using Iterator = ResourceAccessRangeMap::iterator;
1527 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1528
1529 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1530 auto &access_state = pos->second;
1531 barrier_op_(&access_state);
1532 return pos;
1533 }
1534
1535 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1536
1537 private:
John Zulaufd5115702021-01-18 12:34:33 -07001538 BarrierOp barrier_op_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001539};
1540
John Zulauf1e331ec2020-12-04 18:29:38 -07001541// This functor resolves the pendinging state.
1542class ResolvePendingBarrierFunctor {
1543 public:
1544 using Iterator = ResourceAccessRangeMap::iterator;
1545 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1546
1547 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1548 auto &access_state = pos->second;
1549 access_state.ApplyPendingBarriers(tag_);
1550 return pos;
1551 }
1552
John Zulauf14940722021-04-12 15:19:02 -06001553 ResolvePendingBarrierFunctor(ResourceUsageTag tag) : tag_(tag) {}
John Zulauf1e331ec2020-12-04 18:29:38 -07001554
1555 private:
John Zulauf14940722021-04-12 15:19:02 -06001556 const ResourceUsageTag tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001557};
1558
John Zulauf8e3c3e92021-01-06 11:19:36 -07001559void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001560 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf8e3c3e92021-01-06 11:19:36 -07001561 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001562 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001563}
1564
John Zulauf8e3c3e92021-01-06 11:19:36 -07001565void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf14940722021-04-12 15:19:02 -06001566 const ResourceAccessRange &range, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001567 if (!SimpleBinding(buffer)) return;
1568 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001569 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001570}
John Zulauf355e49b2020-04-24 15:11:15 -06001571
John Zulauf8e3c3e92021-01-06 11:19:36 -07001572void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001573 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1574 if (!SimpleBinding(image)) return;
1575 const auto base_address = ResourceBaseAddress(image);
1576 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1577 const auto address_type = ImageAddressType(image);
1578 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1579 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1580}
1581void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001582 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001583 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001584 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001585 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001586 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1587 base_address);
1588 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001589 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001590 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001591}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001592
1593void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
John Zulauf14940722021-04-12 15:19:02 -06001594 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001595 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1596 if (!gen) return;
1597 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1598 const auto address_type = view_gen.GetAddressType();
1599 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1600 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001601}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001602
John Zulauf8e3c3e92021-01-06 11:19:36 -07001603void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001604 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
John Zulauf14940722021-04-12 15:19:02 -06001605 const VkExtent3D &extent, const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001606 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1607 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001608 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001609}
1610
John Zulaufd0ec59f2021-03-13 14:25:08 -07001611template <typename Action, typename RangeGen>
1612void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1613 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1614 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001615}
1616
1617template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001618void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1619 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1620 if (!gen) return;
1621 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001622}
1623
John Zulaufd0ec59f2021-03-13 14:25:08 -07001624void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1625 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001626 const ResourceUsageTag tag) {
John Zulauf7635de32020-05-29 17:14:15 -06001627 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001628 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001629}
1630
John Zulaufd0ec59f2021-03-13 14:25:08 -07001631void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06001632 uint32_t subpass, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001633 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001634
1635 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1636 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001637 const auto &view_gen = attachment_views[i];
1638 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001639
1640 const auto &ci = attachment_ci[i];
1641 const bool has_depth = FormatHasDepth(ci.format);
1642 const bool has_stencil = FormatHasStencil(ci.format);
1643 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001644 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001645
1646 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001647 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1648 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001649 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001650 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001651 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1652 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001653 }
John Zulauf57261402021-08-13 11:32:06 -06001654 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001655 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001656 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1657 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001658 }
1659 }
1660 }
1661 }
1662}
1663
John Zulauf540266b2020-04-06 18:54:53 -06001664template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001665void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001666 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001667 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001668 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001669 }
1670}
1671
1672void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001673 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1674 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001675 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001676 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001677 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001678 }
1679 }
1680}
1681
John Zulauf355e49b2020-04-24 15:11:15 -06001682// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001683HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1684 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001685
John Zulauf355e49b2020-04-24 15:11:15 -06001686 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001687 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001688
1689 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001690 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1691 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001692 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001693 if (!hazard.hazard) {
1694 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001695 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001696 }
John Zulaufa0a98292020-09-18 09:30:10 -06001697
John Zulauf355e49b2020-04-24 15:11:15 -06001698 return hazard;
1699}
1700
John Zulaufb02c1eb2020-10-06 16:33:36 -06001701void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulauf14940722021-04-12 15:19:02 -06001702 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001703 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001704 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001705 for (const auto &transition : transitions) {
1706 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001707 const auto &view_gen = attachment_views[transition.attachment];
1708 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001709
1710 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1711 assert(trackback);
1712
1713 // Import the attachments into the current context
1714 const auto *prev_context = trackback->context;
1715 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001716 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001717 auto &target_map = GetAccessStateMap(address_type);
1718 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001719 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1720 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001721 }
1722
John Zulauf86356ca2020-10-19 11:46:41 -06001723 // If there were no transitions skip this global map walk
1724 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001725 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001726 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001727 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001728}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001729
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001730void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulauf669dfd52021-01-27 17:15:28 -07001731 auto *events_context = GetCurrentEventsContext();
1732 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06001733 events_context->ApplyBarrier(src, dst);
John Zulauf4a6105a2020-11-17 15:11:05 -07001734}
1735
locke-lunarg61870c22020-06-09 14:51:50 -06001736bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1737 const char *func_name) const {
1738 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001739 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001740 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001741 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001742 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001743 return skip;
1744 }
1745
1746 using DescriptorClass = cvdescriptorset::DescriptorClass;
1747 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1748 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1749 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1750 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1751
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001752 for (const auto &stage_state : pipe->stage_state) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001753 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1754 pipe->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001755 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001756 }
locke-lunarg61870c22020-06-09 14:51:50 -06001757 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001758 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set;
locke-lunarg61870c22020-06-09 14:51:50 -06001759 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001760 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001761 const auto descriptor_type = binding_it.GetType();
1762 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1763 auto array_idx = 0;
1764
1765 if (binding_it.IsVariableDescriptorCount()) {
1766 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1767 }
1768 SyncStageAccessIndex sync_index =
1769 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1770
1771 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1772 uint32_t index = i - index_range.start;
1773 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1774 switch (descriptor->GetClass()) {
1775 case DescriptorClass::ImageSampler:
1776 case DescriptorClass::Image: {
1777 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001778 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001779 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001780 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1781 img_view_state = image_sampler_descriptor->GetImageViewState();
1782 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001783 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001784 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1785 img_view_state = image_descriptor->GetImageViewState();
1786 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001787 }
1788 if (!img_view_state) continue;
John Zulauf361fb532020-07-22 10:45:39 -06001789 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001790 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1791 // Descriptors, so we do not have to worry about depth slicing here.
1792 // See: VUID 00343
1793 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001794 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001795 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001796
1797 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1798 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1799 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001800 // Input attachments are subject to raster ordering rules
1801 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001802 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001803 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001804 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001805 }
John Zulauf110413c2021-03-20 05:38:38 -06001806
John Zulauf33fc1d52020-07-17 11:01:10 -06001807 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001808 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001809 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001810 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1811 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001812 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001813 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1814 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1815 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001816 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1817 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001818 set_binding.first.binding, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001819 }
1820 break;
1821 }
1822 case DescriptorClass::TexelBuffer: {
1823 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1824 if (!buf_view_state) continue;
1825 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001826 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001827 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001828 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001829 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001830 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001831 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1832 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001833 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1834 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1835 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001836 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001837 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001838 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001839 }
1840 break;
1841 }
1842 case DescriptorClass::GeneralBuffer: {
1843 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1844 auto buf_state = buffer_descriptor->GetBufferState();
1845 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001846 const ResourceAccessRange range =
1847 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001848 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001849 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001850 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001851 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001852 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1853 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001854 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1855 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1856 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001857 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001858 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001859 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001860 }
1861 break;
1862 }
1863 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1864 default:
1865 break;
1866 }
1867 }
1868 }
1869 }
1870 return skip;
1871}
1872
1873void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
John Zulauf14940722021-04-12 15:19:02 -06001874 const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001875 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001876 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001877 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001878 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001879 return;
1880 }
1881
1882 using DescriptorClass = cvdescriptorset::DescriptorClass;
1883 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1884 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1885 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1886 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1887
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001888 for (const auto &stage_state : pipe->stage_state) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001889 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1890 pipe->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001891 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001892 }
locke-lunarg61870c22020-06-09 14:51:50 -06001893 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001894 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set;
locke-lunarg61870c22020-06-09 14:51:50 -06001895 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001896 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001897 const auto descriptor_type = binding_it.GetType();
1898 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1899 auto array_idx = 0;
1900
1901 if (binding_it.IsVariableDescriptorCount()) {
1902 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1903 }
1904 SyncStageAccessIndex sync_index =
1905 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1906
1907 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1908 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1909 switch (descriptor->GetClass()) {
1910 case DescriptorClass::ImageSampler:
1911 case DescriptorClass::Image: {
1912 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1913 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1914 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1915 } else {
1916 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1917 }
1918 if (!img_view_state) continue;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001919 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1920 // Descriptors, so we do not have to worry about depth slicing here.
1921 // See: VUID 00343
1922 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06001923 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001924 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06001925 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1926 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1927 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
1928 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001929 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001930 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
1931 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001932 }
locke-lunarg61870c22020-06-09 14:51:50 -06001933 break;
1934 }
1935 case DescriptorClass::TexelBuffer: {
1936 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1937 if (!buf_view_state) continue;
1938 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001939 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001940 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001941 break;
1942 }
1943 case DescriptorClass::GeneralBuffer: {
1944 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1945 auto buf_state = buffer_descriptor->GetBufferState();
1946 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001947 const ResourceAccessRange range =
1948 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07001949 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001950 break;
1951 }
1952 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1953 default:
1954 break;
1955 }
1956 }
1957 }
1958 }
1959}
1960
1961bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1962 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001963 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001964 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001965 return skip;
1966 }
1967
1968 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1969 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001970 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001971
1972 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001973 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001974 if (binding_description.binding < binding_buffers_size) {
1975 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06001976 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001977
locke-lunarg1ae57d62020-11-18 10:49:19 -07001978 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001979 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1980 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07001981 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06001982 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001983 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001984 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
1985 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1986 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001987 }
1988 }
1989 }
1990 return skip;
1991}
1992
John Zulauf14940722021-04-12 15:19:02 -06001993void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001994 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001995 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001996 return;
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 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2012 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002013 }
2014 }
2015}
2016
2017bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2018 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002019 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002020 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002021 }
locke-lunarg61870c22020-06-09 14:51:50 -06002022
locke-lunarg1ae57d62020-11-18 10:49:19 -07002023 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002024 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002025 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2026 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002027 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002028 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002029 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002030 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2031 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2032 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002033 }
2034
2035 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2036 // We will detect more accurate range in the future.
2037 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2038 return skip;
2039}
2040
John Zulauf14940722021-04-12 15:19:02 -06002041void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002042 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 -06002043
locke-lunarg1ae57d62020-11-18 10:49:19 -07002044 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002045 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002046 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2047 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002048 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002049
2050 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2051 // We will detect more accurate range in the future.
2052 RecordDrawVertex(UINT32_MAX, 0, tag);
2053}
2054
2055bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002056 bool skip = false;
2057 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002058 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002059 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002060}
2061
John Zulauf14940722021-04-12 15:19:02 -06002062void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002063 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002064 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002065 }
locke-lunarg61870c22020-06-09 14:51:50 -06002066}
2067
John Zulauf64ffe552021-02-06 10:25:07 -07002068void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2069 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
John Zulauf14940722021-04-12 15:19:02 -06002070 const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002071 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002072 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002073 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002074 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002075 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002076}
2077
John Zulauf8eda1562021-04-13 17:06:41 -06002078void CommandBufferAccessContext::RecordNextSubpass(ResourceUsageTag prev_tag, ResourceUsageTag next_tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002079 assert(current_renderpass_context_);
John Zulauf64ffe552021-02-06 10:25:07 -07002080 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002081 current_context_ = &current_renderpass_context_->CurrentContext();
2082}
2083
John Zulauf8eda1562021-04-13 17:06:41 -06002084void CommandBufferAccessContext::RecordEndRenderPass(const ResourceUsageTag tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06002085 assert(current_renderpass_context_);
2086 if (!current_renderpass_context_) return;
2087
John Zulauf8eda1562021-04-13 17:06:41 -06002088 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002089 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002090 current_renderpass_context_ = nullptr;
2091}
2092
John Zulauf4a6105a2020-11-17 15:11:05 -07002093void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2094 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002095 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2096 if (event_state) {
2097 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002098 }
2099}
2100
John Zulauf64ffe552021-02-06 10:25:07 -07002101bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002102 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002103 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002104 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002105 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002106 if (!pipe) {
2107 return skip;
2108 }
2109
2110 const auto &create_info = pipe->create_info.graphics;
2111 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002112 return skip;
2113 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002114 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002115 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002116
John Zulauf1a224292020-06-30 14:52:13 -06002117 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002118 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002119 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2120 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002121 if (location >= subpass.colorAttachmentCount ||
2122 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002123 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002124 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002125 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2126 if (!view_gen.IsValid()) continue;
2127 HazardResult hazard =
2128 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2129 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002130 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002131 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002132 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002133 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002134 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002135 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002136 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002137 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002138 }
2139 }
2140 }
locke-lunarg37047832020-06-12 13:44:45 -06002141
2142 // PHASE1 TODO: Add layout based read/vs. write selection.
2143 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002144 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002145 GetSubpassDepthStencilAttachmentIndex(pipe->create_info.graphics.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002146
2147 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2148 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2149 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002150 bool depth_write = false, stencil_write = false;
2151
2152 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002153 if (!FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2154 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002155 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2156 depth_write = true;
2157 }
2158 // PHASE1 TODO: It needs to check if stencil is writable.
2159 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2160 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2161 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002162 if (!FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002163 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2164 stencil_write = true;
2165 }
2166
2167 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2168 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002169 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2170 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2171 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002172 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002173 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002174 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002175 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002176 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002177 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2178 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002179 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002180 }
2181 }
2182 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002183 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2184 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2185 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002186 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002187 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002188 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002189 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002190 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002191 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2192 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002193 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002194 }
locke-lunarg61870c22020-06-09 14:51:50 -06002195 }
2196 }
2197 return skip;
2198}
2199
John Zulauf14940722021-04-12 15:19:02 -06002200void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002201 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002202 if (!pipe) {
2203 return;
2204 }
2205
2206 const auto &create_info = pipe->create_info.graphics;
2207 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002208 return;
2209 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002210 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002211 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002212
John Zulauf1a224292020-06-30 14:52:13 -06002213 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002214 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002215 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2216 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002217 if (location >= subpass.colorAttachmentCount ||
2218 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002219 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002220 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002221 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2222 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2223 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2224 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002225 }
2226 }
locke-lunarg37047832020-06-12 13:44:45 -06002227
2228 // PHASE1 TODO: Add layout based read/vs. write selection.
2229 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002230 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002231 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002232 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2233 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2234 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002235 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002236 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2237 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002238
2239 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002240 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2241 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002242 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2243 depth_write = true;
2244 }
2245 // PHASE1 TODO: It needs to check if stencil is writable.
2246 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2247 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2248 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002249 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002250 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2251 stencil_write = true;
2252 }
2253
John Zulaufd0ec59f2021-03-13 14:25:08 -07002254 if (depth_write || stencil_write) {
2255 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2256 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2257 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2258 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002259 }
locke-lunarg61870c22020-06-09 14:51:50 -06002260 }
2261}
2262
John Zulauf64ffe552021-02-06 10:25:07 -07002263bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002264 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002265 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002266 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002267 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002268 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002269 func_name);
2270
John Zulauf355e49b2020-04-24 15:11:15 -06002271 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002272 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002273 skip |=
2274 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002275 if (!skip) {
2276 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2277 // on a copy of the (empty) next context.
2278 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2279 AccessContext temp_context(next_context);
2280 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002281 skip |=
2282 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002283 }
John Zulauf7635de32020-05-29 17:14:15 -06002284 return skip;
2285}
John Zulauf64ffe552021-02-06 10:25:07 -07002286bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002287 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002288 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002289 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002290 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002291 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2292
2293 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002294 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002295 return skip;
2296}
2297
John Zulauf64ffe552021-02-06 10:25:07 -07002298AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002299 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002300}
2301
John Zulauf64ffe552021-02-06 10:25:07 -07002302bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2303 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002304 bool skip = false;
2305
John Zulauf7635de32020-05-29 17:14:15 -06002306 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2307 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2308 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2309 // to apply and only copy then, if this proves a hot spot.
2310 std::unique_ptr<AccessContext> proxy_for_current;
2311
John Zulauf355e49b2020-04-24 15:11:15 -06002312 // Validate the "finalLayout" transitions to external
2313 // Get them from where there we're hidding in the extra entry.
2314 const auto &final_transitions = rp_state_->subpass_transitions.back();
2315 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002316 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002317 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2318 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002319 auto *context = trackback.context;
2320
2321 if (transition.prev_pass == current_subpass_) {
2322 if (!proxy_for_current) {
2323 // 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 -07002324 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002325 }
2326 context = proxy_for_current.get();
2327 }
2328
John Zulaufa0a98292020-09-18 09:30:10 -06002329 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2330 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002331 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002332 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002333 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002334 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002335 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2336 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2337 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2338 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002339 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002340 }
2341 }
2342 return skip;
2343}
2344
John Zulauf14940722021-04-12 15:19:02 -06002345void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002346 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002347 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002348}
2349
John Zulauf14940722021-04-12 15:19:02 -06002350void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002351 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2352 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002353
2354 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2355 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002356 const AttachmentViewGen &view_gen = attachment_views_[i];
2357 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002358
2359 const auto &ci = attachment_ci[i];
2360 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002361 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002362 const bool is_color = !(has_depth || has_stencil);
2363
2364 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002365 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2366 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2367 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2368 SyncOrdering::kColorAttachment, tag);
2369 }
John Zulauf1507ee42020-05-18 11:33:09 -06002370 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002371 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002372 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2373 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2374 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2375 SyncOrdering::kDepthStencilAttachment, tag);
2376 }
John Zulauf1507ee42020-05-18 11:33:09 -06002377 }
2378 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002379 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2380 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2381 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2382 SyncOrdering::kDepthStencilAttachment, tag);
2383 }
John Zulauf1507ee42020-05-18 11:33:09 -06002384 }
2385 }
2386 }
2387 }
2388}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002389AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2390 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2391 AttachmentViewGenVector view_gens;
2392 VkExtent3D extent = CastTo3D(render_area.extent);
2393 VkOffset3D offset = CastTo3D(render_area.offset);
2394 view_gens.reserve(attachment_views.size());
2395 for (const auto *view : attachment_views) {
2396 view_gens.emplace_back(view, offset, extent);
2397 }
2398 return view_gens;
2399}
John Zulauf64ffe552021-02-06 10:25:07 -07002400RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2401 VkQueueFlags queue_flags,
2402 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2403 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002404 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002405 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002406 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002407 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002408 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002409 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002410 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002411}
John Zulauf14940722021-04-12 15:19:02 -06002412void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag tag) {
John Zulauf64ffe552021-02-06 10:25:07 -07002413 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002414 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002415 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002416 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002417}
John Zulauf1507ee42020-05-18 11:33:09 -06002418
John Zulauf14940722021-04-12 15:19:02 -06002419void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag prev_subpass_tag, const ResourceUsageTag next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002420 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002421 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2422 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002423
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002424 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2425 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002426 current_subpass_++;
2427 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002428 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2429 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002430 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002431}
2432
John Zulauf14940722021-04-12 15:19:02 -06002433void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002434 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002435 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2436 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002437
John Zulauf355e49b2020-04-24 15:11:15 -06002438 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002439 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002440
2441 // Add the "finalLayout" transitions to external
2442 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002443 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2444 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2445 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002446 const auto &final_transitions = rp_state_->subpass_transitions.back();
2447 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002448 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002449 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002450 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002451 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002452 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002453 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002454 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002455 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002456 }
2457}
2458
Jeremy Gebben40a22942020-12-22 14:22:06 -07002459SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002460 SyncExecScope result;
2461 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002462 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2463 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002464 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2465 return result;
2466}
2467
Jeremy Gebben40a22942020-12-22 14:22:06 -07002468SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002469 SyncExecScope result;
2470 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002471 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2472 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002473 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2474 return result;
2475}
2476
2477SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002478 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002479 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002480 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002481 dst_access_scope = 0;
2482}
2483
2484template <typename Barrier>
2485SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002486 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002487 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002488 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002489 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2490}
2491
2492SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002493 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2494 if (barrier) {
2495 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002496 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002497 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002498
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002499 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002500 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002501 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2502
2503 } else {
2504 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002505 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002506 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2507
2508 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002509 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002510 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2511 }
2512}
2513
2514template <typename Barrier>
2515SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2516 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2517 src_exec_scope = src.exec_scope;
2518 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2519
2520 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002521 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002522 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002523}
2524
John Zulaufb02c1eb2020-10-06 16:33:36 -06002525// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2526void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2527 for (const auto &barrier : barriers) {
2528 ApplyBarrier(barrier, layout_transition);
2529 }
2530}
2531
John Zulauf89311b42020-09-29 16:28:47 -06002532// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2533// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2534// lazily, s.t. no previous access reports should need layout transitions.
John Zulauf14940722021-04-12 15:19:02 -06002535void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002536 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002537 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002538 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002539 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002540 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002541 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002542 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002543}
John Zulauf9cb530d2019-09-30 14:14:10 -06002544HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2545 HazardResult hazard;
2546 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002547 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002548 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002549 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002550 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002551 }
2552 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002553 // Write operation:
2554 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2555 // If reads exists -- test only against them because either:
2556 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2557 // * 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
2558 // the current write happens after the reads, so just test the write against the reades
2559 // Otherwise test against last_write
2560 //
2561 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002562 if (last_reads.size()) {
2563 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002564 if (IsReadHazard(usage_stage, read_access)) {
2565 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2566 break;
2567 }
2568 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002569 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002570 // Write-After-Write check -- if we have a previous write to test against
2571 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002572 }
2573 }
2574 return hazard;
2575}
2576
John Zulauf8e3c3e92021-01-06 11:19:36 -07002577HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2578 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002579 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2580 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002581 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002582 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002583 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2584 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002585 if (IsRead(usage_bit)) {
2586 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2587 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2588 if (is_raw_hazard) {
2589 // NOTE: we know last_write is non-zero
2590 // See if the ordering rules save us from the simple RAW check above
2591 // First check to see if the current usage is covered by the ordering rules
2592 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2593 const bool usage_is_ordered =
2594 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2595 if (usage_is_ordered) {
2596 // Now see of the most recent write (or a subsequent read) are ordered
2597 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2598 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002599 }
2600 }
John Zulauf4285ee92020-09-23 10:20:52 -06002601 if (is_raw_hazard) {
2602 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2603 }
John Zulauf361fb532020-07-22 10:45:39 -06002604 } else {
2605 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002606 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002607 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002608 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002609 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002610 if (usage_write_is_ordered) {
2611 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2612 ordered_stages = GetOrderedStages(ordering);
2613 }
2614 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2615 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002616 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002617 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2618 if (IsReadHazard(usage_stage, read_access)) {
2619 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2620 break;
2621 }
John Zulaufd14743a2020-07-03 09:42:39 -06002622 }
2623 }
John Zulauf4285ee92020-09-23 10:20:52 -06002624 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002625 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002626 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002627 }
John Zulauf69133422020-05-20 14:55:53 -06002628 }
2629 }
2630 return hazard;
2631}
2632
John Zulauf2f952d22020-02-10 11:34:51 -07002633// Asynchronous Hazards occur between subpasses with no connection through the DAG
John Zulauf14940722021-04-12 15:19:02 -06002634HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002635 HazardResult hazard;
2636 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002637 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2638 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2639 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002640 if (IsRead(usage)) {
John Zulauf14940722021-04-12 15:19:02 -06002641 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002642 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002643 }
2644 } else {
John Zulauf14940722021-04-12 15:19:02 -06002645 if (last_write.any() && (write_tag >= start_tag)) {
John Zulauf59e25072020-07-17 10:55:21 -06002646 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002647 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002648 // 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 -07002649 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002650 if (read_access.tag >= start_tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002651 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002652 break;
2653 }
2654 }
John Zulauf2f952d22020-02-10 11:34:51 -07002655 }
2656 }
2657 return hazard;
2658}
2659
Jeremy Gebben40a22942020-12-22 14:22:06 -07002660HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002661 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002662 // Only supporting image layout transitions for now
2663 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2664 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002665 // only test for WAW if there no intervening read operations.
2666 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002667 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002668 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002669 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002670 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002671 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002672 break;
2673 }
2674 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002675 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2676 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2677 }
2678
2679 return hazard;
2680}
2681
Jeremy Gebben40a22942020-12-22 14:22:06 -07002682HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002683 const SyncStageAccessFlags &src_access_scope,
John Zulauf14940722021-04-12 15:19:02 -06002684 const ResourceUsageTag event_tag) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07002685 // Only supporting image layout transitions for now
2686 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2687 HazardResult hazard;
2688 // only test for WAW if there no intervening read operations.
2689 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2690
John Zulaufab7756b2020-12-29 16:10:16 -07002691 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002692 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2693 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002694 for (const auto &read_access : last_reads) {
John Zulauf14940722021-04-12 15:19:02 -06002695 if (read_access.tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002696 // The read is in the events first synchronization scope, so we use a barrier hazard check
2697 // If the read stage is not in the src sync scope
2698 // *AND* not execution chained with an existing sync barrier (that's the or)
2699 // then the barrier access is unsafe (R/W after R)
2700 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2701 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2702 break;
2703 }
2704 } else {
2705 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2706 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2707 }
2708 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002709 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002710 // 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 -06002711 if (write_tag < event_tag) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002712 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2713 // So do a normal barrier hazard check
2714 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2715 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2716 }
2717 } else {
2718 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002719 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2720 }
John Zulaufd14743a2020-07-03 09:42:39 -06002721 }
John Zulauf361fb532020-07-22 10:45:39 -06002722
John Zulauf0cb5be22020-01-23 12:18:22 -07002723 return hazard;
2724}
2725
John Zulauf5f13a792020-03-10 07:31:21 -06002726// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2727// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2728// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2729void ResourceAccessState::Resolve(const ResourceAccessState &other) {
John Zulauf14940722021-04-12 15:19:02 -06002730 if (write_tag < other.write_tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06002731 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2732 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002733 *this = other;
John Zulauf14940722021-04-12 15:19:02 -06002734 } else if (other.write_tag == write_tag) {
2735 // 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 -06002736 // dependency chaining logic or any stage expansion)
2737 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002738 pending_write_barriers |= other.pending_write_barriers;
2739 pending_layout_transition |= other.pending_layout_transition;
2740 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002741
John Zulaufd14743a2020-07-03 09:42:39 -06002742 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002743 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002744 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002745 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002746 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002747 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002748 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002749 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2750 // but we should wait on profiling data for that.
2751 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002752 auto &my_read = last_reads[my_read_index];
2753 if (other_read.stage == my_read.stage) {
John Zulauf14940722021-04-12 15:19:02 -06002754 if (my_read.tag < other_read.tag) {
John Zulauf4285ee92020-09-23 10:20:52 -06002755 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002756 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002757 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002758 my_read.pending_dep_chain = other_read.pending_dep_chain;
2759 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2760 // May require tracking more than one access per stage.
2761 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002762 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06002763 // Since I'm overwriting the fragement stage read, also update the input attachment info
2764 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002765 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002766 }
John Zulauf14940722021-04-12 15:19:02 -06002767 } else if (other_read.tag == my_read.tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06002768 // The read tags match so merge the barriers
2769 my_read.barriers |= other_read.barriers;
2770 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002771 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002772
John Zulauf5f13a792020-03-10 07:31:21 -06002773 break;
2774 }
2775 }
2776 } else {
2777 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002778 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002779 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002780 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002781 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002782 }
John Zulauf5f13a792020-03-10 07:31:21 -06002783 }
2784 }
John Zulauf361fb532020-07-22 10:45:39 -06002785 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002786 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2787 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002788
2789 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2790 // of the copy and other into this using the update first logic.
2791 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2792 // of the other first_accesses... )
2793 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2794 FirstAccesses firsts(std::move(first_accesses_));
2795 first_accesses_.clear();
2796 first_read_stages_ = 0U;
2797 auto a = firsts.begin();
2798 auto a_end = firsts.end();
2799 for (auto &b : other.first_accesses_) {
John Zulauf14940722021-04-12 15:19:02 -06002800 // TODO: Determine whether some tag offset will be needed for PHASE II
2801 while ((a != a_end) && (a->tag < b.tag)) {
John Zulauffaea0ee2021-01-14 14:01:32 -07002802 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2803 ++a;
2804 }
2805 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2806 }
2807 for (; a != a_end; ++a) {
2808 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2809 }
2810 }
John Zulauf5f13a792020-03-10 07:31:21 -06002811}
2812
John Zulauf14940722021-04-12 15:19:02 -06002813void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002814 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2815 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002816 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002817 // Mulitple outstanding reads may be of interest and do dependency chains independently
2818 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2819 const auto usage_stage = PipelineStageBit(usage_index);
2820 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002821 for (auto &read_access : last_reads) {
2822 if (read_access.stage == usage_stage) {
2823 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002824 break;
2825 }
2826 }
2827 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002828 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002829 last_read_stages |= usage_stage;
2830 }
John Zulauf4285ee92020-09-23 10:20:52 -06002831
2832 // 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 -07002833 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002834 // TODO Revisit re: multiple reads for a given stage
2835 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002836 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002837 } else {
2838 // Assume write
2839 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002840 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002841 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002842 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002843}
John Zulauf5f13a792020-03-10 07:31:21 -06002844
John Zulauf89311b42020-09-29 16:28:47 -06002845// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2846// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2847// We can overwrite them as *this* write is now after them.
2848//
2849// 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 -06002850void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002851 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002852 last_read_stages = 0;
2853 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002854 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002855
2856 write_barriers = 0;
2857 write_dependency_chain = 0;
2858 write_tag = tag;
2859 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002860}
2861
John Zulauf89311b42020-09-29 16:28:47 -06002862// Apply the memory barrier without updating the existing barriers. The execution barrier
2863// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2864// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2865// replace the current write barriers or add to them, so accumulate to pending as well.
2866void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2867 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2868 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002869 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2870 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2871 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2872 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07002873 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002874 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002875 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002876 }
John Zulauf89311b42020-09-29 16:28:47 -06002877 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2878 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002879
John Zulauf89311b42020-09-29 16:28:47 -06002880 if (!pending_layout_transition) {
2881 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2882 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002883 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06002884 // 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 -07002885 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
2886 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002887 }
2888 }
John Zulaufa0a98292020-09-18 09:30:10 -06002889 }
John Zulaufa0a98292020-09-18 09:30:10 -06002890}
2891
John Zulauf4a6105a2020-11-17 15:11:05 -07002892// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
2893// changes the "chaining" state, but to keep barriers independent. See discussion above.
John Zulauf14940722021-04-12 15:19:02 -06002894void ResourceAccessState::ApplyBarrier(const ResourceUsageTag scope_tag, const SyncBarrier &barrier, bool layout_transition) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002895 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
2896 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
2897 // in order to know if it's in the excecution scope
2898 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
2899 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
2900 // errors w.r.t. "most recent" accesses.
John Zulauf14940722021-04-12 15:19:02 -06002901 if (layout_transition || ((write_tag < scope_tag) && (barrier.src_access_scope & last_write).any())) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002902 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002903 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002904 }
2905 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2906 pending_layout_transition |= layout_transition;
2907
2908 if (!pending_layout_transition) {
2909 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2910 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002911 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002912 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
2913 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
2914 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
2915 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
2916 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
2917 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
2918 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulauf14940722021-04-12 15:19:02 -06002919 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 -07002920 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002921 }
2922 }
2923 }
2924}
John Zulauf14940722021-04-12 15:19:02 -06002925void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag tag) {
John Zulauf89311b42020-09-29 16:28:47 -06002926 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002927 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2928 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07002929 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06002930 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002931 }
John Zulauf89311b42020-09-29 16:28:47 -06002932
2933 // Apply the accumulate execution barriers (and thus update chaining information)
2934 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07002935 for (auto &read_access : last_reads) {
2936 read_access.barriers |= read_access.pending_dep_chain;
2937 read_execution_barriers |= read_access.barriers;
2938 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06002939 }
2940
2941 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2942 write_dependency_chain |= pending_write_dep_chain;
2943 write_barriers |= pending_write_barriers;
2944 pending_write_dep_chain = 0;
2945 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002946}
2947
John Zulauf59e25072020-07-17 10:55:21 -06002948// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07002949VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
2950 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002951
John Zulaufab7756b2020-12-29 16:10:16 -07002952 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002953 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002954 barriers = read_access.barriers;
2955 break;
John Zulauf59e25072020-07-17 10:55:21 -06002956 }
2957 }
John Zulauf4285ee92020-09-23 10:20:52 -06002958
John Zulauf59e25072020-07-17 10:55:21 -06002959 return barriers;
2960}
2961
Jeremy Gebben40a22942020-12-22 14:22:06 -07002962inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002963 assert(IsRead(usage));
2964 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2965 // * the previous reads are not hazards, and thus last_write must be visible and available to
2966 // any reads that happen after.
2967 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2968 // 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 -07002969 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002970}
2971
Jeremy Gebben40a22942020-12-22 14:22:06 -07002972VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002973 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07002974 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06002975 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002976 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002977 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002978 // 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 -07002979 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06002980 }
2981
2982 return ordered_stages;
2983}
2984
John Zulauf14940722021-04-12 15:19:02 -06002985void ResourceAccessState::UpdateFirst(const ResourceUsageTag tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
John Zulauffaea0ee2021-01-14 14:01:32 -07002986 // Only record until we record a write.
2987 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07002988 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07002989 if (0 == (usage_stage & first_read_stages_)) {
2990 // If this is a read we haven't seen or a write, record.
2991 first_read_stages_ |= usage_stage;
2992 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
2993 }
2994 }
2995}
2996
John Zulaufd1f85d42020-04-15 12:23:15 -06002997void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06002998 auto *access_context = GetAccessContextNoInsert(command_buffer);
2999 if (access_context) {
3000 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003001 }
3002}
3003
John Zulaufd1f85d42020-04-15 12:23:15 -06003004void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3005 auto access_found = cb_access_state.find(command_buffer);
3006 if (access_found != cb_access_state.end()) {
3007 access_found->second->Reset();
3008 cb_access_state.erase(access_found);
3009 }
3010}
3011
John Zulauf9cb530d2019-09-30 14:14:10 -06003012bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3013 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3014 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003015 const auto *cb_context = GetAccessContext(commandBuffer);
3016 assert(cb_context);
3017 if (!cb_context) return skip;
3018 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003019
John Zulauf3d84f1b2020-03-09 13:33:25 -06003020 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003021 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003022 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003023
3024 for (uint32_t region = 0; region < regionCount; region++) {
3025 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003026 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003027 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003028 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003029 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003030 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003031 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003032 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003033 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003034 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003035 }
John Zulauf16adfc92020-04-08 10:28:33 -06003036 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003037 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003038 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003039 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003040 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003041 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003042 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003043 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003044 }
3045 }
3046 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003047 }
3048 return skip;
3049}
3050
3051void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3052 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003053 auto *cb_context = GetAccessContext(commandBuffer);
3054 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003055 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003056 auto *context = cb_context->GetCurrentAccessContext();
3057
John Zulauf9cb530d2019-09-30 14:14:10 -06003058 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003059 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003060
3061 for (uint32_t region = 0; region < regionCount; region++) {
3062 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003063 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003064 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003065 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003066 }
John Zulauf16adfc92020-04-08 10:28:33 -06003067 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003068 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003069 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003070 }
3071 }
3072}
3073
John Zulauf4a6105a2020-11-17 15:11:05 -07003074void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3075 // Clear out events from the command buffer contexts
3076 for (auto &cb_context : cb_access_state) {
3077 cb_context.second->RecordDestroyEvent(event);
3078 }
3079}
3080
Jeff Leger178b1e52020-10-05 12:22:23 -04003081bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3082 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3083 bool skip = false;
3084 const auto *cb_context = GetAccessContext(commandBuffer);
3085 assert(cb_context);
3086 if (!cb_context) return skip;
3087 const auto *context = cb_context->GetCurrentAccessContext();
3088
3089 // If we have no previous accesses, we have no hazards
3090 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3091 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3092
3093 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3094 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3095 if (src_buffer) {
3096 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003097 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003098 if (hazard.hazard) {
3099 // TODO -- add tag information to log msg when useful.
3100 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3101 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3102 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003103 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003104 }
3105 }
3106 if (dst_buffer && !skip) {
3107 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003108 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003109 if (hazard.hazard) {
3110 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3111 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3112 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003113 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003114 }
3115 }
3116 if (skip) break;
3117 }
3118 return skip;
3119}
3120
3121void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3122 auto *cb_context = GetAccessContext(commandBuffer);
3123 assert(cb_context);
3124 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3125 auto *context = cb_context->GetCurrentAccessContext();
3126
3127 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3128 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3129
3130 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3131 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3132 if (src_buffer) {
3133 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003134 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003135 }
3136 if (dst_buffer) {
3137 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003138 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003139 }
3140 }
3141}
3142
John Zulauf5c5e88d2019-12-26 11:22:02 -07003143bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3144 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3145 const VkImageCopy *pRegions) const {
3146 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003147 const auto *cb_access_context = GetAccessContext(commandBuffer);
3148 assert(cb_access_context);
3149 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003150
John Zulauf3d84f1b2020-03-09 13:33:25 -06003151 const auto *context = cb_access_context->GetCurrentAccessContext();
3152 assert(context);
3153 if (!context) return skip;
3154
3155 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3156 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003157 for (uint32_t region = 0; region < regionCount; region++) {
3158 const auto &copy_region = pRegions[region];
3159 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003160 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003161 copy_region.srcOffset, copy_region.extent);
3162 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003163 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003164 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003165 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003166 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003167 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003168 }
3169
3170 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003171 VkExtent3D dst_copy_extent =
3172 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003173 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003174 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003175 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003176 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003177 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003178 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003179 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003180 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003181 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003182 }
3183 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003184
John Zulauf5c5e88d2019-12-26 11:22:02 -07003185 return skip;
3186}
3187
3188void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3189 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3190 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003191 auto *cb_access_context = GetAccessContext(commandBuffer);
3192 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003193 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003194 auto *context = cb_access_context->GetCurrentAccessContext();
3195 assert(context);
3196
John Zulauf5c5e88d2019-12-26 11:22:02 -07003197 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003198 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003199
3200 for (uint32_t region = 0; region < regionCount; region++) {
3201 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003202 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003203 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003204 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003205 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003206 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003207 VkExtent3D dst_copy_extent =
3208 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003209 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003210 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003211 }
3212 }
3213}
3214
Jeff Leger178b1e52020-10-05 12:22:23 -04003215bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3216 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3217 bool skip = false;
3218 const auto *cb_access_context = GetAccessContext(commandBuffer);
3219 assert(cb_access_context);
3220 if (!cb_access_context) return skip;
3221
3222 const auto *context = cb_access_context->GetCurrentAccessContext();
3223 assert(context);
3224 if (!context) return skip;
3225
3226 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3227 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3228 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3229 const auto &copy_region = pCopyImageInfo->pRegions[region];
3230 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003231 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003232 copy_region.srcOffset, copy_region.extent);
3233 if (hazard.hazard) {
3234 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3235 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3236 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003237 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003238 }
3239 }
3240
3241 if (dst_image) {
3242 VkExtent3D dst_copy_extent =
3243 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003244 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003245 copy_region.dstOffset, dst_copy_extent);
3246 if (hazard.hazard) {
3247 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3248 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3249 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003250 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003251 }
3252 if (skip) break;
3253 }
3254 }
3255
3256 return skip;
3257}
3258
3259void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3260 auto *cb_access_context = GetAccessContext(commandBuffer);
3261 assert(cb_access_context);
3262 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3263 auto *context = cb_access_context->GetCurrentAccessContext();
3264 assert(context);
3265
3266 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3267 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3268
3269 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3270 const auto &copy_region = pCopyImageInfo->pRegions[region];
3271 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003272 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003273 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003274 }
3275 if (dst_image) {
3276 VkExtent3D dst_copy_extent =
3277 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003278 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003279 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003280 }
3281 }
3282}
3283
John Zulauf9cb530d2019-09-30 14:14:10 -06003284bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3285 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3286 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3287 uint32_t bufferMemoryBarrierCount,
3288 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3289 uint32_t imageMemoryBarrierCount,
3290 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3291 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003292 const auto *cb_access_context = GetAccessContext(commandBuffer);
3293 assert(cb_access_context);
3294 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003295
John Zulauf36ef9282021-02-02 11:47:24 -07003296 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3297 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3298 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3299 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003300 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003301 return skip;
3302}
3303
3304void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3305 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3306 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3307 uint32_t bufferMemoryBarrierCount,
3308 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3309 uint32_t imageMemoryBarrierCount,
3310 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003311 auto *cb_access_context = GetAccessContext(commandBuffer);
3312 assert(cb_access_context);
3313 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003314
John Zulauf8eda1562021-04-13 17:06:41 -06003315 CommandBufferAccessContext::SyncOpPointer sync_op(
3316 new SyncOpPipelineBarrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask, dstStageMask,
3317 dependencyFlags, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
3318 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers));
3319 const auto tag = sync_op->Record(cb_access_context);
3320 cb_access_context->AddSyncOp(tag, std::move(sync_op));
John Zulauf9cb530d2019-09-30 14:14:10 -06003321}
3322
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003323bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3324 const VkDependencyInfoKHR *pDependencyInfo) const {
3325 bool skip = false;
3326 const auto *cb_access_context = GetAccessContext(commandBuffer);
3327 assert(cb_access_context);
3328 if (!cb_access_context) return skip;
3329
3330 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3331 skip = pipeline_barrier.Validate(*cb_access_context);
3332 return skip;
3333}
3334
3335void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3336 auto *cb_access_context = GetAccessContext(commandBuffer);
3337 assert(cb_access_context);
3338 if (!cb_access_context) return;
3339
John Zulauf8eda1562021-04-13 17:06:41 -06003340 CommandBufferAccessContext::SyncOpPointer sync_op(
3341 new SyncOpPipelineBarrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo));
3342 const auto tag = sync_op->Record(cb_access_context);
3343 cb_access_context->AddSyncOp(tag, std::move(sync_op));
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003344}
3345
John Zulauf9cb530d2019-09-30 14:14:10 -06003346void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3347 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3348 // The state tracker sets up the device state
3349 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3350
John Zulauf5f13a792020-03-10 07:31:21 -06003351 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3352 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003353 // TODO: Find a good way to do this hooklessly.
3354 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3355 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3356 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3357
John Zulaufd1f85d42020-04-15 12:23:15 -06003358 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3359 sync_device_state->ResetCommandBufferCallback(command_buffer);
3360 });
3361 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3362 sync_device_state->FreeCommandBufferCallback(command_buffer);
3363 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003364}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003365
John Zulauf355e49b2020-04-24 15:11:15 -06003366bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003367 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003368 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003369 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003370 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003371 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003372 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003373 }
John Zulauf355e49b2020-04-24 15:11:15 -06003374 return skip;
3375}
3376
3377bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3378 VkSubpassContents contents) const {
3379 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003380 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003381 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003382 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003383 return skip;
3384}
3385
3386bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003387 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003388 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003389 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003390 return skip;
3391}
3392
3393bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3394 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003395 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003396 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003397 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003398 return skip;
3399}
3400
John Zulauf3d84f1b2020-03-09 13:33:25 -06003401void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3402 VkResult result) {
3403 // The state tracker sets up the command buffer state
3404 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3405
3406 // Create/initialize the structure that trackers accesses at the command buffer scope.
3407 auto cb_access_context = GetAccessContext(commandBuffer);
3408 assert(cb_access_context);
3409 cb_access_context->Reset();
3410}
3411
3412void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003413 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003414 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003415 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003416 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003417 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003418 }
3419}
3420
3421void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3422 VkSubpassContents contents) {
3423 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003424 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003425 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003426 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003427}
3428
3429void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3430 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3431 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003432 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003433}
3434
3435void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3436 const VkRenderPassBeginInfo *pRenderPassBegin,
3437 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3438 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003439 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003440}
3441
Mike Schuchardt2df08912020-12-15 16:28:09 -08003442bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003443 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003444 bool skip = false;
3445
3446 auto cb_context = GetAccessContext(commandBuffer);
3447 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003448 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003449 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003450 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003451}
3452
3453bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3454 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003455 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003456 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003457 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003458 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3459 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003460 return skip;
3461}
3462
Mike Schuchardt2df08912020-12-15 16:28:09 -08003463bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3464 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003465 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003466 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003467 return skip;
3468}
3469
3470bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3471 const VkSubpassEndInfo *pSubpassEndInfo) const {
3472 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003473 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003474 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003475}
3476
3477void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003478 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003479 auto cb_context = GetAccessContext(commandBuffer);
3480 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003481 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003482
sfricke-samsung85584a72021-09-30 21:43:38 -07003483 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003484 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003485}
3486
3487void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3488 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003489 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003490 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003491 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003492}
3493
3494void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3495 const VkSubpassEndInfo *pSubpassEndInfo) {
3496 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003497 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003498}
3499
3500void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3501 const VkSubpassEndInfo *pSubpassEndInfo) {
3502 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003503 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003504}
3505
sfricke-samsung85584a72021-09-30 21:43:38 -07003506bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3507 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003508 bool skip = false;
3509
3510 auto cb_context = GetAccessContext(commandBuffer);
3511 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003512 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003513
sfricke-samsung85584a72021-09-30 21:43:38 -07003514 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003515 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003516 return skip;
3517}
3518
3519bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3520 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003521 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003522 return skip;
3523}
3524
Mike Schuchardt2df08912020-12-15 16:28:09 -08003525bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003526 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003527 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003528 return skip;
3529}
3530
3531bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003532 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003533 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003534 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003535 return skip;
3536}
3537
sfricke-samsung85584a72021-09-30 21:43:38 -07003538void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003539 // Resolve the all subpass contexts to the command buffer contexts
3540 auto cb_context = GetAccessContext(commandBuffer);
3541 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003542 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003543
sfricke-samsung85584a72021-09-30 21:43:38 -07003544 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003545 sync_op.Record(cb_context);
3546 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003547}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003548
John Zulauf33fc1d52020-07-17 11:01:10 -06003549// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3550// updates to a resource which do not conflict at the byte level.
3551// TODO: Revisit this rule to see if it needs to be tighter or looser
3552// TODO: Add programatic control over suppression heuristics
3553bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3554 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3555}
3556
John Zulauf3d84f1b2020-03-09 13:33:25 -06003557void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003558 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003559 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003560}
3561
3562void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003563 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003564 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003565}
3566
3567void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003568 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003569 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003570}
locke-lunarga19c71d2020-03-02 18:17:04 -07003571
Jeff Leger178b1e52020-10-05 12:22:23 -04003572template <typename BufferImageCopyRegionType>
3573bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3574 VkImageLayout dstImageLayout, uint32_t regionCount,
3575 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003576 bool skip = false;
3577 const auto *cb_access_context = GetAccessContext(commandBuffer);
3578 assert(cb_access_context);
3579 if (!cb_access_context) return skip;
3580
Jeff Leger178b1e52020-10-05 12:22:23 -04003581 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3582 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3583
locke-lunarga19c71d2020-03-02 18:17:04 -07003584 const auto *context = cb_access_context->GetCurrentAccessContext();
3585 assert(context);
3586 if (!context) return skip;
3587
3588 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003589 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3590
3591 for (uint32_t region = 0; region < regionCount; region++) {
3592 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003593 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003594 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003595 if (src_buffer) {
3596 ResourceAccessRange src_range =
3597 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003598 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003599 if (hazard.hazard) {
3600 // PHASE1 TODO -- add tag information to log msg when useful.
3601 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3602 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3603 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003604 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003605 }
3606 }
3607
Jeremy Gebben40a22942020-12-22 14:22:06 -07003608 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003609 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003610 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003611 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003612 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003613 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003614 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003615 }
3616 if (skip) break;
3617 }
3618 if (skip) break;
3619 }
3620 return skip;
3621}
3622
Jeff Leger178b1e52020-10-05 12:22:23 -04003623bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3624 VkImageLayout dstImageLayout, uint32_t regionCount,
3625 const VkBufferImageCopy *pRegions) const {
3626 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3627 COPY_COMMAND_VERSION_1);
3628}
3629
3630bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3631 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3632 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3633 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3634 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3635}
3636
3637template <typename BufferImageCopyRegionType>
3638void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3639 VkImageLayout dstImageLayout, uint32_t regionCount,
3640 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003641 auto *cb_access_context = GetAccessContext(commandBuffer);
3642 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003643
3644 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3645 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3646
3647 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003648 auto *context = cb_access_context->GetCurrentAccessContext();
3649 assert(context);
3650
3651 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003652 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003653
3654 for (uint32_t region = 0; region < regionCount; region++) {
3655 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003656 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003657 if (src_buffer) {
3658 ResourceAccessRange src_range =
3659 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003660 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003661 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003662 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003663 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003664 }
3665 }
3666}
3667
Jeff Leger178b1e52020-10-05 12:22:23 -04003668void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3669 VkImageLayout dstImageLayout, uint32_t regionCount,
3670 const VkBufferImageCopy *pRegions) {
3671 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3672 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3673}
3674
3675void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3676 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3677 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3678 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3679 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3680 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3681}
3682
3683template <typename BufferImageCopyRegionType>
3684bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3685 VkBuffer dstBuffer, uint32_t regionCount,
3686 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003687 bool skip = false;
3688 const auto *cb_access_context = GetAccessContext(commandBuffer);
3689 assert(cb_access_context);
3690 if (!cb_access_context) return skip;
3691
Jeff Leger178b1e52020-10-05 12:22:23 -04003692 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3693 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3694
locke-lunarga19c71d2020-03-02 18:17:04 -07003695 const auto *context = cb_access_context->GetCurrentAccessContext();
3696 assert(context);
3697 if (!context) return skip;
3698
3699 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3700 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06003701 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07003702 for (uint32_t region = 0; region < regionCount; region++) {
3703 const auto &copy_region = pRegions[region];
3704 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003705 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003706 copy_region.imageOffset, copy_region.imageExtent);
3707 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003708 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003709 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003710 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003711 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003712 }
John Zulauf477700e2021-01-06 11:41:49 -07003713 if (dst_mem) {
3714 ResourceAccessRange dst_range =
3715 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003716 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07003717 if (hazard.hazard) {
3718 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3719 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3720 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003721 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003722 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003723 }
3724 }
3725 if (skip) break;
3726 }
3727 return skip;
3728}
3729
Jeff Leger178b1e52020-10-05 12:22:23 -04003730bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3731 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3732 const VkBufferImageCopy *pRegions) const {
3733 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3734 COPY_COMMAND_VERSION_1);
3735}
3736
3737bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3738 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3739 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3740 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3741 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3742}
3743
3744template <typename BufferImageCopyRegionType>
3745void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3746 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3747 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003748 auto *cb_access_context = GetAccessContext(commandBuffer);
3749 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003750
3751 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3752 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3753
3754 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003755 auto *context = cb_access_context->GetCurrentAccessContext();
3756 assert(context);
3757
3758 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003759 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06003760 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06003761 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003762
3763 for (uint32_t region = 0; region < regionCount; region++) {
3764 const auto &copy_region = pRegions[region];
3765 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003766 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003767 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003768 if (dst_buffer) {
3769 ResourceAccessRange dst_range =
3770 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003771 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003772 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003773 }
3774 }
3775}
3776
Jeff Leger178b1e52020-10-05 12:22:23 -04003777void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3778 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3779 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3780 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3781}
3782
3783void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3784 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3785 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3786 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3787 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3788 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3789}
3790
3791template <typename RegionType>
3792bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3793 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3794 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003795 bool skip = false;
3796 const auto *cb_access_context = GetAccessContext(commandBuffer);
3797 assert(cb_access_context);
3798 if (!cb_access_context) return skip;
3799
3800 const auto *context = cb_access_context->GetCurrentAccessContext();
3801 assert(context);
3802 if (!context) return skip;
3803
3804 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3805 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3806
3807 for (uint32_t region = 0; region < regionCount; region++) {
3808 const auto &blit_region = pRegions[region];
3809 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003810 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3811 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3812 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3813 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3814 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3815 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003816 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003817 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003818 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003819 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003820 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003821 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003822 }
3823 }
3824
3825 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003826 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3827 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3828 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3829 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3830 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3831 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003832 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003833 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003834 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003835 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003836 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003837 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003838 }
3839 if (skip) break;
3840 }
3841 }
3842
3843 return skip;
3844}
3845
Jeff Leger178b1e52020-10-05 12:22:23 -04003846bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3847 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3848 const VkImageBlit *pRegions, VkFilter filter) const {
3849 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3850 "vkCmdBlitImage");
3851}
3852
3853bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3854 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3855 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3856 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3857 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3858}
3859
3860template <typename RegionType>
3861void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3862 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3863 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003864 auto *cb_access_context = GetAccessContext(commandBuffer);
3865 assert(cb_access_context);
3866 auto *context = cb_access_context->GetCurrentAccessContext();
3867 assert(context);
3868
3869 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003870 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003871
3872 for (uint32_t region = 0; region < regionCount; region++) {
3873 const auto &blit_region = pRegions[region];
3874 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003875 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3876 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3877 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3878 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3879 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3880 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003881 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003882 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003883 }
3884 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003885 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3886 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3887 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3888 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3889 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3890 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003891 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003892 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003893 }
3894 }
3895}
locke-lunarg36ba2592020-04-03 09:42:04 -06003896
Jeff Leger178b1e52020-10-05 12:22:23 -04003897void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3898 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3899 const VkImageBlit *pRegions, VkFilter filter) {
3900 auto *cb_access_context = GetAccessContext(commandBuffer);
3901 assert(cb_access_context);
3902 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3903 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3904 pRegions, filter);
3905 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3906}
3907
3908void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3909 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3910 auto *cb_access_context = GetAccessContext(commandBuffer);
3911 assert(cb_access_context);
3912 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3913 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3914 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3915 pBlitImageInfo->filter, tag);
3916}
3917
John Zulauffaea0ee2021-01-14 14:01:32 -07003918bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3919 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
3920 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
3921 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003922 bool skip = false;
3923 if (drawCount == 0) return skip;
3924
3925 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3926 VkDeviceSize size = struct_size;
3927 if (drawCount == 1 || stride == size) {
3928 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003929 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003930 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3931 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003932 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003933 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003934 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003935 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003936 }
3937 } else {
3938 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003939 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003940 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3941 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003942 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003943 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3944 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003945 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003946 break;
3947 }
3948 }
3949 }
3950 return skip;
3951}
3952
John Zulauf14940722021-04-12 15:19:02 -06003953void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag tag, const VkDeviceSize struct_size,
locke-lunarg61870c22020-06-09 14:51:50 -06003954 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3955 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003956 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3957 VkDeviceSize size = struct_size;
3958 if (drawCount == 1 || stride == size) {
3959 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003960 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003961 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003962 } else {
3963 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003964 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003965 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
3966 tag);
locke-lunargff255f92020-05-13 18:53:52 -06003967 }
3968 }
3969}
3970
John Zulauffaea0ee2021-01-14 14:01:32 -07003971bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3972 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3973 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003974 bool skip = false;
3975
3976 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003977 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003978 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3979 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003980 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003981 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003982 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003983 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003984 }
3985 return skip;
3986}
3987
John Zulauf14940722021-04-12 15:19:02 -06003988void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06003989 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003990 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003991 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003992}
3993
locke-lunarg36ba2592020-04-03 09:42:04 -06003994bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06003995 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06003996 const auto *cb_access_context = GetAccessContext(commandBuffer);
3997 assert(cb_access_context);
3998 if (!cb_access_context) return skip;
3999
locke-lunarg61870c22020-06-09 14:51:50 -06004000 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004001 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004002}
4003
4004void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004005 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004006 auto *cb_access_context = GetAccessContext(commandBuffer);
4007 assert(cb_access_context);
4008 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004009
locke-lunarg61870c22020-06-09 14:51:50 -06004010 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004011}
locke-lunarge1a67022020-04-29 00:15:36 -06004012
4013bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004014 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004015 const auto *cb_access_context = GetAccessContext(commandBuffer);
4016 assert(cb_access_context);
4017 if (!cb_access_context) return skip;
4018
4019 const auto *context = cb_access_context->GetCurrentAccessContext();
4020 assert(context);
4021 if (!context) return skip;
4022
locke-lunarg61870c22020-06-09 14:51:50 -06004023 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004024 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4025 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004026 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004027}
4028
4029void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004030 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004031 auto *cb_access_context = GetAccessContext(commandBuffer);
4032 assert(cb_access_context);
4033 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4034 auto *context = cb_access_context->GetCurrentAccessContext();
4035 assert(context);
4036
locke-lunarg61870c22020-06-09 14:51:50 -06004037 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4038 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004039}
4040
4041bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4042 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004043 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004044 const auto *cb_access_context = GetAccessContext(commandBuffer);
4045 assert(cb_access_context);
4046 if (!cb_access_context) return skip;
4047
locke-lunarg61870c22020-06-09 14:51:50 -06004048 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4049 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4050 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004051 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004052}
4053
4054void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4055 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004056 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004057 auto *cb_access_context = GetAccessContext(commandBuffer);
4058 assert(cb_access_context);
4059 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004060
locke-lunarg61870c22020-06-09 14:51:50 -06004061 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4062 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4063 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004064}
4065
4066bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4067 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004068 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004069 const auto *cb_access_context = GetAccessContext(commandBuffer);
4070 assert(cb_access_context);
4071 if (!cb_access_context) return skip;
4072
locke-lunarg61870c22020-06-09 14:51:50 -06004073 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4074 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4075 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004076 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004077}
4078
4079void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4080 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004081 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004082 auto *cb_access_context = GetAccessContext(commandBuffer);
4083 assert(cb_access_context);
4084 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004085
locke-lunarg61870c22020-06-09 14:51:50 -06004086 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4087 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4088 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004089}
4090
4091bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4092 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004093 bool skip = false;
4094 if (drawCount == 0) return skip;
4095
locke-lunargff255f92020-05-13 18:53:52 -06004096 const auto *cb_access_context = GetAccessContext(commandBuffer);
4097 assert(cb_access_context);
4098 if (!cb_access_context) return skip;
4099
4100 const auto *context = cb_access_context->GetCurrentAccessContext();
4101 assert(context);
4102 if (!context) return skip;
4103
locke-lunarg61870c22020-06-09 14:51:50 -06004104 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4105 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004106 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4107 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004108
4109 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4110 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4111 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004112 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004113 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004114}
4115
4116void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4117 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004118 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004119 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004120 auto *cb_access_context = GetAccessContext(commandBuffer);
4121 assert(cb_access_context);
4122 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4123 auto *context = cb_access_context->GetCurrentAccessContext();
4124 assert(context);
4125
locke-lunarg61870c22020-06-09 14:51:50 -06004126 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4127 cb_access_context->RecordDrawSubpassAttachment(tag);
4128 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004129
4130 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4131 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4132 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004133 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004134}
4135
4136bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4137 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004138 bool skip = false;
4139 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004140 const auto *cb_access_context = GetAccessContext(commandBuffer);
4141 assert(cb_access_context);
4142 if (!cb_access_context) return skip;
4143
4144 const auto *context = cb_access_context->GetCurrentAccessContext();
4145 assert(context);
4146 if (!context) return skip;
4147
locke-lunarg61870c22020-06-09 14:51:50 -06004148 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4149 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004150 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4151 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004152
4153 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4154 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4155 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004156 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004157 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004158}
4159
4160void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4161 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004162 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004163 auto *cb_access_context = GetAccessContext(commandBuffer);
4164 assert(cb_access_context);
4165 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4166 auto *context = cb_access_context->GetCurrentAccessContext();
4167 assert(context);
4168
locke-lunarg61870c22020-06-09 14:51:50 -06004169 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4170 cb_access_context->RecordDrawSubpassAttachment(tag);
4171 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004172
4173 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4174 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4175 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004176 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004177}
4178
4179bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4180 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4181 uint32_t stride, const char *function) const {
4182 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004183 const auto *cb_access_context = GetAccessContext(commandBuffer);
4184 assert(cb_access_context);
4185 if (!cb_access_context) return skip;
4186
4187 const auto *context = cb_access_context->GetCurrentAccessContext();
4188 assert(context);
4189 if (!context) return skip;
4190
locke-lunarg61870c22020-06-09 14:51:50 -06004191 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4192 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004193 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4194 maxDrawCount, stride, function);
4195 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004196
4197 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4198 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4199 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004200 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004201 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004202}
4203
4204bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4205 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4206 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004207 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4208 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004209}
4210
sfricke-samsung85584a72021-09-30 21:43:38 -07004211void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4212 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4213 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004214 auto *cb_access_context = GetAccessContext(commandBuffer);
4215 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004216 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004217 auto *context = cb_access_context->GetCurrentAccessContext();
4218 assert(context);
4219
locke-lunarg61870c22020-06-09 14:51:50 -06004220 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4221 cb_access_context->RecordDrawSubpassAttachment(tag);
4222 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4223 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004224
4225 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4226 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4227 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004228 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004229}
4230
sfricke-samsung85584a72021-09-30 21:43:38 -07004231void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4232 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4233 uint32_t stride) {
4234 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4235 stride);
4236 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4237 CMD_DRAWINDIRECTCOUNT);
4238}
locke-lunarge1a67022020-04-29 00:15:36 -06004239bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4240 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4241 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004242 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4243 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004244}
4245
4246void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4247 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4248 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004249 StateTracker::PreCallRecordCmdDrawIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4250 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004251 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4252 CMD_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004253}
4254
4255bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4256 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4257 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004258 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4259 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004260}
4261
4262void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4263 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4264 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004265 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4266 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004267 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4268 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004269}
4270
4271bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4272 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4273 uint32_t stride, const char *function) const {
4274 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004275 const auto *cb_access_context = GetAccessContext(commandBuffer);
4276 assert(cb_access_context);
4277 if (!cb_access_context) return skip;
4278
4279 const auto *context = cb_access_context->GetCurrentAccessContext();
4280 assert(context);
4281 if (!context) return skip;
4282
locke-lunarg61870c22020-06-09 14:51:50 -06004283 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4284 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004285 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4286 offset, maxDrawCount, stride, function);
4287 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
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, function);
locke-lunargff255f92020-05-13 18:53:52 -06004293 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004294}
4295
4296bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4297 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4298 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004299 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4300 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004301}
4302
sfricke-samsung85584a72021-09-30 21:43:38 -07004303void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4304 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4305 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004306 auto *cb_access_context = GetAccessContext(commandBuffer);
4307 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004308 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004309 auto *context = cb_access_context->GetCurrentAccessContext();
4310 assert(context);
4311
locke-lunarg61870c22020-06-09 14:51:50 -06004312 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4313 cb_access_context->RecordDrawSubpassAttachment(tag);
4314 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4315 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004316
4317 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4318 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004319 // We will update the index and vertex buffer in SubmitQueue in the future.
4320 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004321}
4322
sfricke-samsung85584a72021-09-30 21:43:38 -07004323void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4324 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4325 uint32_t maxDrawCount, uint32_t stride) {
4326 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4327 maxDrawCount, stride);
4328 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4329 CMD_DRAWINDEXEDINDIRECTCOUNT);
4330}
4331
locke-lunarge1a67022020-04-29 00:15:36 -06004332bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4333 VkDeviceSize offset, VkBuffer countBuffer,
4334 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4335 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004336 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4337 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004338}
4339
4340void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4341 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4342 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004343 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4344 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004345 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4346 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004347}
4348
4349bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4350 VkDeviceSize offset, VkBuffer countBuffer,
4351 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4352 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004353 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4354 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004355}
4356
4357void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4358 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4359 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004360 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4361 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004362 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4363 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004364}
4365
4366bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4367 const VkClearColorValue *pColor, uint32_t rangeCount,
4368 const VkImageSubresourceRange *pRanges) const {
4369 bool skip = false;
4370 const auto *cb_access_context = GetAccessContext(commandBuffer);
4371 assert(cb_access_context);
4372 if (!cb_access_context) return skip;
4373
4374 const auto *context = cb_access_context->GetCurrentAccessContext();
4375 assert(context);
4376 if (!context) return skip;
4377
4378 const auto *image_state = Get<IMAGE_STATE>(image);
4379
4380 for (uint32_t index = 0; index < rangeCount; index++) {
4381 const auto &range = pRanges[index];
4382 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004383 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004384 if (hazard.hazard) {
4385 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004386 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004387 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004388 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004389 }
4390 }
4391 }
4392 return skip;
4393}
4394
4395void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4396 const VkClearColorValue *pColor, uint32_t rangeCount,
4397 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004398 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004399 auto *cb_access_context = GetAccessContext(commandBuffer);
4400 assert(cb_access_context);
4401 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4402 auto *context = cb_access_context->GetCurrentAccessContext();
4403 assert(context);
4404
4405 const auto *image_state = Get<IMAGE_STATE>(image);
4406
4407 for (uint32_t index = 0; index < rangeCount; index++) {
4408 const auto &range = pRanges[index];
4409 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004410 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004411 }
4412 }
4413}
4414
4415bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4416 VkImageLayout imageLayout,
4417 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4418 const VkImageSubresourceRange *pRanges) const {
4419 bool skip = false;
4420 const auto *cb_access_context = GetAccessContext(commandBuffer);
4421 assert(cb_access_context);
4422 if (!cb_access_context) return skip;
4423
4424 const auto *context = cb_access_context->GetCurrentAccessContext();
4425 assert(context);
4426 if (!context) return skip;
4427
4428 const auto *image_state = Get<IMAGE_STATE>(image);
4429
4430 for (uint32_t index = 0; index < rangeCount; index++) {
4431 const auto &range = pRanges[index];
4432 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004433 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004434 if (hazard.hazard) {
4435 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004436 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004437 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004438 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004439 }
4440 }
4441 }
4442 return skip;
4443}
4444
4445void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4446 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4447 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004448 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004449 auto *cb_access_context = GetAccessContext(commandBuffer);
4450 assert(cb_access_context);
4451 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4452 auto *context = cb_access_context->GetCurrentAccessContext();
4453 assert(context);
4454
4455 const auto *image_state = Get<IMAGE_STATE>(image);
4456
4457 for (uint32_t index = 0; index < rangeCount; index++) {
4458 const auto &range = pRanges[index];
4459 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004460 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004461 }
4462 }
4463}
4464
4465bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4466 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4467 VkDeviceSize dstOffset, VkDeviceSize stride,
4468 VkQueryResultFlags flags) const {
4469 bool skip = false;
4470 const auto *cb_access_context = GetAccessContext(commandBuffer);
4471 assert(cb_access_context);
4472 if (!cb_access_context) return skip;
4473
4474 const auto *context = cb_access_context->GetCurrentAccessContext();
4475 assert(context);
4476 if (!context) return skip;
4477
4478 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4479
4480 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004481 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004482 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004483 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004484 skip |=
4485 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4486 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004487 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004488 }
4489 }
locke-lunargff255f92020-05-13 18:53:52 -06004490
4491 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004492 return skip;
4493}
4494
4495void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4496 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4497 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004498 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4499 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004500 auto *cb_access_context = GetAccessContext(commandBuffer);
4501 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004502 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004503 auto *context = cb_access_context->GetCurrentAccessContext();
4504 assert(context);
4505
4506 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4507
4508 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004509 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004510 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004511 }
locke-lunargff255f92020-05-13 18:53:52 -06004512
4513 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004514}
4515
4516bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4517 VkDeviceSize size, uint32_t data) const {
4518 bool skip = false;
4519 const auto *cb_access_context = GetAccessContext(commandBuffer);
4520 assert(cb_access_context);
4521 if (!cb_access_context) return skip;
4522
4523 const auto *context = cb_access_context->GetCurrentAccessContext();
4524 assert(context);
4525 if (!context) return skip;
4526
4527 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4528
4529 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004530 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004531 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004532 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004533 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004534 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004535 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004536 }
4537 }
4538 return skip;
4539}
4540
4541void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4542 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004543 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004544 auto *cb_access_context = GetAccessContext(commandBuffer);
4545 assert(cb_access_context);
4546 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4547 auto *context = cb_access_context->GetCurrentAccessContext();
4548 assert(context);
4549
4550 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4551
4552 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004553 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004554 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004555 }
4556}
4557
4558bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4559 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4560 const VkImageResolve *pRegions) const {
4561 bool skip = false;
4562 const auto *cb_access_context = GetAccessContext(commandBuffer);
4563 assert(cb_access_context);
4564 if (!cb_access_context) return skip;
4565
4566 const auto *context = cb_access_context->GetCurrentAccessContext();
4567 assert(context);
4568 if (!context) return skip;
4569
4570 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4571 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4572
4573 for (uint32_t region = 0; region < regionCount; region++) {
4574 const auto &resolve_region = pRegions[region];
4575 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004576 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004577 resolve_region.srcOffset, resolve_region.extent);
4578 if (hazard.hazard) {
4579 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004580 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004581 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004582 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004583 }
4584 }
4585
4586 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004587 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004588 resolve_region.dstOffset, resolve_region.extent);
4589 if (hazard.hazard) {
4590 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004591 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004592 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004593 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004594 }
4595 if (skip) break;
4596 }
4597 }
4598
4599 return skip;
4600}
4601
4602void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4603 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4604 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004605 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4606 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004607 auto *cb_access_context = GetAccessContext(commandBuffer);
4608 assert(cb_access_context);
4609 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4610 auto *context = cb_access_context->GetCurrentAccessContext();
4611 assert(context);
4612
4613 auto *src_image = Get<IMAGE_STATE>(srcImage);
4614 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4615
4616 for (uint32_t region = 0; region < regionCount; region++) {
4617 const auto &resolve_region = pRegions[region];
4618 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004619 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004620 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004621 }
4622 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004623 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004624 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004625 }
4626 }
4627}
4628
Jeff Leger178b1e52020-10-05 12:22:23 -04004629bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4630 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4631 bool skip = false;
4632 const auto *cb_access_context = GetAccessContext(commandBuffer);
4633 assert(cb_access_context);
4634 if (!cb_access_context) return skip;
4635
4636 const auto *context = cb_access_context->GetCurrentAccessContext();
4637 assert(context);
4638 if (!context) return skip;
4639
4640 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4641 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4642
4643 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4644 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4645 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004646 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004647 resolve_region.srcOffset, resolve_region.extent);
4648 if (hazard.hazard) {
4649 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4650 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4651 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004652 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004653 }
4654 }
4655
4656 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004657 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004658 resolve_region.dstOffset, resolve_region.extent);
4659 if (hazard.hazard) {
4660 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4661 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4662 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004663 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004664 }
4665 if (skip) break;
4666 }
4667 }
4668
4669 return skip;
4670}
4671
4672void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4673 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4674 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4675 auto *cb_access_context = GetAccessContext(commandBuffer);
4676 assert(cb_access_context);
4677 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4678 auto *context = cb_access_context->GetCurrentAccessContext();
4679 assert(context);
4680
4681 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4682 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4683
4684 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4685 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4686 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004687 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004688 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004689 }
4690 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004691 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004692 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004693 }
4694 }
4695}
4696
locke-lunarge1a67022020-04-29 00:15:36 -06004697bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4698 VkDeviceSize dataSize, const void *pData) const {
4699 bool skip = false;
4700 const auto *cb_access_context = GetAccessContext(commandBuffer);
4701 assert(cb_access_context);
4702 if (!cb_access_context) return skip;
4703
4704 const auto *context = cb_access_context->GetCurrentAccessContext();
4705 assert(context);
4706 if (!context) return skip;
4707
4708 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4709
4710 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004711 // VK_WHOLE_SIZE not allowed
4712 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004713 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004714 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004715 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004716 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004717 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004718 }
4719 }
4720 return skip;
4721}
4722
4723void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4724 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004725 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004726 auto *cb_access_context = GetAccessContext(commandBuffer);
4727 assert(cb_access_context);
4728 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4729 auto *context = cb_access_context->GetCurrentAccessContext();
4730 assert(context);
4731
4732 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4733
4734 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004735 // VK_WHOLE_SIZE not allowed
4736 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004737 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004738 }
4739}
locke-lunargff255f92020-05-13 18:53:52 -06004740
4741bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4742 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4743 bool skip = false;
4744 const auto *cb_access_context = GetAccessContext(commandBuffer);
4745 assert(cb_access_context);
4746 if (!cb_access_context) return skip;
4747
4748 const auto *context = cb_access_context->GetCurrentAccessContext();
4749 assert(context);
4750 if (!context) return skip;
4751
4752 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4753
4754 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004755 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004756 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06004757 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004758 skip |=
4759 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4760 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004761 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004762 }
4763 }
4764 return skip;
4765}
4766
4767void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4768 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004769 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004770 auto *cb_access_context = GetAccessContext(commandBuffer);
4771 assert(cb_access_context);
4772 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4773 auto *context = cb_access_context->GetCurrentAccessContext();
4774 assert(context);
4775
4776 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4777
4778 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004779 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004780 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004781 }
4782}
John Zulauf49beb112020-11-04 16:06:31 -07004783
4784bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4785 bool skip = false;
4786 const auto *cb_context = GetAccessContext(commandBuffer);
4787 assert(cb_context);
4788 if (!cb_context) return skip;
4789
John Zulauf36ef9282021-02-02 11:47:24 -07004790 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004791 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004792}
4793
4794void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4795 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4796 auto *cb_context = GetAccessContext(commandBuffer);
4797 assert(cb_context);
4798 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07004799 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4800 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004801}
4802
John Zulauf4edde622021-02-15 08:54:50 -07004803bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4804 const VkDependencyInfoKHR *pDependencyInfo) const {
4805 bool skip = false;
4806 const auto *cb_context = GetAccessContext(commandBuffer);
4807 assert(cb_context);
4808 if (!cb_context || !pDependencyInfo) return skip;
4809
4810 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4811 return set_event_op.Validate(*cb_context);
4812}
4813
4814void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4815 const VkDependencyInfoKHR *pDependencyInfo) {
4816 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
4817 auto *cb_context = GetAccessContext(commandBuffer);
4818 assert(cb_context);
4819 if (!cb_context || !pDependencyInfo) return;
4820
4821 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4822 set_event_op.Record(cb_context);
4823}
4824
John Zulauf49beb112020-11-04 16:06:31 -07004825bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4826 VkPipelineStageFlags stageMask) const {
4827 bool skip = false;
4828 const auto *cb_context = GetAccessContext(commandBuffer);
4829 assert(cb_context);
4830 if (!cb_context) return skip;
4831
John Zulauf36ef9282021-02-02 11:47:24 -07004832 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004833 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004834}
4835
4836void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4837 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4838 auto *cb_context = GetAccessContext(commandBuffer);
4839 assert(cb_context);
4840 if (!cb_context) return;
4841
John Zulauf36ef9282021-02-02 11:47:24 -07004842 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4843 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004844}
4845
John Zulauf4edde622021-02-15 08:54:50 -07004846bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4847 VkPipelineStageFlags2KHR stageMask) const {
4848 bool skip = false;
4849 const auto *cb_context = GetAccessContext(commandBuffer);
4850 assert(cb_context);
4851 if (!cb_context) return skip;
4852
4853 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4854 return reset_event_op.Validate(*cb_context);
4855}
4856
4857void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4858 VkPipelineStageFlags2KHR stageMask) {
4859 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
4860 auto *cb_context = GetAccessContext(commandBuffer);
4861 assert(cb_context);
4862 if (!cb_context) return;
4863
4864 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4865 reset_event_op.Record(cb_context);
4866}
4867
John Zulauf49beb112020-11-04 16:06:31 -07004868bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4869 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4870 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4871 uint32_t bufferMemoryBarrierCount,
4872 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4873 uint32_t imageMemoryBarrierCount,
4874 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4875 bool skip = false;
4876 const auto *cb_context = GetAccessContext(commandBuffer);
4877 assert(cb_context);
4878 if (!cb_context) return skip;
4879
John Zulauf36ef9282021-02-02 11:47:24 -07004880 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4881 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4882 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07004883 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004884}
4885
4886void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4887 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4888 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4889 uint32_t bufferMemoryBarrierCount,
4890 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4891 uint32_t imageMemoryBarrierCount,
4892 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4893 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4894 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4895 imageMemoryBarrierCount, pImageMemoryBarriers);
4896
4897 auto *cb_context = GetAccessContext(commandBuffer);
4898 assert(cb_context);
4899 if (!cb_context) return;
4900
John Zulauf36ef9282021-02-02 11:47:24 -07004901 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4902 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4903 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulauf8eda1562021-04-13 17:06:41 -06004904 wait_events_op.Record(cb_context);
4905 return;
John Zulauf4a6105a2020-11-17 15:11:05 -07004906}
4907
John Zulauf4edde622021-02-15 08:54:50 -07004908bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4909 const VkDependencyInfoKHR *pDependencyInfos) const {
4910 bool skip = false;
4911 const auto *cb_context = GetAccessContext(commandBuffer);
4912 assert(cb_context);
4913 if (!cb_context) return skip;
4914
4915 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4916 skip |= wait_events_op.Validate(*cb_context);
4917 return skip;
4918}
4919
4920void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4921 const VkDependencyInfoKHR *pDependencyInfos) {
4922 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
4923
4924 auto *cb_context = GetAccessContext(commandBuffer);
4925 assert(cb_context);
4926 if (!cb_context) return;
4927
4928 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4929 wait_events_op.Record(cb_context);
4930}
4931
John Zulauf4a6105a2020-11-17 15:11:05 -07004932void SyncEventState::ResetFirstScope() {
4933 for (const auto address_type : kAddressTypes) {
4934 first_scope[static_cast<size_t>(address_type)].clear();
4935 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004936 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07004937}
4938
4939// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07004940SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004941 IgnoreReason reason = NotIgnored;
4942
John Zulauf4edde622021-02-15 08:54:50 -07004943 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
4944 reason = SetVsWait2;
4945 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
4946 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07004947 } else if (unsynchronized_set) {
4948 reason = SetRace;
4949 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004950 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07004951 if (missing_bits) reason = MissingStageBits;
4952 }
4953
4954 return reason;
4955}
4956
Jeremy Gebben40a22942020-12-22 14:22:06 -07004957bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004958 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
4959 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
4960 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07004961}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004962
John Zulauf36ef9282021-02-02 11:47:24 -07004963SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
4964 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4965 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07004966 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4967 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4968 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07004969 : SyncOpBase(cmd), barriers_(1) {
4970 auto &barrier_set = barriers_[0];
4971 barrier_set.dependency_flags = dependencyFlags;
4972 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
4973 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004974 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07004975 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
4976 pMemoryBarriers);
4977 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4978 bufferMemoryBarrierCount, pBufferMemoryBarriers);
4979 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4980 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004981}
4982
John Zulauf4edde622021-02-15 08:54:50 -07004983SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
4984 const VkDependencyInfoKHR *dep_infos)
4985 : SyncOpBase(cmd), barriers_(event_count) {
4986 for (uint32_t i = 0; i < event_count; i++) {
4987 const auto &dep_info = dep_infos[i];
4988 auto &barrier_set = barriers_[i];
4989 barrier_set.dependency_flags = dep_info.dependencyFlags;
4990 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
4991 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
4992 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
4993 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
4994 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
4995 dep_info.pMemoryBarriers);
4996 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
4997 dep_info.pBufferMemoryBarriers);
4998 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
4999 dep_info.pImageMemoryBarriers);
5000 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005001}
5002
John Zulauf36ef9282021-02-02 11:47:24 -07005003SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005004 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5005 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5006 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5007 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5008 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005009 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005010 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5011
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005012SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5013 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005014 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005015
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005016bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5017 bool skip = false;
5018 const auto *context = cb_context.GetCurrentAccessContext();
5019 assert(context);
5020 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005021 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5022
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005023 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005024 const auto &barrier_set = barriers_[0];
5025 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5026 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5027 const auto *image_state = image_barrier.image.get();
5028 if (!image_state) continue;
5029 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5030 if (hazard.hazard) {
5031 // PHASE1 TODO -- add tag information to log msg when useful.
5032 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005033 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005034 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5035 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5036 string_SyncHazard(hazard.hazard), image_barrier.index,
5037 sync_state.report_data->FormatHandle(image_handle).c_str(),
5038 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005039 }
5040 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005041 return skip;
5042}
5043
John Zulaufd5115702021-01-18 12:34:33 -07005044struct SyncOpPipelineBarrierFunctorFactory {
5045 using BarrierOpFunctor = PipelineBarrierOp;
5046 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5047 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5048 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5049 using BufferRange = ResourceAccessRange;
5050 using ImageRange = subresource_adapter::ImageRangeGenerator;
5051 using GlobalRange = ResourceAccessRange;
5052
5053 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5054 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5055 }
John Zulauf14940722021-04-12 15:19:02 -06005056 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005057 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5058 }
5059 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5060 return GlobalBarrierOpFunctor(barrier, false);
5061 }
5062
5063 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5064 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5065 const auto base_address = ResourceBaseAddress(buffer);
5066 return (range + base_address);
5067 }
John Zulauf110413c2021-03-20 05:38:38 -06005068 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005069 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005070
5071 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005072 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005073 return range_gen;
5074 }
5075 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5076};
5077
5078template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005079void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005080 AccessContext *context) {
5081 for (const auto &barrier : barriers) {
5082 const auto *state = barrier.GetState();
5083 if (state) {
5084 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5085 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5086 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5087 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5088 }
5089 }
5090}
5091
5092template <typename Barriers, typename FunctorFactory>
John Zulauf14940722021-04-12 15:19:02 -06005093void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag tag,
John Zulaufd5115702021-01-18 12:34:33 -07005094 AccessContext *access_context) {
5095 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5096 for (const auto &barrier : barriers) {
5097 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5098 }
5099 for (const auto address_type : kAddressTypes) {
5100 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5101 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5102 }
5103}
5104
John Zulauf8eda1562021-04-13 17:06:41 -06005105ResourceUsageTag SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005106 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf8eda1562021-04-13 17:06:41 -06005107 auto *events_context = cb_context->GetCurrentEventsContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005108 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005109
John Zulauf8eda1562021-04-13 17:06:41 -06005110 SyncOpPipelineBarrierFunctorFactory factory;
John Zulauf4edde622021-02-15 08:54:50 -07005111 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5112 assert(barriers_.size() == 1);
5113 const auto &barrier_set = barriers_[0];
5114 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5115 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5116 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulauf4edde622021-02-15 08:54:50 -07005117 if (barrier_set.single_exec_scope) {
John Zulauf8eda1562021-04-13 17:06:41 -06005118 events_context->ApplyBarrier(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005119 } else {
5120 for (const auto &barrier : barrier_set.memory_barriers) {
John Zulauf8eda1562021-04-13 17:06:41 -06005121 events_context->ApplyBarrier(barrier.src_exec_scope, barrier.dst_exec_scope);
John Zulauf4edde622021-02-15 08:54:50 -07005122 }
5123 }
John Zulauf8eda1562021-04-13 17:06:41 -06005124
5125 return tag;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005126}
5127
John Zulauf8eda1562021-04-13 17:06:41 -06005128bool SyncOpPipelineBarrier::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5129 CommandBufferAccessContext *active_context) const {
5130 return false;
5131}
5132
5133void SyncOpPipelineBarrier::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5134 CommandBufferAccessContext *active_context) const {}
5135
John Zulauf4edde622021-02-15 08:54:50 -07005136void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5137 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5138 const VkMemoryBarrier *barriers) {
5139 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005140 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005141 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005142 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005143 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005144 }
5145 if (0 == memory_barrier_count) {
5146 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005147 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005148 }
John Zulauf4edde622021-02-15 08:54:50 -07005149 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005150}
5151
John Zulauf4edde622021-02-15 08:54:50 -07005152void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5153 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5154 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5155 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005156 for (uint32_t index = 0; index < barrier_count; index++) {
5157 const auto &barrier = barriers[index];
5158 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5159 if (buffer) {
5160 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5161 const auto range = MakeRange(barrier.offset, barrier_size);
5162 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005163 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005164 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005165 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005166 }
5167 }
5168}
5169
John Zulauf4edde622021-02-15 08:54:50 -07005170void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5171 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5172 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005173 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005174 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005175 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5176 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5177 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005178 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005179 }
John Zulauf4edde622021-02-15 08:54:50 -07005180 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005181}
5182
John Zulauf4edde622021-02-15 08:54:50 -07005183void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5184 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5185 const VkBufferMemoryBarrier2KHR *barriers) {
5186 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005187 for (uint32_t index = 0; index < barrier_count; index++) {
5188 const auto &barrier = barriers[index];
5189 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5190 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5191 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5192 if (buffer) {
5193 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5194 const auto range = MakeRange(barrier.offset, barrier_size);
5195 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005196 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005197 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005198 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005199 }
5200 }
5201}
5202
John Zulauf4edde622021-02-15 08:54:50 -07005203void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5204 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5205 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5206 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005207 for (uint32_t index = 0; index < barrier_count; index++) {
5208 const auto &barrier = barriers[index];
5209 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5210 if (image) {
5211 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5212 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005213 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005214 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005215 image_memory_barriers.emplace_back();
5216 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005217 }
5218 }
5219}
John Zulaufd5115702021-01-18 12:34:33 -07005220
John Zulauf4edde622021-02-15 08:54:50 -07005221void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5222 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5223 const VkImageMemoryBarrier2KHR *barriers) {
5224 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005225 for (uint32_t index = 0; index < barrier_count; index++) {
5226 const auto &barrier = barriers[index];
5227 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5228 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5229 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5230 if (image) {
5231 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5232 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005233 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005234 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005235 image_memory_barriers.emplace_back();
5236 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005237 }
5238 }
5239}
5240
John Zulauf36ef9282021-02-02 11:47:24 -07005241SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005242 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5243 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5244 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5245 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005246 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005247 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5248 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005249 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005250}
5251
John Zulauf4edde622021-02-15 08:54:50 -07005252SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5253 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5254 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5255 MakeEventsList(sync_state, eventCount, pEvents);
5256 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5257}
5258
John Zulaufd5115702021-01-18 12:34:33 -07005259bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005260 const char *const ignored = "Wait operation is ignored for this event.";
5261 bool skip = false;
5262 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005263 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005264
John Zulauf4edde622021-02-15 08:54:50 -07005265 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5266 const auto &barrier_set = barriers_[barrier_set_index];
5267 if (barrier_set.single_exec_scope) {
5268 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5269 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5270 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5271 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5272 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5273 } else {
5274 const auto &barriers = barrier_set.memory_barriers;
5275 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5276 const auto &barrier = barriers[barrier_index];
5277 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5278 const std::string vuid =
5279 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5280 skip =
5281 sync_state.LogInfo(command_buffer_handle, vuid,
5282 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5283 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5284 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5285 }
5286 }
5287 }
5288 }
John Zulaufd5115702021-01-18 12:34:33 -07005289 }
5290
Jeremy Gebben40a22942020-12-22 14:22:06 -07005291 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005292 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005293 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005294 const auto *events_context = cb_context.GetCurrentEventsContext();
5295 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005296 size_t barrier_set_index = 0;
5297 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5298 for (size_t event_index = 0; event_index < events_.size(); event_index++)
5299 for (const auto &event : events_) {
5300 const auto *sync_event = events_context->Get(event.get());
5301 const auto &barrier_set = barriers_[barrier_set_index];
5302 if (!sync_event) {
5303 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5304 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5305 // new validation error... wait without previously submitted set event...
5306 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
5307 barrier_set_index += barrier_set_incr;
5308 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005309 }
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005310 const auto event_handle = sync_event->event->event();
John Zulauf4edde622021-02-15 08:54:50 -07005311 // TODO add "destroyed" checks
5312
5313 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5314 const auto &src_exec_scope = barrier_set.src_exec_scope;
5315 event_stage_masks |= sync_event->scope.mask_param;
5316 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5317 if (ignore_reason) {
5318 switch (ignore_reason) {
5319 case SyncEventState::ResetWaitRace:
5320 case SyncEventState::Reset2WaitRace: {
5321 // Four permuations of Reset and Wait calls...
5322 const char *vuid =
5323 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5324 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5325 vuid =
Jeremy Gebben476f5e22021-03-01 15:27:20 -07005326 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831" : "VUID-vkCmdResetEvent2KHR-event-03832";
John Zulauf4edde622021-02-15 08:54:50 -07005327 }
5328 const char *const message =
5329 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5330 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5331 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5332 CommandTypeString(sync_event->last_command), ignored);
5333 break;
5334 }
5335 case SyncEventState::SetRace: {
5336 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5337 // this event
5338 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5339 const char *const message =
5340 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5341 const char *const reason = "First synchronization scope is undefined.";
5342 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5343 sync_state.report_data->FormatHandle(event_handle).c_str(),
5344 CommandTypeString(sync_event->last_command), reason, ignored);
5345 break;
5346 }
5347 case SyncEventState::MissingStageBits: {
5348 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5349 // Issue error message that event waited for is not in wait events scope
5350 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5351 const char *const message =
5352 "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5353 ". Bits missing from srcStageMask %s. %s";
5354 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5355 sync_state.report_data->FormatHandle(event_handle).c_str(),
5356 sync_event->scope.mask_param, src_exec_scope.mask_param,
5357 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5358 break;
5359 }
5360 case SyncEventState::SetVsWait2: {
5361 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5362 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5363 sync_state.report_data->FormatHandle(event_handle).c_str(),
5364 CommandTypeString(sync_event->last_command));
5365 break;
5366 }
5367 default:
5368 assert(ignore_reason == SyncEventState::NotIgnored);
5369 }
5370 } else if (barrier_set.image_memory_barriers.size()) {
5371 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5372 const auto *context = cb_context.GetCurrentAccessContext();
5373 assert(context);
5374 for (const auto &image_memory_barrier : image_memory_barriers) {
5375 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5376 const auto *image_state = image_memory_barrier.image.get();
5377 if (!image_state) continue;
John Zulauf110413c2021-03-20 05:38:38 -06005378 const auto &subresource_range = image_memory_barrier.range;
John Zulauf4edde622021-02-15 08:54:50 -07005379 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5380 const auto hazard =
5381 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5382 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5383 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005384 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
John Zulauf4edde622021-02-15 08:54:50 -07005385 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5386 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005387 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
John Zulauf4edde622021-02-15 08:54:50 -07005388 cb_context.FormatUsage(hazard).c_str());
5389 break;
5390 }
John Zulaufd5115702021-01-18 12:34:33 -07005391 }
5392 }
John Zulauf4edde622021-02-15 08:54:50 -07005393 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5394 // 03839
5395 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005396 }
John Zulaufd5115702021-01-18 12:34:33 -07005397
5398 // 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 -07005399 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 -07005400 if (extra_stage_bits) {
5401 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005402 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5403 const char *const vuid =
5404 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005405 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005406 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005407 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005408 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005409 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005410 " vkCmdSetEvent may be in previously submitted command buffer.");
5411 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005412 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005413 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005414 }
5415 }
5416 return skip;
5417}
5418
5419struct SyncOpWaitEventsFunctorFactory {
5420 using BarrierOpFunctor = WaitEventBarrierOp;
5421 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5422 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5423 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5424 using BufferRange = EventSimpleRangeGenerator;
5425 using ImageRange = EventImageRangeGenerator;
5426 using GlobalRange = EventSimpleRangeGenerator;
5427
5428 // Need to restrict to only valid exec and access scope for this event
5429 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5430 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005431 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005432 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5433 return barrier;
5434 }
5435 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5436 auto barrier = RestrictToEvent(barrier_arg);
5437 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5438 }
John Zulauf14940722021-04-12 15:19:02 -06005439 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, ResourceUsageTag tag) const {
John Zulaufd5115702021-01-18 12:34:33 -07005440 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5441 }
5442 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5443 auto barrier = RestrictToEvent(barrier_arg);
5444 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5445 }
5446
5447 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5448 const AccessAddressType address_type = GetAccessAddressType(buffer);
5449 const auto base_address = ResourceBaseAddress(buffer);
5450 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5451 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5452 return filtered_range_gen;
5453 }
John Zulauf110413c2021-03-20 05:38:38 -06005454 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005455 if (!SimpleBinding(image)) return ImageRange();
5456 const auto address_type = GetAccessAddressType(image);
5457 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005458 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005459 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5460
5461 return filtered_range_gen;
5462 }
5463 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5464 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5465 }
5466 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5467 SyncEventState *sync_event;
5468};
5469
John Zulauf8eda1562021-04-13 17:06:41 -06005470ResourceUsageTag SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07005471 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005472 auto *access_context = cb_context->GetCurrentAccessContext();
5473 assert(access_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005474 if (!access_context) return tag;
John Zulauf669dfd52021-01-27 17:15:28 -07005475 auto *events_context = cb_context->GetCurrentEventsContext();
5476 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005477 if (!events_context) return tag;
John Zulaufd5115702021-01-18 12:34:33 -07005478
5479 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5480 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5481 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5482 access_context->ResolvePreviousAccesses();
5483
John Zulaufd5115702021-01-18 12:34:33 -07005484 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5485 // 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 -07005486 size_t barrier_set_index = 0;
5487 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5488 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005489 for (auto &event_shared : events_) {
5490 if (!event_shared.get()) continue;
5491 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005492
John Zulauf4edde622021-02-15 08:54:50 -07005493 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005494
John Zulauf4edde622021-02-15 08:54:50 -07005495 const auto &barrier_set = barriers_[barrier_set_index];
5496 const auto &dst = barrier_set.dst_exec_scope;
5497 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005498 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5499 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5500 // of the barriers is maintained.
5501 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005502 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5503 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5504 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005505
5506 // Apply the global barrier to the event itself (for race condition tracking)
5507 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5508 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5509 sync_event->barriers |= dst.exec_scope;
5510 } else {
5511 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5512 sync_event->barriers = 0U;
5513 }
John Zulauf4edde622021-02-15 08:54:50 -07005514 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005515 }
5516
5517 // Apply the pending barriers
5518 ResolvePendingBarrierFunctor apply_pending_action(tag);
5519 access_context->ApplyToContext(apply_pending_action);
John Zulauf8eda1562021-04-13 17:06:41 -06005520
5521 return tag;
John Zulaufd5115702021-01-18 12:34:33 -07005522}
5523
John Zulauf8eda1562021-04-13 17:06:41 -06005524bool SyncOpWaitEvents::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5525 CommandBufferAccessContext *active_context) const {
5526 return false;
5527}
5528
5529void SyncOpWaitEvents::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5530 CommandBufferAccessContext *active_context) const {}
5531
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005532bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5533 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5534 bool skip = false;
5535 const auto *cb_access_context = GetAccessContext(commandBuffer);
5536 assert(cb_access_context);
5537 if (!cb_access_context) return skip;
5538
5539 const auto *context = cb_access_context->GetCurrentAccessContext();
5540 assert(context);
5541 if (!context) return skip;
5542
5543 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5544
5545 if (dst_buffer) {
5546 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5547 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5548 if (hazard.hazard) {
5549 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5550 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5551 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
John Zulauf14940722021-04-12 15:19:02 -06005552 cb_access_context->FormatUsage(hazard).c_str());
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005553 }
5554 }
5555 return skip;
5556}
5557
John Zulauf669dfd52021-01-27 17:15:28 -07005558void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005559 events_.reserve(event_count);
5560 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005561 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005562 }
5563}
John Zulauf6ce24372021-01-30 05:56:25 -07005564
John Zulauf36ef9282021-02-02 11:47:24 -07005565SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005566 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005567 : SyncOpBase(cmd),
5568 event_(sync_state.GetShared<EVENT_STATE>(event)),
5569 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005570
5571bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005572 auto *events_context = cb_context.GetCurrentEventsContext();
5573 assert(events_context);
5574 bool skip = false;
5575 if (!events_context) return skip;
5576
5577 const auto &sync_state = cb_context.GetSyncState();
5578 const auto *sync_event = events_context->Get(event_);
5579 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5580
5581 const char *const set_wait =
5582 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5583 "hazards.";
5584 const char *message = set_wait; // Only one message this call.
5585 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5586 const char *vuid = nullptr;
5587 switch (sync_event->last_command) {
5588 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005589 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005590 // Needs a barrier between set and reset
5591 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5592 break;
John Zulauf4edde622021-02-15 08:54:50 -07005593 case CMD_WAITEVENTS:
5594 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005595 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5596 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5597 break;
5598 }
5599 default:
5600 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005601 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5602 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005603 break;
5604 }
5605 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005606 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
5607 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005608 CommandTypeString(sync_event->last_command));
5609 }
5610 }
5611 return skip;
5612}
5613
John Zulauf8eda1562021-04-13 17:06:41 -06005614ResourceUsageTag SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
5615 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005616 auto *events_context = cb_context->GetCurrentEventsContext();
5617 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005618 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005619
5620 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06005621 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07005622
5623 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005624 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005625 sync_event->unsynchronized_set = CMD_NONE;
5626 sync_event->ResetFirstScope();
5627 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06005628
5629 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005630}
5631
John Zulauf8eda1562021-04-13 17:06:41 -06005632bool SyncOpResetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5633 CommandBufferAccessContext *active_context) const {
5634 return false;
5635}
5636
5637void SyncOpResetEvent::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5638 CommandBufferAccessContext *active_context) const {}
5639
John Zulauf36ef9282021-02-02 11:47:24 -07005640SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005641 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005642 : SyncOpBase(cmd),
5643 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005644 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5645 dep_info_() {}
5646
5647SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5648 const VkDependencyInfoKHR &dep_info)
5649 : SyncOpBase(cmd),
5650 event_(sync_state.GetShared<EVENT_STATE>(event)),
5651 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5652 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005653
5654bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5655 // 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 -07005656 bool skip = false;
5657
5658 const auto &sync_state = cb_context.GetSyncState();
5659 auto *events_context = cb_context.GetCurrentEventsContext();
5660 assert(events_context);
5661 if (!events_context) return skip;
5662
5663 const auto *sync_event = events_context->Get(event_);
5664 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5665
5666 const char *const reset_set =
5667 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5668 "hazards.";
5669 const char *const wait =
5670 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5671
5672 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07005673 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07005674 const char *message = nullptr;
5675 switch (sync_event->last_command) {
5676 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005677 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005678 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07005679 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07005680 message = reset_set;
5681 break;
5682 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005683 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005684 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07005685 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07005686 message = reset_set;
5687 break;
5688 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07005689 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005690 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07005691 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07005692 message = wait;
5693 break;
5694 default:
5695 // The only other valid last command that wasn't one.
5696 assert(sync_event->last_command == CMD_NONE);
5697 break;
5698 }
John Zulauf4edde622021-02-15 08:54:50 -07005699 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07005700 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07005701 std::string vuid("SYNC-");
5702 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005703 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
5704 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005705 CommandTypeString(sync_event->last_command));
5706 }
5707 }
5708
5709 return skip;
5710}
5711
John Zulauf8eda1562021-04-13 17:06:41 -06005712ResourceUsageTag SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf36ef9282021-02-02 11:47:24 -07005713 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005714 auto *events_context = cb_context->GetCurrentEventsContext();
5715 auto *access_context = cb_context->GetCurrentAccessContext();
5716 assert(events_context);
John Zulauf8eda1562021-04-13 17:06:41 -06005717 if (!events_context) return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005718
5719 auto *sync_event = events_context->GetFromShared(event_);
John Zulauf8eda1562021-04-13 17:06:41 -06005720 if (!sync_event) return tag; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulauf6ce24372021-01-30 05:56:25 -07005721
5722 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5723 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5724 // any issues caused by naive scope setting here.
5725
5726 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5727 // Given:
5728 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5729 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5730
5731 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5732 sync_event->unsynchronized_set = sync_event->last_command;
5733 sync_event->ResetFirstScope();
5734 } else if (sync_event->scope.exec_scope == 0) {
5735 // We only set the scope if there isn't one
5736 sync_event->scope = src_exec_scope_;
5737
5738 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5739 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5740 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5741 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5742 }
5743 };
5744 access_context->ForAll(set_scope);
5745 sync_event->unsynchronized_set = CMD_NONE;
5746 sync_event->first_scope_tag = tag;
5747 }
John Zulauf4edde622021-02-15 08:54:50 -07005748 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
5749 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005750 sync_event->barriers = 0U;
John Zulauf8eda1562021-04-13 17:06:41 -06005751
5752 return tag;
John Zulauf6ce24372021-01-30 05:56:25 -07005753}
John Zulauf64ffe552021-02-06 10:25:07 -07005754
John Zulauf8eda1562021-04-13 17:06:41 -06005755bool SyncOpSetEvent::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5756 CommandBufferAccessContext *active_context) const {
5757 return false;
5758}
5759
5760void SyncOpSetEvent::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5761 CommandBufferAccessContext *active_context) const {}
5762
John Zulauf64ffe552021-02-06 10:25:07 -07005763SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
5764 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07005765 const VkSubpassBeginInfo *pSubpassBeginInfo)
5766 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07005767 if (pRenderPassBegin) {
5768 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
5769 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
5770 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
5771 if (fb_state) {
5772 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
5773 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
5774 // Note that this a safe to presist as long as shared_attachments is not cleared
5775 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08005776 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07005777 attachments_.emplace_back(attachment.get());
5778 }
5779 }
5780 if (pSubpassBeginInfo) {
5781 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
5782 }
5783 }
5784}
5785
5786bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5787 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
5788 bool skip = false;
5789
5790 assert(rp_state_.get());
5791 if (nullptr == rp_state_.get()) return skip;
5792 auto &rp_state = *rp_state_.get();
5793
5794 const uint32_t subpass = 0;
5795
5796 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
5797 // hasn't happened yet)
5798 const std::vector<AccessContext> empty_context_vector;
5799 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
5800 cb_context.GetCurrentAccessContext());
5801
5802 // Validate attachment operations
5803 if (attachments_.size() == 0) return skip;
5804 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07005805
5806 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
5807 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
5808 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
5809 // operations (though it's currently a messy approach)
5810 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
5811 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005812
5813 // Validate load operations if there were no layout transition hazards
5814 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07005815 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
5816 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005817 }
5818
5819 return skip;
5820}
5821
John Zulauf8eda1562021-04-13 17:06:41 -06005822ResourceUsageTag SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5823 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf64ffe552021-02-06 10:25:07 -07005824 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5825 assert(rp_state_.get());
John Zulauf8eda1562021-04-13 17:06:41 -06005826 if (nullptr == rp_state_.get()) return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07005827 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
John Zulauf8eda1562021-04-13 17:06:41 -06005828
5829 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07005830}
5831
John Zulauf8eda1562021-04-13 17:06:41 -06005832bool SyncOpBeginRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5833 CommandBufferAccessContext *active_context) const {
5834 return false;
5835}
5836
5837void SyncOpBeginRenderPass::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5838 CommandBufferAccessContext *active_context) const {}
5839
John Zulauf64ffe552021-02-06 10:25:07 -07005840SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07005841 const VkSubpassEndInfo *pSubpassEndInfo)
5842 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07005843 if (pSubpassBeginInfo) {
5844 subpass_begin_info_.initialize(pSubpassBeginInfo);
5845 }
5846 if (pSubpassEndInfo) {
5847 subpass_end_info_.initialize(pSubpassEndInfo);
5848 }
5849}
5850
5851bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
5852 bool skip = false;
5853 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5854 if (!renderpass_context) return skip;
5855
5856 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
5857 return skip;
5858}
5859
John Zulauf8eda1562021-04-13 17:06:41 -06005860ResourceUsageTag SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07005861 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06005862 // TODO PHASE2 Need to fix renderpass tagging/segregation of barrier and access operations for QueueSubmit time validation
5863 auto prev_tag = cb_context->NextCommandTag(cmd_);
5864 auto next_tag = cb_context->NextSubcommandTag(cmd_);
5865
5866 cb_context->RecordNextSubpass(prev_tag, next_tag);
5867 // TODO PHASE2 This needs to be the tag of the barrier operations
5868 return prev_tag;
5869}
5870
5871bool SyncOpNextSubpass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5872 CommandBufferAccessContext *active_context) const {
5873 return false;
John Zulauf64ffe552021-02-06 10:25:07 -07005874}
5875
sfricke-samsung85584a72021-09-30 21:43:38 -07005876SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
5877 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07005878 if (pSubpassEndInfo) {
5879 subpass_end_info_.initialize(pSubpassEndInfo);
5880 }
5881}
5882
John Zulauf8eda1562021-04-13 17:06:41 -06005883void SyncOpNextSubpass::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5884 CommandBufferAccessContext *active_context) const {}
5885
John Zulauf64ffe552021-02-06 10:25:07 -07005886bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5887 bool skip = false;
5888 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5889
5890 if (!renderpass_context) return skip;
5891 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
5892 return skip;
5893}
5894
John Zulauf8eda1562021-04-13 17:06:41 -06005895ResourceUsageTag SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf64ffe552021-02-06 10:25:07 -07005896 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
John Zulauf8eda1562021-04-13 17:06:41 -06005897 const auto tag = cb_context->NextCommandTag(cmd_);
5898 cb_context->RecordEndRenderPass(tag);
5899 return tag;
John Zulauf64ffe552021-02-06 10:25:07 -07005900}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005901
John Zulauf8eda1562021-04-13 17:06:41 -06005902bool SyncOpEndRenderPass::ReplayValidate(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5903 CommandBufferAccessContext *active_context) const {
5904 return false;
5905}
5906
5907void SyncOpEndRenderPass::ReplayRecord(ResourceUsageTag recorded_tag, const CommandBufferAccessContext &recorded_context,
5908 CommandBufferAccessContext *active_context) const {}
5909
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005910void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5911 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
5912 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
5913 auto *cb_access_context = GetAccessContext(commandBuffer);
5914 assert(cb_access_context);
5915 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5916 auto *context = cb_access_context->GetCurrentAccessContext();
5917 assert(context);
5918
5919 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5920
5921 if (dst_buffer) {
5922 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5923 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
5924 }
5925}
John Zulaufd05c5842021-03-26 11:32:16 -06005926
John Zulaufd0ec59f2021-03-13 14:25:08 -07005927AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
5928 : view_(view), view_mask_(), gen_store_() {
5929 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
5930 const IMAGE_STATE &image_state = *view_->image_state.get();
5931 const auto base_address = ResourceBaseAddress(image_state);
5932 const auto *encoder = image_state.fragment_encoder.get();
5933 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06005934 // Get offset and extent for the view, accounting for possible depth slicing
5935 const VkOffset3D zero_offset = view->GetOffset();
5936 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07005937 // Intentional copy
5938 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
5939 view_mask_ = subres_range.aspectMask;
5940 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
5941 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5942
5943 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
5944 if (depth && (depth != view_mask_)) {
5945 subres_range.aspectMask = depth;
5946 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5947 }
5948 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
5949 if (stencil && (stencil != view_mask_)) {
5950 subres_range.aspectMask = stencil;
5951 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5952 }
5953}
5954
5955const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
5956 const ImageRangeGen *got = nullptr;
5957 switch (gen_type) {
5958 case kViewSubresource:
5959 got = &gen_store_[kViewSubresource];
5960 break;
5961 case kRenderArea:
5962 got = &gen_store_[kRenderArea];
5963 break;
5964 case kDepthOnlyRenderArea:
5965 got =
5966 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
5967 break;
5968 case kStencilOnlyRenderArea:
5969 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
5970 : &gen_store_[Gen::kStencilOnlyRenderArea];
5971 break;
5972 default:
5973 assert(got);
5974 }
5975 return got;
5976}
5977
5978AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
5979 assert(IsValid());
5980 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
5981 if (depth_op) {
5982 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
5983 if (stencil_op) {
5984 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5985 return kRenderArea;
5986 }
5987 return kDepthOnlyRenderArea;
5988 }
5989 if (stencil_op) {
5990 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5991 return kStencilOnlyRenderArea;
5992 }
5993
5994 assert(depth_op || stencil_op);
5995 return kRenderArea;
5996}
5997
5998AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }
John Zulauf8eda1562021-04-13 17:06:41 -06005999
6000void SyncEventsContext::ApplyBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
6001 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
6002 for (auto &event_pair : map_) {
6003 assert(event_pair.second); // Shouldn't be storing empty
6004 auto &sync_event = *event_pair.second;
6005 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
6006 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
6007 sync_event.barriers |= dst.exec_scope;
6008 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
6009 }
6010 }
6011}