blob: e4063a3d3d6584462cf5ebcfb2fcb904dabab0d9 [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
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -0700168static std::string string_UsageTag(const ResourceUsageTag &tag) {
169 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
ZaOniRinku56b86472021-03-23 20:25:05 +0100196 out << ", " << string_UsageTag(tag) << ", reset_no: " << reset_count_ << ")";
John Zulauf1dae9192020-06-16 15:46:44 -0600197 return out.str();
198}
199
John Zulaufd14743a2020-07-03 09:42:39 -0600200// NOTE: the attachement read flag is put *only* in the access scope and not in the exect scope, since the ordering
201// rules apply only to this specific access for this stage, and not the stage as a whole. The ordering detection
202// also reflects this special case for read hazard detection (using access instead of exec scope)
Jeremy Gebben40a22942020-12-22 14:22:06 -0700203static constexpr VkPipelineStageFlags2KHR kColorAttachmentExecScope = VK_PIPELINE_STAGE_2_COLOR_ATTACHMENT_OUTPUT_BIT_KHR;
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700204static const SyncStageAccessFlags kColorAttachmentAccessScope =
205 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_BIT |
206 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT |
207 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE_BIT |
208 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebben40a22942020-12-22 14:22:06 -0700209static constexpr VkPipelineStageFlags2KHR kDepthStencilAttachmentExecScope =
210 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 -0700211static const SyncStageAccessFlags kDepthStencilAttachmentAccessScope =
212 SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_EARLY_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
213 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_READ_BIT | SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT |
214 SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT; // Note: this is intentionally not in the exec scope
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -0700215static constexpr VkPipelineStageFlags2KHR kRasterAttachmentExecScope = kDepthStencilAttachmentExecScope | kColorAttachmentExecScope;
John Zulauf8e3c3e92021-01-06 11:19:36 -0700216static const SyncStageAccessFlags kRasterAttachmentAccessScope = kDepthStencilAttachmentAccessScope | kColorAttachmentAccessScope;
John Zulaufb027cdb2020-05-21 14:25:22 -0600217
John Zulauf8e3c3e92021-01-06 11:19:36 -0700218ResourceAccessState::OrderingBarriers ResourceAccessState::kOrderingRules = {
Jeremy Gebben40a22942020-12-22 14:22:06 -0700219 {{VK_PIPELINE_STAGE_2_NONE_KHR, SyncStageAccessFlags()},
John Zulauf8e3c3e92021-01-06 11:19:36 -0700220 {kColorAttachmentExecScope, kColorAttachmentAccessScope},
221 {kDepthStencilAttachmentExecScope, kDepthStencilAttachmentAccessScope},
222 {kRasterAttachmentExecScope, kRasterAttachmentAccessScope}}};
223
John Zulauf7635de32020-05-29 17:14:15 -0600224// Sometimes we have an internal access conflict, and we using the kCurrentCommandTag to set and detect in temporary/proxy contexts
John Zulauffaea0ee2021-01-14 14:01:32 -0700225static const ResourceUsageTag kCurrentCommandTag(ResourceUsageTag::kMaxIndex, ResourceUsageTag::kMaxCount,
226 ResourceUsageTag::kMaxCount, CMD_NONE);
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:
608 UpdateStateResolveAction(AccessContext &context, const 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_;
617 const ResourceUsageTag &tag_;
618};
619
John Zulauf59e25072020-07-17 10:55:21 -0600620void HazardResult::Set(const ResourceAccessState *access_state_, SyncStageAccessIndex usage_index_, SyncHazard hazard_,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -0700621 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_); }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001146 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1147 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 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001160 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1161 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 }
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07001256 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const 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,
1271 const ResourceUsageTag &scope_tag)
1272 : 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 }
1293 HazardResult DetectAsync(const ResourceAccessRangeMap::const_iterator &pos, const ResourceUsageTag &start_tag) const {
1294 // 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_;
1305 const ResourceUsageTag &scope_tag_;
1306};
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 Zulauf8e3c3e92021-01-06 11:19:36 -07001456 SyncOrdering ordering_rule_, const ResourceUsageTag &tag_)
1457 : 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 Zulauf9cb530d2019-09-30 14:14:10 -06001462 const ResourceUsageTag &tag;
1463};
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 {
1477 const ResourceUsageTag *scope_tag;
1478 SyncBarrier barrier;
1479 bool layout_transition;
1480 WaitEventBarrierOp(const ResourceUsageTag &scope_tag_, const SyncBarrier &barrier_, bool layout_transition_)
1481 : scope_tag(&scope_tag_), barrier(barrier_), layout_transition(layout_transition_) {}
1482 WaitEventBarrierOp() = default;
1483 void operator()(ResourceAccessState *access_state) const {
1484 assert(scope_tag); // Not valid to have a non-scope op executed, default construct included for std::vector support
1485 access_state->ApplyBarrier(*scope_tag, barrier, layout_transition);
1486 }
1487};
John Zulauf1e331ec2020-12-04 18:29:38 -07001488
John Zulauf4a6105a2020-11-17 15:11:05 -07001489// This functor applies a collection of barriers, updating the "pending state" in each touched memory range, and optionally
1490// resolves the pending state. Suitable for processing Global memory barriers, or Subpass Barriers when the "final" barrier
1491// of a collection is known/present.
John Zulauf1e331ec2020-12-04 18:29:38 -07001492template <typename BarrierOp>
John Zulauf89311b42020-09-29 16:28:47 -06001493class ApplyBarrierOpsFunctor {
1494 public:
John Zulauf5c5e88d2019-12-26 11:22:02 -07001495 using Iterator = ResourceAccessRangeMap::iterator;
1496 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
John Zulauf9cb530d2019-09-30 14:14:10 -06001497
John Zulauf5c5e88d2019-12-26 11:22:02 -07001498 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
John Zulauf9cb530d2019-09-30 14:14:10 -06001499 auto &access_state = pos->second;
John Zulauf1e331ec2020-12-04 18:29:38 -07001500 for (const auto &op : barrier_ops_) {
1501 op(&access_state);
John Zulauf89311b42020-09-29 16:28:47 -06001502 }
John Zulauf9cb530d2019-09-30 14:14:10 -06001503
John Zulauf89311b42020-09-29 16:28:47 -06001504 if (resolve_) {
1505 // If this is the last (or only) batch, we can do the pending resolve as the last step in this operation to avoid
1506 // another walk
1507 access_state.ApplyPendingBarriers(tag_);
John Zulauf9cb530d2019-09-30 14:14:10 -06001508 }
1509 return pos;
1510 }
1511
John Zulauf89311b42020-09-29 16:28:47 -06001512 // A valid tag is required IFF layout_transition is true, as transitions are write ops
John Zulaufd5115702021-01-18 12:34:33 -07001513 ApplyBarrierOpsFunctor(bool resolve, size_t size_hint, const ResourceUsageTag &tag)
1514 : resolve_(resolve), barrier_ops_(), tag_(tag) {
1515 barrier_ops_.reserve(size_hint);
1516 }
1517 void EmplaceBack(const BarrierOp &op) { barrier_ops_.emplace_back(op); }
John Zulauf89311b42020-09-29 16:28:47 -06001518
1519 private:
1520 bool resolve_;
John Zulaufd5115702021-01-18 12:34:33 -07001521 std::vector<BarrierOp> barrier_ops_;
John Zulauf1e331ec2020-12-04 18:29:38 -07001522 const ResourceUsageTag &tag_;
1523};
1524
John Zulauf4a6105a2020-11-17 15:11:05 -07001525// This functor applies a single barrier, updating the "pending state" in each touched memory range, but does not
1526// resolve the pendinging state. Suitable for processing Image and Buffer barriers from PipelineBarriers or Events
1527template <typename BarrierOp>
1528class ApplyBarrierFunctor {
1529 public:
1530 using Iterator = ResourceAccessRangeMap::iterator;
1531 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1532
1533 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1534 auto &access_state = pos->second;
1535 barrier_op_(&access_state);
1536 return pos;
1537 }
1538
1539 ApplyBarrierFunctor(const BarrierOp &barrier_op) : barrier_op_(barrier_op) {}
1540
1541 private:
John Zulaufd5115702021-01-18 12:34:33 -07001542 BarrierOp barrier_op_;
John Zulauf4a6105a2020-11-17 15:11:05 -07001543};
1544
John Zulauf1e331ec2020-12-04 18:29:38 -07001545// This functor resolves the pendinging state.
1546class ResolvePendingBarrierFunctor {
1547 public:
1548 using Iterator = ResourceAccessRangeMap::iterator;
1549 inline Iterator Infill(ResourceAccessRangeMap *accesses, Iterator pos, ResourceAccessRange range) const { return pos; }
1550
1551 Iterator operator()(ResourceAccessRangeMap *accesses, Iterator pos) const {
1552 auto &access_state = pos->second;
1553 access_state.ApplyPendingBarriers(tag_);
1554 return pos;
1555 }
1556
1557 ResolvePendingBarrierFunctor(const ResourceUsageTag &tag) : tag_(tag) {}
1558
1559 private:
John Zulauf89311b42020-09-29 16:28:47 -06001560 const ResourceUsageTag &tag_;
John Zulauf9cb530d2019-09-30 14:14:10 -06001561};
1562
John Zulauf8e3c3e92021-01-06 11:19:36 -07001563void AccessContext::UpdateAccessState(AccessAddressType type, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
1564 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
1565 UpdateMemoryAccessStateFunctor action(type, *this, current_usage, ordering_rule, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001566 UpdateMemoryAccessState(&GetAccessStateMap(type), range, action);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001567}
1568
John Zulauf8e3c3e92021-01-06 11:19:36 -07001569void AccessContext::UpdateAccessState(const BUFFER_STATE &buffer, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001570 const ResourceAccessRange &range, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001571 if (!SimpleBinding(buffer)) return;
1572 const auto base_address = ResourceBaseAddress(buffer);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001573 UpdateAccessState(AccessAddressType::kLinear, current_usage, ordering_rule, range + base_address, tag);
John Zulauf16adfc92020-04-08 10:28:33 -06001574}
John Zulauf355e49b2020-04-24 15:11:15 -06001575
John Zulauf8e3c3e92021-01-06 11:19:36 -07001576void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf110413c2021-03-20 05:38:38 -06001577 const VkImageSubresourceRange &subresource_range, const ResourceUsageTag &tag) {
1578 if (!SimpleBinding(image)) return;
1579 const auto base_address = ResourceBaseAddress(image);
1580 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
1581 const auto address_type = ImageAddressType(image);
1582 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1583 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
1584}
1585void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001586 const VkImageSubresourceRange &subresource_range, const VkOffset3D &offset,
John Zulauf540266b2020-04-06 18:54:53 -06001587 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf16adfc92020-04-08 10:28:33 -06001588 if (!SimpleBinding(image)) return;
John Zulauf16adfc92020-04-08 10:28:33 -06001589 const auto base_address = ResourceBaseAddress(image);
John Zulauf150e5332020-12-03 08:52:52 -07001590 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, offset, extent,
1591 base_address);
1592 const auto address_type = ImageAddressType(image);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001593 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
John Zulauf110413c2021-03-20 05:38:38 -06001594 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, &range_gen);
John Zulauf3d84f1b2020-03-09 13:33:25 -06001595}
John Zulaufd0ec59f2021-03-13 14:25:08 -07001596
1597void AccessContext::UpdateAccessState(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type,
1598 SyncStageAccessIndex current_usage, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
1599 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1600 if (!gen) return;
1601 subresource_adapter::ImageRangeGenerator range_gen(*gen);
1602 const auto address_type = view_gen.GetAddressType();
1603 UpdateMemoryAccessStateFunctor action(address_type, *this, current_usage, ordering_rule, tag);
1604 ApplyUpdateAction(address_type, action, &range_gen);
John Zulauf7635de32020-05-29 17:14:15 -06001605}
John Zulauf3d84f1b2020-03-09 13:33:25 -06001606
John Zulauf8e3c3e92021-01-06 11:19:36 -07001607void AccessContext::UpdateAccessState(const IMAGE_STATE &image, SyncStageAccessIndex current_usage, SyncOrdering ordering_rule,
John Zulauf355e49b2020-04-24 15:11:15 -06001608 const VkImageSubresourceLayers &subresource, const VkOffset3D &offset,
1609 const VkExtent3D &extent, const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06001610 VkImageSubresourceRange subresource_range = {subresource.aspectMask, subresource.mipLevel, 1, subresource.baseArrayLayer,
1611 subresource.layerCount};
John Zulauf8e3c3e92021-01-06 11:19:36 -07001612 UpdateAccessState(image, current_usage, ordering_rule, subresource_range, offset, extent, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06001613}
1614
John Zulaufd0ec59f2021-03-13 14:25:08 -07001615template <typename Action, typename RangeGen>
1616void AccessContext::ApplyUpdateAction(AccessAddressType address_type, const Action &action, RangeGen *range_gen_arg) {
1617 assert(range_gen_arg); // Old Google C++ styleguide require non-const object pass by * not &, but this isn't an optional arg.
1618 UpdateMemoryAccessState(&GetAccessStateMap(address_type), action, range_gen_arg);
John Zulauf540266b2020-04-06 18:54:53 -06001619}
1620
1621template <typename Action>
John Zulaufd0ec59f2021-03-13 14:25:08 -07001622void AccessContext::ApplyUpdateAction(const AttachmentViewGen &view_gen, AttachmentViewGen::Gen gen_type, const Action &action) {
1623 const ImageRangeGen *gen = view_gen.GetRangeGen(gen_type);
1624 if (!gen) return;
1625 UpdateMemoryAccessState(&GetAccessStateMap(view_gen.GetAddressType()), action, *gen);
John Zulauf540266b2020-04-06 18:54:53 -06001626}
1627
John Zulaufd0ec59f2021-03-13 14:25:08 -07001628void AccessContext::UpdateAttachmentResolveAccess(const RENDER_PASS_STATE &rp_state,
1629 const AttachmentViewGenVector &attachment_views, uint32_t subpass,
John Zulauf7635de32020-05-29 17:14:15 -06001630 const ResourceUsageTag &tag) {
1631 UpdateStateResolveAction update(*this, tag);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001632 ResolveOperation(update, rp_state, attachment_views, subpass);
John Zulauf7635de32020-05-29 17:14:15 -06001633}
1634
John Zulaufd0ec59f2021-03-13 14:25:08 -07001635void AccessContext::UpdateAttachmentStoreAccess(const RENDER_PASS_STATE &rp_state, const AttachmentViewGenVector &attachment_views,
1636 uint32_t subpass, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06001637 const auto *attachment_ci = rp_state.createInfo.pAttachments;
John Zulaufaff20662020-06-01 14:07:58 -06001638
1639 for (uint32_t i = 0; i < rp_state.createInfo.attachmentCount; i++) {
1640 if (rp_state.attachment_last_subpass[i] == subpass) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001641 const auto &view_gen = attachment_views[i];
1642 if (!view_gen.IsValid()) continue; // UNUSED
John Zulaufaff20662020-06-01 14:07:58 -06001643
1644 const auto &ci = attachment_ci[i];
1645 const bool has_depth = FormatHasDepth(ci.format);
1646 const bool has_stencil = FormatHasStencil(ci.format);
1647 const bool is_color = !(has_depth || has_stencil);
John Zulauf57261402021-08-13 11:32:06 -06001648 const bool store_op_stores = ci.storeOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001649
1650 if (is_color && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001651 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
1652 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001653 } else {
John Zulaufaff20662020-06-01 14:07:58 -06001654 if (has_depth && store_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001655 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
1656 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001657 }
John Zulauf57261402021-08-13 11:32:06 -06001658 const bool stencil_op_stores = ci.stencilStoreOp != VK_ATTACHMENT_STORE_OP_NONE_EXT;
John Zulaufaff20662020-06-01 14:07:58 -06001659 if (has_stencil && stencil_op_stores) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07001660 UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
1661 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE, SyncOrdering::kRaster, tag);
John Zulaufaff20662020-06-01 14:07:58 -06001662 }
1663 }
1664 }
1665 }
1666}
1667
John Zulauf540266b2020-04-06 18:54:53 -06001668template <typename Action>
John Zulaufd5115702021-01-18 12:34:33 -07001669void AccessContext::ApplyToContext(const Action &barrier_action) {
John Zulauf540266b2020-04-06 18:54:53 -06001670 // Note: Barriers do *not* cross context boundaries, applying to accessess within.... (at least for renderpass subpasses)
John Zulauf16adfc92020-04-08 10:28:33 -06001671 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001672 UpdateMemoryAccessState(&GetAccessStateMap(address_type), kFullRange, barrier_action);
John Zulauf540266b2020-04-06 18:54:53 -06001673 }
1674}
1675
1676void AccessContext::ResolveChildContexts(const std::vector<AccessContext> &contexts) {
John Zulauf540266b2020-04-06 18:54:53 -06001677 for (uint32_t subpass_index = 0; subpass_index < contexts.size(); subpass_index++) {
1678 auto &context = contexts[subpass_index];
John Zulauf22aefed2021-03-11 18:14:35 -07001679 ApplyTrackbackStackAction barrier_action(context.GetDstExternalTrackBack().barriers);
John Zulauf16adfc92020-04-08 10:28:33 -06001680 for (const auto address_type : kAddressTypes) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001681 context.ResolveAccessRange(address_type, kFullRange, barrier_action, &GetAccessStateMap(address_type), nullptr, false);
John Zulauf540266b2020-04-06 18:54:53 -06001682 }
1683 }
1684}
1685
John Zulauf355e49b2020-04-24 15:11:15 -06001686// Suitable only for *subpass* access contexts
John Zulaufd0ec59f2021-03-13 14:25:08 -07001687HazardResult AccessContext::DetectSubpassTransitionHazard(const TrackBack &track_back, const AttachmentViewGen &attach_view) const {
1688 if (!attach_view.IsValid()) return HazardResult();
John Zulauf355e49b2020-04-24 15:11:15 -06001689
John Zulauf355e49b2020-04-24 15:11:15 -06001690 // We should never ask for a transition from a context we don't have
John Zulauf7635de32020-05-29 17:14:15 -06001691 assert(track_back.context);
John Zulauf355e49b2020-04-24 15:11:15 -06001692
1693 // Do the detection against the specific prior context independent of other contexts. (Synchronous only)
John Zulaufa0a98292020-09-18 09:30:10 -06001694 // Hazard detection for the transition can be against the merged of the barriers (it only uses src_...)
1695 const auto merged_barrier = MergeBarriers(track_back.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001696 HazardResult hazard = track_back.context->DetectImageBarrierHazard(attach_view, merged_barrier, kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06001697 if (!hazard.hazard) {
1698 // The Async hazard check is against the current context's async set.
John Zulaufd0ec59f2021-03-13 14:25:08 -07001699 hazard = DetectImageBarrierHazard(attach_view, merged_barrier, kDetectAsync);
John Zulauf355e49b2020-04-24 15:11:15 -06001700 }
John Zulaufa0a98292020-09-18 09:30:10 -06001701
John Zulauf355e49b2020-04-24 15:11:15 -06001702 return hazard;
1703}
1704
John Zulaufb02c1eb2020-10-06 16:33:36 -06001705void AccessContext::RecordLayoutTransitions(const RENDER_PASS_STATE &rp_state, uint32_t subpass,
John Zulaufd0ec59f2021-03-13 14:25:08 -07001706 const AttachmentViewGenVector &attachment_views, const ResourceUsageTag &tag) {
John Zulaufb02c1eb2020-10-06 16:33:36 -06001707 const auto &transitions = rp_state.subpass_transitions[subpass];
John Zulauf646cc292020-10-23 09:16:45 -06001708 const ResourceAccessState empty_infill;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001709 for (const auto &transition : transitions) {
1710 const auto prev_pass = transition.prev_pass;
John Zulaufd0ec59f2021-03-13 14:25:08 -07001711 const auto &view_gen = attachment_views[transition.attachment];
1712 if (!view_gen.IsValid()) continue;
John Zulaufb02c1eb2020-10-06 16:33:36 -06001713
1714 const auto *trackback = GetTrackBackFromSubpass(prev_pass);
1715 assert(trackback);
1716
1717 // Import the attachments into the current context
1718 const auto *prev_context = trackback->context;
1719 assert(prev_context);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001720 const auto address_type = view_gen.GetAddressType();
John Zulaufb02c1eb2020-10-06 16:33:36 -06001721 auto &target_map = GetAccessStateMap(address_type);
1722 ApplySubpassTransitionBarriersAction barrier_action(trackback->barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07001723 prev_context->ResolveAccessRange(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action, &target_map,
1724 &empty_infill);
John Zulaufb02c1eb2020-10-06 16:33:36 -06001725 }
1726
John Zulauf86356ca2020-10-19 11:46:41 -06001727 // If there were no transitions skip this global map walk
1728 if (transitions.size()) {
John Zulauf1e331ec2020-12-04 18:29:38 -07001729 ResolvePendingBarrierFunctor apply_pending_action(tag);
John Zulaufd5115702021-01-18 12:34:33 -07001730 ApplyToContext(apply_pending_action);
John Zulauf86356ca2020-10-19 11:46:41 -06001731 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06001732}
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001733
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001734void CommandBufferAccessContext::ApplyGlobalBarriersToEvents(const SyncExecScope &src, const SyncExecScope &dst) {
1735 const bool all_commands_bit = 0 != (src.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
John Zulauf669dfd52021-01-27 17:15:28 -07001736
1737 auto *events_context = GetCurrentEventsContext();
1738 assert(events_context);
1739 for (auto &event_pair : *events_context) {
John Zulauf4a6105a2020-11-17 15:11:05 -07001740 assert(event_pair.second); // Shouldn't be storing empty
1741 auto &sync_event = *event_pair.second;
1742 // Events don't happen at a stage, so we need to check and store the unexpanded ALL_COMMANDS if set for inter-event-calls
Jeremy Gebben9893daf2021-01-04 10:40:50 -07001743 if ((sync_event.barriers & src.exec_scope) || all_commands_bit) {
1744 sync_event.barriers |= dst.exec_scope;
1745 sync_event.barriers |= dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
John Zulauf4a6105a2020-11-17 15:11:05 -07001746 }
1747 }
1748}
1749
John Zulauf355e49b2020-04-24 15:11:15 -06001750
locke-lunarg61870c22020-06-09 14:51:50 -06001751bool CommandBufferAccessContext::ValidateDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1752 const char *func_name) const {
1753 bool skip = false;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001754 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001755 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001756 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001757 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001758 return skip;
1759 }
1760
1761 using DescriptorClass = cvdescriptorset::DescriptorClass;
1762 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1763 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1764 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1765 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1766
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001767 for (const auto &stage_state : pipe->stage_state) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001768 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1769 pipe->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001770 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001771 }
locke-lunarg61870c22020-06-09 14:51:50 -06001772 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001773 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set;
locke-lunarg61870c22020-06-09 14:51:50 -06001774 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001775 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001776 const auto descriptor_type = binding_it.GetType();
1777 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1778 auto array_idx = 0;
1779
1780 if (binding_it.IsVariableDescriptorCount()) {
1781 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1782 }
1783 SyncStageAccessIndex sync_index =
1784 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1785
1786 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1787 uint32_t index = i - index_range.start;
1788 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1789 switch (descriptor->GetClass()) {
1790 case DescriptorClass::ImageSampler:
1791 case DescriptorClass::Image: {
1792 const IMAGE_VIEW_STATE *img_view_state = nullptr;
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001793 VkImageLayout image_layout;
locke-lunarg61870c22020-06-09 14:51:50 -06001794 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001795 const auto image_sampler_descriptor = static_cast<const ImageSamplerDescriptor *>(descriptor);
1796 img_view_state = image_sampler_descriptor->GetImageViewState();
1797 image_layout = image_sampler_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001798 } else {
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001799 const auto image_descriptor = static_cast<const ImageDescriptor *>(descriptor);
1800 img_view_state = image_descriptor->GetImageViewState();
1801 image_layout = image_descriptor->GetImageLayout();
locke-lunarg61870c22020-06-09 14:51:50 -06001802 }
1803 if (!img_view_state) continue;
John Zulauf361fb532020-07-22 10:45:39 -06001804 HazardResult hazard;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001805 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1806 // Descriptors, so we do not have to worry about depth slicing here.
1807 // See: VUID 00343
1808 assert(!img_view_state->IsDepthSliced());
John Zulauf110413c2021-03-20 05:38:38 -06001809 const IMAGE_STATE *img_state = img_view_state->image_state.get();
John Zulauf361fb532020-07-22 10:45:39 -06001810 const auto &subresource_range = img_view_state->normalized_subresource_range;
John Zulauf110413c2021-03-20 05:38:38 -06001811
1812 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
1813 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1814 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
John Zulauf361fb532020-07-22 10:45:39 -06001815 // Input attachments are subject to raster ordering rules
1816 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range,
John Zulauf8e3c3e92021-01-06 11:19:36 -07001817 SyncOrdering::kRaster, offset, extent);
John Zulauf361fb532020-07-22 10:45:39 -06001818 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001819 hazard = current_context_->DetectHazard(*img_state, sync_index, subresource_range);
John Zulauf361fb532020-07-22 10:45:39 -06001820 }
John Zulauf110413c2021-03-20 05:38:38 -06001821
John Zulauf33fc1d52020-07-17 11:01:10 -06001822 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
John Zulauf1dae9192020-06-16 15:46:44 -06001823 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001824 img_view_state->image_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001825 "%s: Hazard %s for %s, in %s, and %s, %s, type: %s, imageLayout: %s, binding #%" PRIu32
1826 ", index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06001827 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001828 sync_state_->report_data->FormatHandle(img_view_state->image_view()).c_str(),
1829 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1830 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001831 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
1832 string_VkDescriptorType(descriptor_type), string_VkImageLayout(image_layout),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001833 set_binding.first.binding, index, FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001834 }
1835 break;
1836 }
1837 case DescriptorClass::TexelBuffer: {
1838 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1839 if (!buf_view_state) continue;
1840 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001841 const ResourceAccessRange range = MakeRange(*buf_view_state);
locke-lunarg61870c22020-06-09 14:51:50 -06001842 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf33fc1d52020-07-17 11:01:10 -06001843 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001844 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001845 buf_view_state->buffer_view(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001846 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1847 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001848 sync_state_->report_data->FormatHandle(buf_view_state->buffer_view()).c_str(),
1849 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1850 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001851 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001852 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001853 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001854 }
1855 break;
1856 }
1857 case DescriptorClass::GeneralBuffer: {
1858 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1859 auto buf_state = buffer_descriptor->GetBufferState();
1860 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001861 const ResourceAccessRange range =
1862 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
locke-lunarg61870c22020-06-09 14:51:50 -06001863 auto hazard = current_context_->DetectHazard(*buf_state, sync_index, range);
John Zulauf3ac701a2020-09-07 14:34:41 -06001864 if (hazard.hazard && !sync_state_->SupressedBoundDescriptorWAW(hazard)) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001865 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001866 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001867 "%s: Hazard %s for %s in %s, %s, and %s, type: %s, binding #%d index %d. Access info %s.",
1868 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001869 sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
1870 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(),
1871 sync_state_->report_data->FormatHandle(pipe->pipeline()).c_str(),
locke-lunarg7cc0ead2020-07-17 14:29:16 -06001872 sync_state_->report_data->FormatHandle(descriptor_set->GetSet()).c_str(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001873 string_VkDescriptorType(descriptor_type), set_binding.first.binding, index,
John Zulauffaea0ee2021-01-14 14:01:32 -07001874 FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06001875 }
1876 break;
1877 }
1878 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1879 default:
1880 break;
1881 }
1882 }
1883 }
1884 }
1885 return skip;
1886}
1887
1888void CommandBufferAccessContext::RecordDispatchDrawDescriptorSet(VkPipelineBindPoint pipelineBindPoint,
1889 const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001890 const PIPELINE_STATE *pipe = nullptr;
locke-lunarg61870c22020-06-09 14:51:50 -06001891 const std::vector<LAST_BOUND_STATE::PER_SET> *per_sets = nullptr;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001892 cb_state_->GetCurrentPipelineAndDesriptorSets(pipelineBindPoint, &pipe, &per_sets);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001893 if (!pipe || !per_sets) {
locke-lunarg61870c22020-06-09 14:51:50 -06001894 return;
1895 }
1896
1897 using DescriptorClass = cvdescriptorset::DescriptorClass;
1898 using BufferDescriptor = cvdescriptorset::BufferDescriptor;
1899 using ImageDescriptor = cvdescriptorset::ImageDescriptor;
1900 using ImageSamplerDescriptor = cvdescriptorset::ImageSamplerDescriptor;
1901 using TexelDescriptor = cvdescriptorset::TexelDescriptor;
1902
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001903 for (const auto &stage_state : pipe->stage_state) {
Jeremy Gebben11af9792021-08-20 10:20:09 -06001904 if (stage_state.stage_flag == VK_SHADER_STAGE_FRAGMENT_BIT && pipe->create_info.graphics.pRasterizationState &&
1905 pipe->create_info.graphics.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarge9f1cdf2020-06-12 12:28:57 -06001906 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001907 }
locke-lunarg61870c22020-06-09 14:51:50 -06001908 for (const auto &set_binding : stage_state.descriptor_uses) {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001909 cvdescriptorset::DescriptorSet *descriptor_set = (*per_sets)[set_binding.first.set].bound_descriptor_set;
locke-lunarg61870c22020-06-09 14:51:50 -06001910 cvdescriptorset::DescriptorSetLayout::ConstBindingIterator binding_it(descriptor_set->GetLayout().get(),
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001911 set_binding.first.binding);
locke-lunarg61870c22020-06-09 14:51:50 -06001912 const auto descriptor_type = binding_it.GetType();
1913 cvdescriptorset::IndexRange index_range = binding_it.GetGlobalIndexRange();
1914 auto array_idx = 0;
1915
1916 if (binding_it.IsVariableDescriptorCount()) {
1917 index_range.end = index_range.start + descriptor_set->GetVariableDescriptorCount();
1918 }
1919 SyncStageAccessIndex sync_index =
1920 GetSyncStageAccessIndexsByDescriptorSet(descriptor_type, set_binding.second, stage_state.stage_flag);
1921
1922 for (uint32_t i = index_range.start; i < index_range.end; ++i, ++array_idx) {
1923 const auto *descriptor = descriptor_set->GetDescriptorFromGlobalIndex(i);
1924 switch (descriptor->GetClass()) {
1925 case DescriptorClass::ImageSampler:
1926 case DescriptorClass::Image: {
1927 const IMAGE_VIEW_STATE *img_view_state = nullptr;
1928 if (descriptor->GetClass() == DescriptorClass::ImageSampler) {
1929 img_view_state = static_cast<const ImageSamplerDescriptor *>(descriptor)->GetImageViewState();
1930 } else {
1931 img_view_state = static_cast<const ImageDescriptor *>(descriptor)->GetImageViewState();
1932 }
1933 if (!img_view_state) continue;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06001934 // NOTE: 2D ImageViews of VK_IMAGE_CREATE_2D_ARRAY_COMPATIBLE_BIT Images are not allowed in
1935 // Descriptors, so we do not have to worry about depth slicing here.
1936 // See: VUID 00343
1937 assert(!img_view_state->IsDepthSliced());
locke-lunarg61870c22020-06-09 14:51:50 -06001938 const IMAGE_STATE *img_state = img_view_state->image_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06001939 if (sync_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ) {
John Zulauf110413c2021-03-20 05:38:38 -06001940 const VkExtent3D extent = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.extent);
1941 const VkOffset3D offset = CastTo3D(cb_state_->activeRenderPassBeginInfo.renderArea.offset);
1942 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kRaster,
1943 img_view_state->normalized_subresource_range, offset, extent, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001944 } else {
John Zulauf110413c2021-03-20 05:38:38 -06001945 current_context_->UpdateAccessState(*img_state, sync_index, SyncOrdering::kNonAttachment,
1946 img_view_state->normalized_subresource_range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001947 }
locke-lunarg61870c22020-06-09 14:51:50 -06001948 break;
1949 }
1950 case DescriptorClass::TexelBuffer: {
1951 auto buf_view_state = static_cast<const TexelDescriptor *>(descriptor)->GetBufferViewState();
1952 if (!buf_view_state) continue;
1953 const BUFFER_STATE *buf_state = buf_view_state->buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001954 const ResourceAccessRange range = MakeRange(*buf_view_state);
John Zulauf8e3c3e92021-01-06 11:19:36 -07001955 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001956 break;
1957 }
1958 case DescriptorClass::GeneralBuffer: {
1959 const auto *buffer_descriptor = static_cast<const BufferDescriptor *>(descriptor);
1960 auto buf_state = buffer_descriptor->GetBufferState();
1961 if (!buf_state) continue;
John Zulauf3e86bf02020-09-12 10:47:57 -06001962 const ResourceAccessRange range =
1963 MakeRange(*buf_state, buffer_descriptor->GetOffset(), buffer_descriptor->GetRange());
John Zulauf8e3c3e92021-01-06 11:19:36 -07001964 current_context_->UpdateAccessState(*buf_state, sync_index, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06001965 break;
1966 }
1967 // TODO: INLINE_UNIFORM_BLOCK_EXT, ACCELERATION_STRUCTURE_KHR
1968 default:
1969 break;
1970 }
1971 }
1972 }
1973 }
1974}
1975
1976bool CommandBufferAccessContext::ValidateDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const char *func_name) const {
1977 bool skip = false;
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06001978 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001979 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06001980 return skip;
1981 }
1982
1983 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
1984 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001985 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06001986
1987 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07001988 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06001989 if (binding_description.binding < binding_buffers_size) {
1990 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06001991 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06001992
locke-lunarg1ae57d62020-11-18 10:49:19 -07001993 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06001994 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
1995 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07001996 auto hazard = current_context_->DetectHazard(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06001997 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06001998 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06001999 buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for vertex %s in %s. Access info %s.",
2000 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(buf_state->buffer()).c_str(),
2001 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002002 }
2003 }
2004 }
2005 return skip;
2006}
2007
2008void CommandBufferAccessContext::RecordDrawVertex(uint32_t vertexCount, uint32_t firstVertex, const ResourceUsageTag &tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002009 const auto *pipe = cb_state_->GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002010 if (!pipe) {
locke-lunarg61870c22020-06-09 14:51:50 -06002011 return;
2012 }
2013 const auto &binding_buffers = cb_state_->current_vertex_buffer_binding_info.vertex_buffer_bindings;
2014 const auto &binding_buffers_size = binding_buffers.size();
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002015 const auto &binding_descriptions_size = pipe->vertex_binding_descriptions_.size();
locke-lunarg61870c22020-06-09 14:51:50 -06002016
2017 for (size_t i = 0; i < binding_descriptions_size; ++i) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002018 const auto &binding_description = pipe->vertex_binding_descriptions_[i];
locke-lunarg61870c22020-06-09 14:51:50 -06002019 if (binding_description.binding < binding_buffers_size) {
2020 const auto &binding_buffer = binding_buffers[binding_description.binding];
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002021 if (binding_buffer.buffer_state == nullptr || binding_buffer.buffer_state->Destroyed()) continue;
locke-lunarg61870c22020-06-09 14:51:50 -06002022
locke-lunarg1ae57d62020-11-18 10:49:19 -07002023 auto *buf_state = binding_buffer.buffer_state.get();
John Zulauf3e86bf02020-09-12 10:47:57 -06002024 const ResourceAccessRange range = GetBufferRange(binding_buffer.offset, buf_state->createInfo.size, firstVertex,
2025 vertexCount, binding_description.stride);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002026 current_context_->UpdateAccessState(*buf_state, SYNC_VERTEX_ATTRIBUTE_INPUT_VERTEX_ATTRIBUTE_READ,
2027 SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002028 }
2029 }
2030}
2031
2032bool CommandBufferAccessContext::ValidateDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const char *func_name) const {
2033 bool skip = false;
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002034 if (cb_state_->index_buffer_binding.buffer_state == nullptr || cb_state_->index_buffer_binding.buffer_state->Destroyed()) {
locke-lunarg1ae57d62020-11-18 10:49:19 -07002035 return skip;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002036 }
locke-lunarg61870c22020-06-09 14:51:50 -06002037
locke-lunarg1ae57d62020-11-18 10:49:19 -07002038 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002039 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002040 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2041 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002042 auto hazard = current_context_->DetectHazard(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, range);
locke-lunarg61870c22020-06-09 14:51:50 -06002043 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002044 skip |= sync_state_->LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002045 index_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard), "%s: Hazard %s for index %s in %s. Access info %s.",
2046 func_name, string_SyncHazard(hazard.hazard), sync_state_->report_data->FormatHandle(index_buf_state->buffer()).c_str(),
2047 sync_state_->report_data->FormatHandle(cb_state_->commandBuffer()).c_str(), FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002048 }
2049
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 skip |= ValidateDrawVertex(UINT32_MAX, 0, func_name);
2053 return skip;
2054}
2055
2056void CommandBufferAccessContext::RecordDrawVertexIndex(uint32_t indexCount, uint32_t firstIndex, const ResourceUsageTag &tag) {
Jeremy Gebben9efe1cf2021-05-15 20:05:09 -06002057 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 -06002058
locke-lunarg1ae57d62020-11-18 10:49:19 -07002059 auto *index_buf_state = cb_state_->index_buffer_binding.buffer_state.get();
locke-lunarg61870c22020-06-09 14:51:50 -06002060 const auto index_size = GetIndexAlignment(cb_state_->index_buffer_binding.index_type);
John Zulauf3e86bf02020-09-12 10:47:57 -06002061 const ResourceAccessRange range = GetBufferRange(cb_state_->index_buffer_binding.offset, index_buf_state->createInfo.size,
2062 firstIndex, indexCount, index_size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07002063 current_context_->UpdateAccessState(*index_buf_state, SYNC_INDEX_INPUT_INDEX_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002064
2065 // TODO: For now, we detect the whole vertex buffer. Index buffer could be changed until SubmitQueue.
2066 // We will detect more accurate range in the future.
2067 RecordDrawVertex(UINT32_MAX, 0, tag);
2068}
2069
2070bool CommandBufferAccessContext::ValidateDrawSubpassAttachment(const char *func_name) const {
locke-lunarg7077d502020-06-18 21:37:26 -06002071 bool skip = false;
2072 if (!current_renderpass_context_) return skip;
John Zulauf64ffe552021-02-06 10:25:07 -07002073 skip |= current_renderpass_context_->ValidateDrawSubpassAttachment(GetExecutionContext(), *cb_state_.get(), func_name);
locke-lunarg7077d502020-06-18 21:37:26 -06002074 return skip;
locke-lunarg61870c22020-06-09 14:51:50 -06002075}
2076
2077void CommandBufferAccessContext::RecordDrawSubpassAttachment(const ResourceUsageTag &tag) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002078 if (current_renderpass_context_) {
John Zulauf64ffe552021-02-06 10:25:07 -07002079 current_renderpass_context_->RecordDrawSubpassAttachment(*cb_state_.get(), tag);
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002080 }
locke-lunarg61870c22020-06-09 14:51:50 -06002081}
2082
John Zulauf64ffe552021-02-06 10:25:07 -07002083void CommandBufferAccessContext::RecordBeginRenderPass(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2084 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2085 const ResourceUsageTag &tag) {
John Zulauf355e49b2020-04-24 15:11:15 -06002086 // Create an access context the current renderpass.
John Zulauf64ffe552021-02-06 10:25:07 -07002087 render_pass_contexts_.emplace_back(rp_state, render_area, GetQueueFlags(), attachment_views, &cb_access_context_);
John Zulauf16adfc92020-04-08 10:28:33 -06002088 current_renderpass_context_ = &render_pass_contexts_.back();
John Zulauf64ffe552021-02-06 10:25:07 -07002089 current_renderpass_context_->RecordBeginRenderPass(tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002090 current_context_ = &current_renderpass_context_->CurrentContext();
John Zulauf16adfc92020-04-08 10:28:33 -06002091}
2092
John Zulauf64ffe552021-02-06 10:25:07 -07002093void CommandBufferAccessContext::RecordNextSubpass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002094 assert(current_renderpass_context_);
John Zulauffaea0ee2021-01-14 14:01:32 -07002095 auto prev_tag = NextCommandTag(command);
2096 auto next_tag = NextSubcommandTag(command);
John Zulauf64ffe552021-02-06 10:25:07 -07002097 current_renderpass_context_->RecordNextSubpass(prev_tag, next_tag);
John Zulauf16adfc92020-04-08 10:28:33 -06002098 current_context_ = &current_renderpass_context_->CurrentContext();
2099}
2100
John Zulauf64ffe552021-02-06 10:25:07 -07002101void CommandBufferAccessContext::RecordEndRenderPass(CMD_TYPE command) {
John Zulauf16adfc92020-04-08 10:28:33 -06002102 assert(current_renderpass_context_);
2103 if (!current_renderpass_context_) return;
2104
John Zulauf64ffe552021-02-06 10:25:07 -07002105 current_renderpass_context_->RecordEndRenderPass(&cb_access_context_, NextCommandTag(command));
John Zulauf355e49b2020-04-24 15:11:15 -06002106 current_context_ = &cb_access_context_;
John Zulauf16adfc92020-04-08 10:28:33 -06002107 current_renderpass_context_ = nullptr;
2108}
2109
John Zulauf4a6105a2020-11-17 15:11:05 -07002110void CommandBufferAccessContext::RecordDestroyEvent(VkEvent event) {
2111 // Erase is okay with the key not being
John Zulauf669dfd52021-01-27 17:15:28 -07002112 const auto *event_state = sync_state_->Get<EVENT_STATE>(event);
2113 if (event_state) {
2114 GetCurrentEventsContext()->Destroy(event_state);
John Zulaufd5115702021-01-18 12:34:33 -07002115 }
2116}
2117
John Zulauf64ffe552021-02-06 10:25:07 -07002118bool RenderPassAccessContext::ValidateDrawSubpassAttachment(const CommandExecutionContext &ex_context, const CMD_BUFFER_STATE &cmd,
John Zulauffaea0ee2021-01-14 14:01:32 -07002119 const char *func_name) const {
locke-lunarg61870c22020-06-09 14:51:50 -06002120 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002121 const auto &sync_state = ex_context.GetSyncState();
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002122 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002123 if (!pipe) {
2124 return skip;
2125 }
2126
2127 const auto &create_info = pipe->create_info.graphics;
2128 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002129 return skip;
2130 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002131 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002132 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg37047832020-06-12 13:44:45 -06002133
John Zulauf1a224292020-06-30 14:52:13 -06002134 const auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002135 // Subpass's inputAttachment has been done in ValidateDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002136 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2137 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002138 if (location >= subpass.colorAttachmentCount ||
2139 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002140 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002141 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002142 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2143 if (!view_gen.IsValid()) continue;
2144 HazardResult hazard =
2145 current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kRenderArea,
2146 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment);
locke-lunarg96dc9632020-06-10 17:22:18 -06002147 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002148 const VkImageView view_handle = view_gen.GetViewState()->image_view();
John Zulaufd0ec59f2021-03-13 14:25:08 -07002149 skip |= sync_state.LogError(view_handle, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002150 "%s: Hazard %s for %s in %s, Subpass #%d, and pColorAttachments #%d. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002151 func_name, string_SyncHazard(hazard.hazard),
John Zulaufd0ec59f2021-03-13 14:25:08 -07002152 sync_state.report_data->FormatHandle(view_handle).c_str(),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002153 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002154 location, ex_context.FormatUsage(hazard).c_str());
locke-lunarg61870c22020-06-09 14:51:50 -06002155 }
2156 }
2157 }
locke-lunarg37047832020-06-12 13:44:45 -06002158
2159 // PHASE1 TODO: Add layout based read/vs. write selection.
2160 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002161 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002162 GetSubpassDepthStencilAttachmentIndex(pipe->create_info.graphics.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002163
2164 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2165 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2166 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002167 bool depth_write = false, stencil_write = false;
2168
2169 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002170 if (!FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2171 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002172 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2173 depth_write = true;
2174 }
2175 // PHASE1 TODO: It needs to check if stencil is writable.
2176 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2177 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2178 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002179 if (!FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002180 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2181 stencil_write = true;
2182 }
2183
2184 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2185 if (depth_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002186 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea,
2187 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2188 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002189 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002190 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002191 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002192 "%s: Hazard %s for %s in %s, Subpass #%d, and depth part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002193 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002194 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2195 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002196 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002197 }
2198 }
2199 if (stencil_write) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002200 HazardResult hazard = current_context.DetectHazard(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea,
2201 SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2202 SyncOrdering::kDepthStencilAttachment);
locke-lunarg37047832020-06-12 13:44:45 -06002203 if (hazard.hazard) {
locke-lunarg88dbb542020-06-23 22:05:42 -06002204 skip |= sync_state.LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002205 view_state.image_view(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06002206 "%s: Hazard %s for %s in %s, Subpass #%d, and stencil part of pDepthStencilAttachment. Access info %s.",
locke-lunarg88dbb542020-06-23 22:05:42 -06002207 func_name, string_SyncHazard(hazard.hazard),
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002208 sync_state.report_data->FormatHandle(view_state.image_view()).c_str(),
2209 sync_state.report_data->FormatHandle(cmd.commandBuffer()).c_str(), cmd.activeSubpass,
John Zulauf64ffe552021-02-06 10:25:07 -07002210 ex_context.FormatUsage(hazard).c_str());
locke-lunarg37047832020-06-12 13:44:45 -06002211 }
locke-lunarg61870c22020-06-09 14:51:50 -06002212 }
2213 }
2214 return skip;
2215}
2216
John Zulauf64ffe552021-02-06 10:25:07 -07002217void RenderPassAccessContext::RecordDrawSubpassAttachment(const CMD_BUFFER_STATE &cmd, const ResourceUsageTag &tag) {
Jeremy Gebben159b3cc2021-06-03 09:09:03 -06002218 const auto *pipe = cmd.GetCurrentPipeline(VK_PIPELINE_BIND_POINT_GRAPHICS);
Jeremy Gebben11af9792021-08-20 10:20:09 -06002219 if (!pipe) {
2220 return;
2221 }
2222
2223 const auto &create_info = pipe->create_info.graphics;
2224 if (create_info.pRasterizationState && create_info.pRasterizationState->rasterizerDiscardEnable) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002225 return;
2226 }
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002227 const auto &list = pipe->fragmentShader_writable_output_location_list;
locke-lunarg61870c22020-06-09 14:51:50 -06002228 const auto &subpass = rp_state_->createInfo.pSubpasses[current_subpass_];
locke-lunarg61870c22020-06-09 14:51:50 -06002229
John Zulauf1a224292020-06-30 14:52:13 -06002230 auto &current_context = CurrentContext();
locke-lunarg44f9bb12020-06-10 14:43:57 -06002231 // Subpass's inputAttachment has been done in RecordDispatchDrawDescriptorSet
locke-lunarg96dc9632020-06-10 17:22:18 -06002232 if (subpass.pColorAttachments && subpass.colorAttachmentCount && !list.empty()) {
2233 for (const auto location : list) {
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002234 if (location >= subpass.colorAttachmentCount ||
2235 subpass.pColorAttachments[location].attachment == VK_ATTACHMENT_UNUSED) {
locke-lunarg96dc9632020-06-10 17:22:18 -06002236 continue;
Nathaniel Cesarioce9b4812020-12-17 08:55:28 -07002237 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002238 const AttachmentViewGen &view_gen = attachment_views_[subpass.pColorAttachments[location].attachment];
2239 current_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea,
2240 SYNC_COLOR_ATTACHMENT_OUTPUT_COLOR_ATTACHMENT_WRITE, SyncOrdering::kColorAttachment,
2241 tag);
locke-lunarg61870c22020-06-09 14:51:50 -06002242 }
2243 }
locke-lunarg37047832020-06-12 13:44:45 -06002244
2245 // PHASE1 TODO: Add layout based read/vs. write selection.
2246 // PHASE1 TODO: Read operations for both depth and stencil are possible in the future.
John Zulaufd0ec59f2021-03-13 14:25:08 -07002247 const uint32_t depth_stencil_attachment =
Jeremy Gebben11af9792021-08-20 10:20:09 -06002248 GetSubpassDepthStencilAttachmentIndex(create_info.pDepthStencilState, subpass.pDepthStencilAttachment);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002249 if ((depth_stencil_attachment != VK_ATTACHMENT_UNUSED) && attachment_views_[depth_stencil_attachment].IsValid()) {
2250 const AttachmentViewGen &view_gen = attachment_views_[depth_stencil_attachment];
2251 const IMAGE_VIEW_STATE &view_state = *view_gen.GetViewState();
locke-lunarg37047832020-06-12 13:44:45 -06002252 bool depth_write = false, stencil_write = false;
John Zulaufd0ec59f2021-03-13 14:25:08 -07002253 const bool has_depth = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_DEPTH_BIT);
2254 const bool has_stencil = 0 != (view_state.normalized_subresource_range.aspectMask & VK_IMAGE_ASPECT_STENCIL_BIT);
locke-lunarg37047832020-06-12 13:44:45 -06002255
2256 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002257 if (has_depth && !FormatIsStencilOnly(view_state.create_info.format) && create_info.pDepthStencilState->depthTestEnable &&
2258 create_info.pDepthStencilState->depthWriteEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002259 IsImageLayoutDepthWritable(subpass.pDepthStencilAttachment->layout)) {
2260 depth_write = true;
2261 }
2262 // PHASE1 TODO: It needs to check if stencil is writable.
2263 // If failOp, passOp, or depthFailOp are not KEEP, and writeMask isn't 0, it's writable.
2264 // If depth test is disable, it's considered depth test passes, and then depthFailOp doesn't run.
2265 // PHASE1 TODO: These validation should be in core_checks.
Jeremy Gebben11af9792021-08-20 10:20:09 -06002266 if (has_stencil && !FormatIsDepthOnly(view_state.create_info.format) && create_info.pDepthStencilState->stencilTestEnable &&
locke-lunarg37047832020-06-12 13:44:45 -06002267 IsImageLayoutStencilWritable(subpass.pDepthStencilAttachment->layout)) {
2268 stencil_write = true;
2269 }
2270
John Zulaufd0ec59f2021-03-13 14:25:08 -07002271 if (depth_write || stencil_write) {
2272 const auto ds_gentype = view_gen.GetDepthStencilRenderAreaGenType(depth_write, stencil_write);
2273 // PHASE1 TODO: Add EARLY stage detection based on ExecutionMode.
2274 current_context.UpdateAccessState(view_gen, ds_gentype, SYNC_LATE_FRAGMENT_TESTS_DEPTH_STENCIL_ATTACHMENT_WRITE,
2275 SyncOrdering::kDepthStencilAttachment, tag);
locke-lunarg37047832020-06-12 13:44:45 -06002276 }
locke-lunarg61870c22020-06-09 14:51:50 -06002277 }
2278}
2279
John Zulauf64ffe552021-02-06 10:25:07 -07002280bool RenderPassAccessContext::ValidateNextSubpass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002281 // PHASE1 TODO: Add Validate Preserve attachments
John Zulauf355e49b2020-04-24 15:11:15 -06002282 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002283 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulaufb027cdb2020-05-21 14:25:22 -06002284 current_subpass_);
John Zulauf64ffe552021-02-06 10:25:07 -07002285 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_, attachment_views_,
John Zulaufaff20662020-06-01 14:07:58 -06002286 func_name);
2287
John Zulauf355e49b2020-04-24 15:11:15 -06002288 const auto next_subpass = current_subpass_ + 1;
John Zulauf1507ee42020-05-18 11:33:09 -06002289 const auto &next_context = subpass_contexts_[next_subpass];
John Zulauf64ffe552021-02-06 10:25:07 -07002290 skip |=
2291 next_context.ValidateLayoutTransitions(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002292 if (!skip) {
2293 // To avoid complex (and buggy) duplication of the affect of layout transitions on load operations, we'll record them
2294 // on a copy of the (empty) next context.
2295 // Note: The resource access map should be empty so hopefully this copy isn't too horrible from a perf POV.
2296 AccessContext temp_context(next_context);
2297 temp_context.RecordLayoutTransitions(*rp_state_, next_subpass, attachment_views_, kCurrentCommandTag);
John Zulauf64ffe552021-02-06 10:25:07 -07002298 skip |=
2299 temp_context.ValidateLoadOperation(ex_context, *rp_state_, render_area_, next_subpass, attachment_views_, func_name);
John Zulaufb02c1eb2020-10-06 16:33:36 -06002300 }
John Zulauf7635de32020-05-29 17:14:15 -06002301 return skip;
2302}
John Zulauf64ffe552021-02-06 10:25:07 -07002303bool RenderPassAccessContext::ValidateEndRenderPass(const CommandExecutionContext &ex_context, const char *func_name) const {
John Zulaufaff20662020-06-01 14:07:58 -06002304 // PHASE1 TODO: Validate Preserve
John Zulauf7635de32020-05-29 17:14:15 -06002305 bool skip = false;
John Zulauf64ffe552021-02-06 10:25:07 -07002306 skip |= CurrentContext().ValidateResolveOperations(ex_context, *rp_state_, render_area_, attachment_views_, func_name,
John Zulauf7635de32020-05-29 17:14:15 -06002307 current_subpass_);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002308 skip |= CurrentContext().ValidateStoreOperation(ex_context, *rp_state_, render_area_, current_subpass_,
2309
2310 attachment_views_, func_name);
John Zulauf64ffe552021-02-06 10:25:07 -07002311 skip |= ValidateFinalSubpassLayoutTransitions(ex_context, func_name);
John Zulauf355e49b2020-04-24 15:11:15 -06002312 return skip;
2313}
2314
John Zulauf64ffe552021-02-06 10:25:07 -07002315AccessContext *RenderPassAccessContext::CreateStoreResolveProxy() const {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002316 return CreateStoreResolveProxyContext(CurrentContext(), *rp_state_, current_subpass_, attachment_views_);
John Zulauf7635de32020-05-29 17:14:15 -06002317}
2318
John Zulauf64ffe552021-02-06 10:25:07 -07002319bool RenderPassAccessContext::ValidateFinalSubpassLayoutTransitions(const CommandExecutionContext &ex_context,
2320 const char *func_name) const {
John Zulauf355e49b2020-04-24 15:11:15 -06002321 bool skip = false;
2322
John Zulauf7635de32020-05-29 17:14:15 -06002323 // As validation methods are const and precede the record/update phase, for any tranistions from the current (last)
2324 // subpass, we have to validate them against a copy of the current AccessContext, with resolve operations applied.
2325 // Note: we could be more efficient by tracking whether or not we actually *have* any changes (e.g. attachment resolve)
2326 // to apply and only copy then, if this proves a hot spot.
2327 std::unique_ptr<AccessContext> proxy_for_current;
2328
John Zulauf355e49b2020-04-24 15:11:15 -06002329 // Validate the "finalLayout" transitions to external
2330 // Get them from where there we're hidding in the extra entry.
2331 const auto &final_transitions = rp_state_->subpass_transitions.back();
2332 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002333 const auto &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002334 const auto &trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
2335 assert(trackback.context); // Transitions are given implicit transitions if the StateTracker is working correctly
John Zulauf7635de32020-05-29 17:14:15 -06002336 auto *context = trackback.context;
2337
2338 if (transition.prev_pass == current_subpass_) {
2339 if (!proxy_for_current) {
2340 // 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 -07002341 proxy_for_current.reset(CreateStoreResolveProxy());
John Zulauf7635de32020-05-29 17:14:15 -06002342 }
2343 context = proxy_for_current.get();
2344 }
2345
John Zulaufa0a98292020-09-18 09:30:10 -06002346 // Use the merged barrier for the hazard check (safe since it just considers the src (first) scope.
2347 const auto merged_barrier = MergeBarriers(trackback.barriers);
John Zulaufd0ec59f2021-03-13 14:25:08 -07002348 auto hazard = context->DetectImageBarrierHazard(view_gen, merged_barrier, AccessContext::DetectOptions::kDetectPrevious);
John Zulauf355e49b2020-04-24 15:11:15 -06002349 if (hazard.hazard) {
John Zulauf64ffe552021-02-06 10:25:07 -07002350 skip |= ex_context.GetSyncState().LogError(
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06002351 rp_state_->renderPass(), string_SyncHazardVUID(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07002352 "%s: Hazard %s with last use subpass %" PRIu32 " for attachment %" PRIu32
2353 " final image layout transition (old_layout: %s, new_layout: %s). Access info %s.",
2354 func_name, string_SyncHazard(hazard.hazard), transition.prev_pass, transition.attachment,
2355 string_VkImageLayout(transition.old_layout), string_VkImageLayout(transition.new_layout),
John Zulauf64ffe552021-02-06 10:25:07 -07002356 ex_context.FormatUsage(hazard).c_str());
John Zulauf355e49b2020-04-24 15:11:15 -06002357 }
2358 }
2359 return skip;
2360}
2361
2362void RenderPassAccessContext::RecordLayoutTransitions(const ResourceUsageTag &tag) {
2363 // Add layout transitions...
John Zulaufb02c1eb2020-10-06 16:33:36 -06002364 subpass_contexts_[current_subpass_].RecordLayoutTransitions(*rp_state_, current_subpass_, attachment_views_, tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002365}
2366
John Zulauf64ffe552021-02-06 10:25:07 -07002367void RenderPassAccessContext::RecordLoadOperations(const ResourceUsageTag &tag) {
John Zulauf1507ee42020-05-18 11:33:09 -06002368 const auto *attachment_ci = rp_state_->createInfo.pAttachments;
2369 auto &subpass_context = subpass_contexts_[current_subpass_];
John Zulauf1507ee42020-05-18 11:33:09 -06002370
2371 for (uint32_t i = 0; i < rp_state_->createInfo.attachmentCount; i++) {
2372 if (rp_state_->attachment_first_subpass[i] == current_subpass_) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002373 const AttachmentViewGen &view_gen = attachment_views_[i];
2374 if (!view_gen.IsValid()) continue; // UNUSED
John Zulauf1507ee42020-05-18 11:33:09 -06002375
2376 const auto &ci = attachment_ci[i];
2377 const bool has_depth = FormatHasDepth(ci.format);
John Zulaufb027cdb2020-05-21 14:25:22 -06002378 const bool has_stencil = FormatHasStencil(ci.format);
John Zulauf1507ee42020-05-18 11:33:09 -06002379 const bool is_color = !(has_depth || has_stencil);
2380
2381 if (is_color) {
John Zulauf57261402021-08-13 11:32:06 -06002382 const SyncStageAccessIndex load_op = ColorLoadUsage(ci.loadOp);
2383 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2384 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kRenderArea, load_op,
2385 SyncOrdering::kColorAttachment, tag);
2386 }
John Zulauf1507ee42020-05-18 11:33:09 -06002387 } else {
John Zulauf1507ee42020-05-18 11:33:09 -06002388 if (has_depth) {
John Zulauf57261402021-08-13 11:32:06 -06002389 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.loadOp);
2390 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2391 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kDepthOnlyRenderArea, load_op,
2392 SyncOrdering::kDepthStencilAttachment, tag);
2393 }
John Zulauf1507ee42020-05-18 11:33:09 -06002394 }
2395 if (has_stencil) {
John Zulauf57261402021-08-13 11:32:06 -06002396 const SyncStageAccessIndex load_op = DepthStencilLoadUsage(ci.stencilLoadOp);
2397 if (load_op != SYNC_ACCESS_INDEX_NONE) {
2398 subpass_context.UpdateAccessState(view_gen, AttachmentViewGen::Gen::kStencilOnlyRenderArea, load_op,
2399 SyncOrdering::kDepthStencilAttachment, tag);
2400 }
John Zulauf1507ee42020-05-18 11:33:09 -06002401 }
2402 }
2403 }
2404 }
2405}
John Zulaufd0ec59f2021-03-13 14:25:08 -07002406AttachmentViewGenVector RenderPassAccessContext::CreateAttachmentViewGen(
2407 const VkRect2D &render_area, const std::vector<const IMAGE_VIEW_STATE *> &attachment_views) {
2408 AttachmentViewGenVector view_gens;
2409 VkExtent3D extent = CastTo3D(render_area.extent);
2410 VkOffset3D offset = CastTo3D(render_area.offset);
2411 view_gens.reserve(attachment_views.size());
2412 for (const auto *view : attachment_views) {
2413 view_gens.emplace_back(view, offset, extent);
2414 }
2415 return view_gens;
2416}
John Zulauf64ffe552021-02-06 10:25:07 -07002417RenderPassAccessContext::RenderPassAccessContext(const RENDER_PASS_STATE &rp_state, const VkRect2D &render_area,
2418 VkQueueFlags queue_flags,
2419 const std::vector<const IMAGE_VIEW_STATE *> &attachment_views,
2420 const AccessContext *external_context)
John Zulaufd0ec59f2021-03-13 14:25:08 -07002421 : rp_state_(&rp_state), render_area_(render_area), current_subpass_(0U), attachment_views_() {
John Zulauf355e49b2020-04-24 15:11:15 -06002422 // Add this for all subpasses here so that they exsist during next subpass validation
John Zulauf64ffe552021-02-06 10:25:07 -07002423 subpass_contexts_.reserve(rp_state_->createInfo.subpassCount);
John Zulauf355e49b2020-04-24 15:11:15 -06002424 for (uint32_t pass = 0; pass < rp_state_->createInfo.subpassCount; pass++) {
John Zulauf1a224292020-06-30 14:52:13 -06002425 subpass_contexts_.emplace_back(pass, queue_flags, rp_state_->subpass_dependencies, subpass_contexts_, external_context);
John Zulauf355e49b2020-04-24 15:11:15 -06002426 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002427 attachment_views_ = CreateAttachmentViewGen(render_area, attachment_views);
John Zulauf64ffe552021-02-06 10:25:07 -07002428}
2429void RenderPassAccessContext::RecordBeginRenderPass(const ResourceUsageTag &tag) {
2430 assert(0 == current_subpass_);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002431 subpass_contexts_[current_subpass_].SetStartTag(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002432 RecordLayoutTransitions(tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002433 RecordLoadOperations(tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002434}
John Zulauf1507ee42020-05-18 11:33:09 -06002435
John Zulauf64ffe552021-02-06 10:25:07 -07002436void RenderPassAccessContext::RecordNextSubpass(const ResourceUsageTag &prev_subpass_tag,
John Zulauffaea0ee2021-01-14 14:01:32 -07002437 const ResourceUsageTag &next_subpass_tag) {
John Zulauf7635de32020-05-29 17:14:15 -06002438 // Resolves are against *prior* subpass context and thus *before* the subpass increment
John Zulaufd0ec59f2021-03-13 14:25:08 -07002439 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
2440 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, prev_subpass_tag);
John Zulauf7635de32020-05-29 17:14:15 -06002441
Jeremy Gebben6ea9d9e2020-12-11 09:41:01 -07002442 // Move to the next sub-command for the new subpass. The resolve and store are logically part of the previous
2443 // subpass, so their tag needs to be different from the layout and load operations below.
John Zulauf355e49b2020-04-24 15:11:15 -06002444 current_subpass_++;
2445 assert(current_subpass_ < subpass_contexts_.size());
John Zulauffaea0ee2021-01-14 14:01:32 -07002446 subpass_contexts_[current_subpass_].SetStartTag(next_subpass_tag);
2447 RecordLayoutTransitions(next_subpass_tag);
John Zulauf64ffe552021-02-06 10:25:07 -07002448 RecordLoadOperations(next_subpass_tag);
John Zulauf355e49b2020-04-24 15:11:15 -06002449}
2450
John Zulauf64ffe552021-02-06 10:25:07 -07002451void RenderPassAccessContext::RecordEndRenderPass(AccessContext *external_context, const ResourceUsageTag &tag) {
John Zulaufaff20662020-06-01 14:07:58 -06002452 // Add the resolve and store accesses
John Zulaufd0ec59f2021-03-13 14:25:08 -07002453 CurrentContext().UpdateAttachmentResolveAccess(*rp_state_, attachment_views_, current_subpass_, tag);
2454 CurrentContext().UpdateAttachmentStoreAccess(*rp_state_, attachment_views_, current_subpass_, tag);
John Zulauf7635de32020-05-29 17:14:15 -06002455
John Zulauf355e49b2020-04-24 15:11:15 -06002456 // Export the accesses from the renderpass...
John Zulauf1a224292020-06-30 14:52:13 -06002457 external_context->ResolveChildContexts(subpass_contexts_);
John Zulauf355e49b2020-04-24 15:11:15 -06002458
2459 // Add the "finalLayout" transitions to external
2460 // Get them from where there we're hidding in the extra entry.
John Zulauf89311b42020-09-29 16:28:47 -06002461 // Not that since *final* always comes from *one* subpass per view, we don't have to accumulate the barriers
2462 // TODO Aliasing we may need to reconsider barrier accumulation... though I don't know that it would be valid for aliasing
2463 // that had mulitple final layout transistions from mulitple final subpasses.
John Zulauf355e49b2020-04-24 15:11:15 -06002464 const auto &final_transitions = rp_state_->subpass_transitions.back();
2465 for (const auto &transition : final_transitions) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07002466 const AttachmentViewGen &view_gen = attachment_views_[transition.attachment];
John Zulauf355e49b2020-04-24 15:11:15 -06002467 const auto &last_trackback = subpass_contexts_[transition.prev_pass].GetDstExternalTrackBack();
John Zulaufaa97d8b2020-07-14 10:58:13 -06002468 assert(&subpass_contexts_[transition.prev_pass] == last_trackback.context);
John Zulaufd5115702021-01-18 12:34:33 -07002469 ApplyBarrierOpsFunctor<PipelineBarrierOp> barrier_action(true /* resolve */, last_trackback.barriers.size(), tag);
John Zulauf1e331ec2020-12-04 18:29:38 -07002470 for (const auto &barrier : last_trackback.barriers) {
John Zulaufd5115702021-01-18 12:34:33 -07002471 barrier_action.EmplaceBack(PipelineBarrierOp(barrier, true));
John Zulauf1e331ec2020-12-04 18:29:38 -07002472 }
John Zulaufd0ec59f2021-03-13 14:25:08 -07002473 external_context->ApplyUpdateAction(view_gen, AttachmentViewGen::Gen::kViewSubresource, barrier_action);
John Zulauf355e49b2020-04-24 15:11:15 -06002474 }
2475}
2476
Jeremy Gebben40a22942020-12-22 14:22:06 -07002477SyncExecScope SyncExecScope::MakeSrc(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002478 SyncExecScope result;
2479 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002480 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2481 result.exec_scope = sync_utils::WithEarlierPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002482 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2483 return result;
2484}
2485
Jeremy Gebben40a22942020-12-22 14:22:06 -07002486SyncExecScope SyncExecScope::MakeDst(VkQueueFlags queue_flags, VkPipelineStageFlags2KHR mask_param) {
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002487 SyncExecScope result;
2488 result.mask_param = mask_param;
Jeremy Gebben5f585ae2021-02-02 09:03:06 -07002489 result.expanded_mask = sync_utils::ExpandPipelineStages(mask_param, queue_flags);
2490 result.exec_scope = sync_utils::WithLaterPipelineStages(result.expanded_mask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002491 result.valid_accesses = SyncStageAccess::AccessScopeByStage(result.exec_scope);
2492 return result;
2493}
2494
2495SyncBarrier::SyncBarrier(const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002496 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002497 src_access_scope = 0;
John Zulaufc523bf62021-02-16 08:20:34 -07002498 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002499 dst_access_scope = 0;
2500}
2501
2502template <typename Barrier>
2503SyncBarrier::SyncBarrier(const Barrier &barrier, const SyncExecScope &src, const SyncExecScope &dst) {
John Zulaufc523bf62021-02-16 08:20:34 -07002504 src_exec_scope = src;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002505 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002506 dst_exec_scope = dst;
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002507 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
2508}
2509
2510SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const VkSubpassDependency2 &subpass) {
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002511 const auto barrier = lvl_find_in_chain<VkMemoryBarrier2KHR>(subpass.pNext);
2512 if (barrier) {
2513 auto src = SyncExecScope::MakeSrc(queue_flags, barrier->srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002514 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002515 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier->srcAccessMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002516
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002517 auto dst = SyncExecScope::MakeDst(queue_flags, barrier->dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002518 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002519 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier->dstAccessMask);
2520
2521 } else {
2522 auto src = SyncExecScope::MakeSrc(queue_flags, subpass.srcStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002523 src_exec_scope = src;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002524 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, subpass.srcAccessMask);
2525
2526 auto dst = SyncExecScope::MakeDst(queue_flags, subpass.dstStageMask);
John Zulaufc523bf62021-02-16 08:20:34 -07002527 dst_exec_scope = dst;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002528 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, subpass.dstAccessMask);
2529 }
2530}
2531
2532template <typename Barrier>
2533SyncBarrier::SyncBarrier(VkQueueFlags queue_flags, const Barrier &barrier) {
2534 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
2535 src_exec_scope = src.exec_scope;
2536 src_access_scope = SyncStageAccess::AccessScope(src.valid_accesses, barrier.srcAccessMask);
2537
2538 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
Jeremy Gebben9893daf2021-01-04 10:40:50 -07002539 dst_exec_scope = dst.exec_scope;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07002540 dst_access_scope = SyncStageAccess::AccessScope(dst.valid_accesses, barrier.dstAccessMask);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002541}
2542
John Zulaufb02c1eb2020-10-06 16:33:36 -06002543// Apply a list of barriers, without resolving pending state, useful for subpass layout transitions
2544void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, bool layout_transition) {
2545 for (const auto &barrier : barriers) {
2546 ApplyBarrier(barrier, layout_transition);
2547 }
2548}
2549
John Zulauf89311b42020-09-29 16:28:47 -06002550// ApplyBarriers is design for *fully* inclusive barrier lists without layout tranistions. Designed use was for
2551// inter-subpass barriers for lazy-evaluation of parent context memory ranges. Subpass layout transistions are *not* done
2552// lazily, s.t. no previous access reports should need layout transitions.
John Zulaufb02c1eb2020-10-06 16:33:36 -06002553void ResourceAccessState::ApplyBarriers(const std::vector<SyncBarrier> &barriers, const ResourceUsageTag &tag) {
2554 assert(!pending_layout_transition); // This should never be call in the middle of another barrier application
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002555 assert(pending_write_barriers.none());
John Zulaufb02c1eb2020-10-06 16:33:36 -06002556 assert(!pending_write_dep_chain);
John Zulaufa0a98292020-09-18 09:30:10 -06002557 for (const auto &barrier : barriers) {
John Zulauf89311b42020-09-29 16:28:47 -06002558 ApplyBarrier(barrier, false);
John Zulaufa0a98292020-09-18 09:30:10 -06002559 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002560 ApplyPendingBarriers(tag);
John Zulauf3d84f1b2020-03-09 13:33:25 -06002561}
John Zulauf9cb530d2019-09-30 14:14:10 -06002562HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index) const {
2563 HazardResult hazard;
2564 auto usage = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002565 const auto usage_stage = PipelineStageBit(usage_index);
John Zulauf9cb530d2019-09-30 14:14:10 -06002566 if (IsRead(usage)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002567 if (IsRAWHazard(usage_stage, usage)) {
John Zulauf59e25072020-07-17 10:55:21 -06002568 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002569 }
2570 } else {
John Zulauf361fb532020-07-22 10:45:39 -06002571 // Write operation:
2572 // Check for read operations more recent than last_write (as setting last_write clears reads, that would be *any*
2573 // If reads exists -- test only against them because either:
2574 // * the reads were hazards, and we've reported the hazard, so just test the current write vs. the read operations
2575 // * 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
2576 // the current write happens after the reads, so just test the write against the reades
2577 // Otherwise test against last_write
2578 //
2579 // Look for casus belli for WAR
John Zulaufab7756b2020-12-29 16:10:16 -07002580 if (last_reads.size()) {
2581 for (const auto &read_access : last_reads) {
John Zulauf361fb532020-07-22 10:45:39 -06002582 if (IsReadHazard(usage_stage, read_access)) {
2583 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2584 break;
2585 }
2586 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002587 } else if (last_write.any() && IsWriteHazard(usage)) {
John Zulauf361fb532020-07-22 10:45:39 -06002588 // Write-After-Write check -- if we have a previous write to test against
2589 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002590 }
2591 }
2592 return hazard;
2593}
2594
John Zulauf8e3c3e92021-01-06 11:19:36 -07002595HazardResult ResourceAccessState::DetectHazard(SyncStageAccessIndex usage_index, const SyncOrdering &ordering_rule) const {
2596 const auto &ordering = GetOrderingRules(ordering_rule);
John Zulauf69133422020-05-20 14:55:53 -06002597 // The ordering guarantees act as barriers to the last accesses, independent of synchronization operations
2598 HazardResult hazard;
John Zulauf4285ee92020-09-23 10:20:52 -06002599 const auto usage_bit = FlagBit(usage_index);
John Zulauf361fb532020-07-22 10:45:39 -06002600 const auto usage_stage = PipelineStageBit(usage_index);
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002601 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
2602 const bool last_write_is_ordered = (last_write & ordering.access_scope).any();
John Zulauf4285ee92020-09-23 10:20:52 -06002603 if (IsRead(usage_bit)) {
2604 // Exclude RAW if no write, or write not most "most recent" operation w.r.t. usage;
2605 bool is_raw_hazard = IsRAWHazard(usage_stage, usage_bit);
2606 if (is_raw_hazard) {
2607 // NOTE: we know last_write is non-zero
2608 // See if the ordering rules save us from the simple RAW check above
2609 // First check to see if the current usage is covered by the ordering rules
2610 const bool usage_is_input_attachment = (usage_index == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ);
2611 const bool usage_is_ordered =
2612 (input_attachment_ordering && usage_is_input_attachment) || (0 != (usage_stage & ordering.exec_scope));
2613 if (usage_is_ordered) {
2614 // Now see of the most recent write (or a subsequent read) are ordered
2615 const bool most_recent_is_ordered = last_write_is_ordered || (0 != GetOrderedStages(ordering));
2616 is_raw_hazard = !most_recent_is_ordered;
John Zulauf361fb532020-07-22 10:45:39 -06002617 }
2618 }
John Zulauf4285ee92020-09-23 10:20:52 -06002619 if (is_raw_hazard) {
2620 hazard.Set(this, usage_index, READ_AFTER_WRITE, last_write, write_tag);
2621 }
John Zulauf361fb532020-07-22 10:45:39 -06002622 } else {
2623 // Only check for WAW if there are no reads since last_write
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002624 bool usage_write_is_ordered = (usage_bit & ordering.access_scope).any();
John Zulaufab7756b2020-12-29 16:10:16 -07002625 if (last_reads.size()) {
John Zulauf361fb532020-07-22 10:45:39 -06002626 // Look for any WAR hazards outside the ordered set of stages
Jeremy Gebben40a22942020-12-22 14:22:06 -07002627 VkPipelineStageFlags2KHR ordered_stages = 0;
John Zulauf4285ee92020-09-23 10:20:52 -06002628 if (usage_write_is_ordered) {
2629 // If the usage is ordered, we can ignore all ordered read stages w.r.t. WAR)
2630 ordered_stages = GetOrderedStages(ordering);
2631 }
2632 // If we're tracking any reads that aren't ordered against the current write, got to check 'em all.
2633 if ((ordered_stages & last_read_stages) != last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002634 for (const auto &read_access : last_reads) {
John Zulauf4285ee92020-09-23 10:20:52 -06002635 if (read_access.stage & ordered_stages) continue; // but we can skip the ordered ones
2636 if (IsReadHazard(usage_stage, read_access)) {
2637 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2638 break;
2639 }
John Zulaufd14743a2020-07-03 09:42:39 -06002640 }
2641 }
John Zulauf4285ee92020-09-23 10:20:52 -06002642 } else if (!(last_write_is_ordered && usage_write_is_ordered)) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002643 if (last_write.any() && IsWriteHazard(usage_bit)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002644 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
John Zulauf361fb532020-07-22 10:45:39 -06002645 }
John Zulauf69133422020-05-20 14:55:53 -06002646 }
2647 }
2648 return hazard;
2649}
2650
John Zulauf2f952d22020-02-10 11:34:51 -07002651// Asynchronous Hazards occur between subpasses with no connection through the DAG
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002652HazardResult ResourceAccessState::DetectAsyncHazard(SyncStageAccessIndex usage_index, const ResourceUsageTag &start_tag) const {
John Zulauf2f952d22020-02-10 11:34:51 -07002653 HazardResult hazard;
2654 auto usage = FlagBit(usage_index);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002655 // Async checks need to not go back further than the start of the subpass, as we only want to find hazards between the async
2656 // subpasses. Anything older than that should have been checked at the start of each subpass, taking into account all of
2657 // the raster ordering rules.
John Zulauf2f952d22020-02-10 11:34:51 -07002658 if (IsRead(usage)) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002659 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002660 hazard.Set(this, usage_index, READ_RACING_WRITE, last_write, write_tag);
John Zulauf2f952d22020-02-10 11:34:51 -07002661 }
2662 } else {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002663 if (last_write.any() && (write_tag.index >= start_tag.index)) {
John Zulauf59e25072020-07-17 10:55:21 -06002664 hazard.Set(this, usage_index, WRITE_RACING_WRITE, last_write, write_tag);
John Zulaufab7756b2020-12-29 16:10:16 -07002665 } else if (last_reads.size() > 0) {
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002666 // 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 -07002667 for (const auto &read_access : last_reads) {
2668 if (read_access.tag.index >= start_tag.index) {
2669 hazard.Set(this, usage_index, WRITE_RACING_READ, read_access.access, read_access.tag);
Jeremy Gebbenc4b78c52020-12-11 09:39:47 -07002670 break;
2671 }
2672 }
John Zulauf2f952d22020-02-10 11:34:51 -07002673 }
2674 }
2675 return hazard;
2676}
2677
Jeremy Gebben40a22942020-12-22 14:22:06 -07002678HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002679 const SyncStageAccessFlags &src_access_scope) const {
John Zulauf0cb5be22020-01-23 12:18:22 -07002680 // Only supporting image layout transitions for now
2681 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2682 HazardResult hazard;
John Zulauf361fb532020-07-22 10:45:39 -06002683 // only test for WAW if there no intervening read operations.
2684 // See DetectHazard(SyncStagetAccessIndex) above for more details.
John Zulaufab7756b2020-12-29 16:10:16 -07002685 if (last_reads.size()) {
John Zulauf355e49b2020-04-24 15:11:15 -06002686 // Look at the reads if any
John Zulaufab7756b2020-12-29 16:10:16 -07002687 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002688 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
John Zulauf59e25072020-07-17 10:55:21 -06002689 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
John Zulauf0cb5be22020-01-23 12:18:22 -07002690 break;
2691 }
2692 }
John Zulauf4a6105a2020-11-17 15:11:05 -07002693 } else if (last_write.any() && IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2694 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2695 }
2696
2697 return hazard;
2698}
2699
Jeremy Gebben40a22942020-12-22 14:22:06 -07002700HazardResult ResourceAccessState::DetectBarrierHazard(SyncStageAccessIndex usage_index, VkPipelineStageFlags2KHR src_exec_scope,
John Zulauf4a6105a2020-11-17 15:11:05 -07002701 const SyncStageAccessFlags &src_access_scope,
2702 const ResourceUsageTag &event_tag) const {
2703 // Only supporting image layout transitions for now
2704 assert(usage_index == SyncStageAccessIndex::SYNC_IMAGE_LAYOUT_TRANSITION);
2705 HazardResult hazard;
2706 // only test for WAW if there no intervening read operations.
2707 // See DetectHazard(SyncStagetAccessIndex) above for more details.
2708
John Zulaufab7756b2020-12-29 16:10:16 -07002709 if (last_reads.size()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002710 // Look at the reads if any... if reads exist, they are either the resaon the access is in the event
2711 // first scope, or they are a hazard.
John Zulaufab7756b2020-12-29 16:10:16 -07002712 for (const auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002713 if (read_access.tag.IsBefore(event_tag)) {
2714 // The read is in the events first synchronization scope, so we use a barrier hazard check
2715 // If the read stage is not in the src sync scope
2716 // *AND* not execution chained with an existing sync barrier (that's the or)
2717 // then the barrier access is unsafe (R/W after R)
2718 if (read_access.IsReadBarrierHazard(src_exec_scope)) {
2719 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2720 break;
2721 }
2722 } else {
2723 // The read not in the event first sync scope and so is a hazard vs. the layout transition
2724 hazard.Set(this, usage_index, WRITE_AFTER_READ, read_access.access, read_access.tag);
2725 }
2726 }
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002727 } else if (last_write.any()) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002728 // if there are no reads, the write is either the reason the access is in the event scope... they are a hazard
2729 if (write_tag.IsBefore(event_tag)) {
2730 // The write is in the first sync scope of the event (sync their aren't any reads to be the reason)
2731 // So do a normal barrier hazard check
2732 if (IsWriteBarrierHazard(src_exec_scope, src_access_scope)) {
2733 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2734 }
2735 } else {
2736 // The write isn't in scope, and is thus a hazard to the layout transistion for wait
John Zulauf361fb532020-07-22 10:45:39 -06002737 hazard.Set(this, usage_index, WRITE_AFTER_WRITE, last_write, write_tag);
2738 }
John Zulaufd14743a2020-07-03 09:42:39 -06002739 }
John Zulauf361fb532020-07-22 10:45:39 -06002740
John Zulauf0cb5be22020-01-23 12:18:22 -07002741 return hazard;
2742}
2743
John Zulauf5f13a792020-03-10 07:31:21 -06002744// The logic behind resolves is the same as update, we assume that earlier hazards have be reported, and that no
2745// tranistive hazard can exists with a hazard between the earlier operations. Yes, an early hazard can mask that another
2746// exists, but if you fix *that* hazard it either fixes or unmasks the subsequent ones.
2747void ResourceAccessState::Resolve(const ResourceAccessState &other) {
2748 if (write_tag.IsBefore(other.write_tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002749 // If this is a later write, we've reported any exsiting hazard, and we can just overwrite as the more recent
2750 // operation
John Zulauf5f13a792020-03-10 07:31:21 -06002751 *this = other;
2752 } else if (!other.write_tag.IsBefore(write_tag)) {
2753 // This is the *equals* case for write operations, we merged the write barriers and the read state (but without the
2754 // dependency chaining logic or any stage expansion)
2755 write_barriers |= other.write_barriers;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002756 pending_write_barriers |= other.pending_write_barriers;
2757 pending_layout_transition |= other.pending_layout_transition;
2758 pending_write_dep_chain |= other.pending_write_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002759
John Zulaufd14743a2020-07-03 09:42:39 -06002760 // Merge the read states
John Zulaufab7756b2020-12-29 16:10:16 -07002761 const auto pre_merge_count = last_reads.size();
John Zulauf4285ee92020-09-23 10:20:52 -06002762 const auto pre_merge_stages = last_read_stages;
John Zulaufab7756b2020-12-29 16:10:16 -07002763 for (uint32_t other_read_index = 0; other_read_index < other.last_reads.size(); other_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002764 auto &other_read = other.last_reads[other_read_index];
John Zulauf4285ee92020-09-23 10:20:52 -06002765 if (pre_merge_stages & other_read.stage) {
John Zulauf5f13a792020-03-10 07:31:21 -06002766 // Merge in the barriers for read stages that exist in *both* this and other
John Zulauf4285ee92020-09-23 10:20:52 -06002767 // TODO: This is N^2 with stages... perhaps the ReadStates should be sorted by stage index.
2768 // but we should wait on profiling data for that.
2769 for (uint32_t my_read_index = 0; my_read_index < pre_merge_count; my_read_index++) {
John Zulauf5f13a792020-03-10 07:31:21 -06002770 auto &my_read = last_reads[my_read_index];
2771 if (other_read.stage == my_read.stage) {
2772 if (my_read.tag.IsBefore(other_read.tag)) {
John Zulauf4285ee92020-09-23 10:20:52 -06002773 // Other is more recent, copy in the state
John Zulauf37ceaed2020-07-03 16:18:15 -06002774 my_read.access = other_read.access;
John Zulauf4285ee92020-09-23 10:20:52 -06002775 my_read.tag = other_read.tag;
John Zulaufb02c1eb2020-10-06 16:33:36 -06002776 my_read.pending_dep_chain = other_read.pending_dep_chain;
2777 // TODO: Phase 2 -- review the state merge logic to avoid false positive from overwriting the barriers
2778 // May require tracking more than one access per stage.
2779 my_read.barriers = other_read.barriers;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002780 if (my_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauf4285ee92020-09-23 10:20:52 -06002781 // Since I'm overwriting the fragement stage read, also update the input attachment info
2782 // as this is the only stage that affects it.
John Zulauff51fbb62020-10-02 14:43:24 -06002783 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002784 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002785 } else if (other_read.tag.IsBefore(my_read.tag)) {
2786 // The read tags match so merge the barriers
2787 my_read.barriers |= other_read.barriers;
2788 my_read.pending_dep_chain |= other_read.pending_dep_chain;
John Zulauf5f13a792020-03-10 07:31:21 -06002789 }
John Zulaufb02c1eb2020-10-06 16:33:36 -06002790
John Zulauf5f13a792020-03-10 07:31:21 -06002791 break;
2792 }
2793 }
2794 } else {
2795 // The other read stage doesn't exist in this, so add it.
John Zulaufab7756b2020-12-29 16:10:16 -07002796 last_reads.emplace_back(other_read);
John Zulauf5f13a792020-03-10 07:31:21 -06002797 last_read_stages |= other_read.stage;
Jeremy Gebben40a22942020-12-22 14:22:06 -07002798 if (other_read.stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002799 input_attachment_read = other.input_attachment_read;
John Zulauf4285ee92020-09-23 10:20:52 -06002800 }
John Zulauf5f13a792020-03-10 07:31:21 -06002801 }
2802 }
John Zulauf361fb532020-07-22 10:45:39 -06002803 read_execution_barriers |= other.read_execution_barriers;
John Zulauf4285ee92020-09-23 10:20:52 -06002804 } // the else clause would be that other write is before this write... in which case we supercede the other state and
2805 // ignore it.
John Zulauffaea0ee2021-01-14 14:01:32 -07002806
2807 // Merge first access information by making a copy of this first_access and reconstructing with a shuffle
2808 // of the copy and other into this using the update first logic.
2809 // NOTE: All sorts of additional cleverness could be put into short circuts. (for example back is write and is before front
2810 // of the other first_accesses... )
2811 if (!(first_accesses_ == other.first_accesses_) && !other.first_accesses_.empty()) {
2812 FirstAccesses firsts(std::move(first_accesses_));
2813 first_accesses_.clear();
2814 first_read_stages_ = 0U;
2815 auto a = firsts.begin();
2816 auto a_end = firsts.end();
2817 for (auto &b : other.first_accesses_) {
2818 // TODO: Determine whether "IsBefore" or "IsGloballyBefore" is needed...
2819 while (a != a_end && a->tag.IsBefore(b.tag)) {
2820 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2821 ++a;
2822 }
2823 UpdateFirst(b.tag, b.usage_index, b.ordering_rule);
2824 }
2825 for (; a != a_end; ++a) {
2826 UpdateFirst(a->tag, a->usage_index, a->ordering_rule);
2827 }
2828 }
John Zulauf5f13a792020-03-10 07:31:21 -06002829}
2830
John Zulauf8e3c3e92021-01-06 11:19:36 -07002831void ResourceAccessState::Update(SyncStageAccessIndex usage_index, SyncOrdering ordering_rule, const ResourceUsageTag &tag) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002832 // Move this logic in the ResourceStateTracker as methods, thereof (or we'll repeat it for every flavor of resource...
2833 const auto usage_bit = FlagBit(usage_index);
John Zulauf4285ee92020-09-23 10:20:52 -06002834 if (IsRead(usage_index)) {
John Zulauf9cb530d2019-09-30 14:14:10 -06002835 // Mulitple outstanding reads may be of interest and do dependency chains independently
2836 // However, for purposes of barrier tracking, only one read per pipeline stage matters
2837 const auto usage_stage = PipelineStageBit(usage_index);
2838 if (usage_stage & last_read_stages) {
John Zulaufab7756b2020-12-29 16:10:16 -07002839 for (auto &read_access : last_reads) {
2840 if (read_access.stage == usage_stage) {
2841 read_access.Set(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002842 break;
2843 }
2844 }
2845 } else {
John Zulaufab7756b2020-12-29 16:10:16 -07002846 last_reads.emplace_back(usage_stage, usage_bit, 0, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002847 last_read_stages |= usage_stage;
2848 }
John Zulauf4285ee92020-09-23 10:20:52 -06002849
2850 // 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 -07002851 if (usage_stage == VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR) {
John Zulauff51fbb62020-10-02 14:43:24 -06002852 // TODO Revisit re: multiple reads for a given stage
2853 input_attachment_read = (usage_bit == SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT);
John Zulauf4285ee92020-09-23 10:20:52 -06002854 }
John Zulauf9cb530d2019-09-30 14:14:10 -06002855 } else {
2856 // Assume write
2857 // TODO determine what to do with READ-WRITE operations if any
John Zulauf89311b42020-09-29 16:28:47 -06002858 SetWrite(usage_bit, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06002859 }
John Zulauffaea0ee2021-01-14 14:01:32 -07002860 UpdateFirst(tag, usage_index, ordering_rule);
John Zulauf9cb530d2019-09-30 14:14:10 -06002861}
John Zulauf5f13a792020-03-10 07:31:21 -06002862
John Zulauf89311b42020-09-29 16:28:47 -06002863// Clobber last read and all barriers... because all we have is DANGER, DANGER, WILL ROBINSON!!!
2864// if the last_reads/last_write were unsafe, we've reported them, in either case the prior access is irrelevant.
2865// We can overwrite them as *this* write is now after them.
2866//
2867// Note: intentionally ignore pending barriers and chains (i.e. don't apply or clear them), let ApplyPendingBarriers handle them.
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002868void ResourceAccessState::SetWrite(const SyncStageAccessFlags &usage_bit, const ResourceUsageTag &tag) {
John Zulaufab7756b2020-12-29 16:10:16 -07002869 last_reads.clear();
John Zulauf89311b42020-09-29 16:28:47 -06002870 last_read_stages = 0;
2871 read_execution_barriers = 0;
John Zulauff51fbb62020-10-02 14:43:24 -06002872 input_attachment_read = false; // Denotes no outstanding input attachment read after the last write.
John Zulauf89311b42020-09-29 16:28:47 -06002873
2874 write_barriers = 0;
2875 write_dependency_chain = 0;
2876 write_tag = tag;
2877 last_write = usage_bit;
John Zulauf9cb530d2019-09-30 14:14:10 -06002878}
2879
John Zulauf89311b42020-09-29 16:28:47 -06002880// Apply the memory barrier without updating the existing barriers. The execution barrier
2881// changes the "chaining" state, but to keep barriers independent, we defer this until all barriers
2882// of the batch have been processed. Also, depending on whether layout transition happens, we'll either
2883// replace the current write barriers or add to them, so accumulate to pending as well.
2884void ResourceAccessState::ApplyBarrier(const SyncBarrier &barrier, bool layout_transition) {
2885 // For independent barriers we need to track what the new barriers and dependency chain *will* be when we're done
2886 // applying the memory barriers
John Zulauf86356ca2020-10-19 11:46:41 -06002887 // NOTE: We update the write barrier if the write is in the first access scope or if there is a layout
2888 // transistion, under the theory of "most recent access". If the read/write *isn't* safe
2889 // vs. this layout transition DetectBarrierHazard should report it. We treat the layout
2890 // transistion *as* a write and in scope with the barrier (it's before visibility).
John Zulaufc523bf62021-02-16 08:20:34 -07002891 if (layout_transition || WriteInSourceScopeOrChain(barrier.src_exec_scope.exec_scope, barrier.src_access_scope)) {
John Zulauf89311b42020-09-29 16:28:47 -06002892 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002893 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002894 }
John Zulauf89311b42020-09-29 16:28:47 -06002895 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2896 pending_layout_transition |= layout_transition;
John Zulaufa0a98292020-09-18 09:30:10 -06002897
John Zulauf89311b42020-09-29 16:28:47 -06002898 if (!pending_layout_transition) {
2899 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2900 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002901 for (auto &read_access : last_reads) {
John Zulauf89311b42020-09-29 16:28:47 -06002902 // 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 -07002903 if (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers)) {
2904 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulaufa0a98292020-09-18 09:30:10 -06002905 }
2906 }
John Zulaufa0a98292020-09-18 09:30:10 -06002907 }
John Zulaufa0a98292020-09-18 09:30:10 -06002908}
2909
John Zulauf4a6105a2020-11-17 15:11:05 -07002910// Apply the tag scoped memory barrier without updating the existing barriers. The execution barrier
2911// changes the "chaining" state, but to keep barriers independent. See discussion above.
2912void ResourceAccessState::ApplyBarrier(const ResourceUsageTag &scope_tag, const SyncBarrier &barrier, bool layout_transition) {
2913 // The scope logic for events is, if we're here, the resource usage was flagged as "in the first execution scope" at
2914 // the time of the SetEvent, thus all we need check is whether the access is the same one (i.e. before the scope tag
2915 // in order to know if it's in the excecution scope
2916 // Notice that the layout transition sets the pending barriers *regardless*, as any lack of src_access_scope to
2917 // guard against the layout transition should be reported in the detect barrier hazard phase, and we only report
2918 // errors w.r.t. "most recent" accesses.
2919 if (layout_transition || ((write_tag.IsBefore(scope_tag)) && (barrier.src_access_scope & last_write).any())) {
2920 pending_write_barriers |= barrier.dst_access_scope;
John Zulaufc523bf62021-02-16 08:20:34 -07002921 pending_write_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002922 }
2923 // Track layout transistion as pending as we can't modify last_write until all barriers processed
2924 pending_layout_transition |= layout_transition;
2925
2926 if (!pending_layout_transition) {
2927 // Once we're dealing with a layout transition (which is modelled as a *write*) then the last reads/writes/chains
2928 // don't need to be tracked as we're just going to zero them.
John Zulaufab7756b2020-12-29 16:10:16 -07002929 for (auto &read_access : last_reads) {
John Zulauf4a6105a2020-11-17 15:11:05 -07002930 // If this read is the same one we included in the set event and in scope, then apply the execution barrier...
2931 // NOTE: That's not really correct... this read stage might *not* have been included in the setevent, and the barriers
2932 // representing the chain might have changed since then (that would be an odd usage), so as a first approximation
2933 // we'll assume the barriers *haven't* been changed since (if the tag hasn't), and while this could be a false
2934 // positive in the case of Set; SomeBarrier; Wait; we'll live with it until we can add more state to the first scope
2935 // capture (the specific write and read stages that *were* in scope at the moment of SetEvents.
2936 // TODO: eliminate the false positive by including write/read-stages "in scope" information in SetEvents first_scope
John Zulaufc523bf62021-02-16 08:20:34 -07002937 if (read_access.tag.IsBefore(scope_tag) &&
2938 (barrier.src_exec_scope.exec_scope & (read_access.stage | read_access.barriers))) {
2939 read_access.pending_dep_chain |= barrier.dst_exec_scope.exec_scope;
John Zulauf4a6105a2020-11-17 15:11:05 -07002940 }
2941 }
2942 }
2943}
John Zulauf89311b42020-09-29 16:28:47 -06002944void ResourceAccessState::ApplyPendingBarriers(const ResourceUsageTag &tag) {
2945 if (pending_layout_transition) {
John Zulauf89311b42020-09-29 16:28:47 -06002946 // SetWrite clobbers the read count, and thus we don't have to clear the read_state out.
2947 SetWrite(SYNC_IMAGE_LAYOUT_TRANSITION_BIT, tag); // Side effect notes below
John Zulauffaea0ee2021-01-14 14:01:32 -07002948 UpdateFirst(tag, SYNC_IMAGE_LAYOUT_TRANSITION, SyncOrdering::kNonAttachment);
John Zulauf89311b42020-09-29 16:28:47 -06002949 pending_layout_transition = false;
John Zulauf9cb530d2019-09-30 14:14:10 -06002950 }
John Zulauf89311b42020-09-29 16:28:47 -06002951
2952 // Apply the accumulate execution barriers (and thus update chaining information)
2953 // for layout transition, read count is zeroed by SetWrite, so this will be skipped.
John Zulaufab7756b2020-12-29 16:10:16 -07002954 for (auto &read_access : last_reads) {
2955 read_access.barriers |= read_access.pending_dep_chain;
2956 read_execution_barriers |= read_access.barriers;
2957 read_access.pending_dep_chain = 0;
John Zulauf89311b42020-09-29 16:28:47 -06002958 }
2959
2960 // We OR in the accumulated write chain and barriers even in the case of a layout transition as SetWrite zeros them.
2961 write_dependency_chain |= pending_write_dep_chain;
2962 write_barriers |= pending_write_barriers;
2963 pending_write_dep_chain = 0;
2964 pending_write_barriers = 0;
John Zulauf9cb530d2019-09-30 14:14:10 -06002965}
2966
John Zulauf59e25072020-07-17 10:55:21 -06002967// This should be just Bits or Index, but we don't have an invalid state for Index
Jeremy Gebben40a22942020-12-22 14:22:06 -07002968VkPipelineStageFlags2KHR ResourceAccessState::GetReadBarriers(const SyncStageAccessFlags &usage_bit) const {
2969 VkPipelineStageFlags2KHR barriers = 0U;
John Zulauf4285ee92020-09-23 10:20:52 -06002970
John Zulaufab7756b2020-12-29 16:10:16 -07002971 for (const auto &read_access : last_reads) {
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002972 if ((read_access.access & usage_bit).any()) {
John Zulauf4285ee92020-09-23 10:20:52 -06002973 barriers = read_access.barriers;
2974 break;
John Zulauf59e25072020-07-17 10:55:21 -06002975 }
2976 }
John Zulauf4285ee92020-09-23 10:20:52 -06002977
John Zulauf59e25072020-07-17 10:55:21 -06002978 return barriers;
2979}
2980
Jeremy Gebben40a22942020-12-22 14:22:06 -07002981inline bool ResourceAccessState::IsRAWHazard(VkPipelineStageFlags2KHR usage_stage, const SyncStageAccessFlags &usage) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002982 assert(IsRead(usage));
2983 // Only RAW vs. last_write if it doesn't happen-after any other read because either:
2984 // * the previous reads are not hazards, and thus last_write must be visible and available to
2985 // any reads that happen after.
2986 // * the previous reads *are* hazards to last_write, have been reported, and if that hazard is fixed
2987 // 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 -07002988 return last_write.any() && (0 == (read_execution_barriers & usage_stage)) && IsWriteHazard(usage);
John Zulauf4285ee92020-09-23 10:20:52 -06002989}
2990
Jeremy Gebben40a22942020-12-22 14:22:06 -07002991VkPipelineStageFlags2KHR ResourceAccessState::GetOrderedStages(const OrderingBarrier &ordering) const {
John Zulauf4285ee92020-09-23 10:20:52 -06002992 // Whether the stage are in the ordering scope only matters if the current write is ordered
Jeremy Gebben40a22942020-12-22 14:22:06 -07002993 VkPipelineStageFlags2KHR ordered_stages = last_read_stages & ordering.exec_scope;
John Zulauf4285ee92020-09-23 10:20:52 -06002994 // Special input attachment handling as always (not encoded in exec_scop)
Jeremy Gebbend0de1f82020-11-09 08:21:07 -07002995 const bool input_attachment_ordering = (ordering.access_scope & SYNC_FRAGMENT_SHADER_INPUT_ATTACHMENT_READ_BIT).any();
John Zulauff51fbb62020-10-02 14:43:24 -06002996 if (input_attachment_ordering && input_attachment_read) {
John Zulauf4285ee92020-09-23 10:20:52 -06002997 // 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 -07002998 ordered_stages |= VK_PIPELINE_STAGE_2_FRAGMENT_SHADER_BIT_KHR;
John Zulauf4285ee92020-09-23 10:20:52 -06002999 }
3000
3001 return ordered_stages;
3002}
3003
John Zulauffaea0ee2021-01-14 14:01:32 -07003004void ResourceAccessState::UpdateFirst(const ResourceUsageTag &tag, SyncStageAccessIndex usage_index, SyncOrdering ordering_rule) {
3005 // Only record until we record a write.
3006 if (first_accesses_.empty() || IsRead(first_accesses_.back().usage_index)) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003007 const VkPipelineStageFlags2KHR usage_stage = IsRead(usage_index) ? PipelineStageBit(usage_index) : 0U;
John Zulauffaea0ee2021-01-14 14:01:32 -07003008 if (0 == (usage_stage & first_read_stages_)) {
3009 // If this is a read we haven't seen or a write, record.
3010 first_read_stages_ |= usage_stage;
3011 first_accesses_.emplace_back(tag, usage_index, ordering_rule);
3012 }
3013 }
3014}
3015
John Zulaufd1f85d42020-04-15 12:23:15 -06003016void SyncValidator::ResetCommandBufferCallback(VkCommandBuffer command_buffer) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003017 auto *access_context = GetAccessContextNoInsert(command_buffer);
3018 if (access_context) {
3019 access_context->Reset();
John Zulauf9cb530d2019-09-30 14:14:10 -06003020 }
3021}
3022
John Zulaufd1f85d42020-04-15 12:23:15 -06003023void SyncValidator::FreeCommandBufferCallback(VkCommandBuffer command_buffer) {
3024 auto access_found = cb_access_state.find(command_buffer);
3025 if (access_found != cb_access_state.end()) {
3026 access_found->second->Reset();
3027 cb_access_state.erase(access_found);
3028 }
3029}
3030
John Zulauf9cb530d2019-09-30 14:14:10 -06003031bool SyncValidator::PreCallValidateCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3032 uint32_t regionCount, const VkBufferCopy *pRegions) const {
3033 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003034 const auto *cb_context = GetAccessContext(commandBuffer);
3035 assert(cb_context);
3036 if (!cb_context) return skip;
3037 const auto *context = cb_context->GetCurrentAccessContext();
John Zulauf9cb530d2019-09-30 14:14:10 -06003038
John Zulauf3d84f1b2020-03-09 13:33:25 -06003039 // If we have no previous accesses, we have no hazards
John Zulauf3d84f1b2020-03-09 13:33:25 -06003040 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003041 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003042
3043 for (uint32_t region = 0; region < regionCount; region++) {
3044 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003045 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003046 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003047 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003048 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003049 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003050 "vkCmdCopyBuffer: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003051 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003052 cb_context->FormatUsage(hazard).c_str());
John Zulauf9cb530d2019-09-30 14:14:10 -06003053 }
John Zulauf9cb530d2019-09-30 14:14:10 -06003054 }
John Zulauf16adfc92020-04-08 10:28:33 -06003055 if (dst_buffer && !skip) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003056 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003057 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003058 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003059 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003060 "vkCmdCopyBuffer: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003061 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003062 cb_context->FormatUsage(hazard).c_str());
John Zulauf3d84f1b2020-03-09 13:33:25 -06003063 }
3064 }
3065 if (skip) break;
John Zulauf9cb530d2019-09-30 14:14:10 -06003066 }
3067 return skip;
3068}
3069
3070void SyncValidator::PreCallRecordCmdCopyBuffer(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkBuffer dstBuffer,
3071 uint32_t regionCount, const VkBufferCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003072 auto *cb_context = GetAccessContext(commandBuffer);
3073 assert(cb_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003074 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003075 auto *context = cb_context->GetCurrentAccessContext();
3076
John Zulauf9cb530d2019-09-30 14:14:10 -06003077 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003078 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
John Zulauf9cb530d2019-09-30 14:14:10 -06003079
3080 for (uint32_t region = 0; region < regionCount; region++) {
3081 const auto &copy_region = pRegions[region];
John Zulauf16adfc92020-04-08 10:28:33 -06003082 if (src_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003083 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003084 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003085 }
John Zulauf16adfc92020-04-08 10:28:33 -06003086 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003087 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003088 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003089 }
3090 }
3091}
3092
John Zulauf4a6105a2020-11-17 15:11:05 -07003093void SyncValidator::PreCallRecordDestroyEvent(VkDevice device, VkEvent event, const VkAllocationCallbacks *pAllocator) {
3094 // Clear out events from the command buffer contexts
3095 for (auto &cb_context : cb_access_state) {
3096 cb_context.second->RecordDestroyEvent(event);
3097 }
3098}
3099
Jeff Leger178b1e52020-10-05 12:22:23 -04003100bool SyncValidator::PreCallValidateCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer,
3101 const VkCopyBufferInfo2KHR *pCopyBufferInfos) const {
3102 bool skip = false;
3103 const auto *cb_context = GetAccessContext(commandBuffer);
3104 assert(cb_context);
3105 if (!cb_context) return skip;
3106 const auto *context = cb_context->GetCurrentAccessContext();
3107
3108 // If we have no previous accesses, we have no hazards
3109 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3110 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3111
3112 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3113 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3114 if (src_buffer) {
3115 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003116 auto hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003117 if (hazard.hazard) {
3118 // TODO -- add tag information to log msg when useful.
3119 skip |= LogError(pCopyBufferInfos->srcBuffer, string_SyncHazardVUID(hazard.hazard),
3120 "vkCmdCopyBuffer2KHR(): Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.",
3121 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->srcBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003122 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003123 }
3124 }
3125 if (dst_buffer && !skip) {
3126 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003127 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
Jeff Leger178b1e52020-10-05 12:22:23 -04003128 if (hazard.hazard) {
3129 skip |= LogError(pCopyBufferInfos->dstBuffer, string_SyncHazardVUID(hazard.hazard),
3130 "vkCmdCopyBuffer2KHR(): Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.",
3131 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyBufferInfos->dstBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003132 region, cb_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003133 }
3134 }
3135 if (skip) break;
3136 }
3137 return skip;
3138}
3139
3140void SyncValidator::PreCallRecordCmdCopyBuffer2KHR(VkCommandBuffer commandBuffer, const VkCopyBufferInfo2KHR *pCopyBufferInfos) {
3141 auto *cb_context = GetAccessContext(commandBuffer);
3142 assert(cb_context);
3143 const auto tag = cb_context->NextCommandTag(CMD_COPYBUFFER2KHR);
3144 auto *context = cb_context->GetCurrentAccessContext();
3145
3146 const auto *src_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->srcBuffer);
3147 const auto *dst_buffer = Get<BUFFER_STATE>(pCopyBufferInfos->dstBuffer);
3148
3149 for (uint32_t region = 0; region < pCopyBufferInfos->regionCount; region++) {
3150 const auto &copy_region = pCopyBufferInfos->pRegions[region];
3151 if (src_buffer) {
3152 const ResourceAccessRange src_range = MakeRange(*src_buffer, copy_region.srcOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003153 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003154 }
3155 if (dst_buffer) {
3156 const ResourceAccessRange dst_range = MakeRange(*dst_buffer, copy_region.dstOffset, copy_region.size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003157 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003158 }
3159 }
3160}
3161
John Zulauf5c5e88d2019-12-26 11:22:02 -07003162bool SyncValidator::PreCallValidateCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3163 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3164 const VkImageCopy *pRegions) const {
3165 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003166 const auto *cb_access_context = GetAccessContext(commandBuffer);
3167 assert(cb_access_context);
3168 if (!cb_access_context) return skip;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003169
John Zulauf3d84f1b2020-03-09 13:33:25 -06003170 const auto *context = cb_access_context->GetCurrentAccessContext();
3171 assert(context);
3172 if (!context) return skip;
3173
3174 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3175 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003176 for (uint32_t region = 0; region < regionCount; region++) {
3177 const auto &copy_region = pRegions[region];
3178 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003179 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
John Zulauf3d84f1b2020-03-09 13:33:25 -06003180 copy_region.srcOffset, copy_region.extent);
3181 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003182 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003183 "vkCmdCopyImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003184 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003185 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003186 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003187 }
3188
3189 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003190 VkExtent3D dst_copy_extent =
3191 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003192 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
locke-lunarg1df1f882020-03-02 16:42:08 -07003193 copy_region.dstOffset, dst_copy_extent);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003194 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003195 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003196 "vkCmdCopyImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06003197 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003198 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf5c5e88d2019-12-26 11:22:02 -07003199 }
locke-lunarg1dbbb9e2020-02-28 22:43:53 -07003200 if (skip) break;
John Zulauf5c5e88d2019-12-26 11:22:02 -07003201 }
3202 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003203
John Zulauf5c5e88d2019-12-26 11:22:02 -07003204 return skip;
3205}
3206
3207void SyncValidator::PreCallRecordCmdCopyImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3208 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3209 const VkImageCopy *pRegions) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003210 auto *cb_access_context = GetAccessContext(commandBuffer);
3211 assert(cb_access_context);
John Zulauf2b151bf2020-04-24 15:37:44 -06003212 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003213 auto *context = cb_access_context->GetCurrentAccessContext();
3214 assert(context);
3215
John Zulauf5c5e88d2019-12-26 11:22:02 -07003216 auto *src_image = Get<IMAGE_STATE>(srcImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003217 auto *dst_image = Get<IMAGE_STATE>(dstImage);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003218
3219 for (uint32_t region = 0; region < regionCount; region++) {
3220 const auto &copy_region = pRegions[region];
John Zulauf3d84f1b2020-03-09 13:33:25 -06003221 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003222 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003223 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
John Zulauf5c5e88d2019-12-26 11:22:02 -07003224 }
John Zulauf3d84f1b2020-03-09 13:33:25 -06003225 if (dst_image) {
locke-lunarg1df1f882020-03-02 16:42:08 -07003226 VkExtent3D dst_copy_extent =
3227 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003228 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003229 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
John Zulauf9cb530d2019-09-30 14:14:10 -06003230 }
3231 }
3232}
3233
Jeff Leger178b1e52020-10-05 12:22:23 -04003234bool SyncValidator::PreCallValidateCmdCopyImage2KHR(VkCommandBuffer commandBuffer,
3235 const VkCopyImageInfo2KHR *pCopyImageInfo) const {
3236 bool skip = false;
3237 const auto *cb_access_context = GetAccessContext(commandBuffer);
3238 assert(cb_access_context);
3239 if (!cb_access_context) return skip;
3240
3241 const auto *context = cb_access_context->GetCurrentAccessContext();
3242 assert(context);
3243 if (!context) return skip;
3244
3245 const auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3246 const auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3247 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3248 const auto &copy_region = pCopyImageInfo->pRegions[region];
3249 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003250 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003251 copy_region.srcOffset, copy_region.extent);
3252 if (hazard.hazard) {
3253 skip |= LogError(pCopyImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
3254 "vkCmdCopyImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
3255 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003256 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003257 }
3258 }
3259
3260 if (dst_image) {
3261 VkExtent3D dst_copy_extent =
3262 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003263 auto hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04003264 copy_region.dstOffset, dst_copy_extent);
3265 if (hazard.hazard) {
3266 skip |= LogError(pCopyImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
3267 "vkCmdCopyImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
3268 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pCopyImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003269 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04003270 }
3271 if (skip) break;
3272 }
3273 }
3274
3275 return skip;
3276}
3277
3278void SyncValidator::PreCallRecordCmdCopyImage2KHR(VkCommandBuffer commandBuffer, const VkCopyImageInfo2KHR *pCopyImageInfo) {
3279 auto *cb_access_context = GetAccessContext(commandBuffer);
3280 assert(cb_access_context);
3281 const auto tag = cb_access_context->NextCommandTag(CMD_COPYIMAGE2KHR);
3282 auto *context = cb_access_context->GetCurrentAccessContext();
3283 assert(context);
3284
3285 auto *src_image = Get<IMAGE_STATE>(pCopyImageInfo->srcImage);
3286 auto *dst_image = Get<IMAGE_STATE>(pCopyImageInfo->dstImage);
3287
3288 for (uint32_t region = 0; region < pCopyImageInfo->regionCount; region++) {
3289 const auto &copy_region = pCopyImageInfo->pRegions[region];
3290 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003291 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003292 copy_region.srcSubresource, copy_region.srcOffset, copy_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003293 }
3294 if (dst_image) {
3295 VkExtent3D dst_copy_extent =
3296 GetAdjustedDestImageExtent(src_image->createInfo.format, dst_image->createInfo.format, copy_region.extent);
Jeremy Gebben40a22942020-12-22 14:22:06 -07003297 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003298 copy_region.dstSubresource, copy_region.dstOffset, dst_copy_extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04003299 }
3300 }
3301}
3302
John Zulauf9cb530d2019-09-30 14:14:10 -06003303bool SyncValidator::PreCallValidateCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3304 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3305 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3306 uint32_t bufferMemoryBarrierCount,
3307 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3308 uint32_t imageMemoryBarrierCount,
3309 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
3310 bool skip = false;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003311 const auto *cb_access_context = GetAccessContext(commandBuffer);
3312 assert(cb_access_context);
3313 if (!cb_access_context) return skip;
John Zulauf0cb5be22020-01-23 12:18:22 -07003314
John Zulauf36ef9282021-02-02 11:47:24 -07003315 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3316 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3317 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3318 pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07003319 skip = pipeline_barrier.Validate(*cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003320 return skip;
3321}
3322
3323void SyncValidator::PreCallRecordCmdPipelineBarrier(VkCommandBuffer commandBuffer, VkPipelineStageFlags srcStageMask,
3324 VkPipelineStageFlags dstStageMask, VkDependencyFlags dependencyFlags,
3325 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
3326 uint32_t bufferMemoryBarrierCount,
3327 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
3328 uint32_t imageMemoryBarrierCount,
3329 const VkImageMemoryBarrier *pImageMemoryBarriers) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003330 auto *cb_access_context = GetAccessContext(commandBuffer);
3331 assert(cb_access_context);
3332 if (!cb_access_context) return;
John Zulauf9cb530d2019-09-30 14:14:10 -06003333
John Zulauf36ef9282021-02-02 11:47:24 -07003334 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER, *this, cb_access_context->GetQueueFlags(), srcStageMask,
3335 dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
3336 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
3337 pImageMemoryBarriers);
3338 pipeline_barrier.Record(cb_access_context);
John Zulauf9cb530d2019-09-30 14:14:10 -06003339}
3340
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07003341bool SyncValidator::PreCallValidateCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer,
3342 const VkDependencyInfoKHR *pDependencyInfo) const {
3343 bool skip = false;
3344 const auto *cb_access_context = GetAccessContext(commandBuffer);
3345 assert(cb_access_context);
3346 if (!cb_access_context) return skip;
3347
3348 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3349 skip = pipeline_barrier.Validate(*cb_access_context);
3350 return skip;
3351}
3352
3353void SyncValidator::PreCallRecordCmdPipelineBarrier2KHR(VkCommandBuffer commandBuffer, const VkDependencyInfoKHR *pDependencyInfo) {
3354 auto *cb_access_context = GetAccessContext(commandBuffer);
3355 assert(cb_access_context);
3356 if (!cb_access_context) return;
3357
3358 SyncOpPipelineBarrier pipeline_barrier(CMD_PIPELINEBARRIER2KHR, *this, cb_access_context->GetQueueFlags(), *pDependencyInfo);
3359 pipeline_barrier.Record(cb_access_context);
3360}
3361
John Zulauf9cb530d2019-09-30 14:14:10 -06003362void SyncValidator::PostCallRecordCreateDevice(VkPhysicalDevice gpu, const VkDeviceCreateInfo *pCreateInfo,
3363 const VkAllocationCallbacks *pAllocator, VkDevice *pDevice, VkResult result) {
3364 // The state tracker sets up the device state
3365 StateTracker::PostCallRecordCreateDevice(gpu, pCreateInfo, pAllocator, pDevice, result);
3366
John Zulauf5f13a792020-03-10 07:31:21 -06003367 // Add the callback hooks for the functions that are either broadly or deeply used and that the ValidationStateTracker
3368 // refactor would be messier without.
John Zulauf9cb530d2019-09-30 14:14:10 -06003369 // TODO: Find a good way to do this hooklessly.
3370 ValidationObject *device_object = GetLayerDataPtr(get_dispatch_key(*pDevice), layer_data_map);
3371 ValidationObject *validation_data = GetValidationObject(device_object->object_dispatch, LayerObjectTypeSyncValidation);
3372 SyncValidator *sync_device_state = static_cast<SyncValidator *>(validation_data);
3373
John Zulaufd1f85d42020-04-15 12:23:15 -06003374 sync_device_state->SetCommandBufferResetCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3375 sync_device_state->ResetCommandBufferCallback(command_buffer);
3376 });
3377 sync_device_state->SetCommandBufferFreeCallback([sync_device_state](VkCommandBuffer command_buffer) -> void {
3378 sync_device_state->FreeCommandBufferCallback(command_buffer);
3379 });
John Zulauf9cb530d2019-09-30 14:14:10 -06003380}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003381
John Zulauf355e49b2020-04-24 15:11:15 -06003382bool SyncValidator::ValidateBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003383 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003384 bool skip = false;
John Zulauf355e49b2020-04-24 15:11:15 -06003385 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003386 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003387 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003388 skip = sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003389 }
John Zulauf355e49b2020-04-24 15:11:15 -06003390 return skip;
3391}
3392
3393bool SyncValidator::PreCallValidateCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3394 VkSubpassContents contents) const {
3395 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003396 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003397 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003398 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003399 return skip;
3400}
3401
3402bool SyncValidator::PreCallValidateCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003403 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003404 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003405 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003406 return skip;
3407}
3408
3409bool SyncValidator::PreCallValidateCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3410 const VkRenderPassBeginInfo *pRenderPassBegin,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003411 const VkSubpassBeginInfo *pSubpassBeginInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003412 bool skip = StateTracker::PreCallValidateCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003413 skip |= ValidateBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003414 return skip;
3415}
3416
John Zulauf3d84f1b2020-03-09 13:33:25 -06003417void SyncValidator::PostCallRecordBeginCommandBuffer(VkCommandBuffer commandBuffer, const VkCommandBufferBeginInfo *pBeginInfo,
3418 VkResult result) {
3419 // The state tracker sets up the command buffer state
3420 StateTracker::PostCallRecordBeginCommandBuffer(commandBuffer, pBeginInfo, result);
3421
3422 // Create/initialize the structure that trackers accesses at the command buffer scope.
3423 auto cb_access_context = GetAccessContext(commandBuffer);
3424 assert(cb_access_context);
3425 cb_access_context->Reset();
3426}
3427
3428void SyncValidator::RecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07003429 const VkSubpassBeginInfo *pSubpassBeginInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003430 auto cb_context = GetAccessContext(commandBuffer);
John Zulauf355e49b2020-04-24 15:11:15 -06003431 if (cb_context) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003432 SyncOpBeginRenderPass sync_op(cmd, *this, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003433 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003434 }
3435}
3436
3437void SyncValidator::PostCallRecordCmdBeginRenderPass(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3438 VkSubpassContents contents) {
3439 StateTracker::PostCallRecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003440 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003441 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003442 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, &subpass_begin_info, CMD_BEGINRENDERPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003443}
3444
3445void SyncValidator::PostCallRecordCmdBeginRenderPass2(VkCommandBuffer commandBuffer, const VkRenderPassBeginInfo *pRenderPassBegin,
3446 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3447 StateTracker::PostCallRecordCmdBeginRenderPass2(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003448 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003449}
3450
3451void SyncValidator::PostCallRecordCmdBeginRenderPass2KHR(VkCommandBuffer commandBuffer,
3452 const VkRenderPassBeginInfo *pRenderPassBegin,
3453 const VkSubpassBeginInfo *pSubpassBeginInfo) {
3454 StateTracker::PostCallRecordCmdBeginRenderPass2KHR(commandBuffer, pRenderPassBegin, pSubpassBeginInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003455 RecordCmdBeginRenderPass(commandBuffer, pRenderPassBegin, pSubpassBeginInfo, CMD_BEGINRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003456}
3457
Mike Schuchardt2df08912020-12-15 16:28:09 -08003458bool SyncValidator::ValidateCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003459 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003460 bool skip = false;
3461
3462 auto cb_context = GetAccessContext(commandBuffer);
3463 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003464 if (!cb_context) return skip;
sfricke-samsung85584a72021-09-30 21:43:38 -07003465 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003466 return sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003467}
3468
3469bool SyncValidator::PreCallValidateCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) const {
3470 bool skip = StateTracker::PreCallValidateCmdNextSubpass(commandBuffer, contents);
John Zulauf64ffe552021-02-06 10:25:07 -07003471 // Convert to a NextSubpass2
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003472 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf355e49b2020-04-24 15:11:15 -06003473 subpass_begin_info.contents = contents;
John Zulauf64ffe552021-02-06 10:25:07 -07003474 auto subpass_end_info = LvlInitStruct<VkSubpassEndInfo>();
3475 skip |= ValidateCmdNextSubpass(commandBuffer, &subpass_begin_info, &subpass_end_info, CMD_NEXTSUBPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003476 return skip;
3477}
3478
Mike Schuchardt2df08912020-12-15 16:28:09 -08003479bool SyncValidator::PreCallValidateCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3480 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003481 bool skip = StateTracker::PreCallValidateCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003482 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003483 return skip;
3484}
3485
3486bool SyncValidator::PreCallValidateCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3487 const VkSubpassEndInfo *pSubpassEndInfo) const {
3488 bool skip = StateTracker::PreCallValidateCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003489 skip |= ValidateCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003490 return skip;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003491}
3492
3493void SyncValidator::RecordCmdNextSubpass(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07003494 const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulauf3d84f1b2020-03-09 13:33:25 -06003495 auto cb_context = GetAccessContext(commandBuffer);
3496 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003497 if (!cb_context) return;
John Zulauf3d84f1b2020-03-09 13:33:25 -06003498
sfricke-samsung85584a72021-09-30 21:43:38 -07003499 SyncOpNextSubpass sync_op(cmd, *this, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003500 sync_op.Record(cb_context);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003501}
3502
3503void SyncValidator::PostCallRecordCmdNextSubpass(VkCommandBuffer commandBuffer, VkSubpassContents contents) {
3504 StateTracker::PostCallRecordCmdNextSubpass(commandBuffer, contents);
Mark Lobodzinski6fe9e702020-12-30 15:36:39 -07003505 auto subpass_begin_info = LvlInitStruct<VkSubpassBeginInfo>();
John Zulauf3d84f1b2020-03-09 13:33:25 -06003506 subpass_begin_info.contents = contents;
John Zulauf355e49b2020-04-24 15:11:15 -06003507 RecordCmdNextSubpass(commandBuffer, &subpass_begin_info, nullptr, CMD_NEXTSUBPASS);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003508}
3509
3510void SyncValidator::PostCallRecordCmdNextSubpass2(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3511 const VkSubpassEndInfo *pSubpassEndInfo) {
3512 StateTracker::PostCallRecordCmdNextSubpass2(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
John Zulauf355e49b2020-04-24 15:11:15 -06003513 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003514}
3515
3516void SyncValidator::PostCallRecordCmdNextSubpass2KHR(VkCommandBuffer commandBuffer, const VkSubpassBeginInfo *pSubpassBeginInfo,
3517 const VkSubpassEndInfo *pSubpassEndInfo) {
3518 StateTracker::PostCallRecordCmdNextSubpass2KHR(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003519 RecordCmdNextSubpass(commandBuffer, pSubpassBeginInfo, pSubpassEndInfo, CMD_NEXTSUBPASS2KHR);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003520}
3521
sfricke-samsung85584a72021-09-30 21:43:38 -07003522bool SyncValidator::ValidateCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo,
3523 CMD_TYPE cmd) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003524 bool skip = false;
3525
3526 auto cb_context = GetAccessContext(commandBuffer);
3527 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003528 if (!cb_context) return skip;
John Zulauf355e49b2020-04-24 15:11:15 -06003529
sfricke-samsung85584a72021-09-30 21:43:38 -07003530 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003531 skip |= sync_op.Validate(*cb_context);
John Zulauf355e49b2020-04-24 15:11:15 -06003532 return skip;
3533}
3534
3535bool SyncValidator::PreCallValidateCmdEndRenderPass(VkCommandBuffer commandBuffer) const {
3536 bool skip = StateTracker::PreCallValidateCmdEndRenderPass(commandBuffer);
John Zulauf64ffe552021-02-06 10:25:07 -07003537 skip |= ValidateCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf355e49b2020-04-24 15:11:15 -06003538 return skip;
3539}
3540
Mike Schuchardt2df08912020-12-15 16:28:09 -08003541bool SyncValidator::PreCallValidateCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003542 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003543 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf355e49b2020-04-24 15:11:15 -06003544 return skip;
3545}
3546
3547bool SyncValidator::PreCallValidateCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer,
Mike Schuchardt2df08912020-12-15 16:28:09 -08003548 const VkSubpassEndInfo *pSubpassEndInfo) const {
John Zulauf355e49b2020-04-24 15:11:15 -06003549 bool skip = StateTracker::PreCallValidateCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
sfricke-samsung85584a72021-09-30 21:43:38 -07003550 skip |= ValidateCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf355e49b2020-04-24 15:11:15 -06003551 return skip;
3552}
3553
sfricke-samsung85584a72021-09-30 21:43:38 -07003554void SyncValidator::RecordCmdEndRenderPass(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo, CMD_TYPE cmd) {
John Zulaufe5da6e52020-03-18 15:32:18 -06003555 // Resolve the all subpass contexts to the command buffer contexts
3556 auto cb_context = GetAccessContext(commandBuffer);
3557 assert(cb_context);
John Zulauf64ffe552021-02-06 10:25:07 -07003558 if (!cb_context) return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003559
sfricke-samsung85584a72021-09-30 21:43:38 -07003560 SyncOpEndRenderPass sync_op(cmd, *this, pSubpassEndInfo);
John Zulauf64ffe552021-02-06 10:25:07 -07003561 sync_op.Record(cb_context);
3562 return;
John Zulaufe5da6e52020-03-18 15:32:18 -06003563}
John Zulauf3d84f1b2020-03-09 13:33:25 -06003564
John Zulauf33fc1d52020-07-17 11:01:10 -06003565// Simple heuristic rule to detect WAW operations representing algorithmically safe or increment
3566// updates to a resource which do not conflict at the byte level.
3567// TODO: Revisit this rule to see if it needs to be tighter or looser
3568// TODO: Add programatic control over suppression heuristics
3569bool SyncValidator::SupressedBoundDescriptorWAW(const HazardResult &hazard) const {
3570 return (hazard.hazard == WRITE_AFTER_WRITE) && (FlagBit(hazard.usage_index) == hazard.prior_access);
3571}
3572
John Zulauf3d84f1b2020-03-09 13:33:25 -06003573void SyncValidator::PostCallRecordCmdEndRenderPass(VkCommandBuffer commandBuffer) {
John Zulauf355e49b2020-04-24 15:11:15 -06003574 RecordCmdEndRenderPass(commandBuffer, nullptr, CMD_ENDRENDERPASS);
John Zulauf5a1a5382020-06-22 17:23:25 -06003575 StateTracker::PostCallRecordCmdEndRenderPass(commandBuffer);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003576}
3577
3578void SyncValidator::PostCallRecordCmdEndRenderPass2(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
John Zulauf355e49b2020-04-24 15:11:15 -06003579 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2);
John Zulauf5a1a5382020-06-22 17:23:25 -06003580 StateTracker::PostCallRecordCmdEndRenderPass2(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003581}
3582
3583void SyncValidator::PostCallRecordCmdEndRenderPass2KHR(VkCommandBuffer commandBuffer, const VkSubpassEndInfo *pSubpassEndInfo) {
sfricke-samsung85584a72021-09-30 21:43:38 -07003584 RecordCmdEndRenderPass(commandBuffer, pSubpassEndInfo, CMD_ENDRENDERPASS2KHR);
John Zulauf5a1a5382020-06-22 17:23:25 -06003585 StateTracker::PostCallRecordCmdEndRenderPass2KHR(commandBuffer, pSubpassEndInfo);
John Zulauf3d84f1b2020-03-09 13:33:25 -06003586}
locke-lunarga19c71d2020-03-02 18:17:04 -07003587
Jeff Leger178b1e52020-10-05 12:22:23 -04003588template <typename BufferImageCopyRegionType>
3589bool SyncValidator::ValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3590 VkImageLayout dstImageLayout, uint32_t regionCount,
3591 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003592 bool skip = false;
3593 const auto *cb_access_context = GetAccessContext(commandBuffer);
3594 assert(cb_access_context);
3595 if (!cb_access_context) return skip;
3596
Jeff Leger178b1e52020-10-05 12:22:23 -04003597 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3598 const char *func_name = is_2khr ? "vkCmdCopyBufferToImage2KHR()" : "vkCmdCopyBufferToImage()";
3599
locke-lunarga19c71d2020-03-02 18:17:04 -07003600 const auto *context = cb_access_context->GetCurrentAccessContext();
3601 assert(context);
3602 if (!context) return skip;
3603
3604 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
locke-lunarga19c71d2020-03-02 18:17:04 -07003605 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3606
3607 for (uint32_t region = 0; region < regionCount; region++) {
3608 const auto &copy_region = pRegions[region];
John Zulauf477700e2021-01-06 11:41:49 -07003609 HazardResult hazard;
locke-lunarga19c71d2020-03-02 18:17:04 -07003610 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003611 if (src_buffer) {
3612 ResourceAccessRange src_range =
3613 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003614 hazard = context->DetectHazard(*src_buffer, SYNC_COPY_TRANSFER_READ, src_range);
John Zulauf477700e2021-01-06 11:41:49 -07003615 if (hazard.hazard) {
3616 // PHASE1 TODO -- add tag information to log msg when useful.
3617 skip |= LogError(srcBuffer, string_SyncHazardVUID(hazard.hazard),
3618 "%s: Hazard %s for srcBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3619 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003620 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003621 }
3622 }
3623
Jeremy Gebben40a22942020-12-22 14:22:06 -07003624 hazard = context->DetectHazard(*dst_image, SYNC_COPY_TRANSFER_WRITE, copy_region.imageSubresource,
John Zulauf477700e2021-01-06 11:41:49 -07003625 copy_region.imageOffset, copy_region.imageExtent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003626 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003627 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003628 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003629 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003630 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003631 }
3632 if (skip) break;
3633 }
3634 if (skip) break;
3635 }
3636 return skip;
3637}
3638
Jeff Leger178b1e52020-10-05 12:22:23 -04003639bool SyncValidator::PreCallValidateCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3640 VkImageLayout dstImageLayout, uint32_t regionCount,
3641 const VkBufferImageCopy *pRegions) const {
3642 return ValidateCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions,
3643 COPY_COMMAND_VERSION_1);
3644}
3645
3646bool SyncValidator::PreCallValidateCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3647 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) const {
3648 return ValidateCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3649 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3650 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3651}
3652
3653template <typename BufferImageCopyRegionType>
3654void SyncValidator::RecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3655 VkImageLayout dstImageLayout, uint32_t regionCount,
3656 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003657 auto *cb_access_context = GetAccessContext(commandBuffer);
3658 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003659
3660 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3661 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYBUFFERTOIMAGE2KHR : CMD_COPYBUFFERTOIMAGE;
3662
3663 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003664 auto *context = cb_access_context->GetCurrentAccessContext();
3665 assert(context);
3666
3667 const auto *src_buffer = Get<BUFFER_STATE>(srcBuffer);
John Zulauf16adfc92020-04-08 10:28:33 -06003668 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003669
3670 for (uint32_t region = 0; region < regionCount; region++) {
3671 const auto &copy_region = pRegions[region];
locke-lunarga19c71d2020-03-02 18:17:04 -07003672 if (dst_image) {
John Zulauf477700e2021-01-06 11:41:49 -07003673 if (src_buffer) {
3674 ResourceAccessRange src_range =
3675 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, dst_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003676 context->UpdateAccessState(*src_buffer, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment, src_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003677 }
Jeremy Gebben40a22942020-12-22 14:22:06 -07003678 context->UpdateAccessState(*dst_image, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003679 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003680 }
3681 }
3682}
3683
Jeff Leger178b1e52020-10-05 12:22:23 -04003684void SyncValidator::PreCallRecordCmdCopyBufferToImage(VkCommandBuffer commandBuffer, VkBuffer srcBuffer, VkImage dstImage,
3685 VkImageLayout dstImageLayout, uint32_t regionCount,
3686 const VkBufferImageCopy *pRegions) {
3687 StateTracker::PreCallRecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions);
3688 RecordCmdCopyBufferToImage(commandBuffer, srcBuffer, dstImage, dstImageLayout, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3689}
3690
3691void SyncValidator::PreCallRecordCmdCopyBufferToImage2KHR(VkCommandBuffer commandBuffer,
3692 const VkCopyBufferToImageInfo2KHR *pCopyBufferToImageInfo) {
3693 StateTracker::PreCallRecordCmdCopyBufferToImage2KHR(commandBuffer, pCopyBufferToImageInfo);
3694 RecordCmdCopyBufferToImage(commandBuffer, pCopyBufferToImageInfo->srcBuffer, pCopyBufferToImageInfo->dstImage,
3695 pCopyBufferToImageInfo->dstImageLayout, pCopyBufferToImageInfo->regionCount,
3696 pCopyBufferToImageInfo->pRegions, COPY_COMMAND_VERSION_2);
3697}
3698
3699template <typename BufferImageCopyRegionType>
3700bool SyncValidator::ValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3701 VkBuffer dstBuffer, uint32_t regionCount,
3702 const BufferImageCopyRegionType *pRegions, CopyCommandVersion version) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003703 bool skip = false;
3704 const auto *cb_access_context = GetAccessContext(commandBuffer);
3705 assert(cb_access_context);
3706 if (!cb_access_context) return skip;
3707
Jeff Leger178b1e52020-10-05 12:22:23 -04003708 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3709 const char *func_name = is_2khr ? "vkCmdCopyImageToBuffer2KHR()" : "vkCmdCopyImageToBuffer()";
3710
locke-lunarga19c71d2020-03-02 18:17:04 -07003711 const auto *context = cb_access_context->GetCurrentAccessContext();
3712 assert(context);
3713 if (!context) return skip;
3714
3715 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3716 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06003717 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
locke-lunarga19c71d2020-03-02 18:17:04 -07003718 for (uint32_t region = 0; region < regionCount; region++) {
3719 const auto &copy_region = pRegions[region];
3720 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003721 auto hazard = context->DetectHazard(*src_image, SYNC_COPY_TRANSFER_READ, copy_region.imageSubresource,
locke-lunarga19c71d2020-03-02 18:17:04 -07003722 copy_region.imageOffset, copy_region.imageExtent);
3723 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003724 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003725 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", func_name,
John Zulauf1dae9192020-06-16 15:46:44 -06003726 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003727 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003728 }
John Zulauf477700e2021-01-06 11:41:49 -07003729 if (dst_mem) {
3730 ResourceAccessRange dst_range =
3731 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003732 hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, dst_range);
John Zulauf477700e2021-01-06 11:41:49 -07003733 if (hazard.hazard) {
3734 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
3735 "%s: Hazard %s for dstBuffer %s, region %" PRIu32 ". Access info %s.", func_name,
3736 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003737 cb_access_context->FormatUsage(hazard).c_str());
John Zulauf477700e2021-01-06 11:41:49 -07003738 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003739 }
3740 }
3741 if (skip) break;
3742 }
3743 return skip;
3744}
3745
Jeff Leger178b1e52020-10-05 12:22:23 -04003746bool SyncValidator::PreCallValidateCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage,
3747 VkImageLayout srcImageLayout, VkBuffer dstBuffer, uint32_t regionCount,
3748 const VkBufferImageCopy *pRegions) const {
3749 return ValidateCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions,
3750 COPY_COMMAND_VERSION_1);
3751}
3752
3753bool SyncValidator::PreCallValidateCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3754 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) const {
3755 return ValidateCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3756 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3757 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3758}
3759
3760template <typename BufferImageCopyRegionType>
3761void SyncValidator::RecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3762 VkBuffer dstBuffer, uint32_t regionCount, const BufferImageCopyRegionType *pRegions,
3763 CopyCommandVersion version) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003764 auto *cb_access_context = GetAccessContext(commandBuffer);
3765 assert(cb_access_context);
Jeff Leger178b1e52020-10-05 12:22:23 -04003766
3767 const bool is_2khr = (version == COPY_COMMAND_VERSION_2);
3768 const CMD_TYPE cmd_type = is_2khr ? CMD_COPYIMAGETOBUFFER2KHR : CMD_COPYIMAGETOBUFFER;
3769
3770 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunarga19c71d2020-03-02 18:17:04 -07003771 auto *context = cb_access_context->GetCurrentAccessContext();
3772 assert(context);
3773
3774 const auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003775 auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
Jeremy Gebben6fbf8242021-06-21 09:14:46 -06003776 const auto dst_mem = (dst_buffer && !dst_buffer->sparse) ? dst_buffer->MemState()->mem() : VK_NULL_HANDLE;
John Zulauf5f13a792020-03-10 07:31:21 -06003777 const VulkanTypedHandle dst_handle(dst_mem, kVulkanObjectTypeDeviceMemory);
locke-lunarga19c71d2020-03-02 18:17:04 -07003778
3779 for (uint32_t region = 0; region < regionCount; region++) {
3780 const auto &copy_region = pRegions[region];
3781 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07003782 context->UpdateAccessState(*src_image, SYNC_COPY_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003783 copy_region.imageSubresource, copy_region.imageOffset, copy_region.imageExtent, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003784 if (dst_buffer) {
3785 ResourceAccessRange dst_range =
3786 MakeRange(copy_region.bufferOffset, GetBufferSizeFromCopyImage(copy_region, src_image->createInfo.format));
Jeremy Gebben40a22942020-12-22 14:22:06 -07003787 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, dst_range, tag);
John Zulauf477700e2021-01-06 11:41:49 -07003788 }
locke-lunarga19c71d2020-03-02 18:17:04 -07003789 }
3790 }
3791}
3792
Jeff Leger178b1e52020-10-05 12:22:23 -04003793void SyncValidator::PreCallRecordCmdCopyImageToBuffer(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3794 VkBuffer dstBuffer, uint32_t regionCount, const VkBufferImageCopy *pRegions) {
3795 StateTracker::PreCallRecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions);
3796 RecordCmdCopyImageToBuffer(commandBuffer, srcImage, srcImageLayout, dstBuffer, regionCount, pRegions, COPY_COMMAND_VERSION_1);
3797}
3798
3799void SyncValidator::PreCallRecordCmdCopyImageToBuffer2KHR(VkCommandBuffer commandBuffer,
3800 const VkCopyImageToBufferInfo2KHR *pCopyImageToBufferInfo) {
3801 StateTracker::PreCallRecordCmdCopyImageToBuffer2KHR(commandBuffer, pCopyImageToBufferInfo);
3802 RecordCmdCopyImageToBuffer(commandBuffer, pCopyImageToBufferInfo->srcImage, pCopyImageToBufferInfo->srcImageLayout,
3803 pCopyImageToBufferInfo->dstBuffer, pCopyImageToBufferInfo->regionCount,
3804 pCopyImageToBufferInfo->pRegions, COPY_COMMAND_VERSION_2);
3805}
3806
3807template <typename RegionType>
3808bool SyncValidator::ValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3809 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3810 const RegionType *pRegions, VkFilter filter, const char *apiName) const {
locke-lunarga19c71d2020-03-02 18:17:04 -07003811 bool skip = false;
3812 const auto *cb_access_context = GetAccessContext(commandBuffer);
3813 assert(cb_access_context);
3814 if (!cb_access_context) return skip;
3815
3816 const auto *context = cb_access_context->GetCurrentAccessContext();
3817 assert(context);
3818 if (!context) return skip;
3819
3820 const auto *src_image = Get<IMAGE_STATE>(srcImage);
3821 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
3822
3823 for (uint32_t region = 0; region < regionCount; region++) {
3824 const auto &blit_region = pRegions[region];
3825 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003826 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3827 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3828 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3829 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3830 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3831 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003832 auto hazard = context->DetectHazard(*src_image, SYNC_BLIT_TRANSFER_READ, blit_region.srcSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003833 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003834 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003835 "%s: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003836 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).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 }
3840
3841 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003842 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3843 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3844 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3845 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3846 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3847 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003848 auto hazard = context->DetectHazard(*dst_image, SYNC_BLIT_TRANSFER_WRITE, blit_region.dstSubresource, offset, extent);
locke-lunarga19c71d2020-03-02 18:17:04 -07003849 if (hazard.hazard) {
locke-lunarga0003652020-03-10 11:38:51 -06003850 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
Jeff Leger178b1e52020-10-05 12:22:23 -04003851 "%s: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.", apiName,
John Zulauf1dae9192020-06-16 15:46:44 -06003852 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07003853 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarga19c71d2020-03-02 18:17:04 -07003854 }
3855 if (skip) break;
3856 }
3857 }
3858
3859 return skip;
3860}
3861
Jeff Leger178b1e52020-10-05 12:22:23 -04003862bool SyncValidator::PreCallValidateCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3863 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3864 const VkImageBlit *pRegions, VkFilter filter) const {
3865 return ValidateCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter,
3866 "vkCmdBlitImage");
3867}
3868
3869bool SyncValidator::PreCallValidateCmdBlitImage2KHR(VkCommandBuffer commandBuffer,
3870 const VkBlitImageInfo2KHR *pBlitImageInfo) const {
3871 return ValidateCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3872 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3873 pBlitImageInfo->filter, "vkCmdBlitImage2KHR");
3874}
3875
3876template <typename RegionType>
3877void SyncValidator::RecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3878 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3879 const RegionType *pRegions, VkFilter filter, ResourceUsageTag tag) {
locke-lunarga19c71d2020-03-02 18:17:04 -07003880 auto *cb_access_context = GetAccessContext(commandBuffer);
3881 assert(cb_access_context);
3882 auto *context = cb_access_context->GetCurrentAccessContext();
3883 assert(context);
3884
3885 auto *src_image = Get<IMAGE_STATE>(srcImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003886 auto *dst_image = Get<IMAGE_STATE>(dstImage);
locke-lunarga19c71d2020-03-02 18:17:04 -07003887
3888 for (uint32_t region = 0; region < regionCount; region++) {
3889 const auto &blit_region = pRegions[region];
3890 if (src_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003891 VkOffset3D offset = {std::min(blit_region.srcOffsets[0].x, blit_region.srcOffsets[1].x),
3892 std::min(blit_region.srcOffsets[0].y, blit_region.srcOffsets[1].y),
3893 std::min(blit_region.srcOffsets[0].z, blit_region.srcOffsets[1].z)};
3894 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.srcOffsets[1].x - blit_region.srcOffsets[0].x)),
3895 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].y - blit_region.srcOffsets[0].y)),
3896 static_cast<uint32_t>(abs(blit_region.srcOffsets[1].z - blit_region.srcOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003897 context->UpdateAccessState(*src_image, SYNC_BLIT_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003898 blit_region.srcSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003899 }
3900 if (dst_image) {
locke-lunarg8f93acc2020-06-18 21:26:46 -06003901 VkOffset3D offset = {std::min(blit_region.dstOffsets[0].x, blit_region.dstOffsets[1].x),
3902 std::min(blit_region.dstOffsets[0].y, blit_region.dstOffsets[1].y),
3903 std::min(blit_region.dstOffsets[0].z, blit_region.dstOffsets[1].z)};
3904 VkExtent3D extent = {static_cast<uint32_t>(abs(blit_region.dstOffsets[1].x - blit_region.dstOffsets[0].x)),
3905 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].y - blit_region.dstOffsets[0].y)),
3906 static_cast<uint32_t>(abs(blit_region.dstOffsets[1].z - blit_region.dstOffsets[0].z))};
Jeremy Gebben40a22942020-12-22 14:22:06 -07003907 context->UpdateAccessState(*dst_image, SYNC_BLIT_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07003908 blit_region.dstSubresource, offset, extent, tag);
locke-lunarga19c71d2020-03-02 18:17:04 -07003909 }
3910 }
3911}
locke-lunarg36ba2592020-04-03 09:42:04 -06003912
Jeff Leger178b1e52020-10-05 12:22:23 -04003913void SyncValidator::PreCallRecordCmdBlitImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
3914 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
3915 const VkImageBlit *pRegions, VkFilter filter) {
3916 auto *cb_access_context = GetAccessContext(commandBuffer);
3917 assert(cb_access_context);
3918 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE);
3919 StateTracker::PreCallRecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
3920 pRegions, filter);
3921 RecordCmdBlitImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount, pRegions, filter, tag);
3922}
3923
3924void SyncValidator::PreCallRecordCmdBlitImage2KHR(VkCommandBuffer commandBuffer, const VkBlitImageInfo2KHR *pBlitImageInfo) {
3925 StateTracker::PreCallRecordCmdBlitImage2KHR(commandBuffer, pBlitImageInfo);
3926 auto *cb_access_context = GetAccessContext(commandBuffer);
3927 assert(cb_access_context);
3928 const auto tag = cb_access_context->NextCommandTag(CMD_BLITIMAGE2KHR);
3929 RecordCmdBlitImage(commandBuffer, pBlitImageInfo->srcImage, pBlitImageInfo->srcImageLayout, pBlitImageInfo->dstImage,
3930 pBlitImageInfo->dstImageLayout, pBlitImageInfo->regionCount, pBlitImageInfo->pRegions,
3931 pBlitImageInfo->filter, tag);
3932}
3933
John Zulauffaea0ee2021-01-14 14:01:32 -07003934bool SyncValidator::ValidateIndirectBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3935 VkCommandBuffer commandBuffer, const VkDeviceSize struct_size, const VkBuffer buffer,
3936 const VkDeviceSize offset, const uint32_t drawCount, const uint32_t stride,
3937 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003938 bool skip = false;
3939 if (drawCount == 0) return skip;
3940
3941 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3942 VkDeviceSize size = struct_size;
3943 if (drawCount == 1 || stride == size) {
3944 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003945 const ResourceAccessRange range = MakeRange(offset, size);
locke-lunargff255f92020-05-13 18:53:52 -06003946 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3947 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003948 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003949 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003950 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003951 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003952 }
3953 } else {
3954 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003955 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
locke-lunargff255f92020-05-13 18:53:52 -06003956 auto hazard = context.DetectHazard(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3957 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003958 skip |= LogError(buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003959 "%s: Hazard %s for indirect %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
3960 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003961 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06003962 break;
3963 }
3964 }
3965 }
3966 return skip;
3967}
3968
locke-lunarg61870c22020-06-09 14:51:50 -06003969void SyncValidator::RecordIndirectBuffer(AccessContext &context, const ResourceUsageTag &tag, const VkDeviceSize struct_size,
3970 const VkBuffer buffer, const VkDeviceSize offset, const uint32_t drawCount,
3971 uint32_t stride) {
locke-lunargff255f92020-05-13 18:53:52 -06003972 const auto *buf_state = Get<BUFFER_STATE>(buffer);
3973 VkDeviceSize size = struct_size;
3974 if (drawCount == 1 || stride == size) {
3975 if (drawCount > 1) size *= drawCount;
John Zulauf3e86bf02020-09-12 10:47:57 -06003976 const ResourceAccessRange range = MakeRange(offset, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003977 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06003978 } else {
3979 for (uint32_t i = 0; i < drawCount; ++i) {
John Zulauf3e86bf02020-09-12 10:47:57 -06003980 const ResourceAccessRange range = MakeRange(offset + i * stride, size);
John Zulauf8e3c3e92021-01-06 11:19:36 -07003981 context.UpdateAccessState(*buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range,
3982 tag);
locke-lunargff255f92020-05-13 18:53:52 -06003983 }
3984 }
3985}
3986
John Zulauffaea0ee2021-01-14 14:01:32 -07003987bool SyncValidator::ValidateCountBuffer(const CommandBufferAccessContext &cb_context, const AccessContext &context,
3988 VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
3989 const char *function) const {
locke-lunargff255f92020-05-13 18:53:52 -06003990 bool skip = false;
3991
3992 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06003993 const ResourceAccessRange range = MakeRange(offset, 4);
locke-lunargff255f92020-05-13 18:53:52 -06003994 auto hazard = context.DetectHazard(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, range);
3995 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06003996 skip |= LogError(count_buf_state->buffer(), string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06003997 "%s: Hazard %s for countBuffer %s in %s. Access info %s.", function, string_SyncHazard(hazard.hazard),
John Zulauf1dae9192020-06-16 15:46:44 -06003998 report_data->FormatHandle(buffer).c_str(), report_data->FormatHandle(commandBuffer).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07003999 cb_context.FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004000 }
4001 return skip;
4002}
4003
locke-lunarg61870c22020-06-09 14:51:50 -06004004void SyncValidator::RecordCountBuffer(AccessContext &context, const ResourceUsageTag &tag, VkBuffer buffer, VkDeviceSize offset) {
locke-lunargff255f92020-05-13 18:53:52 -06004005 const auto *count_buf_state = Get<BUFFER_STATE>(buffer);
John Zulauf3e86bf02020-09-12 10:47:57 -06004006 const ResourceAccessRange range = MakeRange(offset, 4);
John Zulauf8e3c3e92021-01-06 11:19:36 -07004007 context.UpdateAccessState(*count_buf_state, SYNC_DRAW_INDIRECT_INDIRECT_COMMAND_READ, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004008}
4009
locke-lunarg36ba2592020-04-03 09:42:04 -06004010bool SyncValidator::PreCallValidateCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) const {
locke-lunargff255f92020-05-13 18:53:52 -06004011 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004012 const auto *cb_access_context = GetAccessContext(commandBuffer);
4013 assert(cb_access_context);
4014 if (!cb_access_context) return skip;
4015
locke-lunarg61870c22020-06-09 14:51:50 -06004016 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatch");
locke-lunargff255f92020-05-13 18:53:52 -06004017 return skip;
locke-lunarg36ba2592020-04-03 09:42:04 -06004018}
4019
4020void SyncValidator::PreCallRecordCmdDispatch(VkCommandBuffer commandBuffer, uint32_t x, uint32_t y, uint32_t z) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004021 StateTracker::PreCallRecordCmdDispatch(commandBuffer, x, y, z);
locke-lunargff255f92020-05-13 18:53:52 -06004022 auto *cb_access_context = GetAccessContext(commandBuffer);
4023 assert(cb_access_context);
4024 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCH);
locke-lunargff255f92020-05-13 18:53:52 -06004025
locke-lunarg61870c22020-06-09 14:51:50 -06004026 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
locke-lunarg36ba2592020-04-03 09:42:04 -06004027}
locke-lunarge1a67022020-04-29 00:15:36 -06004028
4029bool SyncValidator::PreCallValidateCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) const {
locke-lunargff255f92020-05-13 18:53:52 -06004030 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004031 const auto *cb_access_context = GetAccessContext(commandBuffer);
4032 assert(cb_access_context);
4033 if (!cb_access_context) return skip;
4034
4035 const auto *context = cb_access_context->GetCurrentAccessContext();
4036 assert(context);
4037 if (!context) return skip;
4038
locke-lunarg61870c22020-06-09 14:51:50 -06004039 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, "vkCmdDispatchIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004040 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDispatchIndirectCommand), buffer, offset,
4041 1, sizeof(VkDispatchIndirectCommand), "vkCmdDispatchIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004042 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004043}
4044
4045void SyncValidator::PreCallRecordCmdDispatchIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004046 StateTracker::PreCallRecordCmdDispatchIndirect(commandBuffer, buffer, offset);
locke-lunargff255f92020-05-13 18:53:52 -06004047 auto *cb_access_context = GetAccessContext(commandBuffer);
4048 assert(cb_access_context);
4049 const auto tag = cb_access_context->NextCommandTag(CMD_DISPATCHINDIRECT);
4050 auto *context = cb_access_context->GetCurrentAccessContext();
4051 assert(context);
4052
locke-lunarg61870c22020-06-09 14:51:50 -06004053 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_COMPUTE, tag);
4054 RecordIndirectBuffer(*context, tag, sizeof(VkDispatchIndirectCommand), buffer, offset, 1, sizeof(VkDispatchIndirectCommand));
locke-lunarge1a67022020-04-29 00:15:36 -06004055}
4056
4057bool SyncValidator::PreCallValidateCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4058 uint32_t firstVertex, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004059 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004060 const auto *cb_access_context = GetAccessContext(commandBuffer);
4061 assert(cb_access_context);
4062 if (!cb_access_context) return skip;
4063
locke-lunarg61870c22020-06-09 14:51:50 -06004064 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDraw");
4065 skip |= cb_access_context->ValidateDrawVertex(vertexCount, firstVertex, "vkCmdDraw");
4066 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDraw");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004067 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004068}
4069
4070void SyncValidator::PreCallRecordCmdDraw(VkCommandBuffer commandBuffer, uint32_t vertexCount, uint32_t instanceCount,
4071 uint32_t firstVertex, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004072 StateTracker::PreCallRecordCmdDraw(commandBuffer, vertexCount, instanceCount, firstVertex, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004073 auto *cb_access_context = GetAccessContext(commandBuffer);
4074 assert(cb_access_context);
4075 const auto tag = cb_access_context->NextCommandTag(CMD_DRAW);
locke-lunargff255f92020-05-13 18:53:52 -06004076
locke-lunarg61870c22020-06-09 14:51:50 -06004077 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4078 cb_access_context->RecordDrawVertex(vertexCount, firstVertex, tag);
4079 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004080}
4081
4082bool SyncValidator::PreCallValidateCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4083 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) const {
locke-lunarga4d39ea2020-05-22 14:17:29 -06004084 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004085 const auto *cb_access_context = GetAccessContext(commandBuffer);
4086 assert(cb_access_context);
4087 if (!cb_access_context) return skip;
4088
locke-lunarg61870c22020-06-09 14:51:50 -06004089 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexed");
4090 skip |= cb_access_context->ValidateDrawVertexIndex(indexCount, firstIndex, "vkCmdDrawIndexed");
4091 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexed");
locke-lunarga4d39ea2020-05-22 14:17:29 -06004092 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004093}
4094
4095void SyncValidator::PreCallRecordCmdDrawIndexed(VkCommandBuffer commandBuffer, uint32_t indexCount, uint32_t instanceCount,
4096 uint32_t firstIndex, int32_t vertexOffset, uint32_t firstInstance) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004097 StateTracker::PreCallRecordCmdDrawIndexed(commandBuffer, indexCount, instanceCount, firstIndex, vertexOffset, firstInstance);
locke-lunargff255f92020-05-13 18:53:52 -06004098 auto *cb_access_context = GetAccessContext(commandBuffer);
4099 assert(cb_access_context);
4100 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXED);
locke-lunargff255f92020-05-13 18:53:52 -06004101
locke-lunarg61870c22020-06-09 14:51:50 -06004102 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4103 cb_access_context->RecordDrawVertexIndex(indexCount, firstIndex, tag);
4104 cb_access_context->RecordDrawSubpassAttachment(tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004105}
4106
4107bool SyncValidator::PreCallValidateCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4108 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004109 bool skip = false;
4110 if (drawCount == 0) return skip;
4111
locke-lunargff255f92020-05-13 18:53:52 -06004112 const auto *cb_access_context = GetAccessContext(commandBuffer);
4113 assert(cb_access_context);
4114 if (!cb_access_context) return skip;
4115
4116 const auto *context = cb_access_context->GetCurrentAccessContext();
4117 assert(context);
4118 if (!context) return skip;
4119
locke-lunarg61870c22020-06-09 14:51:50 -06004120 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndirect");
4121 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004122 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4123 drawCount, stride, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004124
4125 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4126 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4127 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004128 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, "vkCmdDrawIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004129 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004130}
4131
4132void SyncValidator::PreCallRecordCmdDrawIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4133 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004134 StateTracker::PreCallRecordCmdDrawIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004135 if (drawCount == 0) return;
locke-lunargff255f92020-05-13 18:53:52 -06004136 auto *cb_access_context = GetAccessContext(commandBuffer);
4137 assert(cb_access_context);
4138 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDIRECT);
4139 auto *context = cb_access_context->GetCurrentAccessContext();
4140 assert(context);
4141
locke-lunarg61870c22020-06-09 14:51:50 -06004142 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4143 cb_access_context->RecordDrawSubpassAttachment(tag);
4144 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004145
4146 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4147 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4148 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004149 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004150}
4151
4152bool SyncValidator::PreCallValidateCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4153 uint32_t drawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004154 bool skip = false;
4155 if (drawCount == 0) return skip;
locke-lunargff255f92020-05-13 18:53:52 -06004156 const auto *cb_access_context = GetAccessContext(commandBuffer);
4157 assert(cb_access_context);
4158 if (!cb_access_context) return skip;
4159
4160 const auto *context = cb_access_context->GetCurrentAccessContext();
4161 assert(context);
4162 if (!context) return skip;
4163
locke-lunarg61870c22020-06-09 14:51:50 -06004164 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, "vkCmdDrawIndexedIndirect");
4165 skip |= cb_access_context->ValidateDrawSubpassAttachment("vkCmdDrawIndexedIndirect");
John Zulauffaea0ee2021-01-14 14:01:32 -07004166 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4167 offset, drawCount, stride, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004168
4169 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4170 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4171 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004172 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, "vkCmdDrawIndexedIndirect");
locke-lunargff255f92020-05-13 18:53:52 -06004173 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004174}
4175
4176void SyncValidator::PreCallRecordCmdDrawIndexedIndirect(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4177 uint32_t drawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004178 StateTracker::PreCallRecordCmdDrawIndexedIndirect(commandBuffer, buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004179 auto *cb_access_context = GetAccessContext(commandBuffer);
4180 assert(cb_access_context);
4181 const auto tag = cb_access_context->NextCommandTag(CMD_DRAWINDEXEDINDIRECT);
4182 auto *context = cb_access_context->GetCurrentAccessContext();
4183 assert(context);
4184
locke-lunarg61870c22020-06-09 14:51:50 -06004185 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4186 cb_access_context->RecordDrawSubpassAttachment(tag);
4187 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, drawCount, stride);
locke-lunargff255f92020-05-13 18:53:52 -06004188
4189 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4190 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4191 // We will record the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004192 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004193}
4194
4195bool SyncValidator::ValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4196 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4197 uint32_t stride, const char *function) const {
4198 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004199 const auto *cb_access_context = GetAccessContext(commandBuffer);
4200 assert(cb_access_context);
4201 if (!cb_access_context) return skip;
4202
4203 const auto *context = cb_access_context->GetCurrentAccessContext();
4204 assert(context);
4205 if (!context) return skip;
4206
locke-lunarg61870c22020-06-09 14:51:50 -06004207 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4208 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004209 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndirectCommand), buffer, offset,
4210 maxDrawCount, stride, function);
4211 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004212
4213 // TODO: For now, we validate the whole vertex buffer. It might cause some false positive.
4214 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4215 // We will validate the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004216 skip |= cb_access_context->ValidateDrawVertex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004217 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004218}
4219
4220bool SyncValidator::PreCallValidateCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4221 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4222 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004223 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4224 "vkCmdDrawIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004225}
4226
sfricke-samsung85584a72021-09-30 21:43:38 -07004227void SyncValidator::RecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4228 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4229 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004230 auto *cb_access_context = GetAccessContext(commandBuffer);
4231 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004232 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004233 auto *context = cb_access_context->GetCurrentAccessContext();
4234 assert(context);
4235
locke-lunarg61870c22020-06-09 14:51:50 -06004236 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4237 cb_access_context->RecordDrawSubpassAttachment(tag);
4238 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndirectCommand), buffer, offset, 1, stride);
4239 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004240
4241 // TODO: For now, we record the whole vertex buffer. It might cause some false positive.
4242 // VkDrawIndirectCommand buffer could be changed until SubmitQueue.
4243 // We will record the vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004244 cb_access_context->RecordDrawVertex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004245}
4246
sfricke-samsung85584a72021-09-30 21:43:38 -07004247void SyncValidator::PreCallRecordCmdDrawIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4248 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4249 uint32_t stride) {
4250 StateTracker::PreCallRecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4251 stride);
4252 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4253 CMD_DRAWINDIRECTCOUNT);
4254}
locke-lunarge1a67022020-04-29 00:15:36 -06004255bool SyncValidator::PreCallValidateCmdDrawIndirectCountKHR(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 "vkCmdDrawIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004260}
4261
4262void SyncValidator::PreCallRecordCmdDrawIndirectCountKHR(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::PreCallRecordCmdDrawIndirectCountKHR(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_DRAWINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004269}
4270
4271bool SyncValidator::PreCallValidateCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4272 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4273 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004274 return ValidateCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4275 "vkCmdDrawIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004276}
4277
4278void SyncValidator::PreCallRecordCmdDrawIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4279 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4280 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004281 StateTracker::PreCallRecordCmdDrawIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount,
4282 stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004283 RecordCmdDrawIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4284 CMD_DRAWINDIRECTCOUNTAMD);
locke-lunargff255f92020-05-13 18:53:52 -06004285}
4286
4287bool SyncValidator::ValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4288 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4289 uint32_t stride, const char *function) const {
4290 bool skip = false;
locke-lunargff255f92020-05-13 18:53:52 -06004291 const auto *cb_access_context = GetAccessContext(commandBuffer);
4292 assert(cb_access_context);
4293 if (!cb_access_context) return skip;
4294
4295 const auto *context = cb_access_context->GetCurrentAccessContext();
4296 assert(context);
4297 if (!context) return skip;
4298
locke-lunarg61870c22020-06-09 14:51:50 -06004299 skip |= cb_access_context->ValidateDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, function);
4300 skip |= cb_access_context->ValidateDrawSubpassAttachment(function);
John Zulauffaea0ee2021-01-14 14:01:32 -07004301 skip |= ValidateIndirectBuffer(*cb_access_context, *context, commandBuffer, sizeof(VkDrawIndexedIndirectCommand), buffer,
4302 offset, maxDrawCount, stride, function);
4303 skip |= ValidateCountBuffer(*cb_access_context, *context, commandBuffer, countBuffer, countBufferOffset, function);
locke-lunargff255f92020-05-13 18:53:52 -06004304
4305 // TODO: For now, we validate the whole index and vertex buffer. It might cause some false positive.
4306 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
4307 // We will validate the index and vertex buffer in SubmitQueue in the future.
locke-lunarg61870c22020-06-09 14:51:50 -06004308 skip |= cb_access_context->ValidateDrawVertexIndex(UINT32_MAX, 0, function);
locke-lunargff255f92020-05-13 18:53:52 -06004309 return skip;
locke-lunarge1a67022020-04-29 00:15:36 -06004310}
4311
4312bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4313 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4314 uint32_t maxDrawCount, uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004315 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4316 "vkCmdDrawIndexedIndirectCount");
locke-lunarge1a67022020-04-29 00:15:36 -06004317}
4318
sfricke-samsung85584a72021-09-30 21:43:38 -07004319void SyncValidator::RecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4320 VkBuffer countBuffer, VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4321 uint32_t stride, CMD_TYPE cmd_type) {
locke-lunargff255f92020-05-13 18:53:52 -06004322 auto *cb_access_context = GetAccessContext(commandBuffer);
4323 assert(cb_access_context);
sfricke-samsung85584a72021-09-30 21:43:38 -07004324 const auto tag = cb_access_context->NextCommandTag(cmd_type);
locke-lunargff255f92020-05-13 18:53:52 -06004325 auto *context = cb_access_context->GetCurrentAccessContext();
4326 assert(context);
4327
locke-lunarg61870c22020-06-09 14:51:50 -06004328 cb_access_context->RecordDispatchDrawDescriptorSet(VK_PIPELINE_BIND_POINT_GRAPHICS, tag);
4329 cb_access_context->RecordDrawSubpassAttachment(tag);
4330 RecordIndirectBuffer(*context, tag, sizeof(VkDrawIndexedIndirectCommand), buffer, offset, 1, stride);
4331 RecordCountBuffer(*context, tag, countBuffer, countBufferOffset);
locke-lunargff255f92020-05-13 18:53:52 -06004332
4333 // TODO: For now, we record the whole index and vertex buffer. It might cause some false positive.
4334 // VkDrawIndexedIndirectCommand buffer could be changed until SubmitQueue.
locke-lunarg61870c22020-06-09 14:51:50 -06004335 // We will update the index and vertex buffer in SubmitQueue in the future.
4336 cb_access_context->RecordDrawVertexIndex(UINT32_MAX, 0, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004337}
4338
sfricke-samsung85584a72021-09-30 21:43:38 -07004339void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCount(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4340 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4341 uint32_t maxDrawCount, uint32_t stride) {
4342 StateTracker::PreCallRecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4343 maxDrawCount, stride);
4344 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4345 CMD_DRAWINDEXEDINDIRECTCOUNT);
4346}
4347
locke-lunarge1a67022020-04-29 00:15:36 -06004348bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer,
4349 VkDeviceSize offset, VkBuffer countBuffer,
4350 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4351 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004352 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4353 "vkCmdDrawIndexedIndirectCountKHR");
locke-lunarge1a67022020-04-29 00:15:36 -06004354}
4355
4356void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountKHR(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4357 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4358 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004359 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountKHR(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4360 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004361 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4362 CMD_DRAWINDEXEDINDIRECTCOUNTKHR);
locke-lunarge1a67022020-04-29 00:15:36 -06004363}
4364
4365bool SyncValidator::PreCallValidateCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer,
4366 VkDeviceSize offset, VkBuffer countBuffer,
4367 VkDeviceSize countBufferOffset, uint32_t maxDrawCount,
4368 uint32_t stride) const {
locke-lunargff255f92020-05-13 18:53:52 -06004369 return ValidateCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4370 "vkCmdDrawIndexedIndirectCountAMD");
locke-lunarge1a67022020-04-29 00:15:36 -06004371}
4372
4373void SyncValidator::PreCallRecordCmdDrawIndexedIndirectCountAMD(VkCommandBuffer commandBuffer, VkBuffer buffer, VkDeviceSize offset,
4374 VkBuffer countBuffer, VkDeviceSize countBufferOffset,
4375 uint32_t maxDrawCount, uint32_t stride) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004376 StateTracker::PreCallRecordCmdDrawIndexedIndirectCountAMD(commandBuffer, buffer, offset, countBuffer, countBufferOffset,
4377 maxDrawCount, stride);
sfricke-samsung85584a72021-09-30 21:43:38 -07004378 RecordCmdDrawIndexedIndirectCount(commandBuffer, buffer, offset, countBuffer, countBufferOffset, maxDrawCount, stride,
4379 CMD_DRAWINDEXEDINDIRECTCOUNTAMD);
locke-lunarge1a67022020-04-29 00:15:36 -06004380}
4381
4382bool SyncValidator::PreCallValidateCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4383 const VkClearColorValue *pColor, uint32_t rangeCount,
4384 const VkImageSubresourceRange *pRanges) const {
4385 bool skip = false;
4386 const auto *cb_access_context = GetAccessContext(commandBuffer);
4387 assert(cb_access_context);
4388 if (!cb_access_context) return skip;
4389
4390 const auto *context = cb_access_context->GetCurrentAccessContext();
4391 assert(context);
4392 if (!context) return skip;
4393
4394 const auto *image_state = Get<IMAGE_STATE>(image);
4395
4396 for (uint32_t index = 0; index < rangeCount; index++) {
4397 const auto &range = pRanges[index];
4398 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004399 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004400 if (hazard.hazard) {
4401 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004402 "vkCmdClearColorImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004403 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004404 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004405 }
4406 }
4407 }
4408 return skip;
4409}
4410
4411void SyncValidator::PreCallRecordCmdClearColorImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4412 const VkClearColorValue *pColor, uint32_t rangeCount,
4413 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004414 StateTracker::PreCallRecordCmdClearColorImage(commandBuffer, image, imageLayout, pColor, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004415 auto *cb_access_context = GetAccessContext(commandBuffer);
4416 assert(cb_access_context);
4417 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARCOLORIMAGE);
4418 auto *context = cb_access_context->GetCurrentAccessContext();
4419 assert(context);
4420
4421 const auto *image_state = Get<IMAGE_STATE>(image);
4422
4423 for (uint32_t index = 0; index < rangeCount; index++) {
4424 const auto &range = pRanges[index];
4425 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004426 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004427 }
4428 }
4429}
4430
4431bool SyncValidator::PreCallValidateCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image,
4432 VkImageLayout imageLayout,
4433 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4434 const VkImageSubresourceRange *pRanges) const {
4435 bool skip = false;
4436 const auto *cb_access_context = GetAccessContext(commandBuffer);
4437 assert(cb_access_context);
4438 if (!cb_access_context) return skip;
4439
4440 const auto *context = cb_access_context->GetCurrentAccessContext();
4441 assert(context);
4442 if (!context) return skip;
4443
4444 const auto *image_state = Get<IMAGE_STATE>(image);
4445
4446 for (uint32_t index = 0; index < rangeCount; index++) {
4447 const auto &range = pRanges[index];
4448 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004449 auto hazard = context->DetectHazard(*image_state, SYNC_CLEAR_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004450 if (hazard.hazard) {
4451 skip |= LogError(image, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004452 "vkCmdClearDepthStencilImage: Hazard %s for %s, range index %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004453 string_SyncHazard(hazard.hazard), report_data->FormatHandle(image).c_str(), index,
John Zulauffaea0ee2021-01-14 14:01:32 -07004454 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004455 }
4456 }
4457 }
4458 return skip;
4459}
4460
4461void SyncValidator::PreCallRecordCmdClearDepthStencilImage(VkCommandBuffer commandBuffer, VkImage image, VkImageLayout imageLayout,
4462 const VkClearDepthStencilValue *pDepthStencil, uint32_t rangeCount,
4463 const VkImageSubresourceRange *pRanges) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004464 StateTracker::PreCallRecordCmdClearDepthStencilImage(commandBuffer, image, imageLayout, pDepthStencil, rangeCount, pRanges);
locke-lunarge1a67022020-04-29 00:15:36 -06004465 auto *cb_access_context = GetAccessContext(commandBuffer);
4466 assert(cb_access_context);
4467 const auto tag = cb_access_context->NextCommandTag(CMD_CLEARDEPTHSTENCILIMAGE);
4468 auto *context = cb_access_context->GetCurrentAccessContext();
4469 assert(context);
4470
4471 const auto *image_state = Get<IMAGE_STATE>(image);
4472
4473 for (uint32_t index = 0; index < rangeCount; index++) {
4474 const auto &range = pRanges[index];
4475 if (image_state) {
John Zulauf110413c2021-03-20 05:38:38 -06004476 context->UpdateAccessState(*image_state, SYNC_CLEAR_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004477 }
4478 }
4479}
4480
4481bool SyncValidator::PreCallValidateCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool,
4482 uint32_t firstQuery, uint32_t queryCount, VkBuffer dstBuffer,
4483 VkDeviceSize dstOffset, VkDeviceSize stride,
4484 VkQueryResultFlags flags) const {
4485 bool skip = false;
4486 const auto *cb_access_context = GetAccessContext(commandBuffer);
4487 assert(cb_access_context);
4488 if (!cb_access_context) return skip;
4489
4490 const auto *context = cb_access_context->GetCurrentAccessContext();
4491 assert(context);
4492 if (!context) return skip;
4493
4494 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4495
4496 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004497 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004498 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004499 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004500 skip |=
4501 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4502 "vkCmdCopyQueryPoolResults: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004503 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004504 }
4505 }
locke-lunargff255f92020-05-13 18:53:52 -06004506
4507 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004508 return skip;
4509}
4510
4511void SyncValidator::PreCallRecordCmdCopyQueryPoolResults(VkCommandBuffer commandBuffer, VkQueryPool queryPool, uint32_t firstQuery,
4512 uint32_t queryCount, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4513 VkDeviceSize stride, VkQueryResultFlags flags) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004514 StateTracker::PreCallRecordCmdCopyQueryPoolResults(commandBuffer, queryPool, firstQuery, queryCount, dstBuffer, dstOffset,
4515 stride, flags);
locke-lunarge1a67022020-04-29 00:15:36 -06004516 auto *cb_access_context = GetAccessContext(commandBuffer);
4517 assert(cb_access_context);
locke-lunargff255f92020-05-13 18:53:52 -06004518 const auto tag = cb_access_context->NextCommandTag(CMD_COPYQUERYPOOLRESULTS);
locke-lunarge1a67022020-04-29 00:15:36 -06004519 auto *context = cb_access_context->GetCurrentAccessContext();
4520 assert(context);
4521
4522 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4523
4524 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004525 const ResourceAccessRange range = MakeRange(dstOffset, stride * queryCount);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004526 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004527 }
locke-lunargff255f92020-05-13 18:53:52 -06004528
4529 // TODO:Track VkQueryPool
locke-lunarge1a67022020-04-29 00:15:36 -06004530}
4531
4532bool SyncValidator::PreCallValidateCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4533 VkDeviceSize size, uint32_t data) const {
4534 bool skip = false;
4535 const auto *cb_access_context = GetAccessContext(commandBuffer);
4536 assert(cb_access_context);
4537 if (!cb_access_context) return skip;
4538
4539 const auto *context = cb_access_context->GetCurrentAccessContext();
4540 assert(context);
4541 if (!context) return skip;
4542
4543 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4544
4545 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004546 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004547 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004548 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004549 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004550 "vkCmdFillBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004551 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004552 }
4553 }
4554 return skip;
4555}
4556
4557void SyncValidator::PreCallRecordCmdFillBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4558 VkDeviceSize size, uint32_t data) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004559 StateTracker::PreCallRecordCmdFillBuffer(commandBuffer, dstBuffer, dstOffset, size, data);
locke-lunarge1a67022020-04-29 00:15:36 -06004560 auto *cb_access_context = GetAccessContext(commandBuffer);
4561 assert(cb_access_context);
4562 const auto tag = cb_access_context->NextCommandTag(CMD_FILLBUFFER);
4563 auto *context = cb_access_context->GetCurrentAccessContext();
4564 assert(context);
4565
4566 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4567
4568 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004569 const ResourceAccessRange range = MakeRange(*dst_buffer, dstOffset, size);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004570 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004571 }
4572}
4573
4574bool SyncValidator::PreCallValidateCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4575 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4576 const VkImageResolve *pRegions) const {
4577 bool skip = false;
4578 const auto *cb_access_context = GetAccessContext(commandBuffer);
4579 assert(cb_access_context);
4580 if (!cb_access_context) return skip;
4581
4582 const auto *context = cb_access_context->GetCurrentAccessContext();
4583 assert(context);
4584 if (!context) return skip;
4585
4586 const auto *src_image = Get<IMAGE_STATE>(srcImage);
4587 const auto *dst_image = Get<IMAGE_STATE>(dstImage);
4588
4589 for (uint32_t region = 0; region < regionCount; region++) {
4590 const auto &resolve_region = pRegions[region];
4591 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004592 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004593 resolve_region.srcOffset, resolve_region.extent);
4594 if (hazard.hazard) {
4595 skip |= LogError(srcImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004596 "vkCmdResolveImage: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004597 string_SyncHazard(hazard.hazard), report_data->FormatHandle(srcImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004598 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004599 }
4600 }
4601
4602 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004603 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
locke-lunarge1a67022020-04-29 00:15:36 -06004604 resolve_region.dstOffset, resolve_region.extent);
4605 if (hazard.hazard) {
4606 skip |= LogError(dstImage, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004607 "vkCmdResolveImage: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
John Zulauf1dae9192020-06-16 15:46:44 -06004608 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstImage).c_str(), region,
John Zulauffaea0ee2021-01-14 14:01:32 -07004609 cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004610 }
4611 if (skip) break;
4612 }
4613 }
4614
4615 return skip;
4616}
4617
4618void SyncValidator::PreCallRecordCmdResolveImage(VkCommandBuffer commandBuffer, VkImage srcImage, VkImageLayout srcImageLayout,
4619 VkImage dstImage, VkImageLayout dstImageLayout, uint32_t regionCount,
4620 const VkImageResolve *pRegions) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004621 StateTracker::PreCallRecordCmdResolveImage(commandBuffer, srcImage, srcImageLayout, dstImage, dstImageLayout, regionCount,
4622 pRegions);
locke-lunarge1a67022020-04-29 00:15:36 -06004623 auto *cb_access_context = GetAccessContext(commandBuffer);
4624 assert(cb_access_context);
4625 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE);
4626 auto *context = cb_access_context->GetCurrentAccessContext();
4627 assert(context);
4628
4629 auto *src_image = Get<IMAGE_STATE>(srcImage);
4630 auto *dst_image = Get<IMAGE_STATE>(dstImage);
4631
4632 for (uint32_t region = 0; region < regionCount; region++) {
4633 const auto &resolve_region = pRegions[region];
4634 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004635 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004636 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004637 }
4638 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004639 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004640 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004641 }
4642 }
4643}
4644
Jeff Leger178b1e52020-10-05 12:22:23 -04004645bool SyncValidator::PreCallValidateCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4646 const VkResolveImageInfo2KHR *pResolveImageInfo) const {
4647 bool skip = false;
4648 const auto *cb_access_context = GetAccessContext(commandBuffer);
4649 assert(cb_access_context);
4650 if (!cb_access_context) return skip;
4651
4652 const auto *context = cb_access_context->GetCurrentAccessContext();
4653 assert(context);
4654 if (!context) return skip;
4655
4656 const auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4657 const auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4658
4659 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4660 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4661 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004662 auto hazard = context->DetectHazard(*src_image, SYNC_RESOLVE_TRANSFER_READ, resolve_region.srcSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004663 resolve_region.srcOffset, resolve_region.extent);
4664 if (hazard.hazard) {
4665 skip |= LogError(pResolveImageInfo->srcImage, string_SyncHazardVUID(hazard.hazard),
4666 "vkCmdResolveImage2KHR: Hazard %s for srcImage %s, region %" PRIu32 ". Access info %s.",
4667 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->srcImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004668 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004669 }
4670 }
4671
4672 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004673 auto hazard = context->DetectHazard(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, resolve_region.dstSubresource,
Jeff Leger178b1e52020-10-05 12:22:23 -04004674 resolve_region.dstOffset, resolve_region.extent);
4675 if (hazard.hazard) {
4676 skip |= LogError(pResolveImageInfo->dstImage, string_SyncHazardVUID(hazard.hazard),
4677 "vkCmdResolveImage2KHR: Hazard %s for dstImage %s, region %" PRIu32 ". Access info %s.",
4678 string_SyncHazard(hazard.hazard), report_data->FormatHandle(pResolveImageInfo->dstImage).c_str(),
John Zulauffaea0ee2021-01-14 14:01:32 -07004679 region, cb_access_context->FormatUsage(hazard).c_str());
Jeff Leger178b1e52020-10-05 12:22:23 -04004680 }
4681 if (skip) break;
4682 }
4683 }
4684
4685 return skip;
4686}
4687
4688void SyncValidator::PreCallRecordCmdResolveImage2KHR(VkCommandBuffer commandBuffer,
4689 const VkResolveImageInfo2KHR *pResolveImageInfo) {
4690 StateTracker::PreCallRecordCmdResolveImage2KHR(commandBuffer, pResolveImageInfo);
4691 auto *cb_access_context = GetAccessContext(commandBuffer);
4692 assert(cb_access_context);
4693 const auto tag = cb_access_context->NextCommandTag(CMD_RESOLVEIMAGE2KHR);
4694 auto *context = cb_access_context->GetCurrentAccessContext();
4695 assert(context);
4696
4697 auto *src_image = Get<IMAGE_STATE>(pResolveImageInfo->srcImage);
4698 auto *dst_image = Get<IMAGE_STATE>(pResolveImageInfo->dstImage);
4699
4700 for (uint32_t region = 0; region < pResolveImageInfo->regionCount; region++) {
4701 const auto &resolve_region = pResolveImageInfo->pRegions[region];
4702 if (src_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004703 context->UpdateAccessState(*src_image, SYNC_RESOLVE_TRANSFER_READ, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004704 resolve_region.srcSubresource, resolve_region.srcOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004705 }
4706 if (dst_image) {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004707 context->UpdateAccessState(*dst_image, SYNC_RESOLVE_TRANSFER_WRITE, SyncOrdering::kNonAttachment,
John Zulauf8e3c3e92021-01-06 11:19:36 -07004708 resolve_region.dstSubresource, resolve_region.dstOffset, resolve_region.extent, tag);
Jeff Leger178b1e52020-10-05 12:22:23 -04004709 }
4710 }
4711}
4712
locke-lunarge1a67022020-04-29 00:15:36 -06004713bool SyncValidator::PreCallValidateCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4714 VkDeviceSize dataSize, const void *pData) const {
4715 bool skip = false;
4716 const auto *cb_access_context = GetAccessContext(commandBuffer);
4717 assert(cb_access_context);
4718 if (!cb_access_context) return skip;
4719
4720 const auto *context = cb_access_context->GetCurrentAccessContext();
4721 assert(context);
4722 if (!context) return skip;
4723
4724 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4725
4726 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004727 // VK_WHOLE_SIZE not allowed
4728 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004729 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunarge1a67022020-04-29 00:15:36 -06004730 if (hazard.hazard) {
John Zulauf1dae9192020-06-16 15:46:44 -06004731 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
John Zulauf59e25072020-07-17 10:55:21 -06004732 "vkCmdUpdateBuffer: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004733 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunarge1a67022020-04-29 00:15:36 -06004734 }
4735 }
4736 return skip;
4737}
4738
4739void SyncValidator::PreCallRecordCmdUpdateBuffer(VkCommandBuffer commandBuffer, VkBuffer dstBuffer, VkDeviceSize dstOffset,
4740 VkDeviceSize dataSize, const void *pData) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004741 StateTracker::PreCallRecordCmdUpdateBuffer(commandBuffer, dstBuffer, dstOffset, dataSize, pData);
locke-lunarge1a67022020-04-29 00:15:36 -06004742 auto *cb_access_context = GetAccessContext(commandBuffer);
4743 assert(cb_access_context);
4744 const auto tag = cb_access_context->NextCommandTag(CMD_UPDATEBUFFER);
4745 auto *context = cb_access_context->GetCurrentAccessContext();
4746 assert(context);
4747
4748 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4749
4750 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004751 // VK_WHOLE_SIZE not allowed
4752 const ResourceAccessRange range = MakeRange(dstOffset, dataSize);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004753 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunarge1a67022020-04-29 00:15:36 -06004754 }
4755}
locke-lunargff255f92020-05-13 18:53:52 -06004756
4757bool SyncValidator::PreCallValidateCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4758 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
4759 bool skip = false;
4760 const auto *cb_access_context = GetAccessContext(commandBuffer);
4761 assert(cb_access_context);
4762 if (!cb_access_context) return skip;
4763
4764 const auto *context = cb_access_context->GetCurrentAccessContext();
4765 assert(context);
4766 if (!context) return skip;
4767
4768 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4769
4770 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004771 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004772 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
locke-lunargff255f92020-05-13 18:53:52 -06004773 if (hazard.hazard) {
John Zulauf59e25072020-07-17 10:55:21 -06004774 skip |=
4775 LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
4776 "vkCmdWriteBufferMarkerAMD: Hazard %s for dstBuffer %s. Access info %s.", string_SyncHazard(hazard.hazard),
John Zulauffaea0ee2021-01-14 14:01:32 -07004777 report_data->FormatHandle(dstBuffer).c_str(), cb_access_context->FormatUsage(hazard).c_str());
locke-lunargff255f92020-05-13 18:53:52 -06004778 }
4779 }
4780 return skip;
4781}
4782
4783void SyncValidator::PreCallRecordCmdWriteBufferMarkerAMD(VkCommandBuffer commandBuffer, VkPipelineStageFlagBits pipelineStage,
4784 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
locke-lunarg8ec19162020-06-16 18:48:34 -06004785 StateTracker::PreCallRecordCmdWriteBufferMarkerAMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
locke-lunargff255f92020-05-13 18:53:52 -06004786 auto *cb_access_context = GetAccessContext(commandBuffer);
4787 assert(cb_access_context);
4788 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
4789 auto *context = cb_access_context->GetCurrentAccessContext();
4790 assert(context);
4791
4792 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
4793
4794 if (dst_buffer) {
John Zulauf3e86bf02020-09-12 10:47:57 -06004795 const ResourceAccessRange range = MakeRange(dstOffset, 4);
Jeremy Gebben40a22942020-12-22 14:22:06 -07004796 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
locke-lunargff255f92020-05-13 18:53:52 -06004797 }
4798}
John Zulauf49beb112020-11-04 16:06:31 -07004799
4800bool SyncValidator::PreCallValidateCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) const {
4801 bool skip = false;
4802 const auto *cb_context = GetAccessContext(commandBuffer);
4803 assert(cb_context);
4804 if (!cb_context) return skip;
4805
John Zulauf36ef9282021-02-02 11:47:24 -07004806 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004807 return set_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004808}
4809
4810void SyncValidator::PostCallRecordCmdSetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4811 StateTracker::PostCallRecordCmdSetEvent(commandBuffer, event, stageMask);
4812 auto *cb_context = GetAccessContext(commandBuffer);
4813 assert(cb_context);
4814 if (!cb_context) return;
John Zulauf36ef9282021-02-02 11:47:24 -07004815 SyncOpSetEvent set_event_op(CMD_SETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4816 set_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004817}
4818
John Zulauf4edde622021-02-15 08:54:50 -07004819bool SyncValidator::PreCallValidateCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4820 const VkDependencyInfoKHR *pDependencyInfo) const {
4821 bool skip = false;
4822 const auto *cb_context = GetAccessContext(commandBuffer);
4823 assert(cb_context);
4824 if (!cb_context || !pDependencyInfo) return skip;
4825
4826 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4827 return set_event_op.Validate(*cb_context);
4828}
4829
4830void SyncValidator::PostCallRecordCmdSetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4831 const VkDependencyInfoKHR *pDependencyInfo) {
4832 StateTracker::PostCallRecordCmdSetEvent2KHR(commandBuffer, event, pDependencyInfo);
4833 auto *cb_context = GetAccessContext(commandBuffer);
4834 assert(cb_context);
4835 if (!cb_context || !pDependencyInfo) return;
4836
4837 SyncOpSetEvent set_event_op(CMD_SETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, *pDependencyInfo);
4838 set_event_op.Record(cb_context);
4839}
4840
John Zulauf49beb112020-11-04 16:06:31 -07004841bool SyncValidator::PreCallValidateCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event,
4842 VkPipelineStageFlags stageMask) const {
4843 bool skip = false;
4844 const auto *cb_context = GetAccessContext(commandBuffer);
4845 assert(cb_context);
4846 if (!cb_context) return skip;
4847
John Zulauf36ef9282021-02-02 11:47:24 -07004848 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
John Zulauf6ce24372021-01-30 05:56:25 -07004849 return reset_event_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004850}
4851
4852void SyncValidator::PostCallRecordCmdResetEvent(VkCommandBuffer commandBuffer, VkEvent event, VkPipelineStageFlags stageMask) {
4853 StateTracker::PostCallRecordCmdResetEvent(commandBuffer, event, stageMask);
4854 auto *cb_context = GetAccessContext(commandBuffer);
4855 assert(cb_context);
4856 if (!cb_context) return;
4857
John Zulauf36ef9282021-02-02 11:47:24 -07004858 SyncOpResetEvent reset_event_op(CMD_RESETEVENT, *this, cb_context->GetQueueFlags(), event, stageMask);
4859 reset_event_op.Record(cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004860}
4861
John Zulauf4edde622021-02-15 08:54:50 -07004862bool SyncValidator::PreCallValidateCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4863 VkPipelineStageFlags2KHR stageMask) const {
4864 bool skip = false;
4865 const auto *cb_context = GetAccessContext(commandBuffer);
4866 assert(cb_context);
4867 if (!cb_context) return skip;
4868
4869 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4870 return reset_event_op.Validate(*cb_context);
4871}
4872
4873void SyncValidator::PostCallRecordCmdResetEvent2KHR(VkCommandBuffer commandBuffer, VkEvent event,
4874 VkPipelineStageFlags2KHR stageMask) {
4875 StateTracker::PostCallRecordCmdResetEvent2KHR(commandBuffer, event, stageMask);
4876 auto *cb_context = GetAccessContext(commandBuffer);
4877 assert(cb_context);
4878 if (!cb_context) return;
4879
4880 SyncOpResetEvent reset_event_op(CMD_RESETEVENT2KHR, *this, cb_context->GetQueueFlags(), event, stageMask);
4881 reset_event_op.Record(cb_context);
4882}
4883
John Zulauf49beb112020-11-04 16:06:31 -07004884bool SyncValidator::PreCallValidateCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4885 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4886 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4887 uint32_t bufferMemoryBarrierCount,
4888 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4889 uint32_t imageMemoryBarrierCount,
4890 const VkImageMemoryBarrier *pImageMemoryBarriers) const {
4891 bool skip = false;
4892 const auto *cb_context = GetAccessContext(commandBuffer);
4893 assert(cb_context);
4894 if (!cb_context) return skip;
4895
John Zulauf36ef9282021-02-02 11:47:24 -07004896 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4897 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4898 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufd5115702021-01-18 12:34:33 -07004899 return wait_events_op.Validate(*cb_context);
John Zulauf49beb112020-11-04 16:06:31 -07004900}
4901
4902void SyncValidator::PostCallRecordCmdWaitEvents(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4903 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4904 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
4905 uint32_t bufferMemoryBarrierCount,
4906 const VkBufferMemoryBarrier *pBufferMemoryBarriers,
4907 uint32_t imageMemoryBarrierCount,
4908 const VkImageMemoryBarrier *pImageMemoryBarriers) {
4909 StateTracker::PostCallRecordCmdWaitEvents(commandBuffer, eventCount, pEvents, srcStageMask, dstStageMask, memoryBarrierCount,
4910 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers,
4911 imageMemoryBarrierCount, pImageMemoryBarriers);
4912
4913 auto *cb_context = GetAccessContext(commandBuffer);
4914 assert(cb_context);
4915 if (!cb_context) return;
4916
John Zulauf36ef9282021-02-02 11:47:24 -07004917 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS, *this, cb_context->GetQueueFlags(), eventCount, pEvents, srcStageMask,
4918 dstStageMask, memoryBarrierCount, pMemoryBarriers, bufferMemoryBarrierCount,
4919 pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers);
4920 return wait_events_op.Record(cb_context);
John Zulauf4a6105a2020-11-17 15:11:05 -07004921}
4922
John Zulauf4edde622021-02-15 08:54:50 -07004923bool SyncValidator::PreCallValidateCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4924 const VkDependencyInfoKHR *pDependencyInfos) const {
4925 bool skip = false;
4926 const auto *cb_context = GetAccessContext(commandBuffer);
4927 assert(cb_context);
4928 if (!cb_context) return skip;
4929
4930 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4931 skip |= wait_events_op.Validate(*cb_context);
4932 return skip;
4933}
4934
4935void SyncValidator::PostCallRecordCmdWaitEvents2KHR(VkCommandBuffer commandBuffer, uint32_t eventCount, const VkEvent *pEvents,
4936 const VkDependencyInfoKHR *pDependencyInfos) {
4937 StateTracker::PostCallRecordCmdWaitEvents2KHR(commandBuffer, eventCount, pEvents, pDependencyInfos);
4938
4939 auto *cb_context = GetAccessContext(commandBuffer);
4940 assert(cb_context);
4941 if (!cb_context) return;
4942
4943 SyncOpWaitEvents wait_events_op(CMD_WAITEVENTS2KHR, *this, cb_context->GetQueueFlags(), eventCount, pEvents, pDependencyInfos);
4944 wait_events_op.Record(cb_context);
4945}
4946
John Zulauf4a6105a2020-11-17 15:11:05 -07004947void SyncEventState::ResetFirstScope() {
4948 for (const auto address_type : kAddressTypes) {
4949 first_scope[static_cast<size_t>(address_type)].clear();
4950 }
Jeremy Gebben9893daf2021-01-04 10:40:50 -07004951 scope = SyncExecScope();
John Zulauf4a6105a2020-11-17 15:11:05 -07004952}
4953
4954// Keep the "ignore this event" logic in same place for ValidateWait and RecordWait to use
John Zulauf4edde622021-02-15 08:54:50 -07004955SyncEventState::IgnoreReason SyncEventState::IsIgnoredByWait(CMD_TYPE cmd, VkPipelineStageFlags2KHR srcStageMask) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004956 IgnoreReason reason = NotIgnored;
4957
John Zulauf4edde622021-02-15 08:54:50 -07004958 if ((CMD_WAITEVENTS2KHR == cmd) && (CMD_SETEVENT == last_command)) {
4959 reason = SetVsWait2;
4960 } else if ((last_command == CMD_RESETEVENT || last_command == CMD_RESETEVENT2KHR) && !HasBarrier(0U, 0U)) {
4961 reason = (last_command == CMD_RESETEVENT) ? ResetWaitRace : Reset2WaitRace;
John Zulauf4a6105a2020-11-17 15:11:05 -07004962 } else if (unsynchronized_set) {
4963 reason = SetRace;
4964 } else {
Jeremy Gebben40a22942020-12-22 14:22:06 -07004965 const VkPipelineStageFlags2KHR missing_bits = scope.mask_param & ~srcStageMask;
John Zulauf4a6105a2020-11-17 15:11:05 -07004966 if (missing_bits) reason = MissingStageBits;
4967 }
4968
4969 return reason;
4970}
4971
Jeremy Gebben40a22942020-12-22 14:22:06 -07004972bool SyncEventState::HasBarrier(VkPipelineStageFlags2KHR stageMask, VkPipelineStageFlags2KHR exec_scope_arg) const {
John Zulauf4a6105a2020-11-17 15:11:05 -07004973 bool has_barrier = (last_command == CMD_NONE) || (stageMask & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT) ||
4974 (barriers & exec_scope_arg) || (barriers & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
4975 return has_barrier;
John Zulauf49beb112020-11-04 16:06:31 -07004976}
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004977
John Zulauf36ef9282021-02-02 11:47:24 -07004978SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
4979 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
4980 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07004981 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
4982 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
4983 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf4edde622021-02-15 08:54:50 -07004984 : SyncOpBase(cmd), barriers_(1) {
4985 auto &barrier_set = barriers_[0];
4986 barrier_set.dependency_flags = dependencyFlags;
4987 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, srcStageMask);
4988 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, dstStageMask);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004989 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
John Zulauf4edde622021-02-15 08:54:50 -07004990 barrier_set.MakeMemoryBarriers(barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags, memoryBarrierCount,
4991 pMemoryBarriers);
4992 barrier_set.MakeBufferMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4993 bufferMemoryBarrierCount, pBufferMemoryBarriers);
4994 barrier_set.MakeImageMemoryBarriers(sync_state, barrier_set.src_exec_scope, barrier_set.dst_exec_scope, dependencyFlags,
4995 imageMemoryBarrierCount, pImageMemoryBarriers);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07004996}
4997
John Zulauf4edde622021-02-15 08:54:50 -07004998SyncOpBarriers::SyncOpBarriers(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t event_count,
4999 const VkDependencyInfoKHR *dep_infos)
5000 : SyncOpBase(cmd), barriers_(event_count) {
5001 for (uint32_t i = 0; i < event_count; i++) {
5002 const auto &dep_info = dep_infos[i];
5003 auto &barrier_set = barriers_[i];
5004 barrier_set.dependency_flags = dep_info.dependencyFlags;
5005 auto stage_masks = sync_utils::GetGlobalStageMasks(dep_info);
5006 barrier_set.src_exec_scope = SyncExecScope::MakeSrc(queue_flags, stage_masks.src);
5007 barrier_set.dst_exec_scope = SyncExecScope::MakeDst(queue_flags, stage_masks.dst);
5008 // Translate the API parameters into structures SyncVal understands directly, and dehandle for safer/faster replay.
5009 barrier_set.MakeMemoryBarriers(queue_flags, dep_info.dependencyFlags, dep_info.memoryBarrierCount,
5010 dep_info.pMemoryBarriers);
5011 barrier_set.MakeBufferMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.bufferMemoryBarrierCount,
5012 dep_info.pBufferMemoryBarriers);
5013 barrier_set.MakeImageMemoryBarriers(sync_state, queue_flags, dep_info.dependencyFlags, dep_info.imageMemoryBarrierCount,
5014 dep_info.pImageMemoryBarriers);
5015 }
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005016}
5017
John Zulauf36ef9282021-02-02 11:47:24 -07005018SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
John Zulaufd5115702021-01-18 12:34:33 -07005019 VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5020 VkDependencyFlags dependencyFlags, uint32_t memoryBarrierCount,
5021 const VkMemoryBarrier *pMemoryBarriers, uint32_t bufferMemoryBarrierCount,
5022 const VkBufferMemoryBarrier *pBufferMemoryBarriers, uint32_t imageMemoryBarrierCount,
5023 const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005024 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, dependencyFlags, memoryBarrierCount, pMemoryBarriers,
John Zulaufd5115702021-01-18 12:34:33 -07005025 bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount, pImageMemoryBarriers) {}
5026
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005027SyncOpPipelineBarrier::SyncOpPipelineBarrier(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags,
5028 const VkDependencyInfoKHR &dep_info)
John Zulauf4edde622021-02-15 08:54:50 -07005029 : SyncOpBarriers(cmd, sync_state, queue_flags, 1, &dep_info) {}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005030
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005031bool SyncOpPipelineBarrier::Validate(const CommandBufferAccessContext &cb_context) const {
5032 bool skip = false;
5033 const auto *context = cb_context.GetCurrentAccessContext();
5034 assert(context);
5035 if (!context) return skip;
John Zulauf6fdf3d02021-03-05 16:50:47 -07005036 assert(barriers_.size() == 1); // PipelineBarriers only support a single barrier set.
5037
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005038 // Validate Image Layout transitions
John Zulauf6fdf3d02021-03-05 16:50:47 -07005039 const auto &barrier_set = barriers_[0];
5040 for (const auto &image_barrier : barrier_set.image_memory_barriers) {
5041 if (image_barrier.new_layout == image_barrier.old_layout) continue; // Only interested in layout transitions at this point.
5042 const auto *image_state = image_barrier.image.get();
5043 if (!image_state) continue;
5044 const auto hazard = context->DetectImageBarrierHazard(image_barrier);
5045 if (hazard.hazard) {
5046 // PHASE1 TODO -- add tag information to log msg when useful.
5047 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005048 const auto image_handle = image_state->image();
John Zulauf6fdf3d02021-03-05 16:50:47 -07005049 skip |= sync_state.LogError(image_handle, string_SyncHazardVUID(hazard.hazard),
5050 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5051 string_SyncHazard(hazard.hazard), image_barrier.index,
5052 sync_state.report_data->FormatHandle(image_handle).c_str(),
5053 cb_context.FormatUsage(hazard).c_str());
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005054 }
5055 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005056 return skip;
5057}
5058
John Zulaufd5115702021-01-18 12:34:33 -07005059struct SyncOpPipelineBarrierFunctorFactory {
5060 using BarrierOpFunctor = PipelineBarrierOp;
5061 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5062 using GlobalBarrierOpFunctor = PipelineBarrierOp;
5063 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5064 using BufferRange = ResourceAccessRange;
5065 using ImageRange = subresource_adapter::ImageRangeGenerator;
5066 using GlobalRange = ResourceAccessRange;
5067
5068 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier, bool layout_transition) const {
5069 return ApplyFunctor(BarrierOpFunctor(barrier, layout_transition));
5070 }
5071 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5072 return GlobalApplyFunctor(true /* resolve */, size_hint, tag);
5073 }
5074 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier) const {
5075 return GlobalBarrierOpFunctor(barrier, false);
5076 }
5077
5078 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range) const {
5079 if (!SimpleBinding(buffer)) return ResourceAccessRange();
5080 const auto base_address = ResourceBaseAddress(buffer);
5081 return (range + base_address);
5082 }
John Zulauf110413c2021-03-20 05:38:38 -06005083 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulauf264cce02021-02-05 14:40:47 -07005084 if (!SimpleBinding(image)) return subresource_adapter::ImageRangeGenerator();
John Zulaufd5115702021-01-18 12:34:33 -07005085
5086 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005087 subresource_adapter::ImageRangeGenerator range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005088 return range_gen;
5089 }
5090 GlobalRange MakeGlobalRangeGen(AccessAddressType) const { return kFullRange; }
5091};
5092
5093template <typename Barriers, typename FunctorFactory>
5094void SyncOpBarriers::ApplyBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5095 AccessContext *context) {
5096 for (const auto &barrier : barriers) {
5097 const auto *state = barrier.GetState();
5098 if (state) {
5099 auto *const accesses = &context->GetAccessStateMap(GetAccessAddressType(*state));
5100 auto update_action = factory.MakeApplyFunctor(barrier.barrier, barrier.IsLayoutTransition());
5101 auto range_gen = factory.MakeRangeGen(*state, barrier.Range());
5102 UpdateMemoryAccessState(accesses, update_action, &range_gen);
5103 }
5104 }
5105}
5106
5107template <typename Barriers, typename FunctorFactory>
5108void SyncOpBarriers::ApplyGlobalBarriers(const Barriers &barriers, const FunctorFactory &factory, const ResourceUsageTag &tag,
5109 AccessContext *access_context) {
5110 auto barriers_functor = factory.MakeGlobalApplyFunctor(barriers.size(), tag);
5111 for (const auto &barrier : barriers) {
5112 barriers_functor.EmplaceBack(factory.MakeGlobalBarrierOpFunctor(barrier));
5113 }
5114 for (const auto address_type : kAddressTypes) {
5115 auto range_gen = factory.MakeGlobalRangeGen(address_type);
5116 UpdateMemoryAccessState(&(access_context->GetAccessStateMap(address_type)), barriers_functor, &range_gen);
5117 }
5118}
5119
John Zulauf36ef9282021-02-02 11:47:24 -07005120void SyncOpPipelineBarrier::Record(CommandBufferAccessContext *cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005121 SyncOpPipelineBarrierFunctorFactory factory;
5122 auto *access_context = cb_context->GetCurrentAccessContext();
John Zulauf36ef9282021-02-02 11:47:24 -07005123 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005124
John Zulauf4edde622021-02-15 08:54:50 -07005125 // Pipeline barriers only have a single barrier set, unlike WaitEvents2
5126 assert(barriers_.size() == 1);
5127 const auto &barrier_set = barriers_[0];
5128 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5129 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5130 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
5131
5132 if (barrier_set.single_exec_scope) {
5133 cb_context->ApplyGlobalBarriersToEvents(barrier_set.src_exec_scope, barrier_set.dst_exec_scope);
5134 } else {
5135 for (const auto &barrier : barrier_set.memory_barriers) {
5136 cb_context->ApplyGlobalBarriersToEvents(barrier.src_exec_scope, barrier.dst_exec_scope);
5137 }
5138 }
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005139}
5140
John Zulauf4edde622021-02-15 08:54:50 -07005141void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(const SyncExecScope &src, const SyncExecScope &dst,
5142 VkDependencyFlags dependency_flags, uint32_t memory_barrier_count,
5143 const VkMemoryBarrier *barriers) {
5144 memory_barriers.reserve(std::max<uint32_t>(1, memory_barrier_count));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005145 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005146 const auto &barrier = barriers[barrier_index];
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005147 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005148 memory_barriers.emplace_back(sync_barrier);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005149 }
5150 if (0 == memory_barrier_count) {
5151 // If there are no global memory barriers, force an exec barrier
John Zulauf4edde622021-02-15 08:54:50 -07005152 memory_barriers.emplace_back(SyncBarrier(src, dst));
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005153 }
John Zulauf4edde622021-02-15 08:54:50 -07005154 single_exec_scope = true;
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005155}
5156
John Zulauf4edde622021-02-15 08:54:50 -07005157void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5158 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5159 uint32_t barrier_count, const VkBufferMemoryBarrier *barriers) {
5160 buffer_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005161 for (uint32_t index = 0; index < barrier_count; index++) {
5162 const auto &barrier = barriers[index];
5163 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5164 if (buffer) {
5165 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5166 const auto range = MakeRange(barrier.offset, barrier_size);
5167 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005168 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005169 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005170 buffer_memory_barriers.emplace_back();
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005171 }
5172 }
5173}
5174
John Zulauf4edde622021-02-15 08:54:50 -07005175void SyncOpBarriers::BarrierSet::MakeMemoryBarriers(VkQueueFlags queue_flags, VkDependencyFlags dependency_flags,
5176 uint32_t memory_barrier_count, const VkMemoryBarrier2KHR *barriers) {
5177 memory_barriers.reserve(memory_barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005178 for (uint32_t barrier_index = 0; barrier_index < memory_barrier_count; barrier_index++) {
John Zulauf4edde622021-02-15 08:54:50 -07005179 const auto &barrier = barriers[barrier_index];
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005180 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5181 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5182 SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005183 memory_barriers.emplace_back(sync_barrier);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005184 }
John Zulauf4edde622021-02-15 08:54:50 -07005185 single_exec_scope = false;
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005186}
5187
John Zulauf4edde622021-02-15 08:54:50 -07005188void SyncOpBarriers::BarrierSet::MakeBufferMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5189 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5190 const VkBufferMemoryBarrier2KHR *barriers) {
5191 buffer_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005192 for (uint32_t index = 0; index < barrier_count; index++) {
5193 const auto &barrier = barriers[index];
5194 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5195 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5196 auto buffer = sync_state.GetShared<BUFFER_STATE>(barrier.buffer);
5197 if (buffer) {
5198 const auto barrier_size = GetBufferWholeSize(*buffer, barrier.offset, barrier.size);
5199 const auto range = MakeRange(barrier.offset, barrier_size);
5200 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005201 buffer_memory_barriers.emplace_back(buffer, sync_barrier, range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005202 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005203 buffer_memory_barriers.emplace_back();
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005204 }
5205 }
5206}
5207
John Zulauf4edde622021-02-15 08:54:50 -07005208void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, const SyncExecScope &src,
5209 const SyncExecScope &dst, VkDependencyFlags dependencyFlags,
5210 uint32_t barrier_count, const VkImageMemoryBarrier *barriers) {
5211 image_memory_barriers.reserve(barrier_count);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005212 for (uint32_t index = 0; index < barrier_count; index++) {
5213 const auto &barrier = barriers[index];
5214 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5215 if (image) {
5216 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5217 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005218 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005219 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005220 image_memory_barriers.emplace_back();
5221 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
John Zulaufe7f6a5e2021-01-16 14:31:18 -07005222 }
5223 }
5224}
John Zulaufd5115702021-01-18 12:34:33 -07005225
John Zulauf4edde622021-02-15 08:54:50 -07005226void SyncOpBarriers::BarrierSet::MakeImageMemoryBarriers(const SyncValidator &sync_state, VkQueueFlags queue_flags,
5227 VkDependencyFlags dependencyFlags, uint32_t barrier_count,
5228 const VkImageMemoryBarrier2KHR *barriers) {
5229 image_memory_barriers.reserve(barrier_count);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005230 for (uint32_t index = 0; index < barrier_count; index++) {
5231 const auto &barrier = barriers[index];
5232 auto src = SyncExecScope::MakeSrc(queue_flags, barrier.srcStageMask);
5233 auto dst = SyncExecScope::MakeDst(queue_flags, barrier.dstStageMask);
5234 const auto image = sync_state.GetShared<IMAGE_STATE>(barrier.image);
5235 if (image) {
5236 auto subresource_range = NormalizeSubresourceRange(image->createInfo, barrier.subresourceRange);
5237 const SyncBarrier sync_barrier(barrier, src, dst);
John Zulauf4edde622021-02-15 08:54:50 -07005238 image_memory_barriers.emplace_back(image, index, sync_barrier, barrier.oldLayout, barrier.newLayout, subresource_range);
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005239 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005240 image_memory_barriers.emplace_back();
5241 image_memory_barriers.back().index = index; // Just in case we're interested in the ones we skipped.
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005242 }
5243 }
5244}
5245
John Zulauf36ef9282021-02-02 11:47:24 -07005246SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
John Zulaufd5115702021-01-18 12:34:33 -07005247 const VkEvent *pEvents, VkPipelineStageFlags srcStageMask, VkPipelineStageFlags dstStageMask,
5248 uint32_t memoryBarrierCount, const VkMemoryBarrier *pMemoryBarriers,
5249 uint32_t bufferMemoryBarrierCount, const VkBufferMemoryBarrier *pBufferMemoryBarriers,
5250 uint32_t imageMemoryBarrierCount, const VkImageMemoryBarrier *pImageMemoryBarriers)
John Zulauf36ef9282021-02-02 11:47:24 -07005251 : SyncOpBarriers(cmd, sync_state, queue_flags, srcStageMask, dstStageMask, VkDependencyFlags(0U), memoryBarrierCount,
John Zulaufd5115702021-01-18 12:34:33 -07005252 pMemoryBarriers, bufferMemoryBarrierCount, pBufferMemoryBarriers, imageMemoryBarrierCount,
5253 pImageMemoryBarriers) {
John Zulauf669dfd52021-01-27 17:15:28 -07005254 MakeEventsList(sync_state, eventCount, pEvents);
John Zulaufd5115702021-01-18 12:34:33 -07005255}
5256
John Zulauf4edde622021-02-15 08:54:50 -07005257SyncOpWaitEvents::SyncOpWaitEvents(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, uint32_t eventCount,
5258 const VkEvent *pEvents, const VkDependencyInfoKHR *pDependencyInfo)
5259 : SyncOpBarriers(cmd, sync_state, queue_flags, eventCount, pDependencyInfo) {
5260 MakeEventsList(sync_state, eventCount, pEvents);
5261 assert(events_.size() == barriers_.size()); // Just so nobody gets clever and decides to cull the event or barrier arrays
5262}
5263
John Zulaufd5115702021-01-18 12:34:33 -07005264bool SyncOpWaitEvents::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulaufd5115702021-01-18 12:34:33 -07005265 const char *const ignored = "Wait operation is ignored for this event.";
5266 bool skip = false;
5267 const auto &sync_state = cb_context.GetSyncState();
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005268 const auto command_buffer_handle = cb_context.GetCBState().commandBuffer();
John Zulaufd5115702021-01-18 12:34:33 -07005269
John Zulauf4edde622021-02-15 08:54:50 -07005270 for (size_t barrier_set_index = 0; barrier_set_index < barriers_.size(); barrier_set_index++) {
5271 const auto &barrier_set = barriers_[barrier_set_index];
5272 if (barrier_set.single_exec_scope) {
5273 if (barrier_set.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5274 const std::string vuid = std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5275 skip = sync_state.LogInfo(command_buffer_handle, vuid,
5276 "%s, srcStageMask includes %s, unsupported by synchronization validation.", CmdName(),
5277 string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT));
5278 } else {
5279 const auto &barriers = barrier_set.memory_barriers;
5280 for (size_t barrier_index = 0; barrier_index < barriers.size(); barrier_index++) {
5281 const auto &barrier = barriers[barrier_index];
5282 if (barrier.src_exec_scope.mask_param & VK_PIPELINE_STAGE_HOST_BIT) {
5283 const std::string vuid =
5284 std::string("SYNC-") + std::string(CmdName()) + std::string("-hostevent-unsupported");
5285 skip =
5286 sync_state.LogInfo(command_buffer_handle, vuid,
5287 "%s, srcStageMask %s of %s %zu, %s %zu, unsupported by synchronization validation.",
5288 CmdName(), string_VkPipelineStageFlagBits(VK_PIPELINE_STAGE_HOST_BIT),
5289 "pDependencyInfo", barrier_set_index, "pMemoryBarriers", barrier_index);
5290 }
5291 }
5292 }
5293 }
John Zulaufd5115702021-01-18 12:34:33 -07005294 }
5295
Jeremy Gebben40a22942020-12-22 14:22:06 -07005296 VkPipelineStageFlags2KHR event_stage_masks = 0U;
John Zulauf4edde622021-02-15 08:54:50 -07005297 VkPipelineStageFlags2KHR barrier_mask_params = 0U;
John Zulaufd5115702021-01-18 12:34:33 -07005298 bool events_not_found = false;
John Zulauf669dfd52021-01-27 17:15:28 -07005299 const auto *events_context = cb_context.GetCurrentEventsContext();
5300 assert(events_context);
John Zulauf4edde622021-02-15 08:54:50 -07005301 size_t barrier_set_index = 0;
5302 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5303 for (size_t event_index = 0; event_index < events_.size(); event_index++)
5304 for (const auto &event : events_) {
5305 const auto *sync_event = events_context->Get(event.get());
5306 const auto &barrier_set = barriers_[barrier_set_index];
5307 if (!sync_event) {
5308 // NOTE PHASE2: This is where we'll need queue submit time validation to come back and check the srcStageMask bits
5309 // or solve this with replay creating the SyncEventState in the queue context... also this will be a
5310 // new validation error... wait without previously submitted set event...
5311 events_not_found = true; // Demote "extra_stage_bits" error to warning, to avoid false positives at *record time*
5312 barrier_set_index += barrier_set_incr;
5313 continue; // Core, Lifetimes, or Param check needs to catch invalid events.
John Zulaufd5115702021-01-18 12:34:33 -07005314 }
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005315 const auto event_handle = sync_event->event->event();
John Zulauf4edde622021-02-15 08:54:50 -07005316 // TODO add "destroyed" checks
5317
5318 barrier_mask_params |= barrier_set.src_exec_scope.mask_param;
5319 const auto &src_exec_scope = barrier_set.src_exec_scope;
5320 event_stage_masks |= sync_event->scope.mask_param;
5321 const auto ignore_reason = sync_event->IsIgnoredByWait(cmd_, src_exec_scope.mask_param);
5322 if (ignore_reason) {
5323 switch (ignore_reason) {
5324 case SyncEventState::ResetWaitRace:
5325 case SyncEventState::Reset2WaitRace: {
5326 // Four permuations of Reset and Wait calls...
5327 const char *vuid =
5328 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent-event-03834" : "VUID-vkCmdResetEvent-event-03835";
5329 if (ignore_reason == SyncEventState::Reset2WaitRace) {
5330 vuid =
Jeremy Gebben476f5e22021-03-01 15:27:20 -07005331 (cmd_ == CMD_WAITEVENTS) ? "VUID-vkCmdResetEvent2KHR-event-03831" : "VUID-vkCmdResetEvent2KHR-event-03832";
John Zulauf4edde622021-02-15 08:54:50 -07005332 }
5333 const char *const message =
5334 "%s: %s %s operation following %s without intervening execution barrier, may cause race condition. %s";
5335 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5336 sync_state.report_data->FormatHandle(event_handle).c_str(), CmdName(),
5337 CommandTypeString(sync_event->last_command), ignored);
5338 break;
5339 }
5340 case SyncEventState::SetRace: {
5341 // Issue error message that Wait is waiting on an signal subject to race condition, and is thus ignored for
5342 // this event
5343 const char *const vuid = "SYNC-vkCmdWaitEvents-unsynchronized-setops";
5344 const char *const message =
5345 "%s: %s Unsychronized %s calls result in race conditions w.r.t. event signalling, %s %s";
5346 const char *const reason = "First synchronization scope is undefined.";
5347 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5348 sync_state.report_data->FormatHandle(event_handle).c_str(),
5349 CommandTypeString(sync_event->last_command), reason, ignored);
5350 break;
5351 }
5352 case SyncEventState::MissingStageBits: {
5353 const auto missing_bits = sync_event->scope.mask_param & ~src_exec_scope.mask_param;
5354 // Issue error message that event waited for is not in wait events scope
5355 const char *const vuid = "VUID-vkCmdWaitEvents-srcStageMask-01158";
5356 const char *const message =
5357 "%s: %s stageMask %" PRIx64 " includes bits not present in srcStageMask 0x%" PRIx64
5358 ". Bits missing from srcStageMask %s. %s";
5359 skip |= sync_state.LogError(event_handle, vuid, message, CmdName(),
5360 sync_state.report_data->FormatHandle(event_handle).c_str(),
5361 sync_event->scope.mask_param, src_exec_scope.mask_param,
5362 sync_utils::StringPipelineStageFlags(missing_bits).c_str(), ignored);
5363 break;
5364 }
5365 case SyncEventState::SetVsWait2: {
5366 skip |= sync_state.LogError(event_handle, "VUID-vkCmdWaitEvents2KHR-pEvents-03837",
5367 "%s: Follows set of %s by %s. Disallowed.", CmdName(),
5368 sync_state.report_data->FormatHandle(event_handle).c_str(),
5369 CommandTypeString(sync_event->last_command));
5370 break;
5371 }
5372 default:
5373 assert(ignore_reason == SyncEventState::NotIgnored);
5374 }
5375 } else if (barrier_set.image_memory_barriers.size()) {
5376 const auto &image_memory_barriers = barrier_set.image_memory_barriers;
5377 const auto *context = cb_context.GetCurrentAccessContext();
5378 assert(context);
5379 for (const auto &image_memory_barrier : image_memory_barriers) {
5380 if (image_memory_barrier.old_layout == image_memory_barrier.new_layout) continue;
5381 const auto *image_state = image_memory_barrier.image.get();
5382 if (!image_state) continue;
John Zulauf110413c2021-03-20 05:38:38 -06005383 const auto &subresource_range = image_memory_barrier.range;
John Zulauf4edde622021-02-15 08:54:50 -07005384 const auto &src_access_scope = image_memory_barrier.barrier.src_access_scope;
5385 const auto hazard =
5386 context->DetectImageBarrierHazard(*image_state, sync_event->scope.exec_scope, src_access_scope,
5387 subresource_range, *sync_event, AccessContext::DetectOptions::kDetectAll);
5388 if (hazard.hazard) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005389 skip |= sync_state.LogError(image_state->image(), string_SyncHazardVUID(hazard.hazard),
John Zulauf4edde622021-02-15 08:54:50 -07005390 "%s: Hazard %s for image barrier %" PRIu32 " %s. Access info %s.", CmdName(),
5391 string_SyncHazard(hazard.hazard), image_memory_barrier.index,
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005392 sync_state.report_data->FormatHandle(image_state->image()).c_str(),
John Zulauf4edde622021-02-15 08:54:50 -07005393 cb_context.FormatUsage(hazard).c_str());
5394 break;
5395 }
John Zulaufd5115702021-01-18 12:34:33 -07005396 }
5397 }
John Zulauf4edde622021-02-15 08:54:50 -07005398 // TODO: Add infrastructure for checking pDependencyInfo's vs. CmdSetEvent2 VUID - vkCmdWaitEvents2KHR - pEvents -
5399 // 03839
5400 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005401 }
John Zulaufd5115702021-01-18 12:34:33 -07005402
5403 // 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 -07005404 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 -07005405 if (extra_stage_bits) {
5406 // Issue error message that event waited for is not in wait events scope
John Zulauf4edde622021-02-15 08:54:50 -07005407 // NOTE: This isn't exactly the right VUID for WaitEvents2, but it's as close as we currently have support for
5408 const char *const vuid =
5409 (CMD_WAITEVENTS == cmd_) ? "VUID-vkCmdWaitEvents-srcStageMask-01158" : "VUID-vkCmdWaitEvents2KHR-pEvents-03838";
John Zulaufd5115702021-01-18 12:34:33 -07005410 const char *const message =
Jeremy Gebben40a22942020-12-22 14:22:06 -07005411 "%s: srcStageMask 0x%" PRIx64 " contains stages not present in pEvents stageMask. Extra stages are %s.%s";
John Zulaufd5115702021-01-18 12:34:33 -07005412 if (events_not_found) {
John Zulauf4edde622021-02-15 08:54:50 -07005413 skip |= sync_state.LogInfo(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005414 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(),
John Zulaufd5115702021-01-18 12:34:33 -07005415 " vkCmdSetEvent may be in previously submitted command buffer.");
5416 } else {
John Zulauf4edde622021-02-15 08:54:50 -07005417 skip |= sync_state.LogError(command_buffer_handle, vuid, message, CmdName(), barrier_mask_params,
Jeremy Gebben40a22942020-12-22 14:22:06 -07005418 sync_utils::StringPipelineStageFlags(extra_stage_bits).c_str(), "");
John Zulaufd5115702021-01-18 12:34:33 -07005419 }
5420 }
5421 return skip;
5422}
5423
5424struct SyncOpWaitEventsFunctorFactory {
5425 using BarrierOpFunctor = WaitEventBarrierOp;
5426 using ApplyFunctor = ApplyBarrierFunctor<BarrierOpFunctor>;
5427 using GlobalBarrierOpFunctor = WaitEventBarrierOp;
5428 using GlobalApplyFunctor = ApplyBarrierOpsFunctor<GlobalBarrierOpFunctor>;
5429 using BufferRange = EventSimpleRangeGenerator;
5430 using ImageRange = EventImageRangeGenerator;
5431 using GlobalRange = EventSimpleRangeGenerator;
5432
5433 // Need to restrict to only valid exec and access scope for this event
5434 // Pass by value is intentional to get a copy we can change without modifying the passed barrier
5435 SyncBarrier RestrictToEvent(SyncBarrier barrier) const {
John Zulaufc523bf62021-02-16 08:20:34 -07005436 barrier.src_exec_scope.exec_scope = sync_event->scope.exec_scope & barrier.src_exec_scope.exec_scope;
John Zulaufd5115702021-01-18 12:34:33 -07005437 barrier.src_access_scope = sync_event->scope.valid_accesses & barrier.src_access_scope;
5438 return barrier;
5439 }
5440 ApplyFunctor MakeApplyFunctor(const SyncBarrier &barrier_arg, bool layout_transition) const {
5441 auto barrier = RestrictToEvent(barrier_arg);
5442 return ApplyFunctor(BarrierOpFunctor(sync_event->first_scope_tag, barrier, layout_transition));
5443 }
5444 GlobalApplyFunctor MakeGlobalApplyFunctor(size_t size_hint, const ResourceUsageTag &tag) const {
5445 return GlobalApplyFunctor(false /* don't resolve */, size_hint, tag);
5446 }
5447 GlobalBarrierOpFunctor MakeGlobalBarrierOpFunctor(const SyncBarrier &barrier_arg) const {
5448 auto barrier = RestrictToEvent(barrier_arg);
5449 return GlobalBarrierOpFunctor(sync_event->first_scope_tag, barrier, false);
5450 }
5451
5452 BufferRange MakeRangeGen(const BUFFER_STATE &buffer, const ResourceAccessRange &range_arg) const {
5453 const AccessAddressType address_type = GetAccessAddressType(buffer);
5454 const auto base_address = ResourceBaseAddress(buffer);
5455 ResourceAccessRange range = SimpleBinding(buffer) ? (range_arg + base_address) : ResourceAccessRange();
5456 EventSimpleRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), range);
5457 return filtered_range_gen;
5458 }
John Zulauf110413c2021-03-20 05:38:38 -06005459 ImageRange MakeRangeGen(const IMAGE_STATE &image, const VkImageSubresourceRange &subresource_range) const {
John Zulaufd5115702021-01-18 12:34:33 -07005460 if (!SimpleBinding(image)) return ImageRange();
5461 const auto address_type = GetAccessAddressType(image);
5462 const auto base_address = ResourceBaseAddress(image);
John Zulauf110413c2021-03-20 05:38:38 -06005463 subresource_adapter::ImageRangeGenerator image_range_gen(*image.fragment_encoder.get(), subresource_range, base_address);
John Zulaufd5115702021-01-18 12:34:33 -07005464 EventImageRangeGenerator filtered_range_gen(sync_event->FirstScope(address_type), image_range_gen);
5465
5466 return filtered_range_gen;
5467 }
5468 GlobalRange MakeGlobalRangeGen(AccessAddressType address_type) const {
5469 return EventSimpleRangeGenerator(sync_event->FirstScope(address_type), kFullRange);
5470 }
5471 SyncOpWaitEventsFunctorFactory(SyncEventState *sync_event_) : sync_event(sync_event_) { assert(sync_event); }
5472 SyncEventState *sync_event;
5473};
5474
John Zulauf36ef9282021-02-02 11:47:24 -07005475void SyncOpWaitEvents::Record(CommandBufferAccessContext *cb_context) const {
5476 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulaufd5115702021-01-18 12:34:33 -07005477 auto *access_context = cb_context->GetCurrentAccessContext();
5478 assert(access_context);
5479 if (!access_context) return;
John Zulauf669dfd52021-01-27 17:15:28 -07005480 auto *events_context = cb_context->GetCurrentEventsContext();
5481 assert(events_context);
5482 if (!events_context) return;
John Zulaufd5115702021-01-18 12:34:33 -07005483
5484 // Unlike PipelineBarrier, WaitEvent is *not* limited to accesses within the current subpass (if any) and thus needs to import
5485 // all accesses. Can instead import for all first_scopes, or a union of them, if this becomes a performance/memory issue,
5486 // but with no idea of the performance of the union, nor of whether it even matters... take the simplest approach here,
5487 access_context->ResolvePreviousAccesses();
5488
John Zulaufd5115702021-01-18 12:34:33 -07005489 // TODO... this needs change the SyncEventContext it's using depending on whether this is replay... the recorded
5490 // 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 -07005491 size_t barrier_set_index = 0;
5492 size_t barrier_set_incr = (barriers_.size() == 1) ? 0 : 1;
5493 assert(barriers_.size() == 1 || (barriers_.size() == events_.size()));
John Zulauf669dfd52021-01-27 17:15:28 -07005494 for (auto &event_shared : events_) {
5495 if (!event_shared.get()) continue;
5496 auto *sync_event = events_context->GetFromShared(event_shared);
John Zulaufd5115702021-01-18 12:34:33 -07005497
John Zulauf4edde622021-02-15 08:54:50 -07005498 sync_event->last_command = cmd_;
John Zulaufd5115702021-01-18 12:34:33 -07005499
John Zulauf4edde622021-02-15 08:54:50 -07005500 const auto &barrier_set = barriers_[barrier_set_index];
5501 const auto &dst = barrier_set.dst_exec_scope;
5502 if (!sync_event->IsIgnoredByWait(cmd_, barrier_set.src_exec_scope.mask_param)) {
John Zulaufd5115702021-01-18 12:34:33 -07005503 // These apply barriers one at a time as the are restricted to the resource ranges specified per each barrier,
5504 // but do not update the dependency chain information (but set the "pending" state) // s.t. the order independence
5505 // of the barriers is maintained.
5506 SyncOpWaitEventsFunctorFactory factory(sync_event);
John Zulauf4edde622021-02-15 08:54:50 -07005507 ApplyBarriers(barrier_set.buffer_memory_barriers, factory, tag, access_context);
5508 ApplyBarriers(barrier_set.image_memory_barriers, factory, tag, access_context);
5509 ApplyGlobalBarriers(barrier_set.memory_barriers, factory, tag, access_context);
John Zulaufd5115702021-01-18 12:34:33 -07005510
5511 // Apply the global barrier to the event itself (for race condition tracking)
5512 // Events don't happen at a stage, so we need to store the unexpanded ALL_COMMANDS if set for inter-event-calls
5513 sync_event->barriers = dst.mask_param & VK_PIPELINE_STAGE_ALL_COMMANDS_BIT;
5514 sync_event->barriers |= dst.exec_scope;
5515 } else {
5516 // We ignored this wait, so we don't have any effective synchronization barriers for it.
5517 sync_event->barriers = 0U;
5518 }
John Zulauf4edde622021-02-15 08:54:50 -07005519 barrier_set_index += barrier_set_incr;
John Zulaufd5115702021-01-18 12:34:33 -07005520 }
5521
5522 // Apply the pending barriers
5523 ResolvePendingBarrierFunctor apply_pending_action(tag);
5524 access_context->ApplyToContext(apply_pending_action);
5525}
5526
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005527bool SyncValidator::PreCallValidateCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5528 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) const {
5529 bool skip = false;
5530 const auto *cb_access_context = GetAccessContext(commandBuffer);
5531 assert(cb_access_context);
5532 if (!cb_access_context) return skip;
5533
5534 const auto *context = cb_access_context->GetCurrentAccessContext();
5535 assert(context);
5536 if (!context) return skip;
5537
5538 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5539
5540 if (dst_buffer) {
5541 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5542 auto hazard = context->DetectHazard(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, range);
5543 if (hazard.hazard) {
5544 skip |= LogError(dstBuffer, string_SyncHazardVUID(hazard.hazard),
5545 "vkCmdWriteBufferMarkerAMD2: Hazard %s for dstBuffer %s. Access info %s.",
5546 string_SyncHazard(hazard.hazard), report_data->FormatHandle(dstBuffer).c_str(),
5547 string_UsageTag(hazard.tag).c_str());
5548 }
5549 }
5550 return skip;
5551}
5552
John Zulauf669dfd52021-01-27 17:15:28 -07005553void SyncOpWaitEvents::MakeEventsList(const SyncValidator &sync_state, uint32_t event_count, const VkEvent *events) {
John Zulaufd5115702021-01-18 12:34:33 -07005554 events_.reserve(event_count);
5555 for (uint32_t event_index = 0; event_index < event_count; event_index++) {
John Zulauf669dfd52021-01-27 17:15:28 -07005556 events_.emplace_back(sync_state.GetShared<EVENT_STATE>(events[event_index]));
John Zulaufd5115702021-01-18 12:34:33 -07005557 }
5558}
John Zulauf6ce24372021-01-30 05:56:25 -07005559
John Zulauf36ef9282021-02-02 11:47:24 -07005560SyncOpResetEvent::SyncOpResetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005561 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005562 : SyncOpBase(cmd),
5563 event_(sync_state.GetShared<EVENT_STATE>(event)),
5564 exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005565
5566bool SyncOpResetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005567 auto *events_context = cb_context.GetCurrentEventsContext();
5568 assert(events_context);
5569 bool skip = false;
5570 if (!events_context) return skip;
5571
5572 const auto &sync_state = cb_context.GetSyncState();
5573 const auto *sync_event = events_context->Get(event_);
5574 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5575
5576 const char *const set_wait =
5577 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5578 "hazards.";
5579 const char *message = set_wait; // Only one message this call.
5580 if (!sync_event->HasBarrier(exec_scope_.mask_param, exec_scope_.exec_scope)) {
5581 const char *vuid = nullptr;
5582 switch (sync_event->last_command) {
5583 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005584 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005585 // Needs a barrier between set and reset
5586 vuid = "SYNC-vkCmdResetEvent-missingbarrier-set";
5587 break;
John Zulauf4edde622021-02-15 08:54:50 -07005588 case CMD_WAITEVENTS:
5589 case CMD_WAITEVENTS2KHR: {
John Zulauf6ce24372021-01-30 05:56:25 -07005590 // Needs to be in the barriers chain (either because of a barrier, or because of dstStageMask
5591 vuid = "SYNC-vkCmdResetEvent-missingbarrier-wait";
5592 break;
5593 }
5594 default:
5595 // The only other valid last command that wasn't one.
John Zulauf4edde622021-02-15 08:54:50 -07005596 assert((sync_event->last_command == CMD_NONE) || (sync_event->last_command == CMD_RESETEVENT) ||
5597 (sync_event->last_command == CMD_RESETEVENT2KHR));
John Zulauf6ce24372021-01-30 05:56:25 -07005598 break;
5599 }
5600 if (vuid) {
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005601 skip |= sync_state.LogError(event_->event(), vuid, message, CmdName(),
5602 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005603 CommandTypeString(sync_event->last_command));
5604 }
5605 }
5606 return skip;
5607}
5608
John Zulauf36ef9282021-02-02 11:47:24 -07005609void SyncOpResetEvent::Record(CommandBufferAccessContext *cb_context) const {
John Zulauf6ce24372021-01-30 05:56:25 -07005610 auto *events_context = cb_context->GetCurrentEventsContext();
5611 assert(events_context);
5612 if (!events_context) return;
5613
5614 auto *sync_event = events_context->GetFromShared(event_);
5615 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5616
5617 // Update the event state
John Zulauf36ef9282021-02-02 11:47:24 -07005618 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005619 sync_event->unsynchronized_set = CMD_NONE;
5620 sync_event->ResetFirstScope();
5621 sync_event->barriers = 0U;
5622}
5623
John Zulauf36ef9282021-02-02 11:47:24 -07005624SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
John Zulauf4edde622021-02-15 08:54:50 -07005625 VkPipelineStageFlags2KHR stageMask)
John Zulauf36ef9282021-02-02 11:47:24 -07005626 : SyncOpBase(cmd),
5627 event_(sync_state.GetShared<EVENT_STATE>(event)),
John Zulauf4edde622021-02-15 08:54:50 -07005628 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, stageMask)),
5629 dep_info_() {}
5630
5631SyncOpSetEvent::SyncOpSetEvent(CMD_TYPE cmd, const SyncValidator &sync_state, VkQueueFlags queue_flags, VkEvent event,
5632 const VkDependencyInfoKHR &dep_info)
5633 : SyncOpBase(cmd),
5634 event_(sync_state.GetShared<EVENT_STATE>(event)),
5635 src_exec_scope_(SyncExecScope::MakeSrc(queue_flags, sync_utils::GetGlobalStageMasks(dep_info).src)),
5636 dep_info_(new safe_VkDependencyInfoKHR(&dep_info)) {}
John Zulauf6ce24372021-01-30 05:56:25 -07005637
5638bool SyncOpSetEvent::Validate(const CommandBufferAccessContext &cb_context) const {
5639 // 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 -07005640 bool skip = false;
5641
5642 const auto &sync_state = cb_context.GetSyncState();
5643 auto *events_context = cb_context.GetCurrentEventsContext();
5644 assert(events_context);
5645 if (!events_context) return skip;
5646
5647 const auto *sync_event = events_context->Get(event_);
5648 if (!sync_event) return skip; // Core, Lifetimes, or Param check needs to catch invalid events.
5649
5650 const char *const reset_set =
5651 "%s: %s %s operation following %s without intervening execution barrier, is a race condition and may result in data "
5652 "hazards.";
5653 const char *const wait =
5654 "%s: %s %s operation following %s without intervening vkCmdResetEvent, may result in data hazard and is ignored.";
5655
5656 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
John Zulauf4edde622021-02-15 08:54:50 -07005657 const char *vuid_stem = nullptr;
John Zulauf6ce24372021-01-30 05:56:25 -07005658 const char *message = nullptr;
5659 switch (sync_event->last_command) {
5660 case CMD_RESETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005661 case CMD_RESETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005662 // Needs a barrier between reset and set
John Zulauf4edde622021-02-15 08:54:50 -07005663 vuid_stem = "-missingbarrier-reset";
John Zulauf6ce24372021-01-30 05:56:25 -07005664 message = reset_set;
5665 break;
5666 case CMD_SETEVENT:
John Zulauf4edde622021-02-15 08:54:50 -07005667 case CMD_SETEVENT2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005668 // Needs a barrier between set and set
John Zulauf4edde622021-02-15 08:54:50 -07005669 vuid_stem = "-missingbarrier-set";
John Zulauf6ce24372021-01-30 05:56:25 -07005670 message = reset_set;
5671 break;
5672 case CMD_WAITEVENTS:
John Zulauf4edde622021-02-15 08:54:50 -07005673 case CMD_WAITEVENTS2KHR:
John Zulauf6ce24372021-01-30 05:56:25 -07005674 // Needs a barrier or is in second execution scope
John Zulauf4edde622021-02-15 08:54:50 -07005675 vuid_stem = "-missingbarrier-wait";
John Zulauf6ce24372021-01-30 05:56:25 -07005676 message = wait;
5677 break;
5678 default:
5679 // The only other valid last command that wasn't one.
5680 assert(sync_event->last_command == CMD_NONE);
5681 break;
5682 }
John Zulauf4edde622021-02-15 08:54:50 -07005683 if (vuid_stem) {
John Zulauf6ce24372021-01-30 05:56:25 -07005684 assert(nullptr != message);
John Zulauf4edde622021-02-15 08:54:50 -07005685 std::string vuid("SYNC-");
5686 vuid.append(CmdName()).append(vuid_stem);
Jeremy Gebben14b0d1a2021-05-15 20:15:41 -06005687 skip |= sync_state.LogError(event_->event(), vuid.c_str(), message, CmdName(),
5688 sync_state.report_data->FormatHandle(event_->event()).c_str(), CmdName(),
John Zulauf6ce24372021-01-30 05:56:25 -07005689 CommandTypeString(sync_event->last_command));
5690 }
5691 }
5692
5693 return skip;
5694}
5695
John Zulauf36ef9282021-02-02 11:47:24 -07005696void SyncOpSetEvent::Record(CommandBufferAccessContext *cb_context) const {
5697 const auto tag = cb_context->NextCommandTag(cmd_);
John Zulauf6ce24372021-01-30 05:56:25 -07005698 auto *events_context = cb_context->GetCurrentEventsContext();
5699 auto *access_context = cb_context->GetCurrentAccessContext();
5700 assert(events_context);
5701 if (!events_context) return;
5702
5703 auto *sync_event = events_context->GetFromShared(event_);
5704 if (!sync_event) return; // Core, Lifetimes, or Param check needs to catch invalid events.
5705
5706 // NOTE: We're going to simply record the sync scope here, as anything else would be implementation defined/undefined
5707 // and we're issuing errors re: missing barriers between event commands, which if the user fixes would fix
5708 // any issues caused by naive scope setting here.
5709
5710 // What happens with two SetEvent is that one cannot know what group of operations will be waited for.
5711 // Given:
5712 // Stuff1; SetEvent; Stuff2; SetEvent; WaitEvents;
5713 // WaitEvents cannot know which of Stuff1, Stuff2, or both has completed execution.
5714
5715 if (!sync_event->HasBarrier(src_exec_scope_.mask_param, src_exec_scope_.exec_scope)) {
5716 sync_event->unsynchronized_set = sync_event->last_command;
5717 sync_event->ResetFirstScope();
5718 } else if (sync_event->scope.exec_scope == 0) {
5719 // We only set the scope if there isn't one
5720 sync_event->scope = src_exec_scope_;
5721
5722 auto set_scope = [&sync_event](AccessAddressType address_type, const ResourceAccessRangeMap::value_type &access) {
5723 auto &scope_map = sync_event->first_scope[static_cast<size_t>(address_type)];
5724 if (access.second.InSourceScopeOrChain(sync_event->scope.exec_scope, sync_event->scope.valid_accesses)) {
5725 scope_map.insert(scope_map.end(), std::make_pair(access.first, true));
5726 }
5727 };
5728 access_context->ForAll(set_scope);
5729 sync_event->unsynchronized_set = CMD_NONE;
5730 sync_event->first_scope_tag = tag;
5731 }
John Zulauf4edde622021-02-15 08:54:50 -07005732 // TODO: Store dep_info_ shared ptr in sync_state for WaitEvents2 validation
5733 sync_event->last_command = cmd_;
John Zulauf6ce24372021-01-30 05:56:25 -07005734 sync_event->barriers = 0U;
5735}
John Zulauf64ffe552021-02-06 10:25:07 -07005736
5737SyncOpBeginRenderPass::SyncOpBeginRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state,
5738 const VkRenderPassBeginInfo *pRenderPassBegin,
sfricke-samsung85584a72021-09-30 21:43:38 -07005739 const VkSubpassBeginInfo *pSubpassBeginInfo)
5740 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07005741 if (pRenderPassBegin) {
5742 rp_state_ = sync_state.GetShared<RENDER_PASS_STATE>(pRenderPassBegin->renderPass);
5743 renderpass_begin_info_ = safe_VkRenderPassBeginInfo(pRenderPassBegin);
5744 const auto *fb_state = sync_state.Get<FRAMEBUFFER_STATE>(pRenderPassBegin->framebuffer);
5745 if (fb_state) {
5746 shared_attachments_ = sync_state.GetSharedAttachmentViews(*renderpass_begin_info_.ptr(), *fb_state);
5747 // TODO: Revisit this when all attachment validation is through SyncOps to see if we can discard the plain pointer copy
5748 // Note that this a safe to presist as long as shared_attachments is not cleared
5749 attachments_.reserve(shared_attachments_.size());
sfricke-samsung01c9ae92021-02-09 22:30:52 -08005750 for (const auto &attachment : shared_attachments_) {
John Zulauf64ffe552021-02-06 10:25:07 -07005751 attachments_.emplace_back(attachment.get());
5752 }
5753 }
5754 if (pSubpassBeginInfo) {
5755 subpass_begin_info_ = safe_VkSubpassBeginInfo(pSubpassBeginInfo);
5756 }
5757 }
5758}
5759
5760bool SyncOpBeginRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5761 // Check if any of the layout transitions are hazardous.... but we don't have the renderpass context to work with, so we
5762 bool skip = false;
5763
5764 assert(rp_state_.get());
5765 if (nullptr == rp_state_.get()) return skip;
5766 auto &rp_state = *rp_state_.get();
5767
5768 const uint32_t subpass = 0;
5769
5770 // Construct the state we can use to validate against... (since validation is const and RecordCmdBeginRenderPass
5771 // hasn't happened yet)
5772 const std::vector<AccessContext> empty_context_vector;
5773 AccessContext temp_context(subpass, cb_context.GetQueueFlags(), rp_state.subpass_dependencies, empty_context_vector,
5774 cb_context.GetCurrentAccessContext());
5775
5776 // Validate attachment operations
5777 if (attachments_.size() == 0) return skip;
5778 const auto &render_area = renderpass_begin_info_.renderArea;
John Zulaufd0ec59f2021-03-13 14:25:08 -07005779
5780 // Since the isn't a valid RenderPassAccessContext until Record, needs to create the view/generator list... we could limit this
5781 // by predicating on whether subpass 0 uses the attachment if it is too expensive to create the full list redundantly here.
5782 // More broadly we could look at thread specific state shared between Validate and Record as is done for other heavyweight
5783 // operations (though it's currently a messy approach)
5784 AttachmentViewGenVector view_gens = RenderPassAccessContext::CreateAttachmentViewGen(render_area, attachments_);
5785 skip |= temp_context.ValidateLayoutTransitions(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005786
5787 // Validate load operations if there were no layout transition hazards
5788 if (!skip) {
John Zulaufd0ec59f2021-03-13 14:25:08 -07005789 temp_context.RecordLayoutTransitions(rp_state, subpass, view_gens, kCurrentCommandTag);
5790 skip |= temp_context.ValidateLoadOperation(cb_context, rp_state, render_area, subpass, view_gens, CmdName());
John Zulauf64ffe552021-02-06 10:25:07 -07005791 }
5792
5793 return skip;
5794}
5795
5796void SyncOpBeginRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5797 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5798 assert(rp_state_.get());
5799 if (nullptr == rp_state_.get()) return;
5800 const auto tag = cb_context->NextCommandTag(cmd_);
5801 cb_context->RecordBeginRenderPass(*rp_state_.get(), renderpass_begin_info_.renderArea, attachments_, tag);
5802}
5803
5804SyncOpNextSubpass::SyncOpNextSubpass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassBeginInfo *pSubpassBeginInfo,
sfricke-samsung85584a72021-09-30 21:43:38 -07005805 const VkSubpassEndInfo *pSubpassEndInfo)
5806 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07005807 if (pSubpassBeginInfo) {
5808 subpass_begin_info_.initialize(pSubpassBeginInfo);
5809 }
5810 if (pSubpassEndInfo) {
5811 subpass_end_info_.initialize(pSubpassEndInfo);
5812 }
5813}
5814
5815bool SyncOpNextSubpass::Validate(const CommandBufferAccessContext &cb_context) const {
5816 bool skip = false;
5817 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5818 if (!renderpass_context) return skip;
5819
5820 skip |= renderpass_context->ValidateNextSubpass(cb_context.GetExecutionContext(), CmdName());
5821 return skip;
5822}
5823
5824void SyncOpNextSubpass::Record(CommandBufferAccessContext *cb_context) const {
5825 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5826 cb_context->RecordNextSubpass(cmd_);
5827}
5828
sfricke-samsung85584a72021-09-30 21:43:38 -07005829SyncOpEndRenderPass::SyncOpEndRenderPass(CMD_TYPE cmd, const SyncValidator &sync_state, const VkSubpassEndInfo *pSubpassEndInfo)
5830 : SyncOpBase(cmd) {
John Zulauf64ffe552021-02-06 10:25:07 -07005831 if (pSubpassEndInfo) {
5832 subpass_end_info_.initialize(pSubpassEndInfo);
5833 }
5834}
5835
5836bool SyncOpEndRenderPass::Validate(const CommandBufferAccessContext &cb_context) const {
5837 bool skip = false;
5838 const auto *renderpass_context = cb_context.GetCurrentRenderPassContext();
5839
5840 if (!renderpass_context) return skip;
5841 skip |= renderpass_context->ValidateEndRenderPass(cb_context.GetExecutionContext(), CmdName());
5842 return skip;
5843}
5844
5845void SyncOpEndRenderPass::Record(CommandBufferAccessContext *cb_context) const {
5846 // TODO PHASE2 need to have a consistent way to record to either command buffer or queue contexts
5847 cb_context->RecordEndRenderPass(cmd_);
5848}
Jeremy Gebbendf3fcc32021-02-15 08:53:17 -07005849
5850void SyncValidator::PreCallRecordCmdWriteBufferMarker2AMD(VkCommandBuffer commandBuffer, VkPipelineStageFlags2KHR pipelineStage,
5851 VkBuffer dstBuffer, VkDeviceSize dstOffset, uint32_t marker) {
5852 StateTracker::PreCallRecordCmdWriteBufferMarker2AMD(commandBuffer, pipelineStage, dstBuffer, dstOffset, marker);
5853 auto *cb_access_context = GetAccessContext(commandBuffer);
5854 assert(cb_access_context);
5855 const auto tag = cb_access_context->NextCommandTag(CMD_WRITEBUFFERMARKERAMD);
5856 auto *context = cb_access_context->GetCurrentAccessContext();
5857 assert(context);
5858
5859 const auto *dst_buffer = Get<BUFFER_STATE>(dstBuffer);
5860
5861 if (dst_buffer) {
5862 const ResourceAccessRange range = MakeRange(dstOffset, 4);
5863 context->UpdateAccessState(*dst_buffer, SYNC_COPY_TRANSFER_WRITE, SyncOrdering::kNonAttachment, range, tag);
5864 }
5865}
John Zulaufd05c5842021-03-26 11:32:16 -06005866
John Zulaufd0ec59f2021-03-13 14:25:08 -07005867AttachmentViewGen::AttachmentViewGen(const IMAGE_VIEW_STATE *view, const VkOffset3D &offset, const VkExtent3D &extent)
5868 : view_(view), view_mask_(), gen_store_() {
5869 if (!view_ || !view_->image_state || !SimpleBinding(*view_->image_state)) return;
5870 const IMAGE_STATE &image_state = *view_->image_state.get();
5871 const auto base_address = ResourceBaseAddress(image_state);
5872 const auto *encoder = image_state.fragment_encoder.get();
5873 if (!encoder) return;
Jeremy Gebben11a68a32021-07-29 11:59:22 -06005874 // Get offset and extent for the view, accounting for possible depth slicing
5875 const VkOffset3D zero_offset = view->GetOffset();
5876 const VkExtent3D &image_extent = view->GetExtent();
John Zulaufd0ec59f2021-03-13 14:25:08 -07005877 // Intentional copy
5878 VkImageSubresourceRange subres_range = view_->normalized_subresource_range;
5879 view_mask_ = subres_range.aspectMask;
5880 gen_store_[Gen::kViewSubresource].emplace(*encoder, subres_range, zero_offset, image_extent, base_address);
5881 gen_store_[Gen::kRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5882
5883 const auto depth = view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT;
5884 if (depth && (depth != view_mask_)) {
5885 subres_range.aspectMask = depth;
5886 gen_store_[Gen::kDepthOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5887 }
5888 const auto stencil = view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT;
5889 if (stencil && (stencil != view_mask_)) {
5890 subres_range.aspectMask = stencil;
5891 gen_store_[Gen::kStencilOnlyRenderArea].emplace(*encoder, subres_range, offset, extent, base_address);
5892 }
5893}
5894
5895const ImageRangeGen *AttachmentViewGen::GetRangeGen(AttachmentViewGen::Gen gen_type) const {
5896 const ImageRangeGen *got = nullptr;
5897 switch (gen_type) {
5898 case kViewSubresource:
5899 got = &gen_store_[kViewSubresource];
5900 break;
5901 case kRenderArea:
5902 got = &gen_store_[kRenderArea];
5903 break;
5904 case kDepthOnlyRenderArea:
5905 got =
5906 (view_mask_ == VK_IMAGE_ASPECT_DEPTH_BIT) ? &gen_store_[Gen::kRenderArea] : &gen_store_[Gen::kDepthOnlyRenderArea];
5907 break;
5908 case kStencilOnlyRenderArea:
5909 got = (view_mask_ == VK_IMAGE_ASPECT_STENCIL_BIT) ? &gen_store_[Gen::kRenderArea]
5910 : &gen_store_[Gen::kStencilOnlyRenderArea];
5911 break;
5912 default:
5913 assert(got);
5914 }
5915 return got;
5916}
5917
5918AttachmentViewGen::Gen AttachmentViewGen::GetDepthStencilRenderAreaGenType(bool depth_op, bool stencil_op) const {
5919 assert(IsValid());
5920 assert(view_mask_ & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT));
5921 if (depth_op) {
5922 assert(view_mask_ & VK_IMAGE_ASPECT_DEPTH_BIT);
5923 if (stencil_op) {
5924 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5925 return kRenderArea;
5926 }
5927 return kDepthOnlyRenderArea;
5928 }
5929 if (stencil_op) {
5930 assert(view_mask_ & VK_IMAGE_ASPECT_STENCIL_BIT);
5931 return kStencilOnlyRenderArea;
5932 }
5933
5934 assert(depth_op || stencil_op);
5935 return kRenderArea;
5936}
5937
5938AccessAddressType AttachmentViewGen::GetAddressType() const { return AccessContext::ImageAddressType(*view_->image_state); }